How Convolutional Neural Network works
By: Bartosz Szabłowski
Tags: Data Augmentation, Dropout, Convolutional Network, Kernel, Pooling

The network starts with the input, which is the image. It is passed sequentially into Convolutional and Pooling layers. Then all Fully connected layers end with the output layer. There are three basic layers, of which the architecture of the convolutional neural network is built. Such a formation allows to give the network an image at the input, and at the output obtain the probability of a class occurrence in the image.

Convolutional Layer

Pixels from the input image are not combined with every hidden neuron. Weave layers narrow the area of the input image (reception area) for each neuron. The size of this area depends on the size of the filter, called kernel. Neurons in the next layer connect only with other neurons in the previous layer. The kernel is a matrix with the dimensions h x w x d and is contained in the splot layer used to detect valuable object features. The letter d indicates the number of different filters used. The size of the filter is determined by the programmer. It is usually 3x3 or 5x5 and the number of filters used. It moves from the upper left to the lower right corner of the image and for each point in space the array elements are multiplied and summed up. The kernel moves one step forward or a random pixel.

Splot operation, Image by Author

Application of the Kernel in the Convolutional layer, Image by Author

Convolutional neural networks use multiple filters to find image features that will allow for object categorization. In the CNN scheme there are many kernels responsible for extracting these features. The edge kernel is used to highlight large differences in pixel values. If the pixel next to it has a similar intensity, it will be black after the transformation. A big difference between them will make it white. The blur kernel filter will blur the differences in the adjacent pixels and reducing noise in the image. The bottom Sobel kernel detects the edges in a specific direction, indicating the differences in adjacent pixels. The emboss kernel filter has a similar principle to the Sobel filter, emphasizing pixel differences in a specific direction, e.g. at a slant. The last filter presented is sharpening. The kernels presented in this article meet the required minimum, to explain the mechanism of their operation in the convolutional neural networks. A single kernel detects a specific feature from the input image area. Many neurons using the same kernel create a feature map. Feature map changes during model teaching with a minimized loss function. The splot layer consists of several feature maps, and the splot operation is performed independently for each filter. The resulting feature maps are detachable. The result of the splot operation is passed to the ReLU activation function. At the output of the splot layer, the feature maps merge and become the final output from it. Values in the filter matrix are updated with the backpropagation. More kernels affect the number of detected features, extending the model training time. During the splot operation the lateral pixels are removed and information is lost. This can be avoided if the input image is surrounded by pixels of 0 (padding) or by copying the nearest pixel. The first stranded layers specialize in recognizing small features. The next ones are responsible for capturing more complex features.

Pooling Layer

The Pooling Layer subsamples the output image from the Convolutional Layer returning a maximum aggregate value (Max Pooling). It also has an option of averaging (Average Pooling), minimizing or converting to another function. The Pooling Layer in this case collects only data using a specific aggregation function. Its task is to halve the size of the height and width of the matrix, leaving the depth (channels) intact. Thanks to this procedure we reduce the risk of overfitting by determining the most influential features of the Pooling Layer. Such a procedure allows training the model faster.

Max Pooling Layer, Image by Author

As in the Convolutional Layer, the neurons of the Pooling Layer merge with other neurons in the previous layer, which are in the reception area. In the above example, a MaxPooling layer of 2 x 2, stride 2 is added. It is moved over the input matrix and for each point in space, the maximum value in the output matrix is placed. The height and width of the output data will be twice as small, reducing the output area four times. CNN presents the best results when alternate layers of Convolutional and Pooling are applied.

Fully Connected Layer

Input data to a fully connected layer is the output data from the last connecting layer. The task of the FC layer is to flatten the value in the vector. This vector is combined with other fully connected layers, creating on top of the Convolutional Neural Network simple neural network. The last layer is responsible for determining the probability of belonging to a particular class. If we are dealing with a multiclassification, the softmax activation function is used. In case of binary classification, the sigmoid function is used.