在 Torch 中填充一个张量

Pad a Tensor in Torch

我在 Torch 张量中有一个 100x100 像素的图像,我想实现 "zoom out" 转换。如何使用 Torch Image 工具箱(或其他工具箱)实现此目的?

我已经通过简单地使用 image.crop 和 image.resize 实现了 "zoom in"。

在 Matlab 中,我会计算图像的平均灰度,用该颜色填充数组 n 个像素(保持原始图像居中),然后将大小调整为 100x100 像素。里面有 Torch 的 "pad Tensor" 函数吗?

谢谢!

Is there a "pad Tensor" function for Torch?

一种可能是使用 nn.Padding module from torch/nn,例如:

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

更新

可以看出implementation padding是通过以下方式获得的:

  1. 分配预期大小的输出张量post-padding 填充颜色,
  2. 用原始值填充输入张量对应的区域感谢narrow

玩具示例:

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)

输出为:

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

对于特定的零填充,还有其他方便的可能性,例如: