AI03, Convolutional neural networks
Back to the previous page
List of posts to read before reading this article
Contents
- Convolution Operation
- Analog and Digital Signals
- 2D Convolution
- Common Image-Processing Filters
- Convolution Neural Networks
- Components of Convolution Neural Networks
- Backpropagation Through the Convolutional Layer
- Backpropagation Through the Pooling Layers
- Weight Sharing Through Convolution and Its Advantages
- Translation Equivariance
- Translation Invariance Due to Pooling
- Dropout Layers and Regularization
- Convolutional Neural Network for Digit Recognition on the MNIST Dataset
- Convolutional Neural Network for Solving Real-World Problems
- Batch Normalization
- Different Architectures in Convolutional Neural Networks
- Transfer Learning
Convolution Operation
OUTPUT
Analog and Digital Signals
OUTPUT
2D Convolution
2D Convolution of an Image to Different LSI System Responses
## Illustate 2D convolution of images through an example
import scipy.signal
import numpy as np
# Take a 7x7 image as example
image = np.array([[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 32, 33, 34, 35],
[36, 37, 38, 39, 40, 41, 42],
[43, 44, 45, 46, 47, 48, 49]])
# Defined an image-processing kernel
filter_kernel = np.array([[-1, 1, -1],
[-2, 3, 1],
[2, -4, 0]])
# Convolve the image with the filter kernel through scipy 2D convolution to produce an output image of same dimension as that of the input
I = scipy.signal.convolve2d(image, filter_kernel,mode='same', boundary='fill', fillvalue=0)
print(I)
# We replicate the logic of a scipy 2D convolution by going through the following steps
# a) The boundaries need to be extended in both directions for the image and padded with zeroes.
# For convolving the 7x7 image by 3x3 kernel, the dimensions need to be extended by (3-1)/2—i.e., 1—
#on either side for each dimension. So a skeleton image of 9x9 image would be created
# in which the boundaries of 1 pixel are pre-filled with zero.
# b) The kernel needs to be flipped—i.e., rotated—by 180 degrees
# c) The flipped kernel needs to placed at each coordinate location for the image and then the sum of
#coordinate-wise product with the image intensities needs to be computed. These sums for each coordinate would give
#the intensities for the output image.
row,col=7,7
## Rotate the filter kernel twice by 90 degrees to get 180 rotation
filter_kernel_flipped = np.rot90(filter_kernel,2)
## Pad the boundaries of the image with zeroes and fill the rest from the original image
image1 = np.zeros((9,9))
for i in range(row):
for j in range(col):
image1[i+1,j+1] = image[i,j]
print(image1)
## Define the output image
image_out = np.zeros((row,col))
## Dynamic shifting of the flipped filter at each image coordinate and then computing the convolved sum.
for i in range(1,1+row):
for j in range(1,1+col):
arr_chunk = np.zeros((3,3))
for k,k1 in zip(range(i-1,i+2),range(3)):
for l,l1 in zip(range(j-1,j+2),range(3)):
arr_chunk[k1,l1] = image1[k,l]
image_out[i-1,j-1] = np.sum(np.multiply(arr_chunk,filter_kernel_flipped))
print(image_out)
OUTPUT
[[ -2 -8 -7 -6 -5 -4 28]
[ 5 -3 -4 -5 -6 -7 28]
[ -2 -10 -11 -12 -13 -14 28]
[ -9 -17 -18 -19 -20 -21 28]
[-16 -24 -25 -26 -27 -28 28]
[-23 -31 -32 -33 -34 -35 28]
[-29 13 13 13 13 13 27]]
[[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 2. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 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 0.]
[ 0. 8. 9. 10. 11. 12. 13. 14. 0.]
[ 0. 15. 16. 17. 18. 19. 20. 21. 0.]
[ 0. 22. 23. 24. 25. 26. 27. 28. 0.]
[ 0. 29. 30. 31. 32. 33. 34. 35. 0.]
[ 0. 36. 37. 38. 39. 40. 41. 42. 0.]
[ 0. 43. 44. 45. 46. 47. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 1. 2. 3. 4. 5. 6. 7. 0.]
[ 0. 8. 9. 10. 11. 12. 13. 14. 0.]
[ 0. 15. 16. 17. 18. 19. 20. 21. 0.]
[ 0. 22. 23. 24. 25. 26. 27. 28. 0.]
[ 0. 29. 30. 31. 32. 33. 34. 35. 0.]
[ 0. 36. 37. 38. 39. 40. 41. 42. 0.]
[ 0. 43. 44. 45. 46. 47. 48. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 1. 2. 3. 4. 5. 6. 7. 0.]
[ 0. 8. 9. 10. 11. 12. 13. 14. 0.]
[ 0. 15. 16. 17. 18. 19. 20. 21. 0.]
[ 0. 22. 23. 24. 25. 26. 27. 28. 0.]
[ 0. 29. 30. 31. 32. 33. 34. 35. 0.]
[ 0. 36. 37. 38. 39. 40. 41. 42. 0.]
[ 0. 43. 44. 45. 46. 47. 48. 49. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[ -2. -8. -7. -6. -5. -4. 28.]
[ 5. -3. -4. -5. -6. -7. 28.]
[ -2. -10. -11. -12. -13. -14. 28.]
[ -9. -17. -18. -19. -20. -21. 28.]
[-16. -24. -25. -26. -27. -28. 28.]
[-23. -31. -32. -33. -34. -35. 28.]
[-29. 13. 13. 13. 13. 13. 27.]]
Common Image-Processing Filters
Mean Filter
# Convolution of an Image with Mean Filter
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import convolve2d
import cv2
img = cv2.imread('monalisa.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray,cmap='gray')
mean = 0
var = 100
sigma = var**0.5
row,col = 650,442
gauss = np.random.normal(mean,sigma,(row,col))
gauss = gauss.reshape(row,col)
gray_noisy = gray + gauss
plt.imshow(gray_noisy,cmap='gray')
## Mean filter
Hm = np.array([[1,1,1],[1,1,1],[1,1,1]])/float(9)
Gm = convolve2d(gray_noisy,Hm,mode='same')
plt.imshow(Gm,cmap='gray')
OUTPUT
Median Filter
## Generate random integers from 0 to 20
## If the value is zero we will replace the image pixel with a low value of 0 that corresponds to a black pixel
## If the value is 20 we will replace the image pixel with a high value of 255 that corresponds to a white pixel
## We have taken 20 integers, out of which we will only tag integers 1 and 20 as salt and pepper noise
## Hence, approximately 10% of the overall pixels are salt and pepper noise. If we want to reduce it
## to 5% we can take integers from 0 to 40 and then treat 0 as an indicator for a black pixel and 40 as an indicator for a white pixel.
np.random.seed(0)
gray_sp = gray*1
sp_indices = np.random.randint(0,21,[row,col])
for i in range(row):
for j in range(col):
if sp_indices[i,j] == 0:
gray_sp[i,j] = 0
if sp_indices[i,j] == 20:
gray_sp[i,j] = 255
plt.imshow(gray_sp,cmap='gray')
## Now we want to remove the salt and pepper noise through a Median filter.
## Using the opencv Median filter for the same
gray_sp_removed = cv2.medianBlur(gray_sp,3)
plt.imshow(gray_sp_removed,cmap='gray')
##Implementation of the 3x3 Median filter without using opencv
gray_sp_removed_exp = gray*1
for i in range(row):
for j in range(col):
local_arr = []
for k in range(np.max([0,i-1]),np.min([i+2,row])):
for l in range(np.max([0,j-1]),np.min([j+2,col])):
local_arr.append(gray_sp[k,l])
gray_sp_removed_exp[i,j] = np.median(local_arr)
plt.imshow(gray_sp_removed_exp,cmap='gray')
OUTPUT
Gaussian Filter
Hg = np.zeros((20,20))
for i in range(20):
for j in range(20):
Hg[i,j] = np.exp(-((i-10)**2 + (j-10)**2)/10)
plt.imshow(Hg,cmap='gray')
gray_blur = convolve2d(gray,Hg,mode='same')
plt.imshow(gray_blur,cmap='gray')
gray_high = 0.1
gray_enhanced = gray + 0.025 * gray_high
plt.imshow(gray_enhanced,cmap='gray')
OUTPUT
Gradient-based Filters
OUTPUT
Sobel Edge-Detection Filter
# Convolution Using a Sobel Filter
Hx = np.array([[ 1,0, -1],[2,0,-2],[1,0,-1]],dtype=np.float32)
Gx = convolve2d(gray,Hx,mode='same')
plt.imshow(Gx,cmap='gray')
Hy = np.array([[ -1,-2, -1],[0,0,0],[1,2,1]],dtype=np.float32)
Gy = convolve2d(gray,Hy,mode='same')
plt.imshow(Gy,cmap='gray')
G = (Gx*Gx + Gy*Gy)**0.5
plt.imshow(G,cmap='gray')
OUTPUT
Convolution Neural Networks
Convolution Layer
def conv2d(x,W,b,strides=1):
x = tf.nn.conv2d(x,W,strides=[1,strides,strides,1],padding='SAME')
x = tf.nn.bias_add(x,b)
return tf.nn.relu(x)
Pooling Layer
def maxpool2d(x,stride=2):
return tf.nn.max_pool(x,ksize=[1,stride,stride,1],strides=[1,stride,stride,1], padding='SAME')
Components of Convolution Neural Networks
OUTPUT
Backpropagation Through the Convolutional Layer
OUTPUT
Backpropagation Through the Pooling Layers
OUTPUT
Weight Sharing Through Convolution and Its Advantages
OUTPUT
Translation Equivariance
OUTPUT
Translation Invariance Due to Pooling
OUTPUT
Dropout Layers and Regularization
OUTPUT
Convolutional Neural Network for Digit Recognition on the MNIST Dataset
OUTPUT
Convolutional Neural Network for Solving Real-World Problems
OUTPUT
Batch Normalization
OUTPUT
Different Architectures in Convolutional Neural Networks
LeNet
OUTPUT
AlexNet
OUTPUT
GoogLeNet(Inception)
OUTPUT
VGG16
OUTPUT
ResNet
OUTPUT
GoogLeNet-v4(Inception-v4)
OUTPUT
SENet
OUTPUT
Transfer Learning
Guidelines for Using Transfer Learning
OUTPUT
Transfer Learning with Google’s InceptionV3
OUTPUT
Transfer Learning with Pre-trained VGG16
OUTPUT
List of posts followed by this article
Reference