-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.lua
204 lines (177 loc) · 6.74 KB
/
main.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
--[[
This file is a modified version of the one from dcgan.torch
(see https://github.com/soumith/dcgan.torch/blob/master/main.lua).
]]--
require 'torch'
require 'nn'
require 'optim'
require 'model'
opt = {
dataset = 'folder', -- folder
batchSize = 64, -- # of images per batch
loadSize = 64, -- rescale images to this value
imgSize = 64, -- size of the images
nz = 100, -- # of dim for Z
ngf = 64, -- # of gen filters in first conv layer
ndf = 64, -- # of discrim filters in first conv layer
nThreads = 0, -- # 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 = 5562, -- display port during training. 0 = no display
winId = 10, -- display window id.
gpu = 1, -- gpu = 0 is CPU mode. gpu=X is GPU mode on GPU X
name = 'experiment1', -- name of your experiment, to save the networks
noise = 'normal', -- uniform / normal
}
-- one-line argument parser. parses enviroment variables to override the defaults
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
opt.manualSeed = torch.random(1, 10000) -- fix seed
print("Random 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.dataset, opt)
print("Dataset: " .. opt.dataset, " Size: ", data:size())
----------------------------------------------------------------------------
local nc = 3
local nz = opt.nz
local ndf = opt.ndf
local ngf = opt.ngf
local real_label = 1
local fake_label = 0
-- load models
local netG = get_netG(nz, ngf, nc)
local netD = get_netD(nc, ndf)
local criterion = nn.BCECriterion()
---------------------------------------------------------------------------
optimStateG = {
learningRate = opt.lr,
beta1 = opt.beta1,
}
optimStateD = {
learningRate = opt.lr,
beta1 = opt.beta1,
}
----------------------------------------------------------------------------
local input = torch.Tensor(opt.batchSize, 3, opt.imgSize, opt.imgSize)
local noise = torch.Tensor(opt.batchSize, nz, 1, 1)
local label = torch.Tensor(opt.batchSize)
local errD, errG
local epoch_tm = torch.Timer()
local tm = torch.Timer()
local data_tm = torch.Timer()
----------------------------------------------------------------------------
if opt.gpu > 0 then
require 'cunn'
require 'cudnn'
cutorch.setDevice(opt.gpu)
input = input:cuda()
noise = noise:cuda()
label = label:cuda()
netD:cuda()
netG:cuda()
criterion:cuda()
cudnn.benchmark = true
cudnn.convert(netG, cudnn)
cudnn.convert(netD, cudnn)
end
local parametersD, gradParametersD = netD:getParameters()
local parametersG, gradParametersG = netG:getParameters()
if opt.display then
disp = require 'display'
disp.configure({ hostname='0.0.0.0', port=opt.display })
end
noise_vis = noise:clone()
if opt.noise == 'uniform' then
noise_vis:uniform(-1, 1)
elseif opt.noise == 'normal' then
noise_vis:normal(0, 1)
end
-- create closure to evaluate f(X) and df/dX of discriminator
local fDx = function(x)
gradParametersD:zero()
-- train with real
data_tm:reset(); data_tm:resume()
local real = data:getBatch()
data_tm:stop()
input:copy(real)
label:fill(real_label)
local output = netD:forward(input)
local errD_real = criterion:forward(output, label)
local df_do = criterion:backward(output, label)
netD:backward(input, df_do)
-- train with fake
if opt.noise == 'uniform' then -- regenerate random noise
noise:uniform(-1, 1)
elseif opt.noise == 'normal' then
noise:normal(0, 1)
end
local fake = netG:forward(noise)
input:copy(fake)
label:fill(fake_label)
local output = netD:forward(input)
local errD_fake = criterion:forward(output, label)
local df_do = criterion:backward(output, label)
netD:backward(input, df_do)
errD = errD_real + errD_fake
return errD, gradParametersD
end
-- create closure to evaluate f(X) and df/dX of generator
local fGx = function(x)
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(noise)
input:copy(fake) ]]--
label:fill(real_label) -- fake labels are real for generator cost
local output = netD.output -- netD:forward(input) was already executed in fDx, so save computation
errG = criterion:forward(output, label)
local df_do = criterion:backward(output, label)
local df_dg = netD:updateGradInput(input, df_do)
netG:backward(noise, df_dg)
return errG, gradParametersG
end
-- train
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 % 2 == 0 and opt.display then
local fake = netG:forward(noise_vis)
local real = data:getBatch()
disp.image(fake, {win=opt.winId, title=opt.name})
disp.image(real, {win=opt.winId * 3, 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: %.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 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
torch.save('checkpoints/' .. opt.name .. '_' .. epoch .. '_net_G.t7', netG:clearState())
torch.save('checkpoints/' .. opt.name .. '_' .. epoch .. '_net_D.t7', netD:clearState())
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