-
Notifications
You must be signed in to change notification settings - Fork 204
/
train.lua
458 lines (410 loc) · 18.6 KB
/
train.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
require 'torch'
require 'nn'
require 'optim'
util = paths.dofile('util.lua')
opt = {
batchSize = 64, -- number of samples to produce
loadSize = 350, -- resize the loaded image to loadsize maintaining aspect ratio. 0 means don't resize. -1 means scale randomly between [0.5,2] -- see donkey_folder.lua
fineSize = 128, -- size of random crops
nBottleneck = 100, -- # of dim for bottleneck of encoder
nef = 64, -- # of encoder filters in first conv layer
ngf = 64, -- # of gen filters in first conv layer
ndf = 64, -- # of discrim filters in first conv layer
nc = 3, -- # of channels in input
wtl2 = 0, -- 0 means don't use else use with this weight
overlapPred = 0, -- overlapping edges
nThreads = 4, -- # of data loading threads to use
niter = 25, -- # of iter at starting learning rate
lr = 0.0002, -- initial learning rate for adam
beta1 = 0.5, -- momentum term of adam
ntrain = math.huge, -- # of examples per epoch. math.huge for full dataset
display = 1, -- display samples while training. 0 = false
display_id = 10, -- display window id.
display_iter = 50, -- # number of iterations after which display is updated
gpu = 1, -- gpu = 0 is CPU mode. gpu=X is GPU mode on GPU X
name = 'train1', -- name of the experiment you are running
manualSeed = 0, -- 0 means random seed
-- Extra Options:
conditionAdv = 0, -- 0 means false else true
noiseGen = 0, -- 0 means false else true
noisetype = 'normal', -- uniform / normal
nz = 100, -- # of dim for Z
}
for k,v in pairs(opt) do opt[k] = tonumber(os.getenv(k)) or os.getenv(k) or opt[k] end
print(opt)
if opt.display == 0 then opt.display = false end
if opt.conditionAdv == 0 then opt.conditionAdv = false end
if opt.noiseGen == 0 then opt.noiseGen = false end
-- set seed
if opt.manualSeed == 0 then
opt.manualSeed = torch.random(1, 10000)
end
print("Seed: " .. opt.manualSeed)
torch.manualSeed(opt.manualSeed)
torch.setnumthreads(1)
torch.setdefaulttensortype('torch.FloatTensor')
-- create data loader
local DataLoader = paths.dofile('data/data.lua')
local data = DataLoader.new(opt.nThreads, opt)
print("Dataset Size: ", data:size())
---------------------------------------------------------------------------
-- Initialize network variables
---------------------------------------------------------------------------
local function weights_init(m)
local name = torch.type(m)
if name:find('Convolution') 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
local nc = opt.nc
local nz = opt.nz
local nBottleneck = opt.nBottleneck
local ndf = opt.ndf
local ngf = opt.ngf
local nef = opt.nef
local real_label = 1
local fake_label = 0
local SpatialBatchNormalization = nn.SpatialBatchNormalization
local SpatialConvolution = nn.SpatialConvolution
local SpatialFullConvolution = nn.SpatialFullConvolution
---------------------------------------------------------------------------
-- Generator net
---------------------------------------------------------------------------
-- Encode Input Context to noise (architecture similar to Discriminator)
local netE = nn.Sequential()
-- input is (nc) x 128 x 128
netE:add(SpatialConvolution(nc, nef, 4, 4, 2, 2, 1, 1))
netE:add(nn.LeakyReLU(0.2, true))
-- state size: (nef) x 64 x 64
netE:add(SpatialConvolution(nef, nef, 4, 4, 2, 2, 1, 1))
netE:add(SpatialBatchNormalization(nef)):add(nn.LeakyReLU(0.2, true))
-- state size: (nef) x 32 x 32
netE:add(SpatialConvolution(nef, nef * 2, 4, 4, 2, 2, 1, 1))
netE:add(SpatialBatchNormalization(nef * 2)):add(nn.LeakyReLU(0.2, true))
-- state size: (nef*2) x 16 x 16
netE:add(SpatialConvolution(nef * 2, nef * 4, 4, 4, 2, 2, 1, 1))
netE:add(SpatialBatchNormalization(nef * 4)):add(nn.LeakyReLU(0.2, true))
-- state size: (nef*4) x 8 x 8
netE:add(SpatialConvolution(nef * 4, nef * 8, 4, 4, 2, 2, 1, 1))
netE:add(SpatialBatchNormalization(nef * 8)):add(nn.LeakyReLU(0.2, true))
-- state size: (nef*8) x 4 x 4
netE:add(SpatialConvolution(nef * 8, nBottleneck, 4, 4))
-- state size: (nBottleneck) x 1 x 1
local netG = nn.Sequential()
local nz_size = nBottleneck
if opt.noiseGen then
local netG_noise = nn.Sequential()
-- input is Z: (nz) x 1 x 1, going into a convolution
netG_noise:add(SpatialConvolution(nz, nz, 1, 1, 1, 1, 0, 0))
-- state size: (nz) x 1 x 1
local netG_pl = nn.ParallelTable();
netG_pl:add(netE)
netG_pl:add(netG_noise)
netG:add(netG_pl)
netG:add(nn.JoinTable(2))
netG:add(SpatialBatchNormalization(nBottleneck+nz)):add(nn.LeakyReLU(0.2, true))
-- state size: (nBottleneck+nz) x 1 x 1
nz_size = nBottleneck+nz
else
netG:add(netE)
netG:add(SpatialBatchNormalization(nBottleneck)):add(nn.LeakyReLU(0.2, true))
nz_size = nBottleneck
end
-- Decode noise to generate image
-- input is Z: (nz_size) x 1 x 1, going into a convolution
netG:add(SpatialFullConvolution(nz_size, ngf * 8, 4, 4))
netG:add(SpatialBatchNormalization(ngf * 8)):add(nn.ReLU(true))
-- state size: (ngf*8) x 4 x 4
netG:add(SpatialFullConvolution(ngf * 8, ngf * 4, 4, 4, 2, 2, 1, 1))
netG:add(SpatialBatchNormalization(ngf * 4)):add(nn.ReLU(true))
-- state size: (ngf*4) x 8 x 8
netG:add(SpatialFullConvolution(ngf * 4, ngf * 2, 4, 4, 2, 2, 1, 1))
netG:add(SpatialBatchNormalization(ngf * 2)):add(nn.ReLU(true))
-- state size: (ngf*2) x 16 x 16
netG:add(SpatialFullConvolution(ngf * 2, ngf, 4, 4, 2, 2, 1, 1))
netG:add(SpatialBatchNormalization(ngf)):add(nn.ReLU(true))
-- state size: (ngf) x 32 x 32
netG:add(SpatialFullConvolution(ngf, nc, 4, 4, 2, 2, 1, 1))
netG:add(nn.Tanh())
-- state size: (nc) x 64 x 64
netG:apply(weights_init)
---------------------------------------------------------------------------
-- Adversarial discriminator net
---------------------------------------------------------------------------
local netD = nn.Sequential()
if opt.conditionAdv then
local netD_ctx = nn.Sequential()
-- input Context: (nc) x 128 x 128, going into a convolution
netD_ctx:add(SpatialConvolution(nc, ndf, 5, 5, 2, 2, 2, 2))
-- state size: (ndf) x 64 x 64
local netD_pred = nn.Sequential()
-- input pred: (nc) x 64 x 64, going into a convolution
netD_pred:add(SpatialConvolution(nc, ndf, 5, 5, 2, 2, 2+32, 2+32)) -- 32: to keep scaling of features same as context
-- state size: (ndf) x 64 x 64
local netD_pl = nn.ParallelTable();
netD_pl:add(netD_ctx)
netD_pl:add(netD_pred)
netD:add(netD_pl)
netD:add(nn.JoinTable(2))
netD:add(nn.LeakyReLU(0.2, true))
-- state size: (ndf * 2) x 64 x 64
netD:add(SpatialConvolution(ndf*2, ndf, 4, 4, 2, 2, 1, 1))
netD:add(SpatialBatchNormalization(ndf)):add(nn.LeakyReLU(0.2, true))
-- state size: (ndf) x 32 x 32
else
-- input is (nc) x 64 x 64, going into a convolution
netD:add(SpatialConvolution(nc, ndf, 4, 4, 2, 2, 1, 1))
netD:add(nn.LeakyReLU(0.2, true))
-- state size: (ndf) x 32 x 32
end
netD:add(SpatialConvolution(ndf, ndf * 2, 4, 4, 2, 2, 1, 1))
netD:add(SpatialBatchNormalization(ndf * 2)):add(nn.LeakyReLU(0.2, true))
-- state size: (ndf*2) x 16 x 16
netD:add(SpatialConvolution(ndf * 2, ndf * 4, 4, 4, 2, 2, 1, 1))
netD:add(SpatialBatchNormalization(ndf * 4)):add(nn.LeakyReLU(0.2, true))
-- state size: (ndf*4) x 8 x 8
netD:add(SpatialConvolution(ndf * 4, ndf * 8, 4, 4, 2, 2, 1, 1))
netD:add(SpatialBatchNormalization(ndf * 8)):add(nn.LeakyReLU(0.2, true))
-- state size: (ndf*8) x 4 x 4
netD:add(SpatialConvolution(ndf * 8, 1, 4, 4))
netD:add(nn.Sigmoid())
-- state size: 1 x 1 x 1
netD:add(nn.View(1):setNumInputDims(3))
-- state size: 1
netD:apply(weights_init)
---------------------------------------------------------------------------
-- Loss Metrics
---------------------------------------------------------------------------
local criterion = nn.BCECriterion()
local criterionMSE
if opt.wtl2~=0 then
criterionMSE = nn.MSECriterion()
end
---------------------------------------------------------------------------
-- Setup Solver
---------------------------------------------------------------------------
print('LR of Gen is ',(opt.wtl2>0 and opt.wtl2<1) and 10 or 1,'times Adv')
optimStateG = {
learningRate = (opt.wtl2>0 and opt.wtl2<1) and opt.lr*10 or opt.lr,
beta1 = opt.beta1,
}
optimStateD = {
learningRate = opt.lr,
beta1 = opt.beta1,
}
---------------------------------------------------------------------------
-- Initialize data variables
---------------------------------------------------------------------------
local input_ctx_vis = torch.Tensor(opt.batchSize, nc, opt.fineSize, opt.fineSize)
local input_ctx = torch.Tensor(opt.batchSize, nc, opt.fineSize, opt.fineSize)
local input_center = torch.Tensor(opt.batchSize, nc, opt.fineSize/2, opt.fineSize/2)
local input_real_center
if opt.wtl2~=0 then
input_real_center = torch.Tensor(opt.batchSize, nc, opt.fineSize/2, opt.fineSize/2)
end
local noise = torch.Tensor(opt.batchSize, nz, 1, 1)
local label = torch.Tensor(opt.batchSize)
local errD, errG, errG_l2
local epoch_tm = torch.Timer()
local tm = torch.Timer()
local data_tm = torch.Timer()
if pcall(require, 'cudnn') and pcall(require, 'cunn') and opt.gpu>0 then
print('Using CUDNN !')
end
if opt.gpu > 0 then
require 'cunn'
cutorch.setDevice(opt.gpu)
input_ctx_vis = input_ctx_vis:cuda(); input_ctx = input_ctx:cuda(); input_center = input_center:cuda()
noise = noise:cuda(); label = label:cuda()
netG = util.cudnn(netG); netD = util.cudnn(netD)
netD:cuda(); netG:cuda(); criterion:cuda();
if opt.wtl2~=0 then
criterionMSE:cuda(); input_real_center = input_real_center:cuda();
end
end
print('NetG:',netG)
print('NetD:',netD)
local parametersD, gradParametersD = netD:getParameters()
local parametersG, gradParametersG = netG:getParameters()
if opt.display then disp = require 'display' end
noise_vis = noise:clone()
if opt.noisetype == 'uniform' then
noise_vis:uniform(-1, 1)
elseif opt.noisetype == 'normal' then
noise_vis:normal(0, 1)
end
---------------------------------------------------------------------------
-- Define generator and adversary closures
---------------------------------------------------------------------------
-- create closure to evaluate f(X) and df/dX of discriminator
local fDx = function(x)
netD:apply(function(m) if torch.type(m):find('Convolution') then m.bias:zero() end end)
netG:apply(function(m) if torch.type(m):find('Convolution') then m.bias:zero() end end)
gradParametersD:zero()
-- train with real
data_tm:reset(); data_tm:resume()
local real_ctx = data:getBatch()
local real_center = real_ctx[{{},{},{1 + opt.fineSize/4, opt.fineSize/2 + opt.fineSize/4},{1 + opt.fineSize/4, opt.fineSize/2 + opt.fineSize/4}}]:clone() -- copy by value
real_ctx[{{},{1},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred}}] = 2*117.0/255.0 - 1.0
real_ctx[{{},{2},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred}}] = 2*104.0/255.0 - 1.0
real_ctx[{{},{3},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred}}] = 2*123.0/255.0 - 1.0
data_tm:stop()
input_ctx:copy(real_ctx)
input_center:copy(real_center)
if opt.wtl2~=0 then
input_real_center:copy(real_center)
end
label:fill(real_label)
local output
if opt.conditionAdv then
output = netD:forward({input_ctx,input_center})
else
output = netD:forward(input_center)
end
local errD_real = criterion:forward(output, label)
local df_do = criterion:backward(output, label)
if opt.conditionAdv then
netD:backward({input_ctx,input_center}, df_do)
else
netD:backward(input_center, df_do)
end
-- train with fake
if opt.noisetype == 'uniform' then -- regenerate random noise
noise:uniform(-1, 1)
elseif opt.noisetype == 'normal' then
noise:normal(0, 1)
end
local fake
if opt.noiseGen then
fake = netG:forward({input_ctx,noise})
else
fake = netG:forward(input_ctx)
end
input_center:copy(fake)
label:fill(fake_label)
local output
if opt.conditionAdv then
output = netD:forward({input_ctx,input_center})
else
output = netD:forward(input_center)
end
local errD_fake = criterion:forward(output, label)
local df_do = criterion:backward(output, label)
if opt.conditionAdv then
netD:backward({input_ctx,input_center}, df_do)
else
netD:backward(input_center, df_do)
end
errD = errD_real + errD_fake
return errD, gradParametersD
end
-- create closure to evaluate f(X) and df/dX of generator
local fGx = function(x)
netD:apply(function(m) if torch.type(m):find('Convolution') then m.bias:zero() end end)
netG:apply(function(m) if torch.type(m):find('Convolution') then m.bias:zero() end end)
gradParametersG:zero()
--[[ the three lines below were already executed in fDx, so save computation
noise:uniform(-1, 1) -- regenerate random noise
local fake = netG:forward({input_ctx,noise})
input_center:copy(fake) ]]--
label:fill(real_label) -- fake labels are real for generator cost
local output = netD.output -- netD:forward({input_ctx,input_center}) was already executed in fDx, so save computation
errG = criterion:forward(output, label)
local df_do = criterion:backward(output, label)
local df_dg
if opt.conditionAdv then
df_dg = netD:updateGradInput({input_ctx,input_center}, df_do)
df_dg = df_dg[2] -- df_dg[2] because conditional GAN
else
df_dg = netD:updateGradInput(input_center, df_do)
end
local errG_total = errG
if opt.wtl2~=0 then
errG_l2 = criterionMSE:forward(input_center, input_real_center)
local df_dg_l2 = criterionMSE:backward(input_center, input_real_center)
if opt.overlapPred==0 then
if (opt.wtl2>0 and opt.wtl2<1) then
df_dg:mul(1-opt.wtl2):add(opt.wtl2,df_dg_l2)
errG_total = (1-opt.wtl2)*errG + opt.wtl2*errG_l2
else
df_dg:add(opt.wtl2,df_dg_l2)
errG_total = errG + opt.wtl2*errG_l2
end
else
local overlapL2Weight = 10
local wtl2Matrix = df_dg_l2:clone():fill(overlapL2Weight*opt.wtl2)
wtl2Matrix[{{},{},{1 + opt.overlapPred, opt.fineSize/2 - opt.overlapPred},{1 + opt.overlapPred, opt.fineSize/2 - opt.overlapPred}}]:fill(opt.wtl2)
if (opt.wtl2>0 and opt.wtl2<1) then
df_dg:mul(1-opt.wtl2):addcmul(1,wtl2Matrix,df_dg_l2)
errG_total = (1-opt.wtl2)*errG + opt.wtl2*errG_l2
else
df_dg:addcmul(1,wtl2Matrix,df_dg_l2)
errG_total = errG + opt.wtl2*errG_l2
end
end
end
if opt.noiseGen then
netG:backward({input_ctx,noise}, df_dg)
else
netG:backward(input_ctx, df_dg)
end
return errG_total, gradParametersG
end
---------------------------------------------------------------------------
-- Train Context Encoder
---------------------------------------------------------------------------
for epoch = 1, opt.niter do
epoch_tm:reset()
local counter = 0
for i = 1, math.min(data:size(), opt.ntrain), opt.batchSize do
tm:reset()
-- (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
optim.adam(fDx, parametersD, optimStateD)
-- (2) Update G network: maximize log(D(G(z)))
optim.adam(fGx, parametersG, optimStateG)
-- display
counter = counter + 1
if counter % opt.display_iter == 0 and opt.display then
local real_ctx = data:getBatch()
local real_center = real_ctx[{{},{},{1 + opt.fineSize/4, opt.fineSize/2 + opt.fineSize/4},{1 + opt.fineSize/4, opt.fineSize/2 + opt.fineSize/4}}]:clone() -- copy by value
real_ctx[{{},{1},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred}}] = 2*117.0/255.0 - 1.0
real_ctx[{{},{2},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred}}] = 2*104.0/255.0 - 1.0
real_ctx[{{},{3},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred}}] = 2*123.0/255.0 - 1.0
input_ctx_vis:copy(real_ctx)
local fake
if opt.noiseGen then
fake = netG:forward({input_ctx_vis,noise_vis})
else
fake = netG:forward(input_ctx_vis)
end
real_ctx[{{},{},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred},{1 + opt.fineSize/4 + opt.overlapPred, opt.fineSize/2 + opt.fineSize/4 - opt.overlapPred}}]:copy(fake[{{},{},{1 + opt.overlapPred, opt.fineSize/2 - opt.overlapPred},{1 + opt.overlapPred, opt.fineSize/2 - opt.overlapPred}}])
disp.image(fake, {win=opt.display_id, title=opt.name})
disp.image(real_center, {win=opt.display_id * 3, title=opt.name})
disp.image(real_ctx, {win=opt.display_id * 6, title=opt.name})
end
-- logging
if ((i-1) / opt.batchSize) % 1 == 0 then
print(('Epoch: [%d][%8d / %8d]\t Time: %.3f DataTime: %.3f '
.. ' Err_G_L2: %.4f Err_G: %.4f Err_D: %.4f'):format(
epoch, ((i-1) / opt.batchSize),
math.floor(math.min(data:size(), opt.ntrain) / opt.batchSize),
tm:time().real, data_tm:time().real, errG_l2 or -1,
errG and errG or -1, errD and errD or -1))
end
end
paths.mkdir('checkpoints')
parametersD, gradParametersD = nil, nil -- nil them to avoid spiking memory
parametersG, gradParametersG = nil, nil
if epoch % 20 == 0 then
util.save('checkpoints/' .. opt.name .. '_' .. epoch .. '_net_G.t7', netG, opt.gpu)
util.save('checkpoints/' .. opt.name .. '_' .. epoch .. '_net_D.t7', netD, opt.gpu)
end
parametersD, gradParametersD = netD:getParameters() -- reflatten the params and get them
parametersG, gradParametersG = netG:getParameters()
print(('End of epoch %d / %d \t Time Taken: %.3f'):format(
epoch, opt.niter, epoch_tm:time().real))
end