-
Notifications
You must be signed in to change notification settings - Fork 8
/
setup.lua
executable file
·390 lines (294 loc) · 11.7 KB
/
setup.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
setup = {}
function setup.getModelOpts()
local model_opts = {}
model_opts.cuda = cuda
model_opts.parent_dir = 'AAE_shape_learner_v2'
model_opts.model_name = 'cell_learner_3_caae_learner'
model_opts.save_dir = model_opts.parent_dir .. '/' .. model_opts.model_name
model_opts.image_dir = '/root/images/2016_11_08_Nuc_Cell_Seg_8_cell_lines_V22/processed_aligned/2D'
model_opts.image_sub_size = 16.96875/2
model_opts.nLatentDims = 16
model_opts.channel_inds_in = torch.LongTensor{1,3}
model_opts.channel_inds_out = torch.LongTensor{1,3}
model_opts.rotate = true
model_opts.nChIn = model_opts.channel_inds_in:size(1)
model_opts.nChOut = model_opts.channel_inds_out:size(1)
model_opts.nClasses = 0
model_opts.nOther = 0
model_opts.dropoutRate = 0.2
model_opts.fullConv = true
model_opts.adversarialGen = false
model_opts.test_model = false
model_opts.verbose = true
paths.mkdir(model_opts.parent_dir)
return model_opts
end
function setup.getModel()
print('Loading model type: ' .. model_opts.model_name)
package.loaded['models/' .. model_opts.model_name] = nil
Model = require ('models/' .. model_opts.model_name)
autoencoder = nil
encoder = nil
decoder = nil
if paths.filep(model_opts.save_dir .. '/decoder.t7') then
print('Loading model from ' .. model_opts.save_dir)
print('Loading encoder')
encoder = torch.load(model_opts.save_dir .. '/encoder.t7')
encoder:float()
encoder:clearState()
collectgarbage()
print('Loading adversary')
adversary = torch.load(model_opts.save_dir .. '/adversary.t7')
adversary:float()
adversary:clearState()
collectgarbage()
if paths.filep(model_opts.save_dir .. '/adversaryGen.t7') then
print('Loading adversaryGen')
adversaryGen = torch.load(model_opts.save_dir .. '/adversaryGen.t7')
adversaryGen:float()
adversaryGen:clearState()
collectgarbage()
else
adversaryGen = nil
end
print('Loading decoder')
decoder = torch.load(model_opts.save_dir .. '/decoder.t7')
decoder:float()
decoder:clearState()
collectgarbage()
if paths.filep(model_opts.save_dir .. '/rng.t7') then
print('Loading RNG')
torch.setRNGState(torch.load(model_opts.save_dir .. '/rng.t7'))
cutorch.setRNGState(torch.load(model_opts.save_dir .. '/rng_cuda.t7'))
end
-- print(gpuIDs)
set_gpu_id = function(model, gpuIDs)
for i = 1, #model.modules do
model.modules[i].device = gpuIDs[i]
if i == #model.modules then
model.modules[i].outdevice = gpu1
else
model.modules[i].outdevice = gpuIDs[i+1]
end
end
return model
end
gpuIDs = torch.zeros(#encoder.modules):fill(gpu1)
-- gpuIDs:sub(9,#encoder.modules):fill(gpu2)
encoder = set_gpu_id(encoder, gpuIDs)
gpuIDs = torch.zeros(#decoder.modules):fill(gpu2)
decoder = set_gpu_id(decoder, gpuIDs)
gpuIDs = torch.zeros(#adversary.modules):fill(gpu2)
adversary = set_gpu_id(adversary, gpuIDs)
if adversaryGen ~= nil then
gpuIDs = torch.zeros(#adversaryGen.modules):fill(gpu3)
adversaryGen = set_gpu_id(adversaryGen, gpuIDs)
end
print('Done loading model')
else
print('Creating new model')
print(model_opts)
Model:create(model_opts)
paths.mkdir(model_opts.save_dir)
print('Done creating model')
decoder = Model.decoder
encoder = Model.encoder
adversary = Model.adversary
adversaryGen = Model.adversaryGen
-- print(gpuIDs)
set_gpu_id = function(model, gpuIDs)
for i = 1, #model.modules do
model.modules[i] = nn.GPU(model.modules[i], gpuIDs[i])
-- model.modules[i].device = gpuIDs[i]
if i == #model.modules then
model.modules[i].outdevice = gpu1
else
model.modules[i].outdevice = gpuIDs[i+1]
end
end
return model
end
gpuIDs = torch.zeros(#encoder.modules):fill(gpu1)
-- gpuIDs:sub(4,#encoder.modules):fill(gpu2)
encoder = set_gpu_id(encoder, gpuIDs)
gpuIDs = torch.zeros(#decoder.modules):fill(gpu2)
decoder = set_gpu_id(decoder, gpuIDs)
gpuIDs = torch.zeros(#adversary.modules):fill(gpu2)
adversary = set_gpu_id(adversary, gpuIDs)
gpuIDs = torch.zeros(#adversaryGen.modules):fill(gpu3)
adversaryGen = set_gpu_id(adversaryGen, gpuIDs)
opts_optnet = {inplace=true, mode='training'}
optnet.optimizeMemory(encoder, dataProvider:getImages(torch.LongTensor{1,2}, 'train'), opts_optnet)
optnet.optimizeMemory(decoder, encoder.output, opts_optnet)
optnet.optimizeMemory(adversary, encoder.output[#encoder.output], opts_optnet)
optnet.optimizeMemory(adversaryGen, dataProvider:getImages(torch.LongTensor{1,2}, 'train'), opts_optnet)
Model = nil
end
criterion_out = nn.BCECriterion()
criterion_label = nn.ClassNLLCriterion()
criterion_other = nn.MSECriterion()
criterion_latent = nn.MSECriterion()
criterion_adv = nn.BCECriterion()
criterionAdvGen = nn.BCECriterion()
if cuda then
-- set which parts of the model are on which gpu
print('Converting to cuda')
encoder:cuda()
decoder:cuda()
adversary:cuda()
if adversaryGen ~= nil then
adversaryGen:cuda()
end
criterion_label:cuda()
criterion_other:cuda()
criterion_latent:cuda()
criterion_out:cuda()
criterion_adv:cuda()
criterionAdvGen:cuda()
print('Done converting to cuda')
end
if model_opts.test_model then
print('Data size')
print(dataProvider.train.inds:size())
encoder:evaluate()
decoder:evaluate()
adversary:evaluate()
local im_in, im_out = dataProvider:getImages(torch.LongTensor{1}, 'train')
local label = dataProvider:getLabels(torch.LongTensor{1}, 'train'):clone()
print('Testing encoder')
local code = encoder:forward(im_in:cuda());
print(im_in:type())
print(im_in:size())
print('Code size:')
print(code)
print(label)
print(torch.cat(code, 2):size())
print('Testing decoder')
local im_out_hat = decoder:forward(code)
print('Out size:')
print(im_out_hat:size())
print(criterion_out:forward(im_out_hat, im_out:cuda()))
-- itorch.image(imtools.im2projection(im_out))
-- itorch.image(imtools.im2projection(im_out_hat))
print('Testing adversary')
print(adversary:forward(code[#code]))
encoder:training()
decoder:training()
adversary:training()
adversaryGen:training()
end
cudnn.benchmark = false
cudnn.fastest = false
cudnn.convert(encoder, cudnn)
cudnn.convert(decoder, cudnn)
cudnn.convert(adversary, cudnn)
if adversaryGen ~= nil then
cudnn.convert(adversaryGen, cudnn)
end
print('Done getting parameters')
end
function setup.getLearnOpts(model_opts)
opt_path = model_opts.save_dir .. '/opt.t7'
optEnc_path = model_opts.save_dir .. '/optEnc.t7'
optDec_path = model_opts.save_dir .. '/optDec.t7'
optAdv_path = model_opts.save_dir .. '/optD.t7'
optAdvGen_path = model_opts.save_dir .. '/optAdvGen.t7'
stateEnc_path = model_opts.save_dir .. '/stateEnc.t7'
stateDec_path = model_opts.save_dir .. '/stateDec.t7'
stateAdv_path = model_opts.save_dir .. '/stateD.t7'
stateAdvGen_path = model_opts.save_dir .. '/stateAdvGen.t7'
plots_path = model_opts.save_dir .. '/plots.t7'
if paths.filep(opt_path) then
print('Loading previous optimizer state')
opt = torch.load(opt_path)
optEnc = torch.load(optEnc_path)
optDec = torch.load(optDec_path)
optAdv = torch.load(optAdv_path)
optAdvGen = torch.load(optAdv_path)
stateEnc = utils.table2cuda(torch.load(stateEnc_path))
stateDec = utils.table2cuda(torch.load(stateDec_path))
stateAdv = utils.table2cuda(torch.load(stateAdv_path))
plots = torch.load(plots_path)
if paths.filep(stateAdvGen_path) then
stateAdvGen = utils.table2cuda(torch.load(stateAdvGen_path))
losses = plots[1][3]:totable()
latentlosses = plots[2][3]:totable()
advlosses = plots[3][3]:totable()
advGenLosses = plots[4][3]:totable()
advMinimaxLoss = plots[5][3]:totable()
advGenMinimaxLoss = plots[6][3]:totable()
reencodelosses = plots[7][3]:totable()
else
print('Could not find stateAdvGen_path: ' .. stateAdvGen_path)
stateAdvGen = {}
losses = plots[1][3]:totable()
latentlosses = plots[2][3]:totable()
advlosses = plots[3][3]:totable()
advMinimaxLoss = plots[4][3]:totable()
reencodelosses = plots[5][3]:totable()
end
else
opt = {}
opt.epoch = 0
opt.nepochs = 2000
opt.adversarial = true
opt.adversarialGen = model_opts.adversarialGen
opt.learningRateA = 0.01
opt.learningRateAdv = 0.01
opt.min_rateA = 0.0000001
opt.min_rateD = 0.0001
opt.learningRateDecay = 0.999
opt.optimizer = 'adam'
opt.batchSize = 64
opt.verbose = model_opts.verbose
-- opt.updateD = 0.50
-- opt.updateA = 0.40
opt.update_thresh = 0.58
opt.saveProgressIter = 5
opt.saveStateIter = 50
optEnc = {}
optEnc.optimizer = opt.optimizer
optEnc.learningRate = opt.learningRateA
optEnc.min_rateA = 0.0000001
optEnc.beta1 = 0.5
-- optEnc.momentum = 0
-- optEnc.numUpdates = 0
-- optEnc.coefL2 = 0
-- optEnc.coefL1 = 0
optDec = {}
optDec.optimizer = opt.optimizer
optDec.learningRate = opt.learningRateA
optDec.min_rateA = 0.0000001
optDec.beta1 = 0.5
-- optDec.momentum = 0
-- optDec.numUpdates = 0
-- optDec.coefL2 = 0
-- optDec.coefL1 = 0
optAdv = {}
optAdv.optimizer = opt.optimizer
optAdv.learningRate = opt.learningRateAdv
optAdv.min_rateA = 0.0000001
optAdv.beta1 = 0.5
-- optAdv.momentum = 0
-- optAdv.numUpdates = 0
-- optAdv.coefL2 = 0
-- optAdv.coefL1 = 0
optAdvGen = {}
optAdvGen.optimizer = opt.optimizer
optAdvGen.learningRate = opt.learningRateAdv
optAdvGen.min_rateA = 0.0000001
optAdvGen.beta1 = 0.5
-- optAdvGen.momentum = 0
-- optAdvGen.numUpdates = 0
-- optAdvGen.coefL2 = 0
-- optAdvGen.coefL1 = 0
stateEnc = {}
stateDec = {}
stateAdv = {}
stateAdvGen = {}
losses, latentlosses, advlosses, advGenLosses, advMinimaxLoss, advGenMinimaxLoss, advlossesGen, reencodelosses = {}, {}, {}, {}, {}, {}, {}, {}, {}
end
plots = {}
loss, advloss, advlossGen = {}, {}, {}
end
return setup