Coding Convolutional Neural Networks (CNN) with TensorFlow

Yash Khanna
Analytics Vidhya
Published in
3 min readAug 19, 2020
source https://cezannec.github.io/assets/cnn_intro/CNN_ex.png

Learn code for convolutional neural networks for computer vision and image processing by building a CNN model with TensorFlow

CNNs are one of the state of the art, Artificial Neural Network design architecture, with one of the best deep learning tools in areas such as image recognition and classification. The Basic Principle behind the working of CNN is the idea of Convolution, producing filtered Feature Maps stacked over each other.

We’ll be using MNIST dataset which is readily available in different libraries. We’ll be importing our dataset from tensorflow library.

Code has been written in a generic template so as to do very minimal modifications and can run on many datasets with very little change.

About CNN

Every CNN is made up of multiple layers, the three main types of layers are convolutional, pooling, and fully-connected.

  1. Convolution Layer (Conv Layer)

The Conv layer is the core building block of a Convolutional Neural Network. The primary purpose of Conv layer is to extract features from the input image.

The convolutional layer can be thought of as the feature extractor of this network, it learns to find spatial features in an input image. This layer is produced by applying a series of many different image filters, also known as convolutional kernels, to an input image. These filters are very small grids of values that slide over an image, pixel-by-pixel, and produce a filtered output image that will be about the same size as the input image. Multiple kernels will produce multiple filtered, output images.

2. Pooling Layer

Pooling layer reduces the size of feature maps by using some functions to summarize sub-regions, such as taking the average or the maximum value. Pooling works by sliding a window across the input and feeding the content of the window to a pooling function.

3. Fully Connected Layer

The Fully Connected layer is configured exactly the way its name implies: it is fully connected with the output of the previous layer. A fully connected layer takes all neurons in the previous layer (be it fully connected, pooling, or convolutional) and connects it to every single neuron it has.

Network Architecture we’re going to use in code

  • Convolution Layer(32 units), Filter shape:(5,5,1), Stride=1, Padding=’SAME’
  • Max pooling Layer, Window shape:(2,2), Stride=1, Padding=’SAME’
  • Convolution Layer(64 units), Filter shape:(5,5,32), Stride=1, Padding=’SAME’
  • Max pooling Layer, Window shape:(2,2), Stride=1, Padding=’SAME’
  • Dense Layer (1024 units)
  • Output Layer (10 units)

Code:

With minimal efforts, we managed to reach an accuracy of 96% which is not that bad for a classification task with 10 labels. This result has been achieved without least optimization of the convolutional neural network’s parameters, and also without any form of regularization.

For regularization we can tweak our cost function or optimizer or simply we can add a Dropout layer after dense layer. Dropout layer is used extensively to prevent overfitting.

To improve the performances, we could set up a more complex model architectures so as to refine the feature extraction.

Conclusion

Through this post, we were able to code the simple Convolutional Neural Network architecture using Python programming and the TensorFlow library.

Thanks for reading!

In case you have any doubts/feedback, kindly comment.

--

--

Yash Khanna
Analytics Vidhya
0 Followers
Writer for

Currently an undergrad Student at GSSIP University, Delhi,India.