I have been working with the convolutional neural network with Keras. I used the below image and converted it into an array.
img_arr = np.array(img)
img_arr = img_arr/255.0
>>>
array([[[0.03137255, 0.0627451 , 0.1372549 ],
[0.01960784, 0.05098039, 0.1254902 ],
[0.03921569, 0.07058824, 0.15294118],
...,
[0.01960784, 0.08627451, 0.04705882],
[0.01568627, 0.06666667, 0.02745098],
[0. , 0.05098039, 0.01176471]],
[[0.35686275, 0.38431373, 0.45490196],
[0.32941176, 0.35686275, 0.42745098],
[0.31372549, 0.34117647, 0.41568627],
...,
img_arr = np.expand_dims(img_arr, 0)
print(img_arr.shape)
>>> (1, 343, 499, 3)
This is how I preprocessed the image and created an Array out of it. Then I ran tf.keras.layers.Conv2D
on this array like this,
conv1 = tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3))
output_conv1 = conv1(img_arr)
print(output_conv1.shape)
>>> TensorShape([1, 341, 497, 32])
The output shape is completely understandable. But the problem is when I print the kernel
shape in Keras,
print(conv1.kernel.shape)
>>> TensorShape([3, 3, 3, 32])
I passed kernel_size as (3, 3)
then why there are '3' (3, 3, 3)
in the kernel shape? The last axis is 32
I think it is because of no. of filters
. But the first 3 axis which is (3, 3, 3)
is the thing which I am not understanding!
CodePudding user response:
I believe the last 3
is referring to the number of channels. You are applying a 3x3
kernel to 3 channels. If you check out the source code, you will quickly understand how the kernel_shape
is built:
kernel_shape = self.kernel_size (input_channel // self.groups, self.filters)