I am trying to implement a convolutional neural netwrok and I don\'t understand why using im2col operation is more efficient. It basically stores the input to be multiplied by f
Well, you are thinking in the right way, In Alex Net almost 95% of the GPU time and 89% on CPU time is spent on the Convolutional Layer and Fully Connected Layer.
The Convolutional Layer and Fully Connected Layer are implemented using GEMM that stands for General Matrix to Matrix Multiplication.
So basically in GEMM, we convert the convolution operation to a Matrix Multiplication operation by using a function called im2col()
which arranges the data in a way that the convolution output can be achieved by Matrix Multiplication.
Now, you may have a question instead of directly doing element wise convolution, why are we adding a step in between to arrange the data in a different way and then use GEMM.
The answer to this is, scientific programmers, have spent decades optimizing code to perform large matrix to matrix multiplications, and the benefits from the very regular patterns of memory access outweigh any other losses. We have an optimized CUDA GEMM API in cuBLAS library, Intel MKL has an optimized CPU GEMM while ciBLAS's GEMM API can be used for devices supporting OpenCL.
Element wise convolution performs badly because of the irregular memory accesses involved in it.
In turn, Im2col()
arranges the data in a way that the memory accesses are regular for Matrix Multiplication.
Im2col()
function adds a lot of data redundancy though, but the performance benefit of using Gemm outweigh this data redundancy.
This is the reason for using Im2col()
operation in Neural Nets.
This link explains how Im2col()
arranges the data for GEMM:
https://petewarden.com/2015/04/20/why-gemm-is-at-the-heart-of-deep-learning/