我根据链接这里训练我的模型。当我训练它时,它几乎达到了90%的精度。我正在使用vgg_bn_drop.lua模型,您可以在链接中找到该模型。但问题是,我不知道如何测试它的单个图像。
我知道怎么测试模型。通过前向通过网络传递图像。
因此,测试该模型将需要modelname:forward(image)。其中,modelname是我训练过的模型的名称,forward用于转发模型,而' image‘是我想要转发的图像位置。现在,我无法从这个网络中计算出单个图像的维数。
所以,我想做的是,拍一张照片。假设图像的维数为3x32x32。通过网络传递并得到结果。这个网络有可能吗?
没有关于如何测试单个图像的文档。
到目前为止我尝试的是,
1)声明一个大小的张量(3x32x32)。让我们称它为图像。‘`image = 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发布于 2016-03-31 19:50:41
错误很明显:nn.BatchNormalization期望一个2D张量作为输入(一批),但是接收一个一维张量。您在输入(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)形状的输入张量。模块现在必须决定它是返回批处理还是返回单个非批处理输出。
batch_size > 1,答案很明显:batch_size*channels*height*width是output_size =>的倍数,输入是批处理=>,输出必须是批处理。batch_size == 1,那么怎么办?1*channels*height*width == output_size,输入是批处理还是非批处理?nn.View假设输入不是,并生成单个输出(没有批处理维度)。为了纠正误解,可以指定非批处理维度的NB数(如果输入有NB+1维度,则为批处理):
view:setNumInputDims(NB)鉴于上述情况,这将解决您的问题:
vgg:add(nn.View(512):setNumInputDims(3))
https://stackoverflow.com/questions/36315851
复制相似问题