使用Torch7模型测试单个图像 [英] Testing a single image with torch7 model
问题描述
我根据链接此处训练了我的模型.当我训练它时,它几乎达到了90%
的精度.我正在使用在链接上找到的vgg_bn_drop.lua
模型.但是问题是,我不知道如何对单个图像进行测试.
I trained my model based on the link here. It achieved almost 90%
accuracy when i trained it. I am using the vgg_bn_drop.lua
model that you will find on the link. But the problem is, i don't know how to test it for a single image.
我知道如何测试模型.通过将图像转发通过网络.
I know how to test a model. By forward passing the image through the network.
因此,测试模型将需要modelname:forward(image)
.其中modelname
是我训练的模型的名称,而forward
用于转发模型,而'image'是我要转发的图像位置.现在,我无法确定该网络中单个图像的尺寸.
So, testing the model will require modelname:forward(image)
. Where the modelname
is the name of the model that i trained and forward
is used to forward the model and 'image' is the image location that i want to forward. Now, I cannot figure what the dimension of the single image will be from this network.
所以, 我想做的是拍摄一张照片.假设图片的尺寸为[3x32x32].通过网络传递并获得结果.这个网络有可能吗?
So, what i want to do is, take an image. Say the image has dimension [3x32x32]. Pass it through the network and get the result. Is it possible with this network?
那里没有文档来测试单个图像.
There was no documentation there how to test it for a single image.
到目前为止,我尝试过的是
What i tried so far was,
1) 声明一个张量大小(3x32x32).让我们称之为图像. `image = Torch.Tensor(3x32x32). 向前传递.
1) Declare a tensor of size (3x32x32). Lets call it image. `image = torch.Tensor(3x32x32). forward pass this.
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)
2) I reshaped the image to (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
所以我尝试了一些方法.但是无法弄清楚如何将单个图像传递到该网络.你能帮我吗?
So I tried out approaches. But could not figure out how to pass a single image to that network. Can you help me out?
模型定义为
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
期望将2D张量作为输入(批处理),但是接收到1D张量.您将批次维度添加到了输入(image:reshape(1,3,32,32)
),但是在通过网络时,维度丢失了. nn.View
模块对此有罪.
Well, the error is clear: nn.BatchNormalization
expects a 2D tensor as an input (a batch), but receives a 1D tensor. You added batch dimension to your input (image:reshape(1,3,32,32)
), but passing through your network, the dimension was lost. nn.View
module is guilty of this.
假设使用以下参数实例化了模块:
Suppose the module was instantiated with the following parameter:
output_size = channels*height*width -- (512 in your case)
view = nn.View(output_size)
,并为其提供形状为batch_size x channels x height x width
(1x512x1x1)的输入张量.
现在,该模块必须确定是要返回批处理还是单个非批处理输出.
and it is given an input tensor of shape batch_size x channels x height x width
(1x512x1x1).
The module now has to decide whether it is expected to return a batch or a single non-batch output.
- 如果
batch_size
> 1,答案很明显:batch_size*channels*height*width
是output_size
的倍数=>输入是批处理=>输出必须是批处理. - 如果
batch_size
== 1,那又是什么?1*channels*height*width
==output_size
,输入是否为批次?nn.View
假定不是,并产生单个输出(无批次尺寸).
- If
batch_size
> 1, the answer is obvious:batch_size*channels*height*width
is a multiple ofoutput_size
=> the input is a batch => the output must be a batch. - If
batch_size
== 1, what then?1*channels*height*width
==output_size
, is the input a batch or not?nn.View
assumes it's not and produces a single output (without batch dimension).
为解决误解,可以指定非批量尺寸的数量NB
(如果输入具有NB + 1尺寸,则为批量):
To fix the misunderstanding, one can specify the number NB
of non-batch dimensions (if input has NB+1 dimensions, it's a batch):
view:setNumInputDims(NB)
鉴于以上所述,这将解决您的问题:
In light of the above, this will solve your problem:
vgg:add(nn.View(512): setNumInputDims(3))
vgg:add(nn.View(512):setNumInputDims(3))
这篇关于使用Torch7模型测试单个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!