Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is using im2col operation in convolutional nets more efficient?

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 filter in separate columns. But why shouldn't loops be used directly to calculate convolution instead of first performing im2col ?

like image 838
Ayush Chaurasia Avatar asked Sep 14 '17 07:09

Ayush Chaurasia


People also ask

What is the benefit of using convolution?

There are two main advantages of Convolutional layers over Fully\enspace connected layers: parameter sharing and. sparsity of connections.

What is a 1D convolution?

The 1D block is composed by a configurable number of filters, where the filter has a set size; a convolution operation is performed between the vector and the filter, producing as output a new vector with as many channels as the number of filters.

How does convolve work?

A convolution is the simple application of a filter to an input that results in an activation. Repeated application of the same filter to an input results in a map of activations called a feature map, indicating the locations and strength of a detected feature in an input, such as an image.

What is convolve in CNN?

In the context of CNN, convolution is a linear operation involving the multiplication of a set of weights with the input images represented by metrics similar to traditional neural networks. Here an array of weights is called a filter or kernel.


1 Answers

  1. 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.

  2. The Convolutional Layer and Fully Connected Layer are implemented using GEMM that stands for General Matrix to Matrix Multiplication.

  3. 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.

  4. 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.

  5. 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.

  6. Element wise convolution performs badly because of the irregular memory accesses involved in it.

  7. In turn, Im2col() arranges the data in a way that the memory accesses are regular for Matrix Multiplication.

  8. Im2col() function adds a lot of data redundancy though, but the performance benefit of using Gemm outweigh this data redundancy.

  9. This is the reason for using Im2col() operation in Neural Nets.

  10. 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/

like image 185
Abhishek Nikam Avatar answered Nov 21 '22 07:11

Abhishek Nikam