I am new to CNN and tensorflow. It has been about 2 days since working through Geron's Hands-on TF book. I'd appreciate if someone can help me.
Goal: Understand how and why keras official documentation (https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D) uses Conv2D (m,n...)
notation.
Problem: I wrote two sets of code. I understood the first one, which uses explicit set of declaration for number of filters
and kernel_size
.
TF and Python Version:
sys.version Out: '3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)]' tf.__version__ Out: '2.3.0'
Code 1:
import tensorflow as tf input_shape = (4, 30, 60, 3) #Sample 30x60 images with RGB channel. `batch_size` = 4 a1=tf.keras.layers.Conv2D(filters=10,kernel_size=(3,3), input_shape=input_shape[1:]) a1(tf.random.normal(input_shape)).shape a1.filters a1.kernel_size model = tf.keras.Sequential() model.add(a1) model.output_shape model.summary()
Output:
Out[99]: TensorShape([4, 28, 58, 10]) #we are not using padding. So, the shape of tensor is 4 batch x 28x58 x 10 filters Out[99]: 10 # number of filters Out[99]: (3, 3) #kernel size Out[99]: (None, 28, 58, 10) #this is the feature map for one image Model: "sequential_24" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_34 (Conv2D) (None, 28, 58, 10) 280 ================================================================= Total params: 280 Trainable params: 280 Non-trainable params: 0 _________________________________________________________________
I am good with above output. I've added my thoughts above.
Code 2: Now, I modified the code above to not explicitly mention kernel_size
and filters
as per official documentation above.
a2=tf.keras.layers.Conv2D(10,3,3, input_shape=input_shape[1:]) #here's the change. a2(tf.random.normal(input_shape)).shape a2.filters a2.kernel_size model = tf.keras.Sequential() model.add(a2) model.output_shape model.summary()
Output:
Out[100]: TensorShape([4, 10, 20, 10]) Out[100]: 10 Out[100]: (3, 3) Out[100]: (None, 10, 20, 10) Model: "sequential_25" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_35 (Conv2D) (None, 10, 20, 10) 280 ================================================================= Total params: 280 Trainable params: 280 Non-trainable params: 0 _________________________________________________________________
As we can see, the only difference is that Code 1 uses Conv2D(filters=10,kernel_size=(3,3),...
while Code 2 uses Conv2D(10,3,3,...
. Moreover filters
and kernel_size
are also the same. However, the output_shape
are completely different.
Why is this? Can someone please explain? I couldn't find anything on keras official documentation (https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D).
https://stackoverflow.com/questions/65417415/tf-keras-layers-conv2d-usage December 23, 2020 at 08:03AM
没有评论:
发表评论