-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path2_0_VAE.lua
380 lines (332 loc) · 16.2 KB
/
2_0_VAE.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
require 'torch'
require 'nn'
local VAE = {}
local SpatialDilatedConvolution = nn.SpatialDilatedConvolution
local SpatialConvolution = nn.SpatialConvolution
local SpatialFullConvolution = nn.SpatialFullConvolution
local SpatialBatchNormalization = nn.SpatialBatchNormalization
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
-- Residual network functions
-- The original ResNet functions were obtained from https://github.com/facebook/fb.resnet.torch and then modified
-- Typically shareGradInput uses the same gradInput storage for all modules
-- of the same type. This is incorrect for some SpatialBatchNormalization
-- modules in this network b/c of the in-place CAddTable. This marks the
-- module so that it's shared only with other modules with the same key
local function ShareGradInput(module, key)
assert(key)
module.__shareGradInputKey = key
return module
end
-- The shortcut layer is either identity or 1x1 convolution
local function shortcut(nInputPlane, nOutputPlane, stride, unconv)
local shortcutType = 'B' -- Fixed for our purposes
local s
local useConv = shortcutType == 'C' or
(shortcutType == 'B' and stride and stride > 1 and nInputPlane ~= nOutputPlane)
if useConv then
-- Do convolution
s = nn.Sequential()
if not unconv or unconv == false then
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 4, 1, stride, stride, 1, 1, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 1, 4, 1, 1))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 4, 4, stride, stride, 1, 1, 2, 2))
return s
else
s:add(SpatialFullConvolution(nInputPlane, nOutputPlane, 1, 4, stride, stride, 1, 1))
s:add(SpatialFullConvolution(nOutputPlane, nOutputPlane, 4, 1, 1, 1, 0, 0))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialFullConvolution(nInputPlane, nOutputPlane, 4, 4, stride, stride, 1, 1))
return s
end
elseif nInputPlane ~= nOutputPlane then
-- Do stride one convolution
s = nn.Sequential()
if not unconv or unconv == false then
s:add(SpatialConvolution(nInputPlane,nOutputPlane,4,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,4))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialConvolution(nInputPlane,nOutputPlane,4,4))
return s
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,4))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,4,1))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,4,4))
return s
end
else
return nn.Identity()
end
end
local function basicblock(n, nO, stride, Type, unconv)
local nInputPlane = n
local nOutputPlane = nO
local block = nn.Sequential()
local s = nn.Sequential()
if Type == 'both_preact' then
block:add(ShareGradInput(SpatialBatchNormalization(nInputPlane), 'preact'))
block:add(nn.ReLU(true))
elseif Type ~= 'no_preact' then
s:add(SpatialBatchNormalization(nInputPlane))
s:add(nn.ReLU(true))
end
if stride and stride == 1 then
if not unconv or unconv == false then
s:add(SpatialConvolution(nInputPlane,nOutputPlane,3,1,1,1,1,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,0,0))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialConvolution(nInputPlane,nOutputPlane,3,3,1,1,1,1))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,3,1,1,1,1))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,3,3,1,1,1,1))
end
elseif stride and stride > 1 then
if not unconv or unconv == false then
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 4, 1, stride, stride, 1, 1, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 1, 4, 1, 1))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialDilatedConvolution(nInputPlane,nOutputPlane,4,4,stride,stride,1,1, 2, 2))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,4,stride,stride,1,1))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,4,1,1,1,0,0))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,4,4,stride,stride,1,1))
end
else
if not unconv or unconv == false then
s:add(SpatialConvolution(nInputPlane,nOutputPlane,4,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,4))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialConvolution(nInputPlane,nOutputPlane,4,4))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,4))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,4,1))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,4,4))
end
end
s:add(SpatialBatchNormalization(nOutputPlane))
s:add(nn.ReLU(true))
if not unconv or unconv == false then
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,1,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,0,0))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,3,1,1,1,1))
else
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,1,3,1,1,1,1))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
-- Uncomment the line below have full filters (adds more parameters)
-- s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,3,1,1,1,1))
end
return block
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, nOutputPlane, stride, unconv)))
:add(nn.CAddTable(true))
end
local function residualBlock(block, nInputPlane, nOutputPlane, count, stride, Type, unconv)
local s = nn.Sequential()
if count < 1 then
return s
end
s:add(block(nInputPlane, nOutputPlane, stride,
Type == 'first' and 'no_preact' or 'both_preact', unconv))
for i=2,count do
s:add(block(nOutputPlane, nOutputPlane, 1))
end
return s
end
local function temp(mod, singleVPNet, nInputCh, nOutputCh)
if not singleVPNet then
-- local tempModParallel = nn.ParallelTable()
-- for i=1, nInputCh do
-- local tempMod = nn.Sequential()
-- tempMod:add(nn.SpatialDilatedConvolution(1, nOutputCh * 4 / nInputCh, 4, 4, 2, 2, 1, 1, 2, 2))
-- tempModParallel:add(tempMod)
-- end
-- local tempMod = nn.Sequential()
-- :add(nn.SplitTable(1))
-- :add(nn.MapTable()
-- :add(nn.Sequential()
-- :add(nn.SplitTable(1))
-- :add(nn.MapTable()
-- :add(nn.View(1, 224, 224)))))
-- :add(nn.MapTable()
-- :add(nn.Sequential()
-- :add(tempModParallel)
-- :add(nn.JoinTable(1))
-- :add(nn.View(1, 280, 110, 110))))
-- :add(nn.JoinTable(1))
-- :add(nn.Sequential():add(nn.SpatialBatchNormalization(nOutputCh * 4)):add(nn.ReLU(true)))
local tempModParallel = nn.ParallelTable()
local tempMod = nn.Sequential()
for i=1, 1 do
-- local tempMod = nn.Sequential()
tempMod:add(nn.Contiguous())
tempMod:add(nn.View(-1, 1, 224, 224))
tempMod:add(nn.SpatialDilatedConvolution(1, nOutputCh * 10 / nInputCh, 4, 4, 2, 2, 1, 1, 2, 2))
tempMod:add(nn.SpatialBatchNormalization(nOutputCh * 10 / nInputCh)):add(nn.ReLU(true))
-- Dilated Convolution
tempMod:add(residualBlock(basicblock, nOutputCh * 10 / nInputCh, nOutputCh * 16 / nInputCh, 1, 2, 'first'))
-- Output feature map size: 53 x 53
tempMod:add(residualBlock(basicblock, nOutputCh * 16 / nInputCh , nOutputCh * 18 / nInputCh, 1, 2))
-- Output feature map size: 25 x 25
tempMod:add(residualBlock(basicblock, nOutputCh * 18 / nInputCh, nOutputCh * 10 / nInputCh, 1, 2))
-- Output feature map size: 11 x 11
tempMod:add(residualBlock(basicblock, nOutputCh * 10 / nInputCh, 4, 1, 2))
-- tempMod:add(ShareGradInput(SpatialBatchNormalization(4), 'last'))
-- Output feature map size: 4 x 4
-- tempModParallel:add(tempMod)
end
local tempMod = nn.Sequential()
:add(nn.SplitTable(2))
-- :add(tempModParallel)
:add(nn.MapTable():add(tempMod))
:add(nn.JoinTable(2))
:add(nn.SpatialBatchNormalization(80))
:add(nn.ReLU(true))
-- :add(nn.MapTable()
-- :add(nn.Sequential()
-- :add(nn.SplitTable(1))
-- :add(nn.MapTable()
-- :add(nn.View(1, 224, 224)))))
-- :add(nn.MapTable()
-- :add(nn.Sequential()
-- :add(tempModParallel)
-- :add(nn.JoinTable(1))
-- :add(nn.View(1, 280, 110, 110))))
-- :add(nn.JoinTable(1))
-- :add(nn.Sequential():add(nn.SpatialBatchNormalization(nOutputCh * 4)):add(nn.ReLU(true)))
mod:add(tempMod)
else
mod:add(SpatialDilatedConvolution(1, nOutputCh * 4, 4, 4, 2, 2, 1, 1, 2, 2))
mod:add(SpatialBatchNormalization(nOutputCh * 4)):add(nn.ReLU(true))
end
return mod
end
function VAE.get_encoder(modelParams)
local nInputCh = modelParams[1] -- or number of view points
local nOutputCh = modelParams[2]
local nLatents = modelParams[3]
local singleVPNet = modelParams[5]
local conditional = modelParams[6]
local numCats = modelParams[7]
local benchmark = modelParams[8]
local dropoutNet = modelParams[9]
local encoder = nn.Sequential()
encoder:add(SpatialDilatedConvolution(not singleVPNet and nInputCh or 1, nOutputCh * 4, 4, 4, 2, 2, 1, 1, 2, 2))
encoder:add(SpatialBatchNormalization(nOutputCh * 4)):add(nn.ReLU(true))
encoder:forward(torch.zeros(4, 20, 224, 224))
-- Output feature map size: 110 x 110
-- Dilated Convolution
encoder:add(residualBlock(basicblock, nOutputCh * 4, nOutputCh * 6, 1, 2, 'first'))
-- Output feature map size: 53 x 53
encoder:add(residualBlock(basicblock, nOutputCh * 6, nOutputCh * 8, 1, 2))
-- Output feature map size: 25 x 25
encoder:add(residualBlock(basicblock, nOutputCh * 8, nOutputCh * 6, 1, 2))
-- Output feature map size: 11 x 11
encoder:add(residualBlock(basicblock, nOutputCh * 6, nOutputCh, 1, 2))
encoder:add(ShareGradInput(SpatialBatchNormalization(nOutputCh), 'last'))
-- Output feature map size: 4 x 4
encoder:add(nn.View(nOutputCh * 4 * 4))
local mean_logvar = nn.ConcatTable()
if not benchmark or singleVPNet or dropoutNet then
mean_logvar:add(nn.Sequential():add(nn.Linear(nOutputCh * 4 * 4, nOutputCh * 4 * 2)):add(nn.ReLU(true)):add(nn.Linear(nOutputCh * 4 * 2, nLatents))) -- The means
mean_logvar:add(nn.Sequential():add(nn.Linear(nOutputCh * 4 * 4, nOutputCh * 4 * 2)):add(nn.ReLU(true)):add(nn.Linear(nOutputCh * 4 * 2, nLatents))) -- Log of the variances
else
mean_logvar:add(nn.Linear(nOutputCh * 4 * 4, nLatents)) -- The means
mean_logvar:add(nn.Linear(nOutputCh * 4 * 4, nLatents)) -- Log of the variances
end
if conditional then
mean_logvar:add(nn.Sequential()
:add(nn.Linear(nOutputCh * 4 * 4, (nOutputCh * 4 * 4) - 50))
:add(nn.ReLU(true))
:add(nn.Linear((nOutputCh * 4 * 4) - 50, numCats)))
end
-- Trying out MVCNN architecture
-- local encoder = nn.Sequential()
-- encoder = temp(encoder, singleVPNet, nInputCh, nOutputCh)
-- encoder:add(nn.View(80 * 4 * 4))
-- local mean_logvar = nn.ConcatTable()
-- if not benchmark or singleVPNet or dropoutNet then
-- mean_logvar:add(nn.Sequential():add(nn.Linear(80 * 4 * 4, 80 * 4 * 2)):add(nn.ReLU(true)):add(nn.Linear(80 * 4 * 2, nLatents))) -- The means
-- mean_logvar:add(nn.Sequential():add(nn.Linear(80 * 4 * 4, 80 * 4 * 2)):add(nn.ReLU(true)):add(nn.Linear(80 * 4 * 2, nLatents))) -- Log of the variances
-- else
-- mean_logvar:add(nn.Linear(80 * 4 * 4, nLatents)) -- The means
-- mean_logvar:add(nn.Linear(80 * 4 * 4, nLatents)) -- Log of the variances
-- end
-- if conditional then
-- mean_logvar:add(nn.Sequential()
-- :add(nn.Linear(80 * 4 * 4, (80 * 4 * 4) - 50))
-- :add(nn.ReLU(true))
-- :add(nn.Linear((80 * 4 * 4) - 50, numCats)))
-- end
encoder:add(mean_logvar)
encoder:apply(weights_init)
encoder:apply(function(m) if torch.type(m):find('Convolution') then m.bias:zero() end end)
return encoder
end
function VAE.get_decoder(modelParams)
local nInputCh = modelParams[1]
local nOutputCh = modelParams[2]
local nLatents = modelParams[3]
local tanh = modelParams[4]
local singleVPNet = modelParams[5]
local conditional = modelParams[6]
local numCats = modelParams[7]
local benchmark = modelParams[8]
local dropoutNet = modelParams[9]
local decoder = nn.Sequential()
if conditional then
decoder:add(nn.JoinTable(2))
end
if not benchmark or singleVPNet or dropoutNet then
decoder:add(nn.Linear(nLatents+numCats, nOutputCh * 4 * 4)):add(nn.ReLU(true))
decoder:add(nn.Linear(nOutputCh * 4 * 4, nOutputCh * 2 * 4 * 4))
else
decoder:add(nn.Linear(nLatents+numCats, nOutputCh * 2 * 4 * 4))
end
decoder:add(nn.View(nOutputCh * 2 , 4, 4))
decoder:add(SpatialBatchNormalization(nOutputCh * 2)):add(nn.ReLU(true))
-- Output feature map size: 4 x 4
decoder:add(residualBlock(basicblock, nOutputCh * 2, nOutputCh * 6, 1, nil, 'first', true))
-- Output feature map size: 7 x 7
decoder:add(residualBlock(basicblock, nOutputCh * 6, nOutputCh * 8, 1, 2, nil, true))
-- Output feature map size: 14 x 14
decoder:add(residualBlock(basicblock, nOutputCh * 8, nOutputCh * 7, 1, 2, nil, true))
decoder:add(ShareGradInput(SpatialBatchNormalization(nOutputCh * 7), 'last'))
-- Output feature map size: 28 x 28
decoder:add(SpatialFullConvolution(nOutputCh * 7, nOutputCh * 6, 4, 4, 2, 2, 1, 1))
decoder:add(SpatialBatchNormalization(nOutputCh * 6)):add(nn.ReLU(true))
-- Output feature map size: 56 x 56
decoder:add(SpatialFullConvolution(nOutputCh * 6, nOutputCh * 4, 4, 4, 2, 2, 1, 1))
decoder:add(SpatialBatchNormalization(nOutputCh * 4)):add(nn.ReLU(true))
-- Output feature map size: 112 x 112
-- temoDeconvLayer1 generates the depth maps
tempDeconvLayer1 = nn.Sequential():add(SpatialFullConvolution(nOutputCh * 4, nInputCh, 4, 4, 2, 2, 1, 1))
if tanh then
tempDeconvLayer1:add(nn.Tanh())
else
tempDeconvLayer1:add(nn.Sigmoid())
end
-- temoDeconvLayer2 generates the silhouettes
tempDeconvLayer2 = nn.Sequential():add(SpatialFullConvolution(nOutputCh * 4, nInputCh, 4, 4, 2, 2, 1, 1)):add(nn.Sigmoid())
decoder:add(nn.ConcatTable():add(tempDeconvLayer1):add(tempDeconvLayer2))
-- Output feature map size: 224 x 224
decoder:apply(weights_init)
decoder:apply(function(m) if torch.type(m):find('Convolution') then m.bias:zero() end end)
return decoder
end
return VAE