-
Notifications
You must be signed in to change notification settings - Fork 6
/
fprop_utils.lua
360 lines (346 loc) · 10.8 KB
/
fprop_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#! /usr/bin/env lua
--[[
Forward pass utilities
1. Contains functions that have be invoked while conducting a fprop for any of the models
2. Glance and Aso-sub differ only in terms of data-preprocessing
3. Seq-sub is completely different and involves sequential processing
]]
require 'nn'
require 'rnn'
require 'sys'
require 'xlua'
require 'paths'
require 'hdf5'
require 'math'
local fprop_utils = torch.class('fprop_utils')
function fprop_utils:__init(feature_directory, imagelist_dir, disc, feature_dimensions, num_classes)
--[[
(Initialize settings for forward passes)
Arguments
**********
feature_directory: the directory containing the feature files
imagelist_dir: directory containing the image lists
disc: the discretization at which we're dealing - 1 for glance, 3 or more for aso-sub/seq-sub
feature_dimensions: dimensions of the feature directory
num_classes: the number of output classes
Returns
**********
(Nothing, just initializes stuff)
]]
self.feat_dir = feature_directory
self.imlist_dir = imagelist_dir
self.feat_dim = feature_dimensions
self.nout = num_classes
self.disc = disc
self.disc_size = disc*disc
if self.disc > 1 then
self.perm1 = torch.eye(self.disc_size)
self.prev_ind = torch.linspace(1, self.disc_size, self.disc_size)
self.new_ind = torch.zeros(self.disc_size)
for i = 1,self.disc_size do
if i % self.disc ~= 0 then
self.new_ind[i] = 1 + self.disc*((i % self.disc) - 1) + ((i - (i % self.disc))/self.disc)
else
self.new_ind[i] = self.disc_size + self.disc*((i % self.disc) - 1) + ((i - (i % self.disc))/self.disc)
end
end
self.perm2 = torch.zeros(self.disc_size, self.disc_size)
for i = 1,self.disc_size do
self.perm2[{{self.prev_ind[i]}, {self.new_ind[i]}}] = 1
end
self.mul_mat = torch.eye(self.disc_size)
self.mul_ind = torch.linspace(1, self.disc_size, self.disc_size)
local a = 1 + ((2*torch.linspace(1, (self.disc - 1)/2, (self.disc - 1)/2)) - 1)*self.disc
local b = 2*torch.linspace(1, (self.disc - 1)/2, (self.disc - 1)/2)*self.disc
for i= 1,(self.disc - 1)/2 do
self.mul_ind[{{a[i], b[i]}}] = torch.linspace(b[i], a[i], self.disc)
end
self.mul_mat = self.mul_mat:fill(0)
for i= 1,self.disc_size do
self.mul_mat[{{i}, {self.mul_ind[i]}}] = 1
end
end
end
function get_batchsize(split_size, des_bsize)
--[[
(Get closest batchsize to cover the entire split)
Arguments
**********
split_size: size of the entire split
des_bsize: desired batch-size
Returns
**********
exp_bsize: the batch-size closest to the one required
]]
local factors = {}
for possible_factor = 1,math.sqrt(split_size),1 do
local remainder = split_size % possible_factor
if remainder == 0 then
local factor, factor_pair = possible_factor, split_size/possible_factor
table.insert(factors, factor)
if factor ~= factor_pair then
table.insert(factors, factor_pair)
end
end
end
table.sort(factors)
factor_tensor = torch.Tensor(factors)
_, ind = torch.min(torch.abs(factor_tensor - des_bsize), 1)
exp_bsize = factor_tensor[ind[1]]
return exp_bsize
end
function prepare_sequence(feat_mat, perm1, perm2, mul_mat)
--[[
(Create permuted ordering of features for sequential subitizing)
Arguments
**********
feat_mat: feature matrix containing features for all the cells
perm1: the first permutation matrix fir seq-sub
perm2: the second permutation matrix fir seq-sub
mul_mat: matrix to fix ordering interms of close-cells
Returns
**********
perm_feat1, perm_feat2: permuted feature vectors
]]
local perm_feat1 = feat_mat:clone()
local perm_feat2 = feat_mat:clone()
perm_feat1 = nn.MM():forward({perm1, perm_feat1})
perm_feat1 = nn.MM():forward({mul_mat, perm_feat1})
perm_feat2 = nn.MM():forward({perm2, perm_feat2})
perm_feat2 = nn.MM():forward({mul_mat, perm_feat2})
return perm_feat1, perm_feat2
end
function fprop_utils:glance_fprop(split_list, model, gpu_flag, gpu_id, cudnn_flag, des_bsize)
--[[
(Forward pass for glancing)
Arguments
**********
split_list: list of images for the corresponding split
model: nn.Sequential() model
gpu_flag: whether to use a GPU or not
gpu_id: corresponding GPU IDs
cudnn_flag: whether to CuDNN
des_bsize: desired batchsize
Returns
**********
Yo: return predictions for the whole split
]]
model:evaluate()
local img_list = {}
local img_file = io.open(self.imlist_dir .. '/' .. split_list)
if img_file then for line in img_file:lines() do table.insert(img_list, line) end end
-- Get batch size
local bsize = get_batchsize(#img_list, des_bsize)
-- Feature tensor per-batch
Xt = torch.zeros(bsize, self.feat_dim)
Yt = torch.zeros(bsize, self.nout)
-- Predictions for the whole split
Yo = torch.zeros(#img_list, self.nout)
if gpu_flag then
require 'cutorch'
require 'cunn'
cutorch.setDevice(gpu_id + 1)
model:cuda()
Yo = Yo:cuda()
if cudnn_flag then
require 'cudnn'
cudnn.benchmark = true
cudnn.fastest = true
cudnn.verbose = true
cudnn.convert(model, cudnn)
end
end
for it = 1,#img_list,bsize do
local batch_id = ((it-1)/bsize) + 1
xlua.progress(batch_id, torch.floor(#img_list/bsize))
if (it + bsize - 1) > #img_list then
break
end
local idx = 1
for i = it,it+bsize-1 do
local feat_path = self.feat_dir .. '/' .. paths.basename(img_list[i], '.jpg') .. '.h5'
local feat_h5 = hdf5.open(feat_path, 'r')
Xt[idx] = feat_h5:read('/data'):all()
feat_h5:close()
idx = idx + 1
end
if gpu_flag then
Xt = Xt:cuda()
Yt = Yt:cuda()
end
Yt = model:forward(Xt)
Yo[{{it, it + bsize - 1}, {1, self.nout}}] = Yt
end
return Yo
end
function fprop_utils:aso_fprop(split_list, model, gpu_flag, gpu_id, cudnn_flag, des_bsize)
--[[
(Forward pass for associative subitizing)
Arguments
**********
split_list: list of images for the corresponding split
model: nn.Sequential() model
gpu_flag: whether to use a GPU or not
gpu_id: corresponding GPU IDs
cudnn_flag: whether to CuDNN
des_bsize: desired batchsize
Returns
**********
Yo: return predictions for the whole split
]]
model:evaluate()
-- Keep smaller batch size for associative subitizing
local img_list = {}
local img_file = io.open(self.imlist_dir .. '/' .. split_list)
if img_file then for line in img_file:lines() do table.insert(img_list, line) end end
-- Get batch size
local bsize = get_batchsize(#img_list, des_bsize)
-- Feature tensor per-batch
Xt = torch.zeros(bsize*self.disc_size, self.feat_dim)
Yt = torch.zeros(bsize*self.disc_size, self.nout)
-- Predictions for the whole split
Yo = torch.zeros(#img_list, self.nout)
if gpu_flag then
require 'cutorch'
require 'cunn'
cutorch.setDevice(gpu_id + 1)
model:cuda()
Yo = Yo:cuda()
if cudnn_flag then
require 'cudnn'
cudnn.benchmark = true
cudnn.fastest = true
cudnn.verbose = true
cudnn.convert(model, cudnn)
end
end
for it = 1,#img_list,bsize do
local batch_id = ((it-1)/bsize) + 1
xlua.progress(batch_id, torch.floor(#img_list/bsize))
if (it + bsize - 1) > #img_list then
break
end
local idx = 1
for i = it,it+bsize-1 do
local feat_path = self.feat_dir .. '/' .. paths.basename(img_list[i], '.jpg') .. '.h5'
local feat_h5 = hdf5.open(feat_path, 'r')
Xt[{{1+(idx-1)*self.disc_size, idx*self.disc_size}, {1, self.feat_dim}}] = feat_h5:read('/data'):all()
feat_h5:close()
idx = idx + 1
end
if gpu_flag then
Xt = Xt:cuda()
Yt = Yt:cuda()
end
Yt = model:forward(Xt)
-- Compress count predictions from the model to get dataset predictions
local comp_idx = 1
for i = it, it+bsize-1 do
local count_ten = Yt[{{1+(comp_idx-1)*self.disc_size, comp_idx*self.disc_size}, {1, self.nout}}]
Yo[{{i}, {1, self.nout}}] = count_ten:double():sum(1):squeeze()
comp_idx = comp_idx + 1
end
end
return Yo
end
function fprop_utils:seq_fprop(split_list, model1, model2, gpu_flag, gpu_id, cudnn_flag, des_bsize)
--[[
(Forward pass for glancing)
Arguments
**********
split_list: list of images for the corresponding split
model1: nn.Sequential() model to get bi-LSTMs output states
model2: nn.Sequential() model to get cell-level predictions
gpu_flag: whether to use a GPU or not
gpu_id: corresponding GPU IDs
cudnn_flag: whether to CuDNN
des_bsize: desired batchsize
Returns
**********
Yo: return predictions for the whole split
]]
model1:evaluate()
model2:evaluate()
-- Keep smaller batch size for associative subitizing
local img_list = {}
local img_file = io.open(self.imlist_dir .. '/' .. split_list)
if img_file then for line in img_file:lines() do table.insert(img_list, line) end end
-- Get batch size
local bsize = get_batchsize(#img_list, des_bsize)
-- Feature tensors and tables per-batch
local Xt1 = torch.zeros(bsize, self.disc_size, self.feat_dim)
local Xt2 = torch.zeros(bsize, self.disc_size, self.feat_dim)
local Yt = torch.zeros(bsize, self.disc_size, self.nout)
-- Predictions for the whole split
local Yo = torch.zeros(#img_list, self.nout)
-- Inverse permutation matrices
local inv1 = nn.MM():forward({self.perm1, self.mul_mat})
inv1 = torch.repeatTensor(inv1, bsize, 1, 1)
local inv2 = nn.MM():forward({self.perm2, self.mul_mat})
inv2 = torch.repeatTensor(inv2, bsize, 1, 1)
if gpu_flag then
require 'cutorch'
require 'cunn'
cutorch.setDevice(gpu_id + 1)
model1:cuda()
model2:cuda()
Yo = Yo:cuda()
if cudnn_flag then
require 'cudnn'
cudnn.benchmark = true
cudnn.fastest = true
cudnn.verbose = true
cudnn.convert(model, cudnn)
end
end
for it = 1,#img_list,bsize do
local batch_id = ((it-1)/bsize) + 1
xlua.progress(batch_id, torch.floor(#img_list/bsize))
if (it + bsize - 1) > #img_list then
break
end
local idx = 1
for i = it,it+bsize-1 do
local feat_path = self.feat_dir .. '/' .. paths.basename(img_list[i], '.jpg') .. '.h5'
local feat_h5 = hdf5.open(feat_path, 'r')
Xt1[idx], Xt2[idx] = prepare_sequence(feat_h5:read('/data'):all(), self.perm1, self.perm2, self.mul_mat)
feat_h5:close()
idx = idx + 1
end
if gpu_flag then
Xt1 = Xt1:cuda()
Xt2 = Xt2:cuda()
Yt = Yt:cuda()
end
local input, target = {}, {}
table.insert(input, Xt1)
table.insert(input, Xt2)
local op_state = model1:forward(input)
local op_state_table = {}
for i = 1,#op_state do
local temp = op_state[i]
temp = temp:double()
table.insert(op_state_table, nn.Unsqueeze(2):forward(temp))
end
local op = nn.JoinTable(2):forward(op_state_table)
local op_1 = op:sub(1, op:size(1), 1, op:size(2)/2, 1, op:size(3))
local op_2 = op:sub(1, op:size(1), 1 + op:size(2)/2, op:size(2), 1, op:size(3))
op_1 = nn.MM():forward({inv1, op_1})
op_2 = nn.MM():forward({inv2, op_2})
op_tensor = torch.cat(op_1, op_2, 3)
if gpu_flag then
op_tensor = op_tensor:cuda()
end
local inter_ip = nn.SplitTable(2):forward(op_tensor)
local output = model2:forward(inter_ip)
local cell_op = {}
for i=1, #output do
local temp = output[i]
temp = temp:double()
temp = temp:cmax(0)
table.insert(cell_op, temp)
end
Yo[{{it, it+bsize-1}, {1, self.nout}}] = nn.CAddTable():forward(cell_op)
end
return Yo
end