-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
686 lines (509 loc) · 23.3 KB
/
models.py
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
from keras.layers import Conv3D, Conv3DTranspose, UpSampling3D, Dense, Reshape, Flatten, Activation, Input, MaxPooling3D, Cropping3D, Concatenate, BatchNormalization, Dropout
from keras.models import Sequential, Model
from keras.layers.advanced_activations import LeakyReLU
from keras.optimizers import Adam
from keras.regularizers import L1L2
import numpy as np
## All Generators:
## Skip Connections is whether to use skip connections
## init_filters is the number of filters in the first conv layer
## (usually default)
## filter_scale determines how fast the filters scale up (filters *= filter_scale every scale level)
## (usually default)
## relu_leak is the parameter for the leaky relu's
## (usually 0.2, default)
## batch_norm is whether to use batch normalization layers
## (usually true)
## bn_momentum is the momentum parameter for the batch norm layers (if included)
## (usually 0.8, default)
## regularization is the L1L2 kernel (weight) regularizer for all convolutional layers, if specified and nonzero
## (usually 0, so no regularization)
## 295 with factor=3 -> output size of 83
def get_generator_arch_a(skip_conns=True, init_filters=12, filter_scale=3, relu_leak=0.2, batch_norm=True, bn_momentum=0.8, regularization=0.0):
init_filters=int(init_filters)
filter_scale=int(filter_scale)
relu_leak=float(relu_leak)
skip_conns=skip_conns in ["True","true",True]
batch_norm=batch_norm in ["True","true",True]
bn_momentum=float(bn_momentum)
regularization=float(regularization)
if regularization > 0.0:
reg = lambda : L1L2(regularization, regularization)
else:
reg = lambda : None
input_layer = Input(shape = (295,295,295,1), name="input1")
filter_count = init_filters
stage1_in = input_layer
conv1_1 = Conv3D(filter_count, (3,3,3), name="conv1_1", padding="valid", kernel_regularizer=reg())(stage1_in)
relu1_1 = LeakyReLU(relu_leak, name="relu1_1")(conv1_1)
conv1_2 = Conv3D(filter_count, (3,3,3), name="conv1_2", padding="valid", kernel_regularizer=reg())(relu1_1)
relu1_2 = LeakyReLU(relu_leak, name="relu1_2")(conv1_2)
if batch_norm:
bn1 = BatchNormalization(momentum=bn_momentum, name="bn1")(relu1_2)
stage1_out = bn1
else:
stage1_out = relu1_2
pool1 = MaxPooling3D((3,3,3), name="pool1")(stage1_out)
filter_count *= filter_scale
stage2_in = pool1
conv2_1 = Conv3D(filter_count, (3,3,3), name="conv2_1", padding="valid", kernel_regularizer=reg())(stage2_in)
relu2_1 = LeakyReLU(relu_leak, name="relu2_1")(conv2_1)
conv2_2 = Conv3D(filter_count, (3,3,3), name="conv2_2", padding="valid", kernel_regularizer=reg())(relu2_1)
relu2_2 = LeakyReLU(relu_leak, name="relu2_2")(conv2_2)
if batch_norm:
bn2 = BatchNormalization(momentum=bn_momentum, name="bn2")(relu2_2)
stage2_out = bn2
else:
stage2_out = relu2_2
pool2 = MaxPooling3D((3,3,3), name="pool2")(stage2_out)
filter_count *= filter_scale
stage3_in = pool2
conv3_1 = Conv3D(filter_count, (3,3,3), name="conv3_1", padding="valid", kernel_regularizer=reg())(stage3_in)
relu3_1 = LeakyReLU(relu_leak, name="relu3_1")(conv3_1)
conv3_2 = Conv3D(filter_count, (3,3,3), name="conv3_2", padding="valid", kernel_regularizer=reg())(relu3_1)
relu3_2 = LeakyReLU(relu_leak, name="relu3_2")(conv3_2)
if batch_norm:
bn3 = BatchNormalization(momentum=bn_momentum, name="bn3")(relu3_2)
stage3_out = bn3
else:
stage3_out = relu3_2
pool3 = MaxPooling3D((3,3,3), name="pool3")(stage3_out)
filter_count *= filter_scale
stage4_in = pool3
conv4_1 = Conv3D(filter_count, (3,3,3), name="conv4_1", padding="valid", kernel_regularizer=reg())(stage4_in)
relu4_1 = LeakyReLU(relu_leak, name="relu4_1")(conv4_1)
conv4_2 = Conv3D(filter_count, (3,3,3), name="conv4_2", padding="valid", kernel_regularizer=reg())(relu4_1)
relu4_2 = LeakyReLU(relu_leak, name="relu4_2")(conv4_2)
if batch_norm:
bn4 = BatchNormalization(momentum=bn_momentum, name="bn4")(relu4_2)
stage4_out = bn4
else:
stage4_out = relu4_2
upsamp1 = UpSampling3D((3,3,3), name="upsamp1")(stage4_out)
#upconv1 = Conv3D(64, (2,2,2), padding=padding, name="upconv1")(upsamp1)
#uprelu1 = LeakyReLU(relu_leak, name="uprelu1")(upconv1)
if skip_conns:
stage3_out_cropped = Cropping3D(cropping=6)(stage3_out)
stage5_in = Concatenate()([upsamp1, stage3_out_cropped])
else:
stage5_in = upsamp1
filter_count //= filter_scale
conv5_1_name = "conv5_1_" + ("sc" if skip_conns else "nsc")
conv5_1 = Conv3D(filter_count, (3,3,3), name=conv5_1_name, padding="valid", kernel_regularizer=reg())(stage5_in)
relu5_1 = LeakyReLU(relu_leak, name="relu5_1")(conv5_1)
conv5_2 = Conv3D(filter_count, (3,3,3), name="conv5_2", padding="valid", kernel_regularizer=reg())(relu5_1)
relu5_2 = LeakyReLU(relu_leak, name="relu5_2")(conv5_2)
if batch_norm:
bn5 = BatchNormalization(momentum=bn_momentum, name="bn5")(relu5_2)
stage5_out = bn5
else:
stage5_out = relu5_2
upsamp2 = UpSampling3D((3,3,3), name="upsamp2")(stage5_out)
#upconv2 = Conv3D(128, (2,2,2), padding=padding, name="upconv2")(upsamp2)
#uprelu2 = LeakyReLU(relu_leak, name="uprelu2")(upconv2)
if skip_conns:
stage2_out_cropped = Cropping3D(cropping=30)(stage2_out)
stage6_in = Concatenate()([upsamp2, stage2_out_cropped])
else:
stage6_in = upsamp2
filter_count //= filter_scale
conv6_1_name = "conv6_1_" + ("sc" if skip_conns else "nsc")
conv6_1 = Conv3D(filter_count, (3,3,3), name=conv6_1_name, padding="valid", kernel_regularizer=reg())(stage6_in)
relu6_1 = LeakyReLU(relu_leak, name="relu6_1")(conv6_1)
conv6_2 = Conv3D(filter_count, (3,3,3), name="conv6_2", padding="valid", kernel_regularizer=reg())(relu6_1)
relu6_2 = LeakyReLU(relu_leak, name="relu6_2")(conv6_2)
if batch_norm:
bn6 = BatchNormalization(momentum=bn_momentum, name="bn6")(relu6_2)
stage6_out = bn6
else:
stage6_out = relu6_2
upsamp3 = UpSampling3D((3,3,3), name="upsamp3")(stage6_out)
#upconv3 = Conv3D(64, (2,2,2), padding=padding, name="upconv3")(upsamp3)
#uprelu3 = LeakyReLU(relu_leak, name="uprelu3")(upconv3)
if skip_conns:
stage1_out_cropped = Cropping3D(cropping=102)(stage1_out)
stage7_in = Concatenate()([upsamp3, stage1_out_cropped])
else:
stage7_in = upsamp3
filter_count //= filter_scale
conv7_1_name = "conv7_1_" + ("sc" if skip_conns else "nsc") ## Used for loading weights into skip connection model from non skip connection model
conv7_1 = Conv3D(filter_count, (3,3,3), name=conv7_1_name, padding="valid", kernel_regularizer=reg())(stage7_in)
relu7_1 = LeakyReLU(relu_leak, name="relu7_1")(conv7_1)
conv7_2 = Conv3D(filter_count, (3,3,3), name="conv7_2", padding="valid", kernel_regularizer=reg())(relu7_1)
relu7_2 = LeakyReLU(relu_leak, name="relu7_2")(conv7_2)
conv7_3 = Conv3D(1, (1,1,1), name="conv7_3", activation="sigmoid", kernel_regularizer=reg())(relu7_2)
output_layer = conv7_3
return Model(input_layer, output_layer)
## 156 with factor=2 -> output size of 68
def get_generator_arch_b(skip_conns=True, init_filters=32, filter_scale=2, relu_leak=0.2, batch_norm=True, bn_momentum=0.8, regularization=0.0):
init_filters=int(init_filters)
filter_scale=int(filter_scale)
relu_leak=float(relu_leak)
skip_conns=skip_conns in ["True","true",True]
batch_norm=batch_norm in ["True","true",True]
bn_momentum=float(bn_momentum)
regularization=float(regularization)
if regularization > 0.0:
reg = lambda : L1L2(regularization, regularization)
else:
reg = lambda : None
input_layer = Input(shape = (156,156,156,1), name="input1")
filter_count = init_filters
stage1_in = input_layer
conv1_1 = Conv3D(filter_count, (3,3,3), name="conv1_1", padding="valid", kernel_regularizer=reg())(stage1_in)
relu1_1 = LeakyReLU(relu_leak, name="relu1_1")(conv1_1)
conv1_2 = Conv3D(filter_count, (3,3,3), name="conv1_2", padding="valid", kernel_regularizer=reg())(relu1_1)
relu1_2 = LeakyReLU(relu_leak, name="relu1_2")(conv1_2)
if batch_norm:
bn1 = BatchNormalization(momentum=bn_momentum, name="bn1")(relu1_2)
stage1_out = bn1
else:
stage1_out = relu1_2
pool1 = MaxPooling3D((2,2,2), name="pool1")(stage1_out)
filter_count *= filter_scale
stage2_in = pool1
conv2_1 = Conv3D(filter_count, (3,3,3), name="conv2_1", padding="valid", kernel_regularizer=reg())(stage2_in)
relu2_1 = LeakyReLU(relu_leak, name="relu2_1")(conv2_1)
conv2_2 = Conv3D(filter_count, (3,3,3), name="conv2_2", padding="valid", kernel_regularizer=reg())(relu2_1)
relu2_2 = LeakyReLU(relu_leak, name="relu2_2")(conv2_2)
if batch_norm:
bn2 = BatchNormalization(momentum=bn_momentum, name="bn2")(relu2_2)
stage2_out = bn2
else:
stage2_out = relu2_2
pool2 = MaxPooling3D((2,2,2), name="pool2")(stage2_out)
filter_count *= filter_scale
stage3_in = pool2
conv3_1 = Conv3D(filter_count, (3,3,3), name="conv3_1", padding="valid", kernel_regularizer=reg())(stage3_in)
relu3_1 = LeakyReLU(relu_leak, name="relu3_1")(conv3_1)
conv3_2 = Conv3D(filter_count, (3,3,3), name="conv3_2", padding="valid", kernel_regularizer=reg())(relu3_1)
relu3_2 = LeakyReLU(relu_leak, name="relu3_2")(conv3_2)
if batch_norm:
bn3 = BatchNormalization(momentum=bn_momentum, name="bn3")(relu3_2)
stage3_out = bn3
else:
stage3_out = relu3_2
pool3 = MaxPooling3D((2,2,2), name="pool3")(stage3_out)
filter_count *= filter_scale
stage4_in = pool3
conv4_1 = Conv3D(filter_count, (3,3,3), name="conv4_1", padding="valid", kernel_regularizer=reg())(stage4_in)
relu4_1 = LeakyReLU(relu_leak, name="relu4_1")(conv4_1)
conv4_2 = Conv3D(filter_count, (3,3,3), name="conv4_2", padding="valid", kernel_regularizer=reg())(relu4_1)
relu4_2 = LeakyReLU(relu_leak, name="relu4_2")(conv4_2)
if batch_norm:
bn4 = BatchNormalization(momentum=bn_momentum, name="bn4")(relu4_2)
stage4_out = bn4
else:
stage4_out = relu4_2
upsamp1 = UpSampling3D((2,2,2), name="upsamp1")(stage4_out)
#upconv1 = Conv3D(64, (2,2,2), padding=padding, name="upconv1")(upsamp1)
#uprelu1 = LeakyReLU(relu_leak, name="uprelu1")(upconv1)
if skip_conns:
stage3_out_cropped = Cropping3D(cropping=4)(stage3_out)
stage5_in = Concatenate()([upsamp1, stage3_out_cropped])
else:
stage5_in = upsamp1
filter_count //= filter_scale
conv5_1_name = "conv5_1_" + ("sc" if skip_conns else "nsc")
conv5_1 = Conv3D(filter_count, (3,3,3), name=conv5_1_name, padding="valid", kernel_regularizer=reg())(stage5_in)
relu5_1 = LeakyReLU(relu_leak, name="relu5_1")(conv5_1)
conv5_2 = Conv3D(filter_count, (3,3,3), name="conv5_2", padding="valid", kernel_regularizer=reg())(relu5_1)
relu5_2 = LeakyReLU(relu_leak, name="relu5_2")(conv5_2)
if batch_norm:
bn5 = BatchNormalization(momentum=bn_momentum, name="bn5")(relu5_2)
stage5_out = bn5
else:
stage5_out = relu5_2
upsamp2 = UpSampling3D((2,2,2), name="upsamp2")(stage5_out)
#upconv2 = Conv3D(128, (2,2,2), padding=padding, name="upconv2")(upsamp2)
#uprelu2 = LeakyReLU(relu_leak, name="uprelu2")(upconv2)
if skip_conns:
stage2_out_cropped = Cropping3D(cropping=16)(stage2_out)
stage6_in = Concatenate()([upsamp2, stage2_out_cropped])
else:
stage6_in = upsamp2
filter_count //= filter_scale
conv6_1_name = "conv6_1_" + ("sc" if skip_conns else "nsc")
conv6_1 = Conv3D(filter_count, (3,3,3), name=conv6_1_name, padding="valid", kernel_regularizer=reg())(stage6_in)
relu6_1 = LeakyReLU(relu_leak, name="relu6_1")(conv6_1)
conv6_2 = Conv3D(filter_count, (3,3,3), name="conv6_2", padding="valid", kernel_regularizer=reg())(relu6_1)
relu6_2 = LeakyReLU(relu_leak, name="relu6_2")(conv6_2)
if batch_norm:
bn6 = BatchNormalization(momentum=bn_momentum, name="bn6")(relu6_2)
stage6_out = bn6
else:
stage6_out = relu6_2
upsamp3 = UpSampling3D((2,2,2), name="upsamp3")(stage6_out)
#upconv3 = Conv3D(64, (2,2,2), padding=padding, name="upconv3")(upsamp3)
#uprelu3 = LeakyReLU(relu_leak, name="uprelu3")(upconv3)
if skip_conns:
stage1_out_cropped = Cropping3D(cropping=40)(stage1_out)
stage7_in = Concatenate()([upsamp3, stage1_out_cropped])
else:
stage7_in = upsamp3
filter_count //= filter_scale
conv7_1_name = "conv7_1_" + ("sc" if skip_conns else "nsc")
conv7_1 = Conv3D(filter_count, (3,3,3), name=conv7_1_name, padding="valid", kernel_regularizer=reg())(stage7_in)
relu7_1 = LeakyReLU(relu_leak, name="relu7_1")(conv7_1)
conv7_2 = Conv3D(filter_count, (3,3,3), name="conv7_2", padding="valid", kernel_regularizer=reg())(relu7_1)
relu7_2 = LeakyReLU(relu_leak, name="relu7_2")(conv7_2)
conv7_3 = Conv3D(1, (1,1,1), name="conv7_3", activation="sigmoid", kernel_regularizer=reg())(relu7_2)
output_layer = conv7_3
return Model(input_layer, output_layer)
## Discriminator
## dropout, if nonzero, determines the dropout rate in the dropout layers (applied after leakyrelu, before batchnorm)
## usually set between 0 and 0.2, higher tends to help with artifacts somewhat
## takes size 83^3
def get_discriminator_arch_a(init_filters=18, filter_scale=2, relu_leak=0.2, batch_norm=True, bn_momentum=0.8, regularization=0.0, dropout=0.0):
init_filters=int(init_filters)
filter_scale=int(filter_scale)
relu_leak=float(relu_leak)
batch_norm=batch_norm in ["True","true",True]
bn_momentum=float(bn_momentum)
regularization=float(regularization)
dropout=float(dropout)
if regularization > 0.0:
reg = lambda : L1L2(regularization, regularization)
else:
reg = lambda : None
input_layer = Input(shape = (83,83,83,1))
filter_count = init_filters
stage1_in = input_layer
conv1_1 = Conv3D(filter_count, (3,3,3), name="conv1_1", padding="valid", kernel_regularizer=reg())(stage1_in)
relu1_1 = LeakyReLU(relu_leak, name="relu1_1")(conv1_1)
if dropout > 0:
relu1_1 = Dropout(dropout, name="drop1_1")(relu1_1)
if batch_norm:
bn1 = BatchNormalization(momentum=bn_momentum, name="bn1")(relu1_1)
stage1_out = bn1
else:
stage1_out = relu1_1
filter_count *= filter_scale
down1 = Conv3D(filter_count, (3,3,3), strides=(3,3,3), name="down1")(stage1_out)
filter_count *= filter_scale
stage2_in = down1
conv2_1 = Conv3D(filter_count, (3,3,3), name="conv2_1", padding="valid", kernel_regularizer=reg())(stage2_in)
relu2_1 = LeakyReLU(relu_leak, name="relu2_1")(conv2_1)
if dropout > 0:
relu2_1 = Dropout(dropout, name="drop2_1")(relu2_1)
conv2_2 = Conv3D(filter_count, (3,3,3), name="conv2_2", padding="valid", kernel_regularizer=reg())(relu2_1)
relu2_2 = LeakyReLU(relu_leak, name="relu2_2")(conv2_2)
if dropout > 0:
relu2_2 = Dropout(dropout, name="drop2_2")(relu2_2)
conv2_3 = Conv3D(filter_count, (3,3,3), name="conv2_3", padding="valid", kernel_regularizer=reg())(relu2_2)
relu2_3 = LeakyReLU(relu_leak, name="relu2_3")(conv2_3)
if dropout > 0:
relu2_3 = Dropout(dropout, name="drop2_3")(relu2_3)
if batch_norm:
bn2 = BatchNormalization(momentum=bn_momentum, name="bn2")(relu2_3)
stage2_out = bn2
else:
stage2_out = relu2_3
filter_count *= filter_scale
down2 = Conv3D(filter_count, (3,3,3), strides=(3,3,3), name="down2")(stage2_out)
filter_count *= filter_scale
stage3_in = down2
conv3_1 = Conv3D(filter_count, (3,3,3), name="conv3_1", padding="valid", kernel_regularizer=reg())(stage3_in)
relu3_1 = LeakyReLU(relu_leak, name="relu3_1")(conv3_1)
if dropout > 0:
relu3_1 = Dropout(dropout, name="drop3_1")(relu3_1)
if batch_norm:
bn3 = BatchNormalization(momentum=bn_momentum, name="bn3")(relu3_1)
stage3_out = bn3
else:
stage3_out = relu3_1
flatten1 = Flatten(name="flatten1")(stage3_out)
dense1 = Dense(64, name="dense1")(flatten1)
relu4 = LeakyReLU(relu_leak, name="relu4")(dense1)
dense2 = Dense(1, activation="sigmoid", name="dense2")(relu4)
output_layer = dense2
return Model(input_layer, output_layer)
## takes size 68^3
def get_discriminator_arch_b(init_filters=32, filter_scale=3, relu_leak=0.2, batch_norm=True, bn_momentum=0.8, regularization=0.0, dropout=0.0):
init_filters=int(init_filters)
filter_scale=int(filter_scale)
relu_leak=float(relu_leak)
batch_norm=batch_norm in ["True","true",True]
bn_momentum=float(bn_momentum)
regularization=float(regularization)
dropout=float(dropout)
if regularization > 0.0:
reg = lambda : L1L2(regularization, regularization)
else:
reg = lambda : None
input_layer = Input(shape = (68,68,68,1))
filter_count = init_filters
stage1_in = input_layer
conv1_1 = Conv3D(filter_count, (3,3,3), name="conv1_1", padding="valid", kernel_regularizer=reg())(stage1_in)
relu1_1 = LeakyReLU(relu_leak, name="relu1_1")(conv1_1)
if dropout > 0:
relu1_1 = Dropout(dropout, name="drop1_1")(relu1_1)
conv1_2 = Conv3D(filter_count, (3,3,3), name="conv1_2", padding="valid", kernel_regularizer=reg())(relu1_1)
relu1_2 = LeakyReLU(relu_leak, name="relu1_2")(conv1_2)
if dropout > 0:
relu1_2 = Dropout(dropout, name="drop1_2")(relu1_2)
if batch_norm:
bn1 = BatchNormalization(momentum=bn_momentum, name="bn1")(relu1_2)
stage1_out = bn1
else:
stage1_out = relu1_2
pool1 = MaxPooling3D((2,2,2), name="pool1")(stage1_out)
filter_count *= filter_scale
stage2_in = pool1
conv2_1 = Conv3D(filter_count, (3,3,3), name="conv2_1", padding="valid", kernel_regularizer=reg())(stage2_in)
relu2_1 = LeakyReLU(relu_leak, name="relu2_1")(conv2_1)
if dropout > 0:
relu2_1 = Dropout(dropout, name="drop2_1")(relu2_1)
conv2_2 = Conv3D(filter_count, (3,3,3), name="conv2_2", padding="valid", kernel_regularizer=reg())(relu2_1)
relu2_2 = LeakyReLU(relu_leak, name="relu2_2")(conv2_2)
if dropout > 0:
relu2_2 = Dropout(dropout, name="drop2_2")(relu2_2)
if batch_norm:
bn2 = BatchNormalization(momentum=bn_momentum, name="bn2")(relu2_2)
stage2_out = bn2
else:
stage2_out = relu2_2
pool2 = MaxPooling3D((2,2,2), name="pool2")(stage2_out)
filter_count *= filter_scale
stage3_in = pool2
conv3_1 = Conv3D(filter_count, (3,3,3), name="conv3_1", padding="valid", kernel_regularizer=reg())(stage3_in)
relu3_1 = LeakyReLU(relu_leak, name="relu3_1")(conv3_1)
if dropout > 0:
relu3_1 = Dropout(dropout, name="drop3_1")(relu3_1)
conv3_2 = Conv3D(filter_count, (3,3,3), name="conv3_2", padding="valid", kernel_regularizer=reg())(relu3_1)
relu3_2 = LeakyReLU(relu_leak, name="relu3_2")(conv3_2)
if dropout > 0:
relu3_2 = Dropout(dropout, name="drop3_2")(relu3_2)
if batch_norm:
bn3 = BatchNormalization(momentum=bn_momentum, name="bn3")(relu3_2)
stage3_out = bn3
else:
stage3_out = relu3_2
pool3 = MaxPooling3D((2,2,2), name="pool3")(stage3_out)
flatten1 = Flatten(name="flatten1")(pool3)
dense1 = Dense(32, name="dense1")(flatten1)
relu4 = LeakyReLU(relu_leak, name="relu4")(dense1)
dense2 = Dense(1, activation="sigmoid", name="dense2")(relu4)
output_layer = dense2
return Model(input_layer, output_layer)
## takes size 68^3
def get_discriminator_arch_c(init_filters=32, filter_scale=3, relu_leak=0.2, batch_norm=True, bn_momentum=0.8, regularization=0.0, dropout=0.0):
init_filters=int(init_filters)
filter_scale=int(filter_scale)
relu_leak=float(relu_leak)
batch_norm=batch_norm in ["True","true",True]
bn_momentum=float(bn_momentum)
regularization=float(regularization)
dropout=float(dropout)
if regularization > 0.0:
reg = lambda : L1L2(regularization, regularization)
else:
reg = lambda : None
input_layer = Input(shape = (68,68,68,1))
filter_count = init_filters
stage1_in = input_layer
conv1_1 = Conv3D(filter_count, (3,3,3), name="conv1_1", padding="valid", kernel_regularizer=reg())(stage1_in)
relu1_1 = LeakyReLU(relu_leak, name="relu1_1")(conv1_1)
if dropout > 0:
relu1_1 = Dropout(dropout, name="drop1_1")(relu1_1)
conv1_2 = Conv3D(filter_count, (3,3,3), name="conv1_2", padding="valid", kernel_regularizer=reg())(relu1_1)
relu1_2 = LeakyReLU(relu_leak, name="relu1_2")(conv1_2)
if dropout > 0:
relu1_2 = Dropout(dropout, name="drop1_2")(relu1_2)
if batch_norm:
bn1 = BatchNormalization(momentum=bn_momentum, name="bn1")(relu1_2)
stage1_out = bn1
else:
stage1_out = relu1_2
down1 = Conv3D(filter_count, (2,2,2), strides=(2,2,2), name="down1")(stage1_out)
filter_count *= filter_scale
stage2_in = down1
conv2_1 = Conv3D(filter_count, (3,3,3), name="conv2_1", padding="valid", kernel_regularizer=reg())(stage2_in)
relu2_1 = LeakyReLU(relu_leak, name="relu2_1")(conv2_1)
if dropout > 0:
relu2_1 = Dropout(dropout, name="drop2_1")(relu2_1)
conv2_2 = Conv3D(filter_count, (3,3,3), name="conv2_2", padding="valid", kernel_regularizer=reg())(relu2_1)
relu2_2 = LeakyReLU(relu_leak, name="relu2_2")(conv2_2)
if dropout > 0:
relu2_2 = Dropout(dropout, name="drop2_2")(relu2_2)
if batch_norm:
bn2 = BatchNormalization(momentum=bn_momentum, name="bn2")(relu2_2)
stage2_out = bn2
else:
stage2_out = relu2_2
down2 = Conv3D(filter_count, (2,2,2), strides=(2,2,2), name="down2")(stage2_out)
filter_count *= filter_scale
stage3_in = down2
conv3_1 = Conv3D(filter_count, (3,3,3), name="conv3_1", padding="valid", kernel_regularizer=reg())(stage3_in)
relu3_1 = LeakyReLU(relu_leak, name="relu3_1")(conv3_1)
if dropout > 0:
relu3_1 = Dropout(dropout, name="drop3_1")(relu3_1)
conv3_2 = Conv3D(filter_count, (3,3,3), name="conv3_2", padding="valid", kernel_regularizer=reg())(relu3_1)
relu3_2 = LeakyReLU(relu_leak, name="relu3_2")(conv3_2)
if dropout > 0:
relu3_2 = Dropout(dropout, name="drop3_2")(relu3_2)
if batch_norm:
bn3 = BatchNormalization(momentum=bn_momentum, name="bn3")(relu3_2)
stage3_out = bn3
else:
stage3_out = relu3_2
down3 = Conv3D(filter_count, (2,2,2), strides=(2,2,2), name="down3")(stage3_out)
flatten1 = Flatten(name="flatten1")(down3)
dense1 = Dense(32, name="dense1")(flatten1)
relu4 = LeakyReLU(relu_leak, name="relu4")(dense1)
dense2 = Dense(1, activation="sigmoid", name="dense2")(relu4)
output_layer = dense2
return Model(input_layer, output_layer)
## Loads weights from a model without skip connections into one with skip connections,
## leaving the weights corresponding to the skip connections alone
## From old_model (no skip connections) -> into -> new_model (with skip connections)
def load_weights_compat(new_model, old_model, load_bn=True):
for layer in old_model.layers:
is_batchnorm = (layer.name[:2]=="bn")
is_skipconn = (layer.name[-3:] == "nsc")
if is_batchnorm:
if load_bn:
new_model.get_layer(layer.name).set_weights(layer.get_weights())
elif is_skipconn: ## Load into bottom-most of the dims
new_m_weights = new_model.get_layer(layer.name[:-3] + "sc").get_weights()
old_m_weights = layer.get_weights()
new_m_weights[1] = old_m_weights[1] # biases don't change
new_m_weights[0][:,:,:,:old_m_weights[0].shape[3],:] = old_m_weights[0]
new_model.get_layer(layer.name[:-3] + "sc").set_weights(new_m_weights)
else:
new_model.get_layer(layer.name).set_weights(layer.get_weights())
def autodetect_skipconn(model):
""" Automatically detects whether skip connections are present in the model.
This works based off of layer names only, and assumes any model with a layer ending
in "nsc" (no skip connection) has no skip connections, and the opposite for any
model with a layer ending in "_sc" (skip connections).
"""
for layer in model.layers:
try:
if layer.name[-3:] == "nsc":
return False
elif layer.name[-3:] == "_sc":
return True
except:
pass
return True
global ARCHITECTURES
## Architecture Format:
## Key: "architecture name"
## Value: 3-tuple consisting of: (constructor_method, input_shape, output_shape)
## (For discriminator, omit output_shape to give a 2-tuple)
## input_shape and output_shape are both integer 3-tuples
ARCHITECTURES = {
"generator":{
"a":(get_generator_arch_a,(295,295,295),(83,83,83)),
"b":(get_generator_arch_b,(156,156,156),(68,68,68))
},
"discriminator":{
"a":(get_discriminator_arch_a,(83,83,83)),
"b":(get_discriminator_arch_b,(68,68,68)),
"c":(get_discriminator_arch_c,(68,68,68))
}
}
## Set Defaults
ARCHITECTURES["generator"]["default"] = ARCHITECTURES["generator"]["b"]
ARCHITECTURES["discriminator"]["default"] = ARCHITECTURES["discriminator"]["b"]