Search code examples
imageimage-processingluatransformtorch

Pad a Tensor in Torch


I have a 100x100 pixel image in a Torch Tensor and I want to implement a "zoom out" transformation. How can I achieve this using the Torch Image toolbox (or other)?

I have already implemented "zoom in" by simply using image.crop followed by image.resize.

In Matlab, I would calculate the mean grayscale of the image, pad the array n pixels with that colour (keeping the original image centred), and then resize to 100x100 pixels. In there a "pad Tensor" function for Torch?

Thanks!


Solution

  • Is there a "pad Tensor" function for Torch?

    One possibility is to use the nn.Padding module from torch/nn, e.g.:

    require 'image'
    require 'nn'
    
    local x = image.lena()
    
    local pad  = 64
    local pix  = 0
    local ndim = x:dim()
    
    local s = nn.Sequential()
      :add(nn.Padding(ndim-1,  pad, ndim, pix))
      :add(nn.Padding(ndim-1, -pad, ndim, pix))
      :add(nn.Padding(ndim,    pad, ndim, pix))
      :add(nn.Padding(ndim,   -pad, ndim, pix))
    
    local y = s:forward(x)
    
    image.display(y) -- this requires qlua
    

    UPDATE

    As can be seen in the implementation padding is obtained by:

    1. allocating an output tensor of the expected size post-padding filled with the padding color,
    2. filling the area corresponding of the input tensor with the original values thanks to narrow.

    Toy example:

    require 'torch'
    
    local input  = torch.zeros(2, 5)
    local dim    = 2 -- target dimension for padding
    local pad    = 3 -- amount of padding
    local pix    = 1 -- pixel value (color)
    
    -- (1) compute the expected size post-padding, allocate a large enough tensor
    --     and fill with expected color
    local size   = input:size()
    size[dim]    = size[dim] + pad
    local output = input.new():resize(size):fill(pix)
    
    -- (2) fill the original area with original values
    local area   = output:narrow(dim, 1, input:size(dim)):copy(input)
    

    This gives as output:

    0  0  0  0  0  1  1  1
    0  0  0  0  0  1  1  1
    [torch.DoubleTensor of size 2x8]
    

    For specific zero-padding there are other convenient possibilities like: