Categories
Misc

Access filters within a convolutional layer – TensorFlow2

I am using TF2.5 & Python3.8 where a conv layer is defined as:

 Conv2D( filters = 64, kernel_size = (3, 3), activation='relu', kernel_initializer = tf.initializers.GlorotNormal(), strides = (1, 1), padding = 'same', ) 

Using a batch of 60 CIFAR-10 dataset as input:

 x.shape # TensorShape([60, 32, 32, 3]) 

Output volume of this layer preserves the spatial width and height (32, 32) and has 64 filters/kernel maps applied to the 60 images as batch-

 conv1(x).shape # TensorShape([60, 32, 32, 64]) conv1.kernel.shape # TensorShape([3, 3, 3, 64]) 

In this output, the first (3, 3) is the spatial width and height of the filters/kernels applied in this conv layer. The third 3 refers to the number of input channels provided to this layer and 64 refers to the number of filters applied.

How can I access the 64 filters applied in this conv layer?

Currently I am using the code:

 filters = conv1.kernel[:, :, 0, :] filters.shape # TensorShape([3, 3, 64]) 

Is this correct? Also, how can I iterate over the 64 different filters of this conv layer?

Thanks

submitted by /u/grid_world
[visit reddit] [comments]

Leave a Reply

Your email address will not be published.