-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.lua
265 lines (236 loc) · 5.9 KB
/
utils.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
function strsplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function center_crop(x, crop)
local crop = math.min(crop, math.min(x:size(2), x:size(3)))
local sx = math.floor((x:size(2) - crop)/2)
local sy = math.floor((x:size(3) - crop)/2)
return image.crop(x, sy, sx, sy+crop, sx+crop)
end
function random_crop(x, crop, sx, sy)
assert(x:dim() == 3)
local crop = math.min(crop, math.min(x:size(2), x:size(3)))
local sx = sx or math.random(0, x:size(2) - crop)
local sy = sy or math.random(0, x:size(3) - crop)
return image.crop(x, sy, sx, sy+crop, sx+crop), sx, sy
end
function adjust_meanstd(x, mean, std)
for c = 1,3 do
x[c]:add(-mean[c]):div(std[c])
end
return x
end
function normalize(x, min, max)
local new_min = min or -1
local new_max = max or 1
local old_min, old_max = x:min(), x:max()
local eps = 1e-7
x:add(-old_min)
x:mul(new_max - new_min)
x:div(old_max - old_min + eps)
x:add(new_min)
return x
end
-- based on https://github.com/wojzaremba/lstm/blob/master/base.lua
function clone_many(net, T)
local clones = {}
local params, grads = net:parameters()
local mem = torch.MemoryFile('w'):binary()
mem:writeObject(net)
for t = 1,T do
local reader = torch.MemoryFile(mem:storage(), 'r'):binary()
local clone = reader:readObject()
reader:close()
local cloneParams, cloneGrads = clone:parameters()
for i = 1, #params do
cloneParams[i]:set(params[i])
cloneGrads[i]:set(grads[i])
end
clones[t] = clone
collectgarbage()
end
mem:close()
return clones
end
function updateConfusion(confusion, output, targets)
local correct = 0
for i = 1,targets:nElement() do
if targets[i] ~= -1 then
local _, ind = output[i]:max(1)
confusion:add(ind[1], targets[i])
if ind[1] == targets[i] then
correct = correct+1
end
end
end
return correct
end
function classResults(outputs, targets)
local ind = {}
local top1, top5, N = 0, 0, 0
local _, sorted = outputs:float():sort(2, true)
for i = 1,opt.batchSize do
if targets[i] > 0 then -- has label
ind[i] = 0
N = N+1
if sorted[i][1] == targets[i] then
top1 = top1 + 1
ind[i] = 1
end
for k = 1,5 do
if sorted[i][k] == targets[i] then
top5 = top5 + 1
break
end
end
end
end
return top1, top5, N, ind
end
function sanitize(net)
local list = net:listModules()
for _,val in ipairs(list) do
for name,field in pairs(val) do
if torch.type(field) == 'cdata' then val[name] = nil end
if name == 'homeGradBuffers' then val[name] = nil end
if name == 'input_gpu' then val['input_gpu'] = {} end
if name == 'gradOutput_gpu' then val['gradOutput_gpu'] = {} end
if name == 'gradInput_gpu' then val['gradInput_gpu'] = {} end
if (name == 'output' or name == 'gradInput') then
if torch.isTensor(val[name]) then
val[name] = field.new()
end
end
if name == 'buffer' or name == 'buffer2' or name == 'normalized'
or name == 'centered' or name == 'addBuffer' then
val[name] = nil
end
end
end
return net
end
local function weights_init(m)
local name = torch.type(m)
if name:find('Convolution') or name:find('Linear') then
m.weight:normal(0.0, 0.02)
m.bias:fill(0)
elseif name:find('BatchNormalization') then
if m.weight then m.weight:normal(1.0, 0.02) end
if m.bias then m.bias:fill(0) end
end
end
function initModel(model)
for _, m in pairs(model:listModules()) do
weights_init(m)
end
end
function isNan(x)
return x:ne(x):sum() > 0
end
function sampleNoise(z)
if opt.noise == 'uniform' then
z:uniform(-1, 1)
else
z:normal()
end
end
function clone_table(t)
local tt = {}
for i=1,#t do
tt[i] = t[i]:clone()
end
end
function zero_table(t)
for k, v in pairs(t) do
t[k]:zero()
end
end
function replace_table(t1, t2)
for i=1,#t1 do
t1[i]:copy(t2[i])
end
end
function write_opt(opt)
local opt_file = io.open(('%s/opt.log'):format(opt.save), 'w')
for k, v in pairs(opt) do
opt_file:write(('%s = %s\n'):format(k, v))
end
opt_file:close()
end
function borderPlot(to_plot, k)
local k = k or 1
local sx = to_plot[1]:size(2)
local sy = to_plot[1]:size(3)
for i=1,#to_plot do
to_plot[i] = to_plot[i]:clone()
to_plot[i][{ {}, {}, {1,k}}]:fill(1)
to_plot[i][{ {}, {}, {sy-k+1,sy}}]:fill(1)
to_plot[i][{ {}, {1,k}, {}}]:fill(1)
to_plot[i][{ {}, {sx-k+1,sx}, {}}]:fill(1)
end
end
function borderPlotRGB(to_plot, rgb)
local nc = to_plot[1]:size(1)
local sx = to_plot[1]:size(2)
local sy = to_plot[1]:size(3)
for i=1,#to_plot do
local im
if nc == 1 then
im = torch.expand(to_plot[i], 3, sx, sy):clone()
else
im = to_plot[i]
end
to_plot[i] = im
for c=1,3 do
to_plot[i][{ c, {}, 1}]:fill(rgb[c])
to_plot[i][{ c, {}, sy}]:fill(rgb[c])
to_plot[i][{ c, 1, {}}]:fill(rgb[c])
to_plot[i][{ c, sx, {}}]:fill(rgb[c])
end
end
end
function borderPlotTensorRGB(x, rgb)
local nc = x:size(1)
local sx = x:size(2)
local sy = x:size(3)
local im
if nc == 1 then
im = torch.expand(x, 3, sx, sy):clone()
else
im = x
end
for c=1,3 do
im[{ c, {}, 1}]:fill(rgb[c])
im[{ c, {}, sy}]:fill(rgb[c])
im[{ c, 1, {}}]:fill(rgb[c])
im[{ c, sx, {}}]:fill(rgb[c])
end
return im
end
function slice_table(input, start, end_)
local result = {}
local index = 1
for i=start, end_ do
result[index] = input[i]
index = index + 1
end
return result
end
function extend_table(input, tail)
for i=1, #tail do
table.insert(input, tail[i])
end
end
function find_index(t, e)
for k, v in pairs(t) do
if v == e then return k end
end
end