-
Notifications
You must be signed in to change notification settings - Fork 1
/
2_0_VAE_deepmerge.lua
601 lines (528 loc) · 25.2 KB
/
2_0_VAE_deepmerge.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
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))
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))
return s
end
elseif nInputPlane ~= nOutputPlane then
s = nn.Sequential()
if not unconv or unconv == false then
s:add(SpatialConvolution(nInputPlane,nOutputPlane,4,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,4))
return s
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,4))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,4,1))
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))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,3,1,1,1,1))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
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))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,4,stride,stride,1,1))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,4,1,1,1,0,0))
end
else
if not unconv or unconv == false then
s:add(SpatialConvolution(nInputPlane,nOutputPlane,4,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,4))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,4))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,4,1))
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))
else
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,1,3,1,1,1,1))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
end
local temp = nn.Sequential()
temp:add(shortcut(nInputPlane, nOutputPlane, stride, unconv))
return block
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, nOutputPlane, stride, unconv)))
:add(nn.CAddTable(true))
end
--=================================== START Global Net ===================================
local function basicblockGlobal(n, nO, stride, shortcutLevel, 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,1,3,2,2,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,2,2,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,3,2,2,0,0))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,1,3,2,2,0,0))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
end
elseif stride and stride > 1 then
if not unconv or unconv == false then
if Type == 'both_preact' then
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 3, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 3, 1, 1, 1))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 1, 3, 1, 1, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 3, 1, 1, 1))
else
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 3, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 3, 1, 1, 1))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 1, 3, 1, 1, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 3, 1, 1, 1))
end
else
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 5, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 5, 1, 1, 1))
end
else
if not unconv or unconv == false then
s:add(SpatialConvolution(nInputPlane,nOutputPlane,1,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1))
else
s:add(SpatialConvolution(nInputPlane,nOutputPlane,1,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1))
end
end
s:add(SpatialBatchNormalization(nOutputPlane))
s:add(nn.ReLU(true))
if not unconv or unconv == false then
if Type == 'both_preact' then
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,2,2))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,1,2))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
else
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,2,2))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,1,2))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
end
else
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,2,2))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,1,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
end
return block
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, nOutputPlane, stride, unconv))) --:add(shortcut_Encoder(nInputPlane, nOutputPlane, stride, shortcutLevel, Type, unconv)))
:add(nn.CAddTable(true))
end
local function basicblockGlobal2(n, nO, stride, shortcutLevel, 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,1,3,2,2,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,2,2,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,3,2,2,0,0))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,1,3,2,2,0,0))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
end
elseif stride and stride > 1 then
if not unconv or unconv == false then
if Type == 'both_preact' then
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 5, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 5, 1, 1, 1))
else
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 3, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 3, 1, 1, 1))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 1, 3, 1, 1, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 3, 1, 1, 1))
end
else
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 5, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 5, 1, 1, 1))
end
else
if not unconv or unconv == false then
s:add(SpatialConvolution(nInputPlane,nOutputPlane,1,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1))
else
s:add(SpatialConvolution(nInputPlane,nOutputPlane,1,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1))
end
end
s:add(SpatialBatchNormalization(nOutputPlane))
s:add(nn.ReLU(true))
if not unconv or unconv == false then
if Type == 'both_preact' then
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,5,1,1,3,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,5,1,1,1,0,0))
else
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,2,2))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,1,2))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
end
else
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,2,2))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,1,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
end
return block
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, nOutputPlane, stride, unconv))) --:add(shortcut_Encoder(nInputPlane, nOutputPlane, stride, shortcutLevel, Type, unconv)))
:add(nn.CAddTable(true))
end
--=================================== END Global Net ===================================
--=================================== START Low Net ===================================
local function basicblockLow(n, nO, stride, shortcutLevel, 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,1,1,2,2,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,1,1,1,0,0))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,1,2,2,0,0))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,1,1,1,1,0,0))
end
elseif stride and stride > 1 then
if not unconv or unconv == false then
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 2, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 2, 1, 1, 1))
else
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 2, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 2, 1, 1, 1))
end
else
if not unconv or unconv == false then
s:add(SpatialConvolution(nInputPlane,nOutputPlane,1,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,1))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,1))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,1,1))
end
end
s:add(SpatialBatchNormalization(nOutputPlane))
s:add(nn.ReLU(true))
if not unconv or unconv == false then
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,2,1,1,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,2,1,1,1,0,0))
else
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,2,2,2,1,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,2,1,2,2,1,1))
end
return block
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, nOutputPlane, stride, unconv))) --:add(shortcut_Encoder(nInputPlane, nOutputPlane, stride, shortcutLevel, Type, unconv)))
:add(nn.CAddTable(true))
end
--=================================== END Low Net ===================================
--=================================== START Mid Net ===================================
local function basicblockMid(n, nO, stride, shortcutLevel, 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,1,3,2,2,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,3,2,2,0,0))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
end
elseif stride and stride > 1 then
if not unconv or unconv == false then
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 3, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 3, 1, 1, 1))
else
s:add(SpatialDilatedConvolution(nInputPlane, nOutputPlane, 1, 3, stride, stride, 0, 0, 2, 2))
s:add(SpatialDilatedConvolution(nOutputPlane, nOutputPlane, 3, 1, 1, 1))
end
else
if not unconv or unconv == false then
s:add(SpatialConvolution(nInputPlane,nOutputPlane,1,3))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1))
else
s:add(SpatialFullConvolution(nInputPlane,nOutputPlane,1,3))
s:add(SpatialFullConvolution(nOutputPlane,nOutputPlane,3,1))
end
end
s:add(SpatialBatchNormalization(nOutputPlane))
s:add(nn.ReLU(true))
if not unconv or unconv == false then
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,1,1,1,1))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
else
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,1,3,2,2,0,0))
s:add(SpatialConvolution(nOutputPlane,nOutputPlane,3,1,1,1,0,0))
end
return block
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, nOutputPlane, stride, unconv))) --:add(shortcut_Encoder(nInputPlane, nOutputPlane, stride, shortcutLevel, Type, unconv)))
:add(nn.CAddTable(true))
end
--=================================== END Mid Net ===================================
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 residualBlock_Encoder(block, nInputPlane, nOutputPlane, count, stride, shortcutLevel, Type, unconv)
local s = nn.Sequential()
if count < 1 then
return s
end
s:add(block(nInputPlane, nOutputPlane, stride, shortcutLevel,
Type == 'first' and 'no_preact' or 'both_preact', unconv))
for i=2,count do
s:add(block(nOutputPlane, nOutputPlane, 1, shortcutLevel))
end
return s
end
function VAE.get_encoder(modelParams)
local nInputCh = modelParams[1] -- or number of view points ie. 20
local nOutputCh = modelParams[2] -- 74
local nLatents = modelParams[3] -- 100
local singleVPNet = modelParams[5] -- 0
local conditional = modelParams[6] -- 0
local numCats = modelParams[7] -- 0
local benchmark = modelParams[8] -- 0
local dropoutNet = modelParams[9] -- 0
local base = nn.Sequential()
base:add(SpatialDilatedConvolution(not singleVPNet and nInputCh or 1, nOutputCh * 4, 4, 4, 2, 2, 1, 1, 2, 2))
base:add(SpatialBatchNormalization(nOutputCh * 4)):add(nn.ReLU(true))
base:forward(torch.zeros(4, 20, 224, 224))
local globalNet = nn.Sequential()
globalNet:add(base:clone('weight', 'bias', 'gradWeight', 'gradBias', 'running_mean', 'runnig_var', 'save_mean', 'save_var'))
globalNet:add(residualBlock_Encoder(basicblockGlobal, nOutputCh * 4, nOutputCh * 6, 1, 2, 'global', 'first', false))
-- Output feature map size: 53 x 53
globalNet:add(residualBlock_Encoder(basicblockGlobal, nOutputCh * 6, nOutputCh * 8, 1, 2, 'global', nil, false))
-- Output feature map size: 25 x 25
globalNet:add(residualBlock_Encoder(basicblockGlobal, nOutputCh * 8, nOutputCh * 6, 1, 2, 'global', nil, false))
-- Output feature map size: 11 x 11
globalNet:add(residualBlock_Encoder(basicblockGlobal2, nOutputCh * 6, nOutputCh * 1, 1, 2, 'global', nil, false))
-- Output feature map size: 4 x 4
local midNet = nn.Sequential()
midNet:add(base:clone('weight', 'bias', 'gradWeight', 'gradBias', 'running_mean', 'runnig_var', 'save_mean', 'save_var'))
midNet:add(residualBlock_Encoder(basicblockMid, nOutputCh * 4, nOutputCh * 6, 1, 2, 'mid', 'first', false))
-- Output feature map size: 53 x 53
midNet:add(residualBlock_Encoder(basicblockMid, nOutputCh * 6, nOutputCh * 8, 1, 2, 'mid', nil, false))
-- Output feature map size: 25 x 25
midNet:add(residualBlock_Encoder(basicblockMid, nOutputCh * 8, nOutputCh * 6, 1, 2, 'mid', nil, false))
-- Output feature map size: 11 x 11
midNet:add(residualBlock_Encoder(basicblockMid, nOutputCh * 6, nOutputCh * 1, 1, 2, 'mid', nil, false))
-- Output feature map size: 4 x 4
local lowNet = nn.Sequential()
lowNet:add(base:clone('weight', 'bias', 'gradWeight', 'gradBias', 'running_mean', 'runnig_var', 'save_mean', 'save_var'))
lowNet:add(residualBlock_Encoder(basicblockLow, nOutputCh * 4, nOutputCh * 6, 1, 2, 'low', 'first', false))
-- Output feature map size: 53 x 53
lowNet:add(residualBlock_Encoder(basicblockLow, nOutputCh * 6, nOutputCh * 8, 1, 2, 'low', nil, false))
-- Output feature map size: 25 x 25
lowNet:add(residualBlock_Encoder(basicblockLow, nOutputCh * 8, nOutputCh * 6, 1, 2, 'low', nil, false))
-- Output feature map size: 11 x 11
lowNet:add(residualBlock_Encoder(basicblockLow, nOutputCh * 6, nOutputCh * 1, 1, 2, 'low', nil, false))
-- Output feature map size: 4 x 4
local mergeNet = nn.Concat(2)
mergeNet:add(globalNet)
mergeNet:add(midNet)
mergeNet:add(lowNet)
local finalEncoder = nn.Sequential()
finalEncoder:add(mergeNet)
finalEncoder:add(nn.SpatialBatchNormalization(nOutputCh*3))
finalEncoder:add(nn.View(nOutputCh * 3 * 4 * 4):setNumInputDims(3))
local mean_logvar = nn.ConcatTable()
if not benchmark or singleVPNet or dropoutNet then
mean_logvar:add(nn.Sequential():add(nn.Linear(nOutputCh * 3 * 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 * 3 * 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
finalEncoder:add(mean_logvar)
finalEncoder:apply(weights_init)
finalEncoder:apply(function(m) if torch.type(m):find('Convolution') then m.bias:zero() end end)
return finalEncoder
--
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