31 December 2016

Torch is an amazing LuaJit/C/CUDA framework for scientific computing and it’s mainly used to develop and train neural networks.

Now, in order to blur an image we have to convolve it with a blurring kernel (also called point-spread-function). Convolutions usually are Torch’s bread and butter.
Unfortunately though, the image.conv2 function we would normally use to convolve an image with a specified kernel does not support performing the operation on GPU, using CUDA. This could be an issue since pretty much any meaningful Torch setup runs in GPU and CPU <–> GPU memory transfers are very slow.

So what to do in case we want to blur, or otherwise convolve an image with a given kernel, on GPU?

We’ll just have to repurpose a nn.SpatialConvolution layer, normally used as a building block for convolutional neural networks, and perform a forward pass! The general procedure is as follows:

  • Instantiate a nn.SpatialConvolution layer with input and output channels matching the number of channels in the input image, so 1 for Grayscale and 3 for RGB.

  • Generate our convolution kernel, Gaussian or otherwise

  • Set the nn.SpatialConvolution weights relative to matching input/output channels to our convolution kernel; set all other weights and biases to 0

  • Move the now ready nn.SpatialConvolution layer to GPU

  • Perform a forward pass on the input image


Here’s a sample Gist for your convenience!