Nishit Mehta
Nishit Mehta

Reputation: 83

Conv2D: How can I get the values of each filter

My model:

classifier = Sequential()

# Convolutional + MaxPooling -> 1
classifier.add(Conv2D(32, (3,3), input_shape = (IMAGE_SIZE, IMAGE_SIZE, 3)))
convout1 = Activation('relu')
classifier.add(convout1)
classifier.add(MaxPooling2D(pool_size = (2,2)))
classifier.add(Dropout(0.25))

I am running the following code to get weights

classifier.layers[0].get_weights()[0]

It returns an array of 3x3x3x32. Shouldn't it return 32 matrices of 3x3?

Upvotes: 4

Views: 1965

Answers (2)

Maxim
Maxim

Reputation: 53758

The weights shape is correct, because the convolutional filter is applied to the whole 3D input volume and the parameters for different channels are not shared (though they are shared spatially). See the picture from CS231n class:

convolution

Yes, the output volume is obtained by summing up the convolutions across the depth volume, but the parameters in each channel are different.

In your case, the channels are RGB (since input_shape = (IMAGE_SIZE, IMAGE_SIZE, 3)), the spatial filter size is 3x3 and there are 32 filters. Hence the result shape is 3x3x3x32 and shape of each filter is 3x3x3.

Upvotes: 1

Dr. Snoopy
Dr. Snoopy

Reputation: 56357

No, the return value has the right shape. What you are not considering is that each of the 32 filters is 3x3 in spatial dimensions, and has three channels, same as the input. This means that each filter also works on the channels dimension. What you expect would only be valid in the case of doing 2D convolution on a one channel image.

Upvotes: 0

Related Questions