top of page
  • last_theorem

Convolutional neural net Part 2 kernel

Updated: Oct 24, 2021

There are certain components of CNN we need to understand here, we are going to look into each of them and see how they work together.


Kernels:

This is a concept adopted from image processing, although there are different image formats, How does the computer see the image data?. In simple, a computer sees the image data as pixel values in a matrix /Tensors. If you manipulate the matrix /Tensors, then you are manipulating the image itself. Consider you have an image data you need to rotate, this is something called transformation operations, you could multiply the image(matrix) with a transformation matrix this results in a new metric which would be your rotated image. This matrix which we use to transform (rotate) the input image is called a kernel. It basically acts as a mask, a particular operation (addition, subtraction, multiplication ) can derive you the desired image operation(in this case its rotation).

These kernels are also called filters, rotation is not the only operation we can do with the kernels, implementing a gaussian blue, making the image sharper, eliminating certain parts of the image all of these are kernel operations. The filter in your Instagram is also kernel.






import cv2
import numpy as np
 
file_path="D:\Google Drive\File transfer_\IMG_20190530_091107__02 (2).jpg"
 
 
 
from PIL import Image
 
image = Image.open(file_path)
new_image = image.resize((125, 150))
new_image
 
pix = np.array(new_image)
size = 15
# generating the kernel
kernel_motion_blur = np.zeros((size, size))
kernel_motion_blur[int((size-1)/2), :] = np.ones(size)
kernel_motion_blur = kernel_motion_blur / size
# applying the kernel to the input image
output = cv2.filter2D(pix, -1, kernel_motion_blur)
cv2.imshow('Motion Blur', output)
cv2.waitKey(0)

A motion blur kernel averages the pixel values in a particular direction. It's like a direction allows a pass filter. A 3x3 horizontal motion-blurring kernel would look this








This will blur the image in a horizontal direction. You can pick any direction and it will work accordingly. The amount of blurring will depend on the size of the kernel. So, if you want to make the image blurrier, just pick bigger size for the kernel. To see the full effect, we have taken a 15x15 kernel in the preceding code. We then use to apply this filter2D kernel to the input image, to obtain the motion-blurred output.




Kernal vs Filter

Sometimes it's a bit hard to distinguish the difference between the kernel and filter, sometimes they are used interchangeably. There is a difference in between both these terms. A “Kernel” refers to a 2D array of weights. The term “filter” is for 3D structures of multiple kernels stacked together. For a 2D filter, the filter is the same as the kernel. But for a 3D filter and most convolutions in deep learning, a filter is a collection of kernels. Each kernel is unique, emphasizing different aspects of the input channel.


42 views0 comments

Recent Posts

See All

Comments


bottom of page