# How can Tensorflow be used to create a convolutional base using Python?

PythonServer Side ProgrammingProgrammingTensorflow

A convolutional neural network would generally consist of combination of the following layers: Convolutional layers, Pooling layers and Dense layers.

Convolutional neural networks have been used to produce great results for a specific kind of problems, such as image recognition. It can be created using the ‘Sequential’ method which is present in the ‘models’ class. Layers can be added to this convolutional network using the ‘add’ method.

We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

print("Creating the convolutional base")
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
print("Description of arhcitecture is")
model.summary()

Code credit: https://www.tensorflow.org/tutorials/images/cnn

## Output

Creating the convolutional base
Description of arhcitecture is
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d (Conv2D)              (None, 30, 30, 32)        896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32)        0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 13, 13, 64)        18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64)          0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 4, 4, 64)          36928
=================================================================
Total params: 56,320
Trainable params: 56,320
Non-trainable params: 0

## Explanation

• The above lines help define the convolutional base using a common pattern.

• This pattern is a stack of Conv2D and MaxPooling2D layers.

• As input, which is a CNN takes tensors of shape (image_height, image_width, color_channels), is taken.

• CNN is configured to process inputs of shape (32, 32, 3), which is the format of CIFAR images.

• This can be done by passing the argument input_shape to our first layer.

• The output of every Conv2D and MaxPooling2D layer is a 3D tensor of shape (height, width, channels).

• The width and height dimensions shrink as the network gets deeper.

• The number of output channels for every Conv2D layer is controlled by the first argument (e.g., 32 or 64).

Published on 11-Feb-2021 09:18:26