-
Notifications
You must be signed in to change notification settings - Fork 90
/
train-vgg-decoder.lua
286 lines (237 loc) · 9.33 KB
/
train-vgg-decoder.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
require 'torch'
lapp = require 'pl.lapp'
opt = lapp[[
==== Required ====
--contentDir (default '') Content images for training
--styleDir (default '') Style images for training
==== Architecture ====
--activation (default 'relu') [relu|prelu|elu]
--instanceNorm Replaces batchnorms with instance norm.
--subpixelConv (default 0) Replaces upsampling with subpixel conv.
--tconv Replaced convs with transposed convs
--upsample (default 'nn') [nn|bilinear]
==== Basic options ====
--maxIter (default 80000)
--imageSize (default 256)
--targetLayer (default 'relu3_1') Target hidden layer
--numSamples (default 2) Batch size for training
--save (default 'vgginv')
--resume (default '') Model location
--gpu (default 0)
==== Optim ====
--learningRate (default 1e-3)
--learningRateDecay (default 1e-4)
--weightDecay (default 0)
--normalize Gradients at the loss function are normalized if enabled
--tv (default 1e-6)
--pixelLoss (default 0)
==== Verbosity ====
--saveEvery (default 500)
--printEvery (default 10)
--display Displays the training progress if enabled
--displayEvery (default 20)
--displayAddr (default '0.0.0.0')
--displayPort (default 8000)
]]
print(opt)
if opt.contentDir == '' then
error('--contentDir must be specified.')
end
if opt.styleDir == '' then
error('--styleDir must be specified.')
end
require 'nn'
require 'cudnn'
require 'cunn'
require 'image'
require 'paths'
require 'optim'
nninit = require 'nninit'
require 'lib/ImageLoaderAsync'
require 'lib/TVLossModule'
require 'lib/NonparametricPatchAutoencoderFactory'
require 'lib/MaxCoord'
require 'lib/InstanceNormalization'
require 'helpers/utils'
if opt.display then
display = require 'display'
display.configure({hostname=opt.displayAddr, port=opt.displayPort})
end
paths.mkdir(opt.save)
torch.save(paths.concat(opt.save, 'options.t7'), opt)
cutorch.setDevice(opt.gpu+1)
---- Arguments ----
local decoderActivation
if opt.activation == 'relu' then
decoderActivation = nn.ReLU
elseif opt.activation == 'prelu' then
decoderActivation = nn.PReLU
elseif opt.activation == 'elu' then
decoderActivation = nn.ELU
else
error('Unknown activation option ' .. opt.activation)
end
---- Load VGG ----
require 'loadcaffe'
vgg = loadcaffe.load('models/VGG_ILSVRC_19_layers_deploy.prototxt', 'models/VGG_ILSVRC_19_layers.caffemodel', 'nn')
---- Extract Encoder and Create Decoder ----
enc = nn.Sequential()
for i=1,#vgg do
local layer = vgg:get(i)
enc:add(layer)
local name = layer.name
if name == opt.targetLayer then
break
end
end
if enc:get(#enc).name ~= opt.targetLayer then
error('Could not find target layer ' .. opt.targetLayer)
end
if opt.resume ~= '' then
dec = torch.load(opt.resume)
else
dec = nn.Sequential()
local lastLayerWidth
for i=#enc,1,-1 do
local layer = enc:get(i)
if torch.type(layer):find('SpatialConvolution') then
local nInputPlane, nOutputPlane = layer.nOutputPlane, layer.nInputPlane
if opt.tconv then
dec:add(nn.SpatialFullConvolution(nInputPlane, nOutputPlane, 3,3):init('weight', nninit.orthogonal, {gain = 'relu'}))
dec:add(nn.SpatialZeroPadding(-1,-1,-1,-1))
else
dec:add(nn.SpatialConvolution(nInputPlane, nOutputPlane, 3,3, 1,1, 1,1):init('weight', nninit.orthogonal, {gain = 'relu'}))
end
if opt.instanceNorm then
dec:add(nn.InstanceNormalization(nOutputPlane))
else
dec:add(nn.SpatialBatchNormalization(nOutputPlane))
end
dec:add(decoderActivation())
lastLayerWidth = nOutputPlane
end
if torch.type(layer):find('MaxPooling') then
if opt.subpixelConv > 0 then
dec:add(nn.SpatialConvolution(lastLayerWidth, lastLayerWidth*4, opt.subpixelConv,opt.subpixelConv, 1,1, (opt.subpixelConv-1)/2,(opt.subpixelConv-1)/2))
dec:add(nn.PixelShuffle(2))
dec:add(decoderActivation())
else
dec:add(nn.SpatialUpSamplingNearest(2))
end
end
end
dec:remove()
dec:remove()
end
enc:insert(nn.TVLossModule(opt.tv), 1)
enc:insert(getPreprocessConv(), 1)
-- make sure to not cudnn the pooling layer.
enc = cudnn.convert(enc, cudnn):cuda()
dec = cudnn.convert(dec, cudnn):cuda()
print(enc)
print(dec)
---- Load Data ----
contentLoader = ImageLoaderAsync(opt.contentDir, opt.numSamples, {H=opt.imageSize, W=opt.imageSize})
styleLoader = ImageLoaderAsync(opt.styleDir, opt.numSamples, {H=opt.imageSize, W=opt.imageSize})
---- Criterion -----
criterion = nn.MSECriterion():cuda()
pixCriterion = nn.AbsCriterion():cuda()
---- Style Swap ----
function style_swap(content_latent, style_latent)
local swap_enc, swap_dec = NonparametricPatchAutoencoderFactory.buildAutoencoder(style_latent, opt.patchSize, 1, false, false, true)
local swap = nn.Sequential()
swap:add(swap_enc)
swap:add(nn.MaxCoord())
swap:add(swap_dec)
swap:evaluate()
swap:cuda()
local swap_latent = swap:forward(content_latent):clone()
swap:clearState()
swap = nil
collectgarbage()
return swap_latent
end
---- Training -----
optim_state = {
learningRate = opt.learningRate,
learningRateDecay = opt.learningRateDecay,
weightDecay = opt.weightDecay,
}
function maybe_print(trainLoss, timer)
if optim_state.iterCounter % opt.printEvery == 0 then
print(string.format('%7d\t\t%e\t%.2f\t%e',
optim_state.iterCounter, trainLoss, timer:time().real, optim_state.learningRate))
timer:reset()
end
end
function maybe_display(inputs, reconstructions)
if opt.display and (optim_state.iterCounter % opt.displayEvery == 0) then
local batch_size = inputs:size(1)
local disp = torch.cat(reconstructions:float(), inputs:float(), 1)
if display_window then
display.image(disp, {win=display_window, max=1, min=0})
else
display_window = display.image(disp, {max=1, min=0})
end
end
end
function maybe_save()
if optim_state.iterCounter % opt.saveEvery == 0 then
paths.mkdir(opt.save)
local loc = paths.concat(opt.save, string.format('dec-%06d.t7', optim_state.iterCounter))
torch.save(loc, cudnn.convert(dec:clearState():clone():float(), nn))
torch.save(paths.concat(opt.save, 'enc.t7'), cudnn.convert(enc:clearState():clone():float(), nn))
end
end
function train()
optim_state.iterCounter = optim_state.iterCounter or 0
local weights, gradients = dec:getParameters()
print('Training...\tTrainErr\ttime\tLearningRate')
local timer = torch.Timer()
while optim_state.iterCounter < opt.maxIter do
function feval(x)
gradients:zero()
optim_state.iterCounter = optim_state.iterCounter + 1
local inputs = torch.FloatTensor(opt.numSamples*2, 3, opt.imageSize, opt.imageSize)
inputs[{{1,opt.numSamples}}] = contentLoader:nextBatch()
inputs[{{opt.numSamples+1,opt.numSamples*2}}] = styleLoader:nextBatch()
inputs = inputs:cuda()
local latent = enc:forward(inputs):clone()
local C,H,W = latent:size(2), latent:size(3), latent:size(4)
-- add more batch dimensions to account for style swaps
latent:resize(opt.numSamples*2 + opt.numSamples^2, C,H,W)
local add = 1
for c=1,opt.numSamples do
for s=1,opt.numSamples do
local content = latent[c]
local style = latent[opt.numSamples+s]
latent[opt.numSamples*2 + add] = style_swap(content, style)
add = add + 1
end
end
---- Dec -> Enc -> Loss
local reconstructed_inputs = dec:forward(latent)
local reconstructed_latent = enc:forward(reconstructed_inputs)
local loss = criterion:forward(reconstructed_latent, latent)
local enc_grad = criterion:backward(reconstructed_latent, latent)
if opt.normalize then
enc_grad:div(torch.norm(enc_grad, 1) + 1e-8)
end
local dec_grad = enc:backward(reconstructed_inputs, enc_grad)
if opt.pixelLoss > 0 then
local pixLoss = pixCriterion:forward(reconstructed_inputs[{{1,opt.numSamples*2}}], inputs)
local dec_grad_pix = pixCriterion:backward(reconstructed_inputs[{{1,opt.numSamples*2}}], inputs)
dec_grad_pix:mul(opt.pixelLoss)
dec_grad[{{1,opt.numSamples*2}}]:add(dec_grad_pix)
end
dec:backward(latent, dec_grad)
maybe_print(loss, timer)
maybe_display(inputs, reconstructed_inputs)
maybe_save()
return loss, gradients
end
optim.adam(feval, weights, optim_state)
collectgarbage()
end
end
train()