forked from junyanz/CycleGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpix2pix_model.lua
216 lines (178 loc) · 7.16 KB
/
pix2pix_model.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
local class = require 'class'
require 'models.base_model'
require 'models.architectures'
require 'util.image_pool'
util = paths.dofile('../util/util.lua')
Pix2PixModel = class('Pix2PixModel', 'BaseModel')
function Pix2PixModel:__init(conf)
conf = conf or {}
end
-- Returns the name of the model
function Pix2PixModel:model_name()
return 'Pix2PixModel'
end
function Pix2PixModel:InitializeStates()
return {learningRate=opt.lr, beta1=opt.beta1,}
end
-- Defines models and networks
function Pix2PixModel:Initialize(opt) -- use lsgan
-- define tensors
local d_input_nc = opt.input_nc + opt.output_nc
self.real_AB = torch.Tensor(opt.batchSize, d_input_nc, opt.fineSize, opt.fineSize)
self.fake_AB = torch.Tensor(opt.batchSize, d_input_nc, opt.fineSize, opt.fineSize)
if opt.test == 0 then
self.fakeABPool = ImagePool(opt.pool_size)
end
-- load/define models
self.criterionGAN = nn.MSECriterion()
self.criterionL1 = nn.AbsCriterion()
local netG, netD = nil, nil
if opt.continue_train == 1 then
if opt.which_epoch then -- which_epoch option exists in test mode
netG = util.load_test_model('G', opt)
netD = util.load_test_model('D', opt)
else
netG = util.load_model('G', opt)
netD = util.load_model('D', opt)
end
else
netG = defineG(opt.input_nc, opt.output_nc, opt.ngf, opt.which_model_netG)
netD = defineD(d_input_nc, opt.ndf, opt.which_model_netD, opt.n_layers_D, false) -- with sigmoid
end
self.netD = netD
self.netG = netG
-- define real/fake labels
netD_output_size = self.netD:forward(self.real_AB):size()
self.fake_label = torch.Tensor(netD_output_size):fill(0.0)
self.real_label = torch.Tensor(netD_output_size):fill(1.0) -- no soft smoothing
self.optimStateD = self:InitializeStates()
self.optimStateG = self:InitializeStates()
self:RefreshParameters()
self.A_idx = {{}, {1, opt.input_nc}, {}, {}}
self.B_idx = {{}, {opt.input_nc+1, opt.input_nc+opt.output_nc}, {}, {}}
print('---------- # Learnable Parameters --------------')
print(('G = %d'):format(self.parametersG:size(1)))
print(('D = %d'):format(self.parametersD:size(1)))
print('------------------------------------------------')
end
-- Runs the forward pass of the network
function Pix2PixModel:Forward(input, opt)
if opt.which_direction == 'BtoA' then
local temp = input.real_A
input.real_A = input.real_B
input.real_B = temp
end
self.real_AB[self.A_idx]:copy(input.real_A)
self.real_AB[self.B_idx]:copy(input.real_B)
self.real_A = self.real_AB[self.A_idx]
self.real_B = self.real_AB[self.B_idx]
self.fake_AB[self.A_idx]:copy(self.real_A)
self.fake_B = self.netG:forward(self.real_A):clone()
self.fake_AB[self.B_idx]:copy(self.fake_B)
end
-- create closure to evaluate f(X) and df/dX of discriminator
function Pix2PixModel:fDx_basic(x, gradParams, netD, netG, real, fake, opt)
util.BiasZero(netD)
util.BiasZero(netG)
gradParams:zero()
-- Real log(D(B))
local output = netD:forward(real)
local errD_real = self.criterionGAN:forward(output, self.real_label)
local df_do = self.criterionGAN:backward(output, self.real_label)
netD:backward(real, df_do)
-- Fake + log(1 - D(G(A)))
output = netD:forward(fake)
local errD_fake = self.criterionGAN:forward(output, self.fake_label)
local df_do2 = self.criterionGAN:backward(output, self.fake_label)
netD:backward(fake, df_do2)
-- calculate loss
local errD = (errD_real + errD_fake) / 2.0
return errD, gradParams
end
function Pix2PixModel:fDx(x, opt)
fake_AB = self.fakeABPool:Query(self.fake_AB)
self.errD, gradParams = self:fDx_basic(x, self.gradParametersD, self.netD, self.netG,
self.real_AB, fake_AB, opt)
return self.errD, gradParams
end
function Pix2PixModel:fGx_basic(x, netG, netD, real, fake, gradParametersG, opt)
util.BiasZero(netG)
util.BiasZero(netD)
gradParametersG:zero()
-- First. G(A) should fake the discriminator
local output = netD:forward(fake)
local errG = self.criterionGAN:forward(output, self.real_label)
local dgan_loss_dd = self.criterionGAN:backward(output, self.real_label)
local dgan_loss_do = netD:updateGradInput(fake, dgan_loss_dd)
-- Second. G(A) should be close to the real
real_B = real[self.B_idx]
real_A = real[self.A_idx]
fake_B = fake[self.B_idx]
local errL1 = self.criterionL1:forward(fake_B, real_B) * opt.lambda
local dl1_loss_do = self.criterionL1:backward(fake_B, real_B) * opt.lambda
netG:backward(real_A, dgan_loss_do[self.B_idx] + dl1_loss_do)
return gradParametersG, errG, errL1
end
function Pix2PixModel:fGx(x, opt)
self.gradParametersG, self.errG, self.errL1 = self:fGx_basic(x, self.netG, self.netD,
self.real_AB, self.fake_AB, self.gradParametersG, opt)
return self.errG, self.gradParametersG
end
-- Runs the backprop gradient descent
-- Corresponds to a single batch of data
function Pix2PixModel:OptimizeParameters(opt)
local fD = function(x) return self:fDx(x, opt) end
local fG = function(x) return self:fGx(x, opt) end
optim.adam(fD, self.parametersD, self.optimStateD)
optim.adam(fG, self.parametersG, self.optimStateG)
end
-- This function can be used to reset momentum after each epoch
function Pix2PixModel:RefreshParameters()
self.parametersD, self.gradParametersD = nil, nil -- nil them to avoid spiking memory
self.parametersG, self.gradParametersG = nil, nil
-- define parameters of optimization
self.parametersG, self.gradParametersG = self.netG:getParameters()
self.parametersD, self.gradParametersD = self.netD:getParameters()
end
-- This function updates the learning rate; lr for the first opt.niter iterations; graduatlly decreases the lr to 0 for the next opt.niter_decay iterations
function Pix2PixModel:UpdateLearningRate(opt)
local lrd = opt.lr / opt.niter_decay
local old_lr = self.optimStateD['learningRate']
local lr = old_lr - lrd
self.optimStateD['learningRate'] = lr
self.optimStateG['learningRate'] = lr
print(('update learning rate: %f -> %f'):format(old_lr, lr))
end
-- Save the current model to the file system
function Pix2PixModel:Save(prefix, opt)
util.save_model(self.netG, prefix .. '_net_G.t7', 1.0)
util.save_model(self.netD, prefix .. '_net_D.t7', 1.0)
end
-- returns a string that describes the current errors
function Pix2PixModel:GetCurrentErrorDescription()
description = ('G: %.4f D: %.4f L1: %.4f'):format(
self.errG and self.errG or -1, self.errD and self.errD or -1, self.errL1 and self.errL1 or -1)
return description
end
-- returns a string that describes the display plot configuration
function Pix2PixModel:DisplayPlot(opt)
return 'errG,errD,errL1'
end
-- returns current errors
function Pix2PixModel:GetCurrentErrors()
local errors = {errG=self.errG, errD=self.errD, errL1=self.errL1}
return errors
end
-- returns a table of image/label pairs that describe
-- the current results.
-- |return|: a table of table. List of image/label pairs
function Pix2PixModel:GetCurrentVisuals(opt, size)
if not size then
size = opt.display_winsize
end
local visuals = {}
table.insert(visuals, {img=self.real_A, label='real_A'})
table.insert(visuals, {img=self.fake_B, label='fake_B'})
table.insert(visuals, {img=self.real_B, label='real_B'})
return visuals
end