[Tensorflow 2.0]Convolutional Neural Network (CNN)
By: A Ydobon
Tags: Machine Learning, Cnn, TensorFlow, Convolutional Network

IV. Create the Convolutional Base

i) First, we would like to simplify the way of constructing a model.

The following is the full model that we will be creating throughout this post.

model = tf.keras.Sequential([

tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),

tf.keras.layers.MaxPooling2D((2,2)),

tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),

tf.keras.layers.MaxPooling2D((2, 2)),

tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),

tf.keras.layers.Flatten(),

tf.keras.layers.Dense(64,activation='relu'),

tf.keras.layers.Dense(10,activation='softmax')

])

However, we can employ model.add method, so that we can skip repeating tf.keras.layers. The idea is that after creating a vacant sequential model, we will be adding layers.

model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))

model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

ii) What are convolution and MaxPooling?

1 — Convolution: the process of applying a kernel or filter to an image.

layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))

input_shape=(32,32,3)

We have covered this! we have an image with size image_height=32 , image_width=32 , with color_channels equal to 3 (color).

We have covered this! we have an image with size , , with color_channels equal to 3 (color). activation=‘relu’

relu is an abbreviation of Rectified Linear Unit and it is most commonly used activation function. This function returns 0 if the input is smaller or equal to 0 and if the input is positive, then the output will be equal to the input. It facilitates the ‘non-linear’ problems.

Whenever you are confused which activation function you have to employ, ‘relu’ is waiting for you.

is an abbreviation of and it is most commonly used activation function. This function returns 0 if the input is smaller or equal to 0 and if the input is positive, then the output will be equal to the input. It facilitates the ‘non-linear’ problems. Whenever you are confused which activation function you have to employ, ‘relu’ is waiting for you. (3,3): kernel is 3*3 grid

32 is the number of kernel filters

[Example] For simplicity, we will take a 6 by 6 pixel image with grey scale in which each pixel has a number between 0 (black) to 255 (white).

2*1 + 5*2+4*1 + 13*2 + 25*4 +15*2 +8*1+6*2+6*1=198

Then what about the values in the margin? Zero-padding can be an option.

We will imagine the 2 by 2 matrix is surrounded by 0s. i.e.,

0 0 0

0 1 0

0 8 2

0*1 +0*2+0*1+0*2+1*4+0*2+0*1+8*2 +2*1=4+16+2=22.

In the coding, we can do the zero-padding by assigning padding= ‘same’

i.e., model.add(layers.Conv2D(32, (3, 3), padding=’same’, activation=’relu’, input_shape=(32, 32, 3)))

zero-padding

Also, we can have ‘no padding’ which will simply not consider the values at the margin by not defining the padding variable (like in our example) or by defining padding= ‘valid’. In no-padding case, we can see that the size of the output is decreased to 4 by 4.

No padding

Although we are looking at an arbitrary kernel, depending on the types of filters, they all have names. Also, since we do not have negative or zero values in our convoluted grid, there is no room for ‘RELU’ but if there were it will interfere.

We can say that the training is a process of finding the best 3 by 3 kernel/filter. Each numbers in the filters is conceptually same as the weights in a linear regression.

2 — MaxPooling: pooling the maximum value in a convoluted image. It is a method of downsampling.

layers.MaxPooling2D((2, 2))

We have created a convoluted image grid, then we will be moving around our 2 by 2 kernel and pick the maximum value.

stride variable can also be assigned which is the number of pixels that the 2 by 2 kernel will slide across the convoluted image.

Can you see that the size of the new image decreased by half?