Convolution Neural Network in torch. Error when training the network

2k views Asked by At

I am trying to base my Convolution neural network upon the following tutorial:

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

The issue is that my images are of different dimensions than those used in the tutorial. (3x200x200). Also I have only two classes.

The following are the changes that I made :

Changing the dataset to be loaded in 1_data.lua.

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

and

nclass,noutputs

in 3_loss.lua and 4_train.lua.

My model is the same as that being trained in the tutorial. For Convenience I'll put the code below :

  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))

I get the following error when I run the doall.lua file :

 ==> 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 

I have been stuck at this for over a day now. Please help.

1

There are 1 answers

1
deltheil On BEST ANSWER

The problem is the convolutional neural network from this tutorial has been made to work with a fixed size input resolution of 32x32 pixels.

Right after the 2 convolutional / pooling layers you obtain 64 feature maps with a 5x5 resolution. This gives an input of 64x5x5 = 1,600 elements for the following fully-connected layers.

As you can see in the tutorial there is a dedicated reshape operation that transforms the 3D input tensor into a 1D tensor with 1,600 elements:

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

When you work with a higher resolution input you produce higher resolution output feature maps, here a 200x200 pixels input gives 64 output feature maps of size 47x47. This is why you obtain this wrong size error.

So you need to adapt the reshape and following linear layers accordingly:

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