Prediction Error for cudnn.SpatialCrossEntropyCriterion

381 views
Skip to first unread message

Sindy Löwe

unread,
Jan 31, 2016, 9:27:35 AM1/31/16
to torch7
Hello,

I'm trying to build a fully convolutional network for semantic segmentation. I am using this gist (https://gist.github.com/soumith/fb1968f617808134b483) as a "deconvolution" layer and the cudnn.SpatialCrossEntropyCriterion.

The optim.ConfusionMatrix doesn't work, since the dimensions of my outputs and targets are too high. Is there another function I could use to get the error?

Thanks in advance!

soumith

unread,
Jan 31, 2016, 9:42:16 AM1/31/16
to torch7 on behalf of Sindy Löwe

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+un...@googlegroups.com.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at https://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/d/optout.

Francisco Vitor Suzano Massa

unread,
Jan 31, 2016, 9:44:58 AM1/31/16
to torch7
You can reshape your outputs and targets to be 2D in order to feed them to the confusion matrix. You will probably need some transpositions, but that should be fine.
Also, if you want a real deconvolution, use SpatialFullConvolution instead https://github.com/torch/nn/blob/master/doc/convolution.md#nn.SpatialFullConvolution

Sindy Löwe

unread,
Feb 5, 2016, 5:52:51 AM2/5/16
to torch7
Thank you!
I got it to work for a total of 10 images, but as soon as I increase the size of my dataset, I get this error message: 

cuda runtime error (77) : an illegal memory access was encountered at /tmp/luarocks_cutorch-scm-1-8582/cutorch/lib/THC/generic/THCStorage.c:147

I tried updating cunn, cudnn and cutorch, but it didn't do the trick.

I guess my data is too big for the confusionMatrix as the error disappears as soon as I don't use batchAdd anymore and it always appears after training a certain number of batches.

How can I solve this?

Etienne Perot

unread,
May 4, 2016, 5:36:48 AM5/4/16
to torch7
Hello,

I don't manage to use the cudnn.SpatialCrossEntropy criterion, 

Basically, my network spit out a CudaTensor, i cast it to double, & forward it inside the criterion like this :

criterion = cudnn.SpatialCrossEntropyCriterion()

...import data

local outputs = model:forward(inputs)
local f = criterion:forward(outputs:double(), targets)

/torch/install/share/lua/5.1/cudnn/init.lua:64: assertion failed!

Could you give some clues how to use it properly?

Thanks a lot in advance!

Etienne Perot

unread,
May 4, 2016, 5:40:33 AM5/4/16
to torch7
Nevermind I forgot to cast the SpatialCrossEntropyCriterion to cuda (with :cuda())
Now i have the same error about the confusion Matrix!

Anyway I'm interested if you got any insights on the confusion matrix as well...
Message has been deleted

Sindy Löwe

unread,
May 4, 2016, 9:24:20 AM5/4/16
to torch7
I didn't get the confusion matrix to work (I actually didn't try for much longer), so I wrote my own little script to calculate the error.

Here's the code:

    outputs = model:forward(inputs)
    _,prediction_sorted = outputs:float():sort(2, true)

    outLabel = opt.numClasses + 1  -- I set all the 255 labels in the VOC dataset to 22 (=opt.numClasses+1)
    local locsToConsider = labels:ne(outLabel)
    locsToConsider = locsToConsider:byte()

    local labelsCopy = labels:long()
    local sumImg = labelsCopy:add(prediction_sorted[{{},1,{},{}}]*outLabel) + 1
    local tmp = sumImg:maskedSelect(locsToConsider)
    tmp = tmp:float()
    local hs = torch.histc(tmp, outLabel*outLabel+1, 1, outLabel*outLabel+1)
    confcounts = confcounts:add(torch.reshape(hs[{{1,outLabel*outLabel}}],outLabel,outLabel))

    for i = 2, opt.numClasses+1 do
      label = confcounts[{i,{}}]:sum()
      result = confcounts[{{},i}]:sum()
      truePos = confcounts[{i,i}]
      single_accuracy[i-1] = 100 * truePos / (label + result - truePos)
    end


Hope it helps!

varghese alex

unread,
Apr 5, 2017, 6:48:59 AM4/5/17
to torch7
Hello Massa,

Based on reply, i did try the following on fully connected neural network

predictions= model:forward(input)    -- size [Batches, # of Classes, W, H]
targets = target                               -- size [Batches, W,H]
local reshape_prediction = prediction:reshape(Batches, # classes * W * H)
local reshape_target = target:reshape(Batches,  W * H)
confusion:batchAdd(reshape_prediction, reshape_target)

However I do get an error

/home/uavws/torch/install/bin/luajit: ...ws/torch/install/share/lua/5.1/optim/ConfusionMatrix.lua:117: bad argument #1 to 'indexAdd' (out of range at /home/uavws/torch/pkg/torch/lib/TH/generic/THTensor.c:738)
stack traceback:
[C]: in function 'indexAdd'
...ws/torch/install/share/lua/5.1/optim/ConfusionMatrix.lua:117: in function 'batchAdd'
5_train.lua:211: in function 'opfunc'
/home/uavws/torch/install/share/lua/5.1/optim/sgd.lua:44: in function 'optimMethod'
5_train.lua:230: in function 'train'
doall.lua:80: in main chunk
[C]: in function 'dofile'
...avws/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk
[C]: at 0x00406670

varghese alex

unread,
Apr 5, 2017, 1:04:46 PM4/5/17
to torch7
require 'cudnn'
require 'cunn'
require 'nn'
require 'optim'
classes={1,2,3,4}    --- creating a dummy four class problem
confusion=optim.ConfusionMatrix(classes)


a= torch.rand(10,2,5,5):cuda()  --- random data
targets= torch.ones(10,3,3)      -- random targets

model=nn.Sequential()                         --- initialize network, fcn
model:add(nn.SpatialConvolutionMM(2,4,3,3))
model:add(cudnn.SpatialLogSoftMax())

model:cuda()

for epochs =1,3 do
    for i =1 ,a:size(1),2 do
        local input = a[{ {i,math.min(i+2-1,a:size(1))},{},{},{} }]
        local temp_target = targets[{ {i,math.min(i+2-1,a:size(1))},{},{} }]
        _,output= model:forward(input:cuda()):max(2) ----- get classes predicted by the network
        local r_output = output:reshape(2,output:size(2)*output:size(3)*output:size(4))   --- reshape the prediction
        local r_target = temp_target:reshape(2,temp_target:size(2)*temp_target:size(3))  --- reshape the targets
        confusion:batchAdd(r_output:float(),r_target:float())    -- populate the confusion matrix
    end
    print (confusion) 
    confusion:zero()
end
Reply all
Reply to author
Forward
0 new messages