forked from escuccim/mammography-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
candidate_3.2.3.01.py
1190 lines (1004 loc) · 43.1 KB
/
candidate_3.2.3.01.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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import os
import wget
from sklearn.model_selection import train_test_split
import tensorflow as tf
from training_utils import download_file, get_batches, read_and_decode_single_example, load_validation_data, \
download_data, evaluate_model, get_training_data, load_weights, flatten, _scale_input_data, augment, _conv2d_batch_norm, standardize, _read_images
import argparse
from tensorboard import summary as summary_lib
# If number of epochs has been passed in use that, otherwise default to 50
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--epochs", help="number of epochs to train", default=30, type=int)
parser.add_argument("-d", "--data", help="which dataset to use", default=12, type=int)
parser.add_argument("-m", "--model", help="model to initialize weights with", default=None)
parser.add_argument("-r", "--restore", help="model to restore and continue training", default=None)
parser.add_argument("-l", "--label", help="how to classify data", default="mask")
parser.add_argument("-a", "--action", help="action to perform", default="train")
parser.add_argument("-f", "--freeze", help="whether to freeze convolutional layers", nargs='?', const=True, default=False)
parser.add_argument("-s", "--stop", help="stop gradient at pool5", nargs='?', const=True, default=False)
parser.add_argument("-t", "--threshold", help="decision threshold", default=0.5, type=float)
parser.add_argument("-c", "--contrast", help="contrast adjustment, if any", default=None, type=float)
parser.add_argument("-n", "--normalize", help="apply per image normalization", nargs='?', const=True, default=False)
parser.add_argument("-w", "--weight", help="weight to give to positive examples in cross-entropy", default=10, type=float)
parser.add_argument("-v", "--version", help="version or run number to assign to model name", default="")
parser.add_argument("--distort", help="use online data augmentation", default=False, const=True, nargs="?")
args = parser.parse_args()
epochs = args.epochs
dataset = args.data
init_model = args.model
restore_model = args.restore
how = args.label
action = args.action
threshold = args.threshold
freeze = args.freeze
stop = args.stop
contrast = args.contrast
normalize = args.normalize
weight = args.weight - 1
distort = args.distort
version = args.version
# figure out how to label the model name
if how == "label":
model_label = "l"
elif how == "normal":
model_label = "b"
elif how == "mask":
model_label = "m"
else:
model_label = "x"
# precalculated pixel mean of images
mu = 104.1353
# download the data
download_data(what=dataset)
## config
batch_size = 16
train_files, total_records = get_training_data(what=dataset)
## Hyperparameters
epsilon = 1e-8
# learning rate
epochs_per_decay = 10
decay_factor = 0.85
staircase = True
# if we are retraining some layers start with smaller learning rate
if not stop and not freeze:
starting_rate = 0.001
else:
starting_rate = 0.0001
# learning rate decay variables
steps_per_epoch = int(total_records / batch_size)
print("Steps per epoch:", steps_per_epoch)
# lambdas
lamC = 0.00001
lamF = 0.00250
# use dropout
dropout = True
fcdropout_rate = 0.25
convdropout_rate = 0.001
pooldropout_rate = 0.1
if how == "label":
num_classes = 5
elif how == "normal":
num_classes = 2
elif how == "mass":
num_classes = 3
elif how == "benign":
num_classes = 3
elif how == "mask":
num_classes = 2
print("Number of classes:", num_classes)
## Build the graph
graph = tf.Graph()
model_name = "model_s3.2.3.01" + model_label + "." + str(dataset) + str(version)
## Change Log
# 0.0.0.4 - increase pool3 to 3x3 with stride 3
# 0.0.0.6 - reduce pool 3 stride back to 2
# 0.0.0.7 - reduce lambda for l2 reg
# 0.0.0.8 - increase conv1 to 7x7 stride 2
# 0.0.0.9 - disable per image normalization
# 0.0.0.10 - commented out batch norm in conv layers, added conv4 and changed stride of convs to 1, increased FC lambda
# 0.0.0.11 - turn dropout for conv layers on
# 0.0.0.12 - added batch norm after pooling layers, increase pool dropout, decrease conv dropout, added extra conv layer to reduce data dimensionality
# 0.0.0.13 - added precision and f1 summaries
# 0.0.0.14 - fixing batch normalization, I don't think it's going to work after each pool
# 0.0.0.15 - reduced xentropy weighting term
# 0.0.0.17 - replaced initial 5x5 conv layers with 3 3x3 layers
# 0.0.0.18 - changed stride of first conv to 2 from 1
# 0.0.0.19 - doubled units in two fc layers
# 0.0.0.20 - lowered learning rate, put a batch norm back in
# 0.0.0.21 - put all batch norms back in
# 0.0.0.22 - increased lambdaC, removed dropout from conv layers
# 1.0.0.23 - added extra conv layers
# 1.0.0.27 - updates to training code and metrics
# 1.0.0.28 - using weighted x-entropy to improve recall
# 1.0.0.29 - updated code to work training to classify for multiple classes
# 1.0.0.29f - putting weighted x-entropy back
# 1.0.0.30b - changed some hyperparameters
# 1.0.0.31l - added decision threshold to predictions
# 1.0.0.33 - scaling input data
# 1.0.0.34 - centering data by 127, not by mean
# 1.0.0.35 - not centering data, just scaling it
# 2.0.0.35 - turning into fcn
# 2.0.0.36 - scaling and centering data?
# 3.0.0.36 - adjusting to do segmentation instead of classification
# 3.0.0.37 - trying to get this to train faster
# 3.0.0.38 - adding tiny value to logits to avoid xe of NaN
# 3.0.0.39 - doing metrics per pixel instead of per image
# 3.0.0.40 - adjusted graph so we can do online data augmentation and labels will be transformed in same way as images
# 3.1.0.40 - adding some layers back in that were previously removed to take more advantage of pre-trained model
# 3.1.0.41 - changed skip connections to try to make it a bit more stable
# 3.1.0.42 - changed one more skip connection
# 3.1.0.43 - trying to not restore batch norm to see if that helps with NaN at test time
# 3.1.0.44 - increased size of upconv filters to try to reduce patchiness of result, removed fc layer 3 as it was losing a lot of data
# 3.1.0.45 - adding some dropout to try to regularize
# 3.2.0.45 - restructuring to accept 320x320 images as input
# 3.2.0.46 - increased sizes of upsample filters
# 3.2.0.47 - changed number of filters again to speed up training
# 3.2.1.48 - adding extra skip connection to try to get better predictions
# 3.2.1.49 - renamed one upconv layer so they can be isolated and trained
# 3.2.2.01 - tweaking the upsampling layers
# 3.2.3.01 - going to train from scratch so adding some extras layers and such
with graph.as_default():
training = tf.placeholder(dtype=tf.bool, name="is_training")
is_testing = tf.placeholder(dtype=bool, shape=(), name="is_testing")
# create global step for decaying learning rate
global_step = tf.Variable(0, trainable=False)
learning_rate = tf.train.exponential_decay(starting_rate,
global_step,
steps_per_epoch * epochs_per_decay,
decay_factor,
staircase=staircase)
with tf.name_scope('inputs') as scope:
with tf.device('/cpu:0'):
# image, label = _read_images("./data/train_images/", 640)
image, label = read_and_decode_single_example(train_files, label_type=how, normalize=False, distort=False, size=640)
X_def, y_def = tf.train.shuffle_batch([image, label], batch_size=batch_size, capacity=2000, num_threads=6, seed=None, min_after_dequeue=1000)
# Placeholders
X = tf.placeholder_with_default(X_def, shape=[None, 640, 640, 1])
y = tf.placeholder_with_default(y_def, shape=[None, 640, 640, 1])
X_fl = tf.cast(X, tf.float32)
# optional online data augmentation
if distort:
X_dis, y_adj = augment(X_fl, y, horizontal_flip=True, augment_labels=True, vertical_flip=True, mixup=0)
else:
y_adj = y
X_dis = X_fl
# cast to float and scale input data
X_adj = _scale_input_data(X_dis, contrast=contrast, mu=127.0, scale=255.0)
# Convolutional layer 1
with tf.name_scope('conv1') as scope:
conv1 = tf.layers.conv2d(
X_adj,
filters=32,
kernel_size=(3, 3),
strides=(2, 2),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=100),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv1'
)
conv1 = tf.layers.batch_normalization(
conv1,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn1'
)
# apply relu
conv1_bn_relu = tf.nn.relu(conv1, name='relu1')
with tf.name_scope('conv1.1') as scope:
conv11 = tf.layers.conv2d(
conv1_bn_relu,
filters=32,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=101),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv1.1'
)
conv11 = tf.layers.batch_normalization(
conv11,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn1.1'
)
# apply relu
conv11 = tf.nn.relu(conv11, name='relu1.1')
with tf.name_scope('conv1.2') as scope:
conv12 = tf.layers.conv2d(
conv11,
filters=32,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=1101),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv1.2'
)
conv12 = tf.layers.batch_normalization(
conv12,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn1.2'
)
# apply relu
conv12_relu = tf.nn.relu(conv12, name='relu1.1')
# Max pooling layer 1
with tf.name_scope('pool1') as scope:
pool1 = tf.layers.max_pooling2d(
conv12_relu,
pool_size=(3, 3),
strides=(2, 2),
padding='SAME',
name='pool1'
)
# optional dropout
if dropout:
pool1 = tf.layers.dropout(pool1, rate=pooldropout_rate, seed=103, training=training)
# Convolutional layer 2
with tf.name_scope('conv2.1') as scope:
conv2 = tf.layers.conv2d(
pool1,
filters=64,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=104),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv2.1'
)
conv2 = tf.layers.batch_normalization(
conv2,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn2.1'
)
# apply relu
conv2 = tf.nn.relu(conv2, name='relu2.1')
# Convolutional layer 2
with tf.name_scope('conv2.2') as scope:
conv22 = tf.layers.conv2d(
conv2,
filters=64,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=1104),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv2.2'
)
conv22 = tf.layers.batch_normalization(
conv22,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn2.2'
)
# apply relu
conv22_relu = tf.nn.relu(conv22, name='relu2.2')
# Max pooling layer 2
with tf.name_scope('pool2') as scope:
pool2 = tf.layers.max_pooling2d(
conv22_relu,
pool_size=(2, 2),
strides=(2, 2),
padding='SAME',
name='pool2'
)
# optional dropout
if dropout:
pool2 = tf.layers.dropout(pool2, rate=pooldropout_rate, seed=106, training=training)
# Convolutional layer 3
with tf.name_scope('conv3.1') as scope:
conv3 = tf.layers.conv2d(
pool2,
filters=128,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=107),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv3.1'
)
conv3 = tf.layers.batch_normalization(
conv3,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn3.1'
)
# apply relu
conv3 = tf.nn.relu(conv3, name='relu3.1')
# Convolutional layer 3
with tf.name_scope('conv3.2') as scope:
conv32 = tf.layers.conv2d(
conv3,
filters=128,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=1107),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv3.2'
)
conv32 = tf.layers.batch_normalization(
conv32,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn3.2'
)
# apply relu
conv32 = tf.nn.relu(conv32, name='relu3.2')
# Max pooling layer 3
with tf.name_scope('pool3') as scope:
pool3 = tf.layers.max_pooling2d(
conv32,
pool_size=(2, 2),
strides=(2, 2),
padding='SAME',
name='pool3'
)
if dropout:
pool3 = tf.layers.dropout(pool3, rate=pooldropout_rate, seed=109, training=training)
# Convolutional layer 4
with tf.name_scope('conv4') as scope:
conv4 = tf.layers.conv2d(
pool3,
filters=256,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=110),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv4'
)
conv4 = tf.layers.batch_normalization(
conv4,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn4'
)
# apply relu
conv4_bn_relu = tf.nn.relu(conv4, name='relu4')
with tf.name_scope('conv4.1') as scope:
conv41 = tf.layers.conv2d(
conv4_bn_relu,
filters=256,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=1710),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv4.1'
)
conv41 = tf.layers.batch_normalization(
conv41,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn4.1'
)
# apply relu
conv41_bn_relu = tf.nn.relu(conv41, name='relu4.1')
# Max pooling layer 4
with tf.name_scope('pool4') as scope:
pool4 = tf.layers.max_pooling2d(
conv41_bn_relu,
pool_size=(2, 2),
strides=(2, 2),
padding='SAME',
name='pool4'
)
if dropout:
pool4 = tf.layers.dropout(pool4, rate=pooldropout_rate, seed=112, training=training)
# Convolutional layer 4
with tf.name_scope('conv5') as scope:
conv5 = tf.layers.conv2d(
pool4,
filters=512,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=113),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv5'
)
conv5 = tf.layers.batch_normalization(
conv5,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn5'
)
# apply relu
conv5_bn_relu = tf.nn.relu(conv5, name='relu5')
with tf.name_scope('conv5.1') as scope:
conv51 = tf.layers.conv2d(
conv5_bn_relu,
filters=512,
kernel_size=(3, 3),
strides=(1, 1),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=1193),
kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=lamC),
name='conv5.1'
)
conv51 = tf.layers.batch_normalization(
conv51,
axis=-1,
momentum=0.99,
epsilon=epsilon,
center=True,
scale=True,
beta_initializer=tf.zeros_initializer(),
gamma_initializer=tf.ones_initializer(),
moving_mean_initializer=tf.zeros_initializer(),
moving_variance_initializer=tf.ones_initializer(),
training=training,
fused=True,
name='bn5.1'
)
# apply relu
conv51_bn_relu = tf.nn.relu(conv51, name='relu5.1')
# Max pooling layer 5
with tf.name_scope('pool5') as scope:
pool5 = tf.layers.max_pooling2d(
conv51_bn_relu,
pool_size=(2, 2),
strides=(2, 2),
padding='SAME',
name='pool5'
)
if dropout:
pool5 = tf.layers.dropout(pool5, rate=pooldropout_rate, seed=115, training=training)
if stop:
pool5 = tf.stop_gradient(pool5, name="pool5_freeze")
fc1 = _conv2d_batch_norm(pool5, 2048, kernel_size=(5, 5), stride=(5, 5), training=training, epsilon=1e-8,
padding="VALID", seed=1013, lambd=lamC, name="fc_1")
fc1= tf.layers.dropout(fc1, rate=fcdropout_rate, seed=11537, training=training)
fc2 = _conv2d_batch_norm(fc1, 2048, kernel_size=(1, 1), stride=(1, 1), training=training, epsilon=1e-8,
padding="VALID", seed=1014, lambd=lamC, name="fc_2")
fc2 = tf.layers.dropout(fc2, rate=fcdropout_rate, seed=12537, training=training)
# upsample back to 5x5
with tf.name_scope('up_conv1') as scope:
unpool1 = tf.layers.conv2d_transpose(
fc2,
filters=512,
kernel_size=(5, 5),
strides=(5, 5),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=11435),
kernel_regularizer=None,
name='up_conv1'
)
unpool1 = unpool1 + pool5
# upsample to 10x10
with tf.name_scope('up_conv2') as scope:
unpool2 = tf.layers.conv2d_transpose(
unpool1,
filters=256,
kernel_size=(4, 4),
strides=(2, 2),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=11435),
kernel_regularizer=None,
name='up_conv2'
)
# skip connection
unpool2 = unpool2 + pool4
unpool2 = tf.nn.elu(unpool2, name="up_conv2_relu")
if dropout:
unpool2 = tf.layers.dropout(unpool2, rate=convdropout_rate, seed=13537, training=training)
# upsample to 20x20
with tf.name_scope('up_conv3') as scope:
unpool3 = tf.layers.conv2d_transpose(
unpool2,
filters=128,
kernel_size=(4, 4),
strides=(2, 2),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=19317),
kernel_regularizer=None,
name='up_conv3'
)
# skip connection
unpool3 = unpool3 + pool3
unpool3 = tf.nn.elu(unpool3, name='relu6.5')
# upsample to 40x40
with tf.name_scope('up_conv4') as scope:
unpool4 = tf.layers.conv2d_transpose(
unpool3,
filters=64,
kernel_size=(4, 4),
strides=(2, 2),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=11728),
kernel_regularizer=None,
name='up_conv4'
)
if dropout:
unpool4 = tf.layers.dropout(unpool4, rate=convdropout_rate, seed=14537, training=training)
unpool4 = unpool4 + pool2
unpool4 = tf.nn.elu(unpool4, name='up_relu4')
# upsample to 80x80
with tf.name_scope('up_conv5') as scope:
unpool5 = tf.layers.conv2d_transpose(
unpool4,
filters=32,
kernel_size=(4, 4),
strides=(2, 2),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=11756),
kernel_regularizer=None,
name='up_conv5'
)
if dropout:
unpool5 = tf.layers.dropout(unpool5, rate=pooldropout_rate, seed=14537, training=training)
# skip connection
unpool5 = unpool5 + pool1
# activation
unpool5 = tf.nn.elu(unpool5, name='relu10')
conv6 = _conv2d_batch_norm(unpool5, 16, kernel_size=(3, 3), stride=(1, 1), training=training, lambd=0.0,
name="up_conv6", activation="elu")
# upsample to 160x160
with tf.name_scope('up_conv7') as scope:
unpool7 = tf.layers.conv2d_transpose(
conv6,
filters=32,
kernel_size=(4, 4),
strides=(2, 2),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=11756),
kernel_regularizer=None,
name='up_conv7'
)
if dropout:
unpool7 = tf.layers.dropout(unpool7, rate=pooldropout_rate, seed=14557, training=training)
unpool7 = unpool7 + conv1
# activation
unpool7 = tf.nn.elu(unpool7, name='relu11')
# one last conv layer before logits
conv8 = _conv2d_batch_norm(unpool7, 16, kernel_size=(3,3), stride=(1,1), training=training, lambd=0.0, name="up_conv8", activation="elu")
# upsample to 320x320
with tf.name_scope('logits') as scope:
logits = tf.layers.conv2d_transpose(
conv8,
filters=2,
kernel_size=(4, 4),
strides=(2, 2),
padding='SAME',
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=5e-2, seed=11793),
kernel_regularizer=None,
name='logits'
)
# get the fully connected variables so we can only train them when retraining the network
fc_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "up_")
tr_logits = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "logits")
with tf.variable_scope('conv1', reuse=True):
conv_kernels1 = tf.get_variable('kernel')
kernel_transposed = tf.transpose(conv_kernels1, [3, 0, 1, 2])
with tf.variable_scope('visualization'):
tf.summary.image('conv1/filters', kernel_transposed, max_outputs=32, collections=["kernels"])
# This will weight the positive examples higher so as to improve recall
weights = tf.multiply(tf.cast(weight, tf.float32), tf.cast(tf.greater(y_adj, 0), tf.float32)) + 1
mean_ce = tf.reduce_mean(tf.losses.sparse_softmax_cross_entropy(labels=y_adj, logits=(logits + 1e-10), weights=weights))
# Add in l2 loss
loss = mean_ce + tf.losses.get_regularization_loss()
# Adam optimizer
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
# Minimize cross-entropy - freeze certain layers depending on input
if freeze:
train_op = optimizer.minimize(loss, global_step=global_step, var_list=fc_vars + tr_logits)
else:
train_op = optimizer.minimize(loss, global_step=global_step)
# predictions = tf.reshape(tf.argmax(logits, axis=-1, output_type=tf.int32), (-1, 320,320))
# if we reshape the predictions it won't work with images of other sizes
predictions = tf.argmax(logits, axis=-1, output_type=tf.int32)
# squash the predictions into a per image prediction - negative images will have a max of 0
pred_sum = tf.reduce_sum(predictions, axis=[1, 2])
image_predictions = tf.cast(tf.greater(pred_sum, (640 * 640 // 750)), dtype=tf.uint8)
image_truth = tf.reduce_max(y_adj, axis=[1, 2])
# image_predictions = tf.reduce_max(predictions, axis=[1,2,3])
# set a threshold on the predictions so we ignore images with only a few positive pixels
pred_sum = tf.reduce_sum(predictions, axis=[1, 2])
image_predictions = tf.cast(tf.greater(pred_sum, (640*640//750)),dtype=tf.uint8)
# get the accuracy per pixel
accuracy, acc_op = tf.metrics.accuracy(
labels=y_adj,
predictions=predictions,
updates_collections=[tf.GraphKeys.UPDATE_OPS, 'metrics_ops'],
name="accuracy",
)
# calculate recall and precision per pixel
recall, rec_op = tf.metrics.recall(labels=y_adj, predictions=predictions,
updates_collections=[tf.GraphKeys.UPDATE_OPS, 'metrics_ops'],
name="pixel_recall")
precision, prec_op = tf.metrics.precision(labels=y_adj, predictions=predictions,
updates_collections=[tf.GraphKeys.UPDATE_OPS, 'metrics_ops'],
name="pixel_precision")
f1_score = 2 * ((precision * recall) / (precision + recall))
# per image metrics
image_accuracy, image_acc_op = tf.metrics.accuracy(
labels=image_truth,
predictions=image_predictions,
updates_collections=[tf.GraphKeys.UPDATE_OPS, 'metrics_ops'],
name="image_accuracy",
)
image_recall, image_rec_op = tf.metrics.recall(labels=image_truth, predictions=image_predictions,
updates_collections=[tf.GraphKeys.UPDATE_OPS, 'metrics_ops'], name="image_recall")
image_precision, image_prec_op = tf.metrics.precision(labels=image_truth, predictions=image_predictions,
updates_collections=[tf.GraphKeys.UPDATE_OPS, 'metrics_ops'], name="image_precision")
tf.summary.scalar('recall_1', recall, collections=["summaries"])
tf.summary.scalar('recall_per_image', image_recall, collections=["summaries"])
tf.summary.scalar('precision_1', precision, collections=["summaries"])
tf.summary.scalar('precision_per_image', image_precision, collections=["summaries"])
tf.summary.scalar('f1_score', f1_score, collections=["summaries"])
# Create summary hooks
tf.summary.scalar('accuracy', accuracy, collections=["summaries"])
tf.summary.scalar('accuracy_per_image', image_accuracy, collections=["summaries"])
tf.summary.scalar('cross_entropy', mean_ce, collections=["summaries"])
tf.summary.scalar('learning_rate', learning_rate, collections=["summaries"])
# add this so that the batch norm gets run
extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
# collect the metrics ops into one op so we can run that at test time
metrics_op = tf.get_collection('metrics_ops')
# Merge all the summaries
merged = tf.summary.merge_all("summaries")
kernel_summaries = tf.summary.merge_all("kernels")
print("Graph created...")
## CONFIGURE OPTIONS
if init_model is not None:
if os.path.exists(os.path.join("model", init_model + '.ckpt.index')):
init = False
else:
init = True
elif restore_model is not None:
if os.path.exists(os.path.join("model", restore_model + '.ckpt.index')):
init = False
else:
init = True
else:
if os.path.exists(os.path.join("model", model_name + '.ckpt.index')):
init = False
else:
init = True
meta_data_every = 1
log_to_tensorboard = True
print_every = 5 # how often to print metrics
checkpoint_every = 1 # how often to save model in epochs
use_gpu = False # whether or not to use the GPU
print_metrics = True # whether to print or plot metrics, if False a plot will be created and updated every epoch
# Initialize metrics or load them from disk if they exist
if os.path.exists(os.path.join("data", model_name + "train_acc.npy")):
train_acc_values = np.load(os.path.join("data", model_name + "train_acc.npy")).tolist()
else:
train_acc_values = []
if os.path.exists(os.path.join("data", model_name + "train_loss.npy")):
train_cost_values = np.load(os.path.join("data", model_name + "train_loss.npy")).tolist()
else:
train_cost_values = []
if os.path.exists(os.path.join("data", model_name + "train_lr.npy")):
train_lr_values = np.load(os.path.join("data", model_name + "train_lr.npy")).tolist()
else:
train_lr_values = []
if os.path.exists(os.path.join("data", model_name + "train_recall.npy")):
train_recall_values = np.load(os.path.join("data", model_name + "train_recall.npy")).tolist()
else:
train_recall_values = []
if os.path.exists(os.path.join("data", model_name + "cv_acc.npy")):
valid_acc_values = np.load(os.path.join("data", model_name + "cv_acc.npy")).tolist()
else:
valid_acc_values = []
if os.path.exists(os.path.join("data", model_name + "cv_loss.npy")):
valid_cost_values = np.load(os.path.join("data", model_name + "cv_loss.npy")).tolist()
else:
valid_cost_values = []
if os.path.exists(os.path.join("data", model_name + "cv_recall.npy")):
valid_recall_values = np.load(os.path.join("data", model_name + "cv_recall.npy")).tolist()
else:
valid_recall_values = []
config = tf.ConfigProto()
## train the model
with tf.Session(graph=graph, config=config) as sess:
if log_to_tensorboard:
train_writer = tf.summary.FileWriter('./logs/tr_' + model_name, sess.graph)
test_writer = tf.summary.FileWriter('./logs/te_' + model_name)
if not print_metrics:
# create a plot to be updated as model is trained
f, ax = plt.subplots(1,4,figsize=(24,5))
# create the saver
saver = tf.train.Saver()
# If the model is new initialize variables, else restore the session
if init:
sess.run(tf.global_variables_initializer())
print("Initializing model...")
else:
# if we are initializing with the weights from another model load it
if init_model is not None:
# initialize the global variables
sess.run(tf.global_variables_initializer())
# create the initializer function to initialize the weights
init_fn = load_weights(init_model, exclude=["fc3", "logits", "bn_conv6", "up_conv7", "bn_up_conv8","bn_up_conv6","bn_up_conv7","conv_up_conv6", "conv_up_conv8","up_conv1","up_conv2","up_conv5","up_conv6", "accuracy", "up_conv4", "up_conv3", "global_step"])
# run the initializer
init_fn(sess)
## reload some weights from one checkpoint and some from a different one
# init_fn = load_weights("model_s3.2.1.48m.12", exclude=["conv_up_conv7", "bn_up_conv7", "fc3", "conv5", "accuracy", "bn5"])
# init_fn(sess)
#
# init_fn = load_weights("model_s3.2.0.47m.12", include=["conv5", "bn5"])
# init_fn(sess)
#
# # reset the global step
initial_global_step = global_step.assign(0)
sess.run(initial_global_step)
print("Initializing weights from model", init_model)
# reset init model so we don't do this again
init_model = None
elif restore_model is not None:
saver.restore(sess, './model/' + restore_model + '.ckpt')
print("Restoring model", restore_model)
initial_global_step = global_step.assign(0)
sess.run(initial_global_step)
# otherwise load this model
else:
saver.restore(sess, './model/' + model_name + '.ckpt')
print("Restoring model", model_name)
# if we are training the model
if action == "train":
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
print("Training model", model_name, "...")