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