The convolution layer uses filters that perform convolution operations as it is
scanning the input I with respect to its dimensions. Lets understand working of 2D convolution layer
with an example. 2D convolution layer can be used from tf.keras.layers
api.
Use Colab notebook for executing code snippets.
In this post we will touch following TensorFlow modules and functions.
tensorflow-datasets
for loading the image dataset tf.expand_dims
and
tf.dtypes.cast
for changing image dimensions and dtype tf.keras.layers.Conv2D
for using the 2D convolution layer plt.imshow
for displaying the imagecaltech101
dataset with tensorflow datasets
import tensorflow as tf
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
ds, dsinfo = tfds.load('caltech101', split='train', with_info=True)
for example in list(ds)[2:3]:
image, label = example['image'], example['label']
print(image.shape)
print(label)
plt.imshow(image)
(225, 300, 3)
tf.Tensor(51, shape=(), dtype=int64)
tf.keras.layers.Conv2D
image = tf.expand_dims(image, 0)
image = tf.dtypes.cast(image, tf.float32)
print(image.shape)
(1, 225, 300, 3)
tf.keras.layers
and provide input image
Number of feature maps generated with 2D convolution layer depends on integer value provided to the
filter argument in the layer, in this example we have filter=5
,
hence 5 feature maps would be generated.
Shape of feature map is controlled with stride argument provided in the layer, in this example
we have stride =(1,1)
, so shape of feature map would be same as of provided input image,
in case we choose stride =(2,2)
then shape of feature map would be half of the size of provided input.
x = tf.keras.layers.Conv2D(filters=5, kernel_size=(2,2), strides=(1,1))(image)
print(x.shape)
(1, 224, 299, 5) # notice change in last dimension as it got changed from 3 to 5 because of filter argument
tf.keras.layers.Conv2D
is 5, all the feature maps can be viewed with following code
plt.imshow(x[0, :, :, 0])
,
plt.imshow(x[0, :, :, 1])
,
plt.imshow(x[0, :, :, 2])
,
plt.imshow(x[0, :, :, 3])
,
plt.imshow(x[0, :, :, 4])
. Below is the example output for first feature map.
Category: TensorFlow