【问题标题】:Convolution Neural Network in torch. Error when training the network火炬中的卷积神经网络。训练网络时出错
【发布时间】:2015-09-06 08:03:05
【问题描述】:

我正在尝试将我的卷积神经网络建立在以下教程的基础之上:

https://github.com/torch/tutorials/tree/master/2_supervised

问题是我的图像与教程中使用的图像尺寸不同。 (3x200x200)。我也只有两个班。

以下是我所做的更改:

在 1_data.lua 中更改要加载的数据集。

nfeats = 3
width = 200
height = 200
ninputs = nfeats*width*height

nclass,noutputs

在 3_loss.lua 和 4_train.lua 中。

我的模型与教程中训练的模型相同。为方便起见,我将代码放在下面:

  model = nn.Sequential()

  -- stage 1 : filter bank -> squashing -> L2 pooling -> normalization
  model:add(nn.SpatialConvolutionMM(nfeats, nstates[1], filtsize, filtsize))
  model:add(nn.Tanh())
  model:add(nn.SpatialLPPooling(nstates[1],2,poolsize,poolsize,poolsize,poolsize))
  model:add(nn.SpatialSubtractiveNormalization(nstates[1], normkernel))

  -- stage 2 : filter bank -> squashing -> L2 pooling -> normalization
  model:add(nn.SpatialConvolutionMM(nstates[1], nstates[2], filtsize, filtsize))
  model:add(nn.Tanh())
  model:add(nn.SpatialLPPooling(nstates[2],2,poolsize,poolsize,poolsize,poolsize))
  model:add(nn.SpatialSubtractiveNormalization(nstates[2], normkernel))

  -- stage 3 : standard 2-layer neural network
  model:add(nn.Reshape(nstates[2]*filtsize*filtsize))
  model:add(nn.Linear(nstates[2]*filtsize*filtsize, nstates[3]))
  model:add(nn.Tanh())
  model:add(nn.Linear(nstates[3], noutputs))

运行 doall.lua 文件时出现以下错误:

 ==> online epoch # 1 [batchSize = 1]   
 /home/torch/install/share/lua/5.1/torch/Tensor.lua:462: Wrong size for view. Input size: 64x47x47. Output size: 64x1600
 stack traceback:
 [C]: in function 'error'
 /home/torch/install/share/lua/5.1/torch/Tensor.lua:462: in function 'view'
 /home/torch/install/share/lua/5.1/nn/Reshape.lua:49: in function 'updateOutput'
 /home/torch/install/share/lua/5.1/nn/Sequential.lua:29: in function 'forward'
 4_train.lua:160: in function 'opfunc'
 /home/torch/install/share/lua/5.1/optim/sgd.lua:43: in function 'optimMethod'
 4_train.lua:184: in function 'train'
 doall.lua:77: in main chunk
 [C]: in function 'dofile'
 [string "_RESULT={dofile('doall.lua' )}"]:1: in main chunk
 [C]: in function 'xpcall'
 /home/torch/install/share/lua/5.1/trepl/init.lua:630: in function 'repl'
 .../torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk
 [C]: at 0x00406670 

我已经坚持了一天多。请帮忙。

【问题讨论】:

  • 我正在使用以下内核:image.gaussian1D(7)。但是我不知道我应该保持的输入图像大小以避免在卷积时碰到角落。有人可以解释一下逻辑吗?

标签: lua neural-network torch


【解决方案1】:

问题是本教程中的卷积神经网络已被设计为使用 32x32 像素的固定尺寸​​输入分辨率

在 2 个卷积/池化层之后,您会获得 64 个 5x5 分辨率的特征图。这为以下全连接层提供了 64x5x5 = 1,600 个元素的输入。

正如您在教程中看到的那样,有一个专用的 reshape 操作将 3D 输入张量转换为具有 1,600 个元素的 1D 张量:

-- nstates[2]*filtsize*filtsize = 64x5x5 = 1,600
model:add(nn.Reshape(nstates[2]*filtsize*filtsize))

当您使用更高分辨率的输入时,您会生成更高分辨率的输出特征图,这里 200x200 像素的输入会产生 64 个大小为 47x47 的输出特征图。这就是您收到此错误大小错误的原因。

所以你需要相应地调整重塑和跟随线性层:

model:add(nn.Reshape(nstates[2]*47*47))
model:add(nn.Linear(nstates[2]*47*47, nstates[3]))

【讨论】:

  • 非常感谢。我从来没有意识到这个模型的末尾有一个神经网络。我还有一个问题:给定任何图像,我想将其归类为“向日葵”和“不是向日葵”。如何才能做到这一点 ?我应该保持第二类是什么?所有图像的数据集 -{向日葵}?但这听起来在计算上是不可行的。这可能与我现有的框架吗?请帮忙。
猜你喜欢
  • 2018-05-01
  • 2019-01-14
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 2020-09-10
  • 2016-07-01
  • 2017-08-31
  • 2016-08-17
相关资源
最近更新 更多