-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathretrive_demo.lua
305 lines (278 loc) · 10.7 KB
/
retrive_demo.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
--require ('mobdebug').start()
require 'dp'
require 'rnn'
require 'optim'
require 'ViewSelect'
require 'RecurrentAttention_ex'
require 'RewardCriterion'
require 'cutorch'
require 'cunn'
torch.setdefaulttensortype('torch.FloatTensor')
local matio = require 'matio'
cmd = torch.CmdLine()
cmd:text()
cmd:text('retrive for a object')
cmd:text('Options:')
cmd:option('--cuda', false, 'model was saved with cuda')
cmd:option('--stochastic', false, 'evaluate the model stochatically. Generate glimpses stochastically')
cmd:option('--view_num', 21, 'num of all view')
cmd:option('--rho', 5, 'time-steps')
cmd:option('--hiddenSize', 256, 'number of hidden units used in Simple RNN')
cmd:text()
local opt = cmd:parse(arg or {})
function getViewId(location,viewsLoc)
local viewId
_,viewId=(viewsLoc-location:resize(1,2):expandAs(viewsLoc)):norm(2,2):min(1)
viewId=viewId[1][1]
return viewId
end
----prepare test data--
main_dir='data_hierarchy_tree'
local threshold=0.98
require('util/viewsLoc')
viewsLoc= getAllViewsLoc()
--local f=torch.load('') -- 1 to 21 views
local f = matio.load('data_hierarchy_tree/5classes_data_test.mat')
local data = f.images.data:type(torch.getdefaulttensortype())
data=data:permute(4,3,1,2):narrow(1,1,50*opt.view_num)
for obj=1,data:size(1),opt.view_num do
cur_data=data:narrow(1,obj,opt.view_num)
local input_view_id=2
local step=1
local input=cur_data[input_view_id]
rnn_results={}
view_seqs={}
locations={}
node_record={}
locations[1]=viewsLoc[input_view_id]
table.insert(view_seqs,input_view_id)
----first load root model --- root torch model start
local cur_ram_model_path=paths.concat(main_dir,'cur_model','model.dat')
local cur_mvcnn_model_path=paths.concat(main_dir,'mvcnn','mvcnn.net')
local cur_train_data_path ---=paths.concat(main_dir,'5classes_data_test.mat')
cur_xp =torch.load(cur_ram_model_path)
cur_model = cur_xp:model().module:float()
cur_mvcnn =torch.load(cur_mvcnn_model_path):float()
cur_fes_extra=cur_mvcnn.modules[1]
cur_ram= cur_model:findModules('RecurrentAttention')[1]
cur_rnn= cur_ram.rnn --here the rnn just for pooling, so no need change
cur_action=cur_ram.action
cur_rnn:forget()
cur_action:forget()
----action init
if not opt.stochastic then
for i=1,opt.rho do
local rn = cur_action:getStepModule(i):findModules('nn.ReinforceNormal')[1]
rn.stdev = 0 -- deterministic
end
end
for i=1,opt.rho do
local viewSelect=cur_rnn:getStepModule(i):findModules('ViewSelect')[1]
viewSelect.evaluation=true
end
initInput = input.new()
initInput:resize(1,opt.hiddenSize):zero()
cur_action:updateOutput{initInput,viewsLoc[input_view_id]}
---first action (location) is given
------------------
cur_classifier=cur_mvcnn.modules[4]
-- compute -----------------------------
-- forward rnn ---
cur_input=cur_fes_extra:updateOutput(input):view(-1)
local cur_rnn_output=cur_rnn:updateOutput{cur_input, locations[step]}
rnn_results[step]=cur_rnn_output
---forward classifier --
local cur_result= cur_classifier:updateOutput(cur_rnn_output)
local maxPro,tar_label
maxPro,tar_label=torch.exp(cur_result):max(1)
local first_tar_label=tar_label[1]
--- if go on --
cur_main_dir=main_dir
local node_count=1
local isreach_leaf_node=false
while not isreach_leaf_node do
if maxPro[1] >threshold then
if node_count==1 and tar_label[1] ~= 1 then
--check for chair
goto continue
end
--- go to sub node ---
node_record[node_count]=tar_label[1]
node_count=node_count+1
step=step+1
local sub_dir_name='subclass' .. tar_label[1]
local sub_node_dir=paths.concat(cur_main_dir,sub_dir_name)
if not paths.dirp(sub_node_dir) then
sub_dir_name='subclass' .. tar_label[1] .. '_oneShot'
isreach_leaf_node=true
sub_node_dir=paths.concat(cur_main_dir,sub_dir_name)
end
----------------------------
---- update path -----------
cur_main_dir=sub_node_dir
cur_ram_model_path = paths.concat(cur_main_dir,'cur_model','model.dat')
cur_mvcnn_model_path = paths.concat(cur_main_dir,'mvcnn','mvcnn.net')
cur_train_data_path = paths.concat(cur_main_dir,'imdAllData3.mat')
if isreach_leaf_node then
cur_train_data_path = paths.concat(cur_main_dir,'oneShot_data.mat')
end
---- compute -------------------
cur_xp =torch.load(cur_ram_model_path)
cur_model = cur_xp:model().module:float()
cur_mvcnn =torch.load(cur_mvcnn_model_path):float()
cur_fes_extra=cur_mvcnn.modules[1]
cur_ram= cur_model:findModules('RecurrentAttention')[1]
cur_action=cur_ram.action
cur_action:forget()
if not opt.stochastic then
for i=1,opt.rho do
local rn = cur_action:getStepModule(i):findModules('nn.ReinforceNormal')[1]
rn.stdev = 0 -- deterministic
end
end
----action init
if step <3 then
initInput = input.new()
initInput:resize(1,opt.hiddenSize):zero()
cur_action:updateOutput{initInput,viewsLoc[input_view_id]}
else
for i=1,step-2 do
if i == 1 then
initInput = input.new()
initInput:resize(1,opt.hiddenSize):zero()
cur_action:updateOutput{initInput,viewsLoc[input_view_id]}
else
cur_action:updateOutput{rnn_results[i-1],locations[i-1]}
end
end
end
cur_classifier=cur_mvcnn.modules[4]
pred_action=cur_action:updateOutput{rnn_results[step-1],locations[step-1]}
locations[step]=pred_action
next_view_id = getViewId(pred_action,viewsLoc)
view_seqs[step]=next_view_id
cur_input=cur_fes_extra:updateOutput(cur_data[next_view_id])
cur_rnn_output=cur_rnn:updateOutput{cur_input, locations[step]}
rnn_results[step]=cur_rnn_output
---forward classifier --
cur_result= cur_classifier:updateOutput(cur_rnn_output)
maxPro,tar_label=torch.exp(cur_result):max(1)
else
-- keep on current node ------
step=step+1
-- go to next view point, forward action net --
pred_action=cur_action:updateOutput{rnn_results[step-1],locations[step-1]}
locations[step]=pred_action
next_view_id = getViewId(pred_action,viewsLoc)
view_seqs[step]=next_view_id
cur_input=cur_fes_extra:updateOutput(cur_data[next_view_id])
cur_rnn_output=cur_rnn:updateOutput{cur_input, locations[step]}
rnn_results[step]=cur_rnn_output
---forward classifier --
cur_result= cur_classifier:updateOutput(cur_rnn_output)
maxPro,tar_label=torch.exp(cur_result):max(1)
if node_count==1 then
first_tar_label=tar_label[1]
end
end
if step == opt.rho then
if first_tar_label ~= 1 then
--check for chair
goto continue
end
-- go to leaf
while not isreach_leaf_node do
node_record[node_count]=tar_label[1]
node_count=node_count+1
local sub_dir_name='subclass' .. tar_label[1]
local sub_node_dir=paths.concat(cur_main_dir,sub_dir_name)
if not paths.dirp(sub_node_dir) then
sub_dir_name='subclass' .. tar_label[1] .. '_oneShot'
isreach_leaf_node=true
sub_node_dir=paths.concat(cur_main_dir,sub_dir_name)
end
----------------------------
---- update path -----------
cur_main_dir=sub_node_dir
cur_ram_model_path = paths.concat(cur_main_dir,'cur_model','model.dat')
cur_mvcnn_model_path = paths.concat(cur_main_dir,'mvcnn','mvcnn.net')
cur_train_data_path = paths.concat(cur_main_dir,'imdAllData3.mat')
if isreach_leaf_node then
cur_train_data_path = paths.concat(cur_main_dir,'oneShot_data.mat')
end
---- compute -------------------
cur_xp =torch.load(cur_ram_model_path)
cur_model = cur_xp:model().module:float()
cur_mvcnn =torch.load(cur_mvcnn_model_path):float()
cur_fes_extra=cur_mvcnn.modules[1]
cur_ram= cur_model:findModules('RecurrentAttention')[1]
cur_action=cur_ram.action
cur_action:forget()
if not opt.stochastic then
for i=1,opt.rho do
local rn = cur_action:getStepModule(i):findModules('nn.ReinforceNormal')[1]
rn.stdev = 0 -- deterministic
end
end
----action init
if step <3 then
initInput = input.new()
initInput:resize(1,opt.hiddenSize):zero()
cur_action:updateOutput{initInput,viewsLoc[input_view_id]}
else
for i=1,step-2 do
if i == 1 then
initInput = input.new()
initInput:resize(1,opt.hiddenSize):zero()
cur_action:updateOutput{initInput,viewsLoc[input_view_id]}
else
cur_action:updateOutput{rnn_results[i-1],locations[i-1]}
end
end
end
cur_classifier=cur_mvcnn.modules[4]
pred_action=cur_action:updateOutput{rnn_results[step-1],locations[step-1]}
locations[step]=pred_action
next_view_id = getViewId(pred_action,viewsLoc)
view_seqs[step]=next_view_id
cur_input=cur_fes_extra:updateOutput(data[next_view_id])
cur_rnn_output=cur_rnn:updateOutput{cur_input, locations[step]}
rnn_results[step]=cur_rnn_output
---forward classifier --
cur_result= cur_classifier:updateOutput(cur_rnn_output)
maxPro,tar_label=torch.exp(cur_result):max(1)
end
end
if isreach_leaf_node then
node_record[node_count]=tar_label[1]
-- retrive_data=torch.load(cur_train_data_path)
local f = matio.load(cur_train_data_path)
local cur_retrive_data = f.images.data:type(torch.getdefaulttensortype())
cur_retrive_data=cur_retrive_data:permute(4,3,1,2)
retrive_data=cur_retrive_data:narrow(1,opt.view_num*(tar_label[1]-1)+1,opt.view_num)
end
end
viewGlimpses = {}
for i=1,#view_seqs do
viewGlimpses[i]=cur_data[view_seqs[i]]
end
if not paths.dirp('retrive_res/view_seqs') then
paths.mkdir('retrive_res/view_seqs')
end
local g = image.toDisplayTensor{input=viewGlimpses,nrow=10,padding=3}
local cur_obj_id=(obj-1)/opt.view_num+1
image.save("retrive_res/view_seqs/obj_".. cur_obj_id .. "th_view_seqs.png", g)
compare_retrive={}
for i=1,10 do
compare_retrive[i]=cur_data[i]
compare_retrive[i+10]=retrive_data[i]
end
if not paths.dirp('retrive_res/compare') then
paths.mkdir('retrive_res/compare')
end
local cop = image.toDisplayTensor{input=compare_retrive,nrow=10,padding=3}
image.save("retrive_res/compare/compare_" .. cur_obj_id .. ".png", cop)
collectgarbage()
::continue::
end
----