CNN Basics — Part 1. CNN’s are a type of space-invariant…
By: Ozgur Guler
Tags: TensorFlow, Keras, Machine Learning, Convolution Neural Net, Computer Vision

CNN’s are a type of space-invariant neural network that can recognise and classify features in images and finds applications in image classification, segmentation and object detection . The connectivity between CNN layers were inspired from mammal visual cortex, where individual neurons respond to stimuli only in a restricted field of the receptive field.

Convolution operation (denoted as *) is superposing the filter onto the image and calculating the sum of the pairwise multiplication of the filter elements and image pixels. Filter is “convolved” — moved through the image with a stride length of s which determines the number of pixels the filter will be shifted for each convolution.(In Keras this corresponds to the Conv2D method).

Image 1 — Convolution Operation

In Computer Vision images are processed as 2D (greyscale )or 3D (RGB ) matrices. In a CNN, for a greyscale image of size (n,n) a submatrix (denoted as a filter or a kernel) of size (m,m) is convolved through the image producing an output matrix of size (n-m+1, n-m+1). (filter will be a 3d matrix for an RGB image). In general the output matrix size will be ((n+2p-f)/s +1, (n+2p-f)/s +1, f = number of filters)(p is padding size, s is stride length, f is the length of the filter.). Kernels are used to extract the features in the image. By combining multiple kernels we create filters that can detect multiple different features from the image. Multiple layers make up a convolution layer.

Filter function can be used for edge detection. e.g. Previtt Kernel [1,1,1;0,0,0;-1,-1,-1] when convolved through an image will help detect the edges i.e. in the output matrix the possible edge locations will have non-zero values (e.g. positive results if edge is a transition from light to dark or negative values if the transition is from dark to light). Edge detection can be extended to find vertical, horizontal edges or edges at any angle using the right kernel for the convolution e.g. Sobell filter, Scharr filter or train and find the best kernel for a task…)

Convolution operation has two problems. First is that the output matrix is smaller, the image shrinks after the convolution. For a NN with >50 layers, if the kernels are chosen such that the image shrinks at each layer, the final image will be much smaller than the original image. This will be a major issue as most of the information in the image would have been lost. Secondly, with convolution, edge pixels are processed less than the pixels that are in the centre and therefore a lot of the information in the edge pixels are lost. To address those two issues we deploy padding e.g. additional layer of p pixels around the image which are by convention set to 0. If we choose padding p such that, the input and output matrix sizes are the same this is called “same convolution”. It can be shown that for same convolution the padding size p needs to be equal to (f-1)/2. Therefore f -filter size is usually an odd number. If there is no padding, this is called “valid convolution”. If the stride length s is >1 than, such convlutions are called “strided convolution”’s.

For an RGB image of size (m, m, l), filter will be (n,n,l) and the output matrix will be (m-n+1, m-n+1). If f different filters are used (e.g. to detect borders at different angles), output matrix will be of size (m-n+1, m-n+1, f) (since each individual filter will create an output matrix of size (m-n_1, m-n+1) .

Other than convolutional layers, CNN’s often also use pooling layers to reduce the size of the representation, to speed the computation, as well as make some of the features that detects a bit more robust.

The pooling (POOL) layer reduces the height and width of the input. It helps reduce computation, as well as helps make feature detectors more invariant to its position in the input.With maxpooling we convolve a filter through our image matrix and simply take the max value of cells exposed to the filter position. Hyperparameters used are f- filter size and s- stride. Common values are f=2, s=2 (p=0 no padding in general with maxpooling). There are no parameters to learn, maxpooling is a fixed function.

The benefits CNN brings to image processing are parameter sharing and sparsity of the connections. Parameter sharing is the same filter being able to detect edges at various points within the image. Sparsity of connections is that in the output matrix each element is produced by a very small subset of the matrix itself. These two features make CNN’s robust and able to produce results with smaller datasets.

CNN’s were the models that allowed computer vision to scale from simple applications to powering sophisticated computer vision models. Little dependence on pre processing and its simplicity makes CNN’s easy to understand and fast to implement. It has the highest accuracy among all alghoritms that predicts images which makes the CNN’s currently the most powerful tool in a CV pratitioner’s toolset.

I will continue digging deeper in CNN’s in further blog posts…