-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.lua
128 lines (114 loc) · 4.8 KB
/
eval.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
local ffi = require 'ffi'
-------------------------------------------------------------------------------
-- Helpful functions for evaluation
-------------------------------------------------------------------------------
--if(opt.show) then
require 'image'
wz = 2 --zoom
local zm = wz*4
if(opt.upsample) then zm = wz end
--end
function tensorMax(input)
local y = input:max()
local i = input:float():eq(y):nonzero()
return i[{{1}, {}}] -- only that row
end
-- output: Bx15x64x64 probabilities for 15 classes, label: Bx64x64
function segmPerformance(output, labels)
-- return av_iou, iou matrix, av_acc, acc_mat, pix_acc av, intersect, union, and nPos
local nBatch = output:size(1)
local nClasses = output:size(2)
local iou = torch.Tensor(nClasses):zero()
local pixelacc = torch.Tensor(nClasses):zero() -- pixel accurate
local intersection = torch.Tensor(nClasses):zero()
local union = torch.Tensor(nClasses):zero()
local npositives = torch.Tensor(nClasses):zero() -- ture pos num
for b = 1, nBatch do
local label = labels[b]
dummy, pred = torch.max(output[b], 1) -- value which seg
for cl = 1, nClasses do
local ix = torch.eq(label, cl) -- indices image
local npos = ix:sum()
local tp = torch.eq(label, pred:float()):cmul(ix):sum() -- true positive
local p = torch.eq(pred, cl):sum() -- cmul elewise prd pos
intersection[cl] = intersection[cl] + tp
union[cl] = union[cl] + (npos + p - tp )
npositives[cl] = npositives[cl] + npos
if(npos + p - tp ~= 0) then
iou[cl] = iou[cl] + tp / (npos + p - tp )
end
if(npos ~= 0) then
pixelacc[cl] = pixelacc[cl] + (tp / npos) -- finally be a 14x ele = 0.8* nBatch , how can this one 72
end
end
end
return iou[{{2, 15}}]:mean()/nBatch, iou, pixelacc[{{2, 15}}]:mean()/nBatch, pixelacc, intersection, union, npositives
end -- two to 15
-- output: Bx20x64x64, label:Bx64x64
function depthRMSE(output, label)
local nBatch = output:size(1)
local nClasses = output:size(2)
local rmse = 0
for b = 1, nBatch do
local ix = torch.ne(label[b], 1):expandAs(label[b]) -- foreground pixels ne:not equal replicat original
local nForeground = ix:sum()
local dummy, pred = torch.max(output[b], 1)
if(label[b][ix]:size():size() ~= 0) then -- not empty
rmse = rmse + torch.sqrt(torch.mean(torch.pow(label[b][ix]:unfold(1, 2, 2) - pred[ix]:unfold(1, 2, 2):float(), 2))) -- looks like for 2 ele op, but not xy, maybe wrong; 1st dim 2 size block
else
print('?!')
-- counter of not evaluated images
end
end
return rmse/nBatch
end
function evalPerf(inputsCPU, labelsCPU, outputs)
-- inputsCPU not used here
local iouBatch = 0
local iouBatchParts = torch.Tensor(opt.segmClasses):zero()
local pxlaccBatch = 0
local pxlaccBatchParts = torch.Tensor(opt.segmClasses):zero()
local intersectionBatch = torch.Tensor(opt.segmClasses):zero()
local unionBatch = torch.Tensor(opt.segmClasses):zero()
local npositives = torch.Tensor(opt.segmClasses):zero()
local rmse = 0
if(opt.nStack > 0) then
assert(#outputs == opt.nStack) -- each table entry is another stack
outputs = outputs[opt.nStack] -- take the last stack output
end
scoresCPU = outputs:float()
if(opt.supervision == 'segm') then
scoresSegm = scoresCPU
labelsSegm = labelsCPU
iouBatch, iouBatchParts, pxlaccBatch, pxlaccBatchParts, intersectionBatch, unionBatch, npositives = segmPerformance(scoresSegm, labelsSegm)
if(opt.show) then
require 'image'
for i = 1, opt.batchSize do
dummy, pred = torch.max(scoresSegm[i], 1)
im = pred:float() -- 1 x 64 x 64
im[1][1][1] = 1
im[1][1][2] = opt.segmClasses
wOutputSegm = image.display({image=image.y2jet(im), win=wOutputSegm, zoom=zm, legend='PRED SEGM'})
end
end
end
if(opt.supervision == 'depth') then
scoresDepth = scoresCPU
labelsDepth = labelsCPU
rmse = depthRMSE(scoresDepth, labelsDepth)
if(opt.show) then
require 'image'
for i = 1, opt.batchSize do
dummy, pred = torch.max(scoresDepth[i], 1)
im = pred:float() -- 1 x 64 x 64
im[1][1][1] = 1
im[1][1][2] = opt.depthClasses + 1
wOutputDepth = image.display({image=image.y2jet(im), win=wOutputDepth, zoom=zm, legend='PRED DEPTH'})
end
end
end
if opt.show then
sys.sleep(2)
end
return pxlaccBatch, iouBatch, intersectionBatch, unionBatch, npositives, rmse
end