使用 torch7 模型测试单个图像

Testing a single image with torch7 model

我根据 link here 训练了我的模型。当我训练它时,它几乎达到了 90% 的准确率。我使用的是 vgg_bn_drop.lua 型号,您可以在 link 上找到它。但问题是,我不知道如何针对单个图像对其进行测试。

我知道如何测试模型。通过网络转发图像。

因此,测试模型需要 modelname:forward(image)。其中 modelname 是我训练的模型的名称, forward 用于转发模型, 'image' 是我要转发的图像位置。现在,我不知道该网络中单个图像的尺寸是多少。

所以, 我想做的是,拍照。假设图像的尺寸为 [3x32x32]。通过网络传递它并得到结果。这个网络有可能吗?

那里没有关于如何针对单个图像对其进行测试的文档。

到目前为止我尝试的是,

1) 声明一个大小为 (3x32x32) 的张量。让我们称之为图像。 `图像 = torch.Tensor(3x32x32)。 向前传递这个。

model:forward(image)

它产生错误 ...h/install/share/lua/5.1/nn/SpatialBatchNormalization.lua:68: only mini-batch supported (4D tensor), got 3D tensor instead

2) 我将图像重塑为 (1,3,32,32)

image = image:reshape(1,3,32,32) 前向传球

model:forward(image)

它产生错误 ...ch/torch/install/share/lua/5.1/nn/BatchNormalization.lua:67: only mini-batch supported (2D tensor), got 1D tensor instead

所以我尝试了一些方法。但无法弄清楚如何将单个图像传递到该网络。你能帮帮我吗?

模型定义为

require 'nn'

local vgg = nn.Sequential()
-- building block
local function ConvBNReLU(nInputPlane, nOutputPlane)
  vgg:add(nn.SpatialConvolution(nInputPlane, nOutputPlane, 3,3, 1,1, 1,1))
  vgg:add(nn.SpatialBatchNormalization(nOutputPlane,1e-3))
  vgg:add(nn.ReLU(true))
  return vgg
end
-- Will use "ceil" MaxPooling because we want to save as much feature space as we can
local MaxPooling = nn.SpatialMaxPooling

ConvBNReLU(3,64):add(nn.Dropout(0.3))
ConvBNReLU(64,64)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(64,128):add(nn.Dropout(0.4))
ConvBNReLU(128,128)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(128,256):add(nn.Dropout(0.4))
ConvBNReLU(256,256):add(nn.Dropout(0.4))
ConvBNReLU(256,256)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(256,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512)
vgg:add(MaxPooling(2,2,2,2):ceil())
vgg:add(nn.View(512))
vgg:add(nn.Dropout(0.5))
vgg:add(nn.Linear(512,512))
vgg:add(nn.BatchNormalization(512))
vgg:add(nn.ReLU(true))
vgg:add(nn.Dropout(0.5))
vgg:add(nn.Linear(512,10))

-- initialization from MSR
local function MSRinit(net)
  local function init(name)
    for k,v in pairs(net:findModules(name)) do
      local n = v.kW*v.kH*v.nOutputPlane
      v.weight:normal(0,math.sqrt(2/n))
      v.bias:zero()
    end
  end
  init'nn.SpatialConvolution'
end

MSRinit(vgg)
return vgg

好吧,错误很明显:nn.BatchNormalization 需要一个二维张量作为输入(一个批次),但收到一个一维张量。您向输入添加了批量维度 (image:reshape(1,3,32,32)),但通过您的网络时,维度丢失了。 nn.View 模块犯了这个错误。

假设模块是使用以下参数实例化的:

output_size = channels*height*width    -- (512 in your case)
view = nn.View(output_size)

并给定一个形状为 batch_size x channels x height x width (1x512x1x1) 的输入张量。 该模块现在必须决定是否需要 return 批处理或单个非批处理输出。

  1. 如果 batch_size > 1,答案很明显:batch_size*channels*height*widthoutput_size 的倍数 => 输入是一个批次 => 输出必须是一个批次。
  2. 如果batch_size == 1,那怎么办? 1*channels*height*width == output_size,输入的是不是批次? nn.View 假定它不是并生成单个输出(没有批次维度)。

为了消除误解,可以指定 NB 个非批次维度(如果输入有 NB+1 个维度,则为批次):

view:setNumInputDims(NB)

鉴于以上所述,这将解决您的问题:

vgg:add(nn.View(512):setNumInputDims(3))