forked from facebookresearch/deepmask
-
Notifications
You must be signed in to change notification settings - Fork 2
/
trainMeters.lua
140 lines (124 loc) · 3.74 KB
/
trainMeters.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
129
130
131
132
133
134
135
136
137
138
139
140
--[[----------------------------------------------------------------------------
Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
Contains the tree metrics used during training/evaluation:
- lossmeter: measure the average loss.
- binarymeter: measure error of predicted objectness score and ground truth
objectness annotation.
- ioumeter: measure iou between infered and ground truth masks.
------------------------------------------------------------------------------]]
--------------------------------------------------------------------------------
-- loss meter
do
local LossMeter = torch.class('LossMeter')
-- init
function LossMeter:__init()
self:reset()
end
-- function: reset
function LossMeter:reset()
self.sum = 0; self.n = 0
end
-- function: add
function LossMeter:add(value,n)
n = n or 1
self.sum = self.sum + value
self.n = self.n + n
end
-- function: value
function LossMeter:value()
return self.sum / self.n
end
end
--------------------------------------------------------------------------------
-- binary meter
do
local BinaryMeter = torch.class('BinaryMeter')
-- init
function BinaryMeter:__init()
self:reset()
end
-- function: reset
function BinaryMeter:reset()
self.acc = 0; self.n = 0
end
-- function: add
function BinaryMeter:add(output, target)
target, output = target:squeeze(), output:squeeze()
assert(output:nElement() == target:nElement(),
'target and output do not match')
local acc = torch.cmul(output,target)
self.acc = self.acc + acc:ge(0):sum()
self.n = self.n + output:size(1)
end
-- function: value
function BinaryMeter:value()
local res = self.acc/self.n
return res*100
end
end
--------------------------------------------------------------------------------
-- iou meter
do
local IouMeter = torch.class('IouMeter')
-- init
function IouMeter:__init(thr,sz)
self.sz = sz
self.iou = torch.Tensor(sz)
self.thr = math.log(thr/(1-thr))
self:reset()
end
-- function: reset
function IouMeter:reset()
self.iou:zero(); self.n = 0
end
-- function: add
function IouMeter:add(output, target)
target, output = target:squeeze():float(), output:squeeze():float()
assert(output:nElement() == target:nElement(),
'target and output do not match')
local batch,h,w = output:size(1),output:size(2),output:size(3)
local nOuts = h*w
local iouptr = self.iou:data()
local int,uni
local pred = output:ge(self.thr)
local pPtr,tPtr = pred:data(), target:data()
for b = 0,batch-1 do
int,uni = 0,0
for i = 0,nOuts-1 do
local id = b*nOuts+i
if pPtr[id] == 1 and tPtr[id] == 1 then int = int + 1 end
if pPtr[id] == 1 or tPtr[id] == 1 then uni = uni + 1 end
end
if uni > 0 then iouptr[self.n+b] = int/uni end
end
self.n = self.n + batch
end
-- function: value
function IouMeter:value(s)
if s then
local res
local nb = math.max(self.iou:ne(0):sum(),1)
local iou = self.iou:narrow(1,1,nb)
if s == 'mean' then
res = iou:mean()
elseif s == 'median' then
res = iou:median():squeeze()
elseif tonumber(s) then
local iouSort, _ = iou:sort()
res = iouSort:ge(tonumber(s)):sum()/nb
elseif s == 'hist' then
res = torch.histc(iou,20)/nb
end
return res*100
else
local value = {}
for _,s in ipairs(self.stats) do
value[s] = self:value(s)
end
return value
end
end
end