Dilated Convolution

02 Mar, 2022
Prerequisite: Convolutional Neural Networks
Dilated Convolution: It is a technique that expands the kernel (input) by inserting holes between its consecutive elements. In simpler terms, it is the same as convolution but it involves pixel skipping, so as to cover a larger area of the input. 
An additional parameter l (dilation factor) tells how much the input is expanded. In other words, based on the value of this parameter, (l-1) pixels are skipped in the kernel. Fig 1 depicts the difference between normal vs dilated convolution. In essence, normal convolution is just a 1-dilated convolution.
Fig 1: Normal Convolution vs Dilated Convolution
Intuition: 
Dilated convolution helps expand the area of the input image covered without pooling. The objective is to cover more information from the output obtained with every convolution operation. This method offers a wider field of view at the same computational cost. We determine the value of the dilation factor (l) by seeing how much information is obtained with each convolution on varying values of l.  
By using this method, we are able to obtain more information without increasing the number of kernel parameters. In Fig 1, the image on the left depicts dilated convolution. On keeping the value of l = 2, we skip 1 pixel (l – 1 pixel) while mapping the filter onto the input, thus covering more information in each step. 
Formula Involved: 
where,
F(s) = Input 
k(t) = Applied Filter
*l = l-dilated convolution
(F*lk)(p) = Output
Advantages of Dilated Convolution:   
Using this method rather than normal convolution is better as: 
Larger receptive field (i.e. no loss of coverage)Computationally efficient (as it provides a larger coverage on the same computation cost)Lesser Memory consumption (as it skips the pooling step) implementationNo loss of resolution of the output image (as we dilate instead of performing pooling)Structure of this convolution helps in maintaining the order of the data. 
Larger receptive field (i.e. no loss of coverage)
Computationally efficient (as it provides a larger coverage on the same computation cost)
Lesser Memory consumption (as it skips the pooling step) implementation
No loss of resolution of the output image (as we dilate instead of performing pooling)
Structure of this convolution helps in maintaining the order of the data. 
Code Implementation: 
Python3
import numpy as npimport tensorflow as tfimport sysfrom  scipy.signal import convolve2d np.random.seed(678)tf.random.set_seed(6789)sess = tf.compat.v1.Session() # Initializing a 9x9 matrix of zeros.mat_size = 9matrix = np.zeros((mat_size,mat_size)).astype(np.float32) # Assigning 1's in the middle of matrix# to create a random input matrixfor x in range(4,7):    for y in range(3,6):        matrix[y,x] = 1 # Creating a random kernel for testkernel = np.array([    [1,2,3],    [4,5,6],    [7,8,9]]).astype(np.float32) print("Original Matrix Shape : ",matrix.shape)print(matrix)print('\n')print("Original kernel Shape : ",kernel.shape)print(kernel) # self-initializing a dilated kernel.# ======[dilation factor = 3]======dilated_kernel = np.array([    [1,0,0,2,0,0,3],    [0,0,0,0,0,0,0],    [0,0,0,0,0,0,0],    [4,0,0,5,0,0,6],    [0,0,0,0,0,0,0],    [0,0,0,0,0,0,0],    [7,0,0,8,0,0,9]]) print('\n')print("Dilated kernel Shape : ",dilated_kernel.shape)print(dilated_kernel) print('\n')print("DILATED CONVOLUTION RESULTS [Dilation Factor = 3]")output = convolve2d(matrix,dilated_kernel,mode='valid')print("Numpy Results Shape: ",output.shape)print(output)
 Output 
The following output is obtained from the above code.  
Output
 
The output obtained is for a dilation factor of 3. For more experimentation, you can initialize the dilated_kernel with different values of the Dilation factor and observe the changes in the output obtained. 
rkbhola5
ddeevviissaavviittaa
Machine Learning
Python
Machine Learning
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
ML | Monte Carlo Tree Search (MCTS)
Introduction to Recurrent Neural Network
Markov Decision Process
Support Vector Machine Algorithm
DBSCAN Clustering in ML | Density based clustering
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
Python Dictionary
How to get column names in Pandas dataframe