Introduction to convolutions using Python

Learning Features
Capability Development or Feature Extraction — it is the process of extracting useful patterns from the input data that will help the forecasting model better understand the real nature of the problem. A good learning function will expose patterns in a way that dramatically improves the accuracy and performance of the applied machine learning algorithms, which would be impossible or too expensive for machine learning itself. The function learning algorithms find common patterns that are important for distinguishing between the required classes and automatically extract them. After this process, they are ready for use in classification or regression problems. 
Let`s take a look at the popular image classification problem, classification of face and faceless face images. In the early days of computer vision, scientists tried to solve this problem by hand coding algorithms to detect possible features of a human face, such as shape, eyes, nose, lips, etc. This approach usually gave poor results because a face can look like so many varieties. that it was impossible to explain even a significant portion of the signs. A simple change in lighting or orientation can change the image in such a way that algorithms can no longer recognize faces. 
In 1998, Yang Lecun introduced the concept of convolutional neural networks, which is capable of classifying images of handwritten characters with an accuracy of about 99 %. The big advantage of convolutional neural networks is that they are extraordinarily good at detecting features in images that grow after each level, resulting in high-level features. The final layers (can be one or more) use all of these generated features for classification or regression. 
convolution
convolution — it is an operation that is performed on an image to extract elements from it, applying a smaller tensor, called a kernel, like a sliding window over the image. Depending on the values ​​in the convolutional kernel, we can pick up certain patterns from the image. In the following example, we will demonstrate how to detect horizontal and vertical edges in an image using the appropriate kernels.

python3

import numpy as np

import matplotlib.pyplot as plt

 
# let img1 be unremarkable image

img1 = np. array ([np.array ([ 200 , 200 ]), np.array ([ 200 , 200 ])])  

img2 = np.array ([np.array ( [ 200 , 200 ]), np.array ([ 0 , 0 ])])

img3 = np.array ([np.array ([ 200 , 0 ]), np.array ([ 200 , 0 ])])

  

kernel_horizontal = np .array ([np.array ([ 2 , 2 ]), np.array ([ - 2 , - 2 ])])

print (kernel_horizontal, `is a kernel for detecting horizontal edges ` )

  

kernel_vertical = np.array ([np.array ([ 2 , - 2 ]), np.array ([ 2 , - 2 ])])

print (kernel_vertical, `is a kernel for detecting vertical edges` )

 
# We`ll apply kernels to images
# element-wise multiplication followed by addition

def apply_kernel (img, kernel):

return np. sum (np.multiply (img, kernel ))

 
# Img1 rendering
plt.imshow (img1)

plt.axis ( `off ` )

plt.title ( ` img1` )

plt.show ()

  
# Checking horizontal and vertical objects in image1

print ( `Horizontal edge confidence score:` , apply_kernel (img1, 

kernel_horizontal))

print ( `Vertical edge confidence score:` , apply_kernel (img1 , 

kernel_vertical))

 
# Img2 rendering
plt.imshow (img2)

plt.axis ( `off` )

plt.title ( `img2` )

plt.show ()

  
# Checking horizontal and vertical objects in image2

print ( `Horizontal edge confidence score:` , apply_kernel (img2, 

kernel_horizontal))

print ( `Vertical edge confidence score:` , apply_kernel (img2, 

kernel_vertical))

 
# Img3 rendering
plt.imshow (img3)

plt.axis ( `off` )

plt.title ( ` img3` )

plt.show ()

 
# Checking horizontal and vertical objects in image3

print ( `Horizontal edge confidence score:` , apply_kernel (img3, 

kernel_horizontal))

print ( `Vertical edge confidence score:` , apply_kernel (img3, 

kernel_vertical))


Exit:

[[ 2 2]
[-2 -2]] is a kernel for detecting horizontal edges
[[2 -2]
[2 -2]] is a kernel for detecting vertical edges

Horizontal edge confidence score: 0
Vertical edge confidence score: 0

Horizontal edge confidence score: 800
Vertical edge confidence score: 0

Horizontal edge confidence score: 0
Vertical edge confidence score: 800