-
Notifications
You must be signed in to change notification settings - Fork 28
/
generate_images.py
1110 lines (909 loc) · 43 KB
/
generate_images.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
"""
Output varying figures for paper
"""
import random
from matplotlib import animation
from torchvision import transforms
import dataset
from helpers import gridify_output, load_parameters
def make_prediction(real, recon, mask, x_t, threshold=0.5, error_fn="sq"):
"""
Make generic prediction and output tensor with order (real, x_lambda, reconstruction, square error, square error
threshold, ground truth mask)
:param real: initial real image x_0
:param recon: reconstruction when diffused to x_t
:param mask: ground truth mask
:param x_t: middle image when initial image x_0 is noised through t time steps
:param threshold: value to take threshold
:param error_fn: square or l1 error - future work could explore error functions in feature space
:return:
"""
if error_fn == "sq":
mse = ((recon - real).square() * 2) - 1
elif error_fn == "l1":
mse = (recon - real)
mse_threshold = mse > (threshold * 2) - 1
mse_threshold = (mse_threshold.float() * 2) - 1
return torch.cat((real, x_t, recon, mse, mse_threshold, mask)), mse_threshold
def output_denoise_sequence(sequence: list, filename, masks, predictions):
"""
sequence is [[t lots of images],[t lots of images],[t lots of images],[t lots of images]]
or sequence is [t lots of images] where t is the number of forwad images plus backward plus
prediction and real mask
:param sequence: sequence of images (see above)
:param filename: output filename
:param masks: list of ground truth masks
:param predictions: list of predicted images
:return:
"""
if len(sequence) > 10:
sequence = [sequence]
# split the forward and backward elements for labelling
relevant_elements_forward = np.linspace(0, len(sequence[0]) // 2, 6).astype(np.int32)
relevant_elements_backward = (-1 * relevant_elements_forward[-2::-1]) - 1
relevant_elements = np.append(relevant_elements_forward, relevant_elements_backward)
# sequence[0].shape # B,C,H,W
# init empty figure
output = torch.empty(13 * len(sequence), sequence[0][0].shape[1], 256, 256, )
# push each subimage into figure
for j, new_sequence in enumerate(sequence):
for i, val in enumerate(relevant_elements):
output[13 * j + i] = new_sequence[val]
output[13 * (j + 1) - 2] = predictions[j]
output[13 * (j + 1) - 1] = masks[j]
output = output.permute(0, 2, 3, 1)
fig, subplots = plt.subplots(
len(sequence), 13, figsize=(13, len(sequence)),
gridspec_kw={'wspace': 0, 'hspace': 0}, squeeze=False
)
# mpl implot images with relevant pixel value renormalisation
for brain in range(len(sequence)):
for noise in range(13):
# plot diffusion sequence
if output[0].shape[-1] == 1:
# img = scale_img(output[13 * brain + noise])
subplots[brain][noise].imshow(
output[13 * brain + noise].reshape(*output[0].shape[-3:-1]).cpu().numpy(), cmap="gray"
)
else:
# mask
if noise < 12:
# img = scale_img(output[13 * brain + noise])
img = (output[13 * brain + noise] + 1) / 2
else:
# prediction
img = output[13 * brain + noise]
# img = scale_img(output[13 * brain + noise])
subplots[brain][noise].imshow(
img.reshape(*output[0].shape[-3:]).cpu().numpy(),
)
subplots[brain][noise].tick_params(
top=False, bottom=False, left=False, right=False,
labelleft=False, labelbottom=False
)
# Add axis labels
for i in range(6):
subplots[0][i].set_xlabel(f"$x_{{{relevant_elements[i]}}}$", fontsize=6)
subplots[0][i].xaxis.set_label_position("top")
for i in range(6, 11):
subplots[0][i].set_xlabel(f"$x_{{{relevant_elements_forward[::-1][1:][i - 6]}}}$", fontsize=6)
subplots[0][i].xaxis.set_label_position("top")
subplots[0][-2].set_xlabel(f"Prediction", fontsize=6)
subplots[0][-2].xaxis.set_label_position("top")
subplots[0][-1].set_xlabel(f"Ground Truth", fontsize=6)
subplots[0][-1].xaxis.set_label_position("top")
plt.savefig(filename)
def output_masked_comparison(sequence, filename, t_distance=250, ):
"""
sequence is ideally [[x_0,recon,mse,threshold_mse,ground_truth]*4] where
[x_0,recon,mse,threshold_mse,ground_truth] is a single (5,1,256,256) torch tensor
or sequence is [x_0,recon,mse,threshold_mse,ground_truth]
:param sequence:
:return:
"""
if type(sequence) == torch.tensor:
sequence = [sequence]
# make plots
fig, subplots = plt.subplots(
len(sequence), 6, constrained_layout=False, figsize=(6, len(sequence)),
squeeze=False,
gridspec_kw={'wspace': 0, 'hspace': 0}
)
plt.tick_params(
top=False, bottom=False, left=False, right=False,
labelleft=False, labelbottom=False
)
for i, brain in enumerate(sequence):
for plot in range(brain.shape[0]):
# print(plot, torch.max(brain[plot]), torch.min(brain[plot]))
# BCHW
if plot > 2:
# pick colourmap
if plot == 3:
cmap = "hot"
else:
cmap = "gray"
# convert square error to grayscale for rgb images
if brain[plot].shape[-3] == 3:
square_error_gray = transforms.functional.rgb_to_grayscale(brain[plot] + 1)
# threshold prediction
if plot == 4:
square_error_gray = ((square_error_gray > 0.1).float() * 2) - 1
subplots[i][plot].imshow(
square_error_gray.permute(1, 2, 0).cpu().numpy(), cmap=cmap
)
else:
subplots[i][plot].imshow((brain[plot] + 1).permute(1, 2, 0).cpu().numpy(), cmap=cmap)
else:
# img = scale_img(brain[plot])
# renorm image
img = (brain[plot] + 1) / 2
subplots[i][plot].imshow(
img.permute(1, 2, 0).cpu().numpy(),
# cmap='gray'
)
subplots[i][plot].tick_params(
top=False, bottom=False, left=False, right=False,
labelleft=False, labelbottom=False
)
# add labels and save
for i, val in enumerate(
["$x_0$", f"$x_{{{t_distance}}}$", "Reconstruction", "Square Error", "Prediction",
"Ground Truth"]
):
subplots[0][i].set_xlabel(f"{val}", fontsize=6)
subplots[0][i].xaxis.set_label_position("top")
plt.savefig(filename)
def make_videos():
"""
generate videos for dataset based on input arguments
:return: selection of videos for dataset of trained model
"""
# load parameters
args, output = load_parameters(device)
in_channels = 1
if args["dataset"].lower() == "leather":
in_channels = 3
# init model, betas and diffusion classes
unet = UNetModel(
args['img_size'][0], args['base_channels'], channel_mults=args['channel_mults'], in_channels=in_channels
)
betas = get_beta_schedule(args['T'], args['beta_schedule'])
diff = GaussianDiffusionModel(
args['img_size'], betas, loss_weight=args['loss_weight'],
loss_type=args['loss-type'], noise=args["noise_fn"], img_channels=in_channels
)
print(args)
# load checkpoint
unet.load_state_dict(output["ema"])
unet.to(device)
unet.eval()
# load specific dataset - ie carpet / leather / MRI
if args["dataset"].lower() == "carpet":
d_set = dataset.DAGM("./DATASETS/CARPET/Class1", True)
elif args["dataset"].lower() == "leather":
d_set = dataset.MVTec(
"./DATASETS/leather", anomalous=True, img_size=args["img_size"],
rgb=True, include_good=False
)
else:
d_set = dataset.AnomalousMRIDataset(
ROOT_DIR=f'{DATASET_PATH}', img_size=args['img_size'],
slice_selection="iterateKnown_restricted", resized=False
)
loader = dataset.init_dataset_loader(d_set, args)
plt.rcParams['figure.dpi'] = 100
# make directories
for i in [f'./final-outputs/', f'./final-outputs/ARGS={args["arg_num"]}']:
if not os.path.exists(i):
os.makedirs(i)
# generate 20 videos
for i in range(20):
# if using simplex noise - select random lambda parameters with weighted probabilities
if str(args["arg_num"]) == '28':
t_distance = np.random.choice([150, 200, 250], p=[0.2, 0.4, 0.4])
else:
# select different parameters
t_distance = np.random.choice([250, 500, 750], p=[0.2, 0.4, 0.4])
print(f"loop {i}")
new = next(loader)
img = new["image"].to(device)
# if mri - select random slice
if args["dataset"] != "carpet" and args["dataset"] != "leather":
slice = np.random.choice([0, 1, 2, 3], p=[0.2, 0.3, 0.3, 0.2])
img = img.reshape(img.shape[1], 1, *args["img_size"])
img = img[slice, ...].reshape(1, 1, *args["img_size"])
# perform diffusion
output = diff.forward_backward(
unet, img,
see_whole_sequence="whole",
# t_distance=5, denoise_fn=args["noise_fn"]
t_distance=t_distance, denoise_fn=args["noise_fn"]
)
# plot, animate and save diffusion process
fig, ax = plt.subplots()
plt.axis('off')
imgs = [[ax.imshow(gridify_output(output[x], 1), animated=True)] for x in range(0, len(output), 2)]
ani = animation.ArtistAnimation(
fig, imgs, interval=25, blit=True,
repeat_delay=1000
)
temp = os.listdir(
f'./final-outputs/ARGS={args["arg_num"]}'
)
output_name = f'./final-outputs/ARGS={args["arg_num"]}/attempt={len(temp) + 1}-sequence.mp4'
ani.save(output_name)
def make_ano_outputs():
args, output = load_parameters(device)
in_channels = 1
if args["dataset"].lower() == "leather":
in_channels = 3
if args["channels"] != "":
in_channels = args["channels"]
unet = UNetModel(
args['img_size'][0], args['base_channels'], channel_mults=args['channel_mults'], in_channels=in_channels
)
betas = get_beta_schedule(args['T'], args['beta_schedule'])
diff = GaussianDiffusionModel(
args['img_size'], betas, loss_weight=args['loss_weight'],
loss_type=args['loss-type'], noise=args["noise_fn"], img_channels=in_channels
)
unet.load_state_dict(output["ema"])
unet.to(device)
unet.eval()
if args["dataset"].lower() == "carpet":
d_set = dataset.DAGM("./DATASETS/CARPET/Class1", True)
elif args["dataset"].lower() == "leather":
d_set = dataset.MVTec(
"./DATASETS/leather", anomalous=True, img_size=args["img_size"],
rgb=True, include_good=False
)
else:
d_set = dataset.AnomalousMRIDataset(
ROOT_DIR=f'{DATASET_PATH}', img_size=args['img_size'],
slice_selection="iterateKnown_restricted", resized=False
)
loader = dataset.init_dataset_loader(d_set, args)
plt.rcParams['figure.dpi'] = 1000
for i in [f'./final-outputs/', f'./final-outputs/ARGS={args["arg_num"]}']:
try:
os.makedirs(i)
except OSError:
pass
# t_distance = 200
for i in range(30):
predictions = []
sequences = []
masks = []
mse_thresholds = []
rows = np.random.choice([2, 3, 4], p=[0.2, 0.3, 0.5, ])
t_distance = np.random.choice([50, 150, 200, 250], p=[0.25, 0.25, 0.25, 0.25])
rows, t_distance = 1, 250
# threshold = np.random.choice([0.1, 0.15, 0.20, 0.25, 0.3, 0.35, 0.4])
threshold = 0.5
print(f"epoch {i}, rows @ epoch: {rows}")
for k in range(rows):
new = next(loader)
while new["filenames"][0][-9:-4] != "19423":
new = next(loader)
# while torch.sum(new["mask"]) < 1000:
# new = next(loader)
img = new["image"].to(device)
img_mask = new["mask"]
img_mask = img_mask.to(device)
if args["dataset"] != "carpet" and args["dataset"] != "leather":
slice = np.random.choice([0, 1, 2, 3], p=[0.2, 0.3, 0.3, 0.2])
img = img.reshape(img.shape[1], 1, *args["img_size"])
img = img[slice, ...].reshape(1, 1, *args["img_size"])
img_mask = img_mask.reshape(img_mask.shape[1], 1, *args["img_size"])
img_mask = img_mask[slice, ...].reshape(1, 1, *args["img_size"])
output = diff.forward_backward(
unet, img,
see_whole_sequence="whole",
# t_distance=5, denoise_fn=args["noise_fn"]
t_distance=t_distance, denoise_fn=args["noise_fn"]
)
if args["dataset"] == "leather":
output_images, mse_threshold = make_prediction(
img, output[-1].to(device),
img_mask, output[t_distance // 2].to(device), threshold=threshold
)
else:
output_images, mse_threshold = make_prediction(
img, output[-1].to(device),
img_mask, output[t_distance // 2].to(device)
)
predictions.append(
output_images
)
mse_thresholds.append(mse_threshold)
masks.append(img_mask)
sequences.append(output)
temp = os.listdir(f"./final-outputs/ARGS={args['arg_num']}")
output_masked_comparison(
predictions, f'./final-outputs/ARGS={args["arg_num"]}/attempt'
f'={len(temp) + 1}-{threshold}-predictions.png', t_distance
)
output_denoise_sequence(
sequences, f'./final-outputs/ARGS={args["arg_num"]}/attempt'
f'={len(temp) + 1}-{threshold}-sequence.png', masks, mse_thresholds
)
plt.close('all')
def make_gauss_simplex_outputs(simplex_argNum="28", gauss_argNum="26"):
"""
Output figure containing both gauss and simplex noise
:param simplex_argNum: arg value of saved simplex checkpoint
:param gauss_argNum: arg value of saved gauss checkpoint
:return:
"""
sys.argv[1] = simplex_argNum
args_simplex, output_simplex = load_parameters(device)
sys.argv[1] = gauss_argNum
args_gauss, output_gauss = load_parameters(device)
# consider different channel sizes
in_channels = 1
if args_simplex["dataset"].lower() == "leather":
in_channels = 3
if args_simplex["channels"] != "":
in_channels = args_simplex["channels"]
# init model and checkpoints
unet_simplex = UNetModel(
args_simplex['img_size'][0], args_simplex['base_channels'], channel_mults=args_simplex['channel_mults'],
in_channels=in_channels
)
unet_gauss = UNetModel(
args_gauss['img_size'][0], args_gauss['base_channels'], channel_mults=args_gauss['channel_mults'],
in_channels=in_channels
)
betas = get_beta_schedule(args_simplex['T'], args_simplex['beta_schedule'])
diff_simplex = GaussianDiffusionModel(
args_simplex['img_size'], betas, loss_weight=args_simplex['loss_weight'],
loss_type=args_simplex['loss-type'], noise=args_simplex["noise_fn"], img_channels=in_channels
)
diff_gauss = GaussianDiffusionModel(
args_simplex['img_size'], betas, loss_weight=args_simplex['loss_weight'],
loss_type=args_simplex['loss-type'], noise=args_gauss["noise_fn"], img_channels=in_channels
)
unet_simplex.load_state_dict(output_simplex["ema"])
unet_gauss.load_state_dict(output_gauss["ema"])
unet_simplex.eval()
unet_gauss.eval()
# init varying datasets
if args_simplex["dataset"].lower() == "carpet":
d_set = dataset.DAGM("./DATASETS/CARPET/Class1", True)
elif args_simplex["dataset"].lower() == "leather":
if in_channels == 3:
d_set = dataset.MVTec(
"./DATASETS/leather", anomalous=True, img_size=args_simplex["img_size"],
rgb=True, include_good=False
)
else:
d_set = dataset.MVTec(
"./DATASETS/leather", anomalous=True, img_size=args_simplex["img_size"],
rgb=False, include_good=False
)
else:
d_set = dataset.AnomalousMRIDataset(
ROOT_DIR=f'{DATASET_PATH}', img_size=args_simplex['img_size'],
slice_selection="iterateKnown_restricted", resized=False
)
loader = dataset.init_dataset_loader(d_set, args_simplex)
plt.rcParams['figure.dpi'] = 1000
for i in [f'./final-outputs/', f'./final-outputs/gauss_simplex', f'./final-outputs/gauss_simplex/'
f'{args_simplex["dataset"]}-{in_channels}']:
if not os.path.exists(i):
os.makedirs(i)
# generate 20 figures
for i in range(20):
predictions = []
unet_simplex.to(device)
# select number of rows - ie 2 simplex and 2 gauss or 1 of each
rows = random.randint(1, 2)
# select lambda to directly compare lambda values
t_distance = np.random.choice([150, 200, 250, 300], p=[0.25, 0.25, 0.25, 0.25])
# select random threshold - this was found to be more of a hyperparameter for textured surfaces such as from
# DAGM and MVTec
threshold = np.random.choice([0.15, 0.2, 0.25])
imgs = []
for k in range(rows):
new = next(loader)
# As the MVTec takes a section of a larger image - omly use an image with more than 1000 pixels in ground
# truth mask to guarantee an anomaly lies
while torch.sum(new["mask"]) < 1000:
new = next(loader)
if args_simplex["dataset"] != "carpet" and args_simplex["dataset"] != "leather":
slice = np.random.choice([0, 1, 2, 3], p=[0.2, 0.3, 0.3, 0.2])
new["image"] = new["image"].reshape(new["image"].shape[1], 1, *args_simplex["img_size"])
new["image"] = new["image"][slice, ...].reshape(1, 1, *args_simplex["img_size"])
new["mask"] = new["mask"].reshape(new["mask"].shape[1], 1, *args_simplex["img_size"])
new["mask"] = new["mask"][slice, ...].reshape(1, 1, *args_simplex["img_size"])
imgs.append(new)
for new in imgs:
img = new["image"].to(device)
img_mask = new["mask"].to(device)
output = diff_simplex.forward_backward(
unet_simplex, img,
see_whole_sequence="whole",
t_distance=t_distance, denoise_fn=args_simplex["noise_fn"]
)
output_images, mse_threshold = make_prediction(
img, output[-1].to(device), img_mask, output[t_distance // 2].to(device), threshold=threshold,
error_fn="sq"
)
predictions.append(
output_images
)
unet_simplex.cpu()
unet_gauss.to(device)
for new in imgs:
img = new["image"].to(device)
img_mask = new["mask"].to(device)
output = diff_gauss.forward_backward(
unet_gauss, img,
see_whole_sequence="whole",
t_distance=t_distance, denoise_fn=args_gauss["noise_fn"]
)
output_images, mse_threshold = make_prediction(
img, output[-1].to(device),
img_mask, output[t_distance // 2].to(device), threshold=threshold, error_fn="sq"
)
predictions.append(
output_images
)
unet_gauss.cpu()
temp = os.listdir(f"./final-outputs/gauss_simplex/{args_simplex['dataset']}-{in_channels}")
output_masked_comparison(
predictions,
f"./final-outputs/gauss_simplex/{args_simplex['dataset']}-{in_channels}/{len(temp) + 1}test{i}"
f"-{threshold}.png",
t_distance
)
def make_test_set_outputs(anomalous=False, t_distance=250):
"""
Generate Fig 1 from paper - containing Gauss and simplex noise on healthy or anomalous test set
:param anomalous: test on anomalous?
:param t_distance: lambda value for diffusion process
:return:
"""
# load both simplex and Gauss checkpoints separately
sys.argv[1] = "28"
args_simplex, output_simplex = load_parameters(device)
sys.argv[1] = "26"
args_gauss, output_gauss = load_parameters(device)
unet_simplex = UNetModel(
args_simplex['img_size'][0], args_simplex['base_channels'], channel_mults=args_simplex['channel_mults']
)
unet_gauss = UNetModel(
args_gauss['img_size'][0], args_gauss['base_channels'], channel_mults=args_gauss['channel_mults']
)
betas = get_beta_schedule(args_simplex['T'], args_simplex['beta_schedule'])
diff_simplex = GaussianDiffusionModel(
args_simplex['img_size'], betas, loss_weight=args_simplex['loss_weight'],
loss_type=args_simplex['loss-type'], noise=args_simplex["noise_fn"]
)
diff_gauss = GaussianDiffusionModel(
args_simplex['img_size'], betas, loss_weight=args_simplex['loss_weight'],
loss_type=args_simplex['loss-type'], noise=args_gauss["noise_fn"]
)
unet_simplex.load_state_dict(output_simplex["ema"])
unet_gauss.load_state_dict(output_gauss["ema"])
unet_simplex.eval()
unet_gauss.eval()
if anomalous:
ano_dataset = dataset.AnomalousMRIDataset(
ROOT_DIR=f'{DATASET_PATH}', img_size=args_simplex['img_size'],
slice_selection="iterateKnown_restricted", resized=False
)
loader = dataset.init_dataset_loader(ano_dataset, args_simplex)
else:
_, testing_dataset = dataset.init_datasets("./", args_simplex)
loader = dataset.init_dataset_loader(testing_dataset, args_simplex)
plt.rcParams['figure.dpi'] = 1000
for i in [f'./final-outputs/', f'./final-outputs/ARGS={args_simplex["arg_num"]}']:
if not os.path.exists(i):
os.makedirs(i)
for i in range(20):
sequences = []
unet_simplex.to(device)
if anomalous:
rows = 1
else:
rows = 2
# rows = np.random.choice([1, 2], p=[0.4, 0.6])
# select each volume for imgs - and diffuse each with simplex noise
imgs = []
for i in range(rows):
new = next(loader)
img = new["image"].to(device)
img = img.reshape(img.shape[1], 1, *args_simplex["img_size"])
if anomalous:
slice = np.random.choice([0, 1, 2, 3], p=[0.2, 0.3, 0.3, 0.2])
img = img[slice, ...].reshape(1, 1, *args_simplex["img_size"])
imgs.append(img)
for k in range(rows):
img = imgs[k]
output = diff_simplex.forward_backward(
unet_simplex, img.reshape(1, 1, *args_simplex["img_size"]),
see_whole_sequence="whole",
t_distance=t_distance, denoise_fn=args_simplex["noise_fn"]
)
sequences.append(output)
unet_simplex.cpu()
# diffuse each with gaussian noise
unet_gauss.to(device)
for k in range(rows):
img = imgs[k]
output = diff_gauss.forward_backward(
unet_gauss, img.reshape(1, 1, *args_simplex["img_size"]),
see_whole_sequence="whole",
t_distance=t_distance, denoise_fn=args_gauss["noise_fn"]
)
sequences.append(output)
unet_gauss.cpu()
# following image generation - output images with similar format to function - output_denoise_sequence
temp = os.listdir(f"./final-outputs/ARGS={args_simplex['arg_num']}")
if len(sequences) > 10:
sequences = [sequences]
relevant_elements_forward = np.linspace(0, len(sequences[0]) // 2, 4).astype(np.int32)
relevant_elements_backward = (-1 * relevant_elements_forward[-2::-1]) - 1
relevant_elements = np.append(relevant_elements_forward, relevant_elements_backward)
output = torch.empty(7 * len(sequences), 1, 256, 256)
# insert each image into empty tensor
for j, new_sequence in enumerate(sequences):
for i, val in enumerate(relevant_elements):
output[7 * j + i] = new_sequence[val]
fig, subplots = plt.subplots(
len(sequences), 7, figsize=(7, len(sequences)),
gridspec_kw={'wspace': 0, 'hspace': 0}, squeeze=False
)
# plot each image
for brain in range(len(sequences)):
for noise in range(7):
# subplots[brain][noise].imshow(output[12 * brain + noise].reshape(256, 256, 1), cmap="gray")
subplots[brain][noise].imshow(
output[7 * brain + noise].reshape(*output[0].shape[-2:]).cpu().numpy(),
cmap="gray"
)
subplots[brain][noise].tick_params(
top=False, bottom=False, left=False, right=False,
labelleft=False, labelbottom=False
)
# subplots[brain][noise].axis('off')
# set labels
for i in range(4):
subplots[0][i].set_xlabel(f"$x_{{{relevant_elements[i]}}}$", fontsize=6)
subplots[0][i].xaxis.set_label_position("top")
for i in range(4, 7):
subplots[0][i].set_xlabel(f"$x_{{{relevant_elements_forward[::-1][1:][i - 4]}}}$", fontsize=6)
subplots[0][i].xaxis.set_label_position("top")
plt.savefig(
f'./final-outputs/ARGS={args_simplex["arg_num"]}/test_set_mixed_attempt'
f'={len(temp) + 1}-sequence.png'
)
plt.close('all')
def make_varying_frequency_outputs():
"""
Figure for affect of simplex frequency on segmentation performance - Fig. 3 from AnoDDPM paper
:return:
"""
# init model and load checkpoint
args, output = load_parameters(device)
unet = UNetModel(args['img_size'][0], args['base_channels'], channel_mults=args['channel_mults'])
betas = get_beta_schedule(args['T'], args['beta_schedule'])
diff = GaussianDiffusionModel(
args['img_size'], betas, loss_weight=args['loss_weight'],
loss_type=args['loss-type'], noise=args["noise_fn"]
)
unet.load_state_dict(output["ema"])
unet.to(device)
unet.eval()
ano_dataset = dataset.AnomalousMRIDataset(
ROOT_DIR=f'{DATASET_PATH}', img_size=args['img_size'],
slice_selection="iterateKnown_restricted", resized=False
)
loader = dataset.init_dataset_loader(ano_dataset, args)
plt.rcParams['figure.dpi'] = 1000
for i in [f'./final-outputs/', f'./final-outputs/ARGS={args["arg_num"]}']:
if not os.path.exists(i):
os.makedirs(i)
# generate 22 images - ie one figure for each volume in MRI dataset
for i in range(22):
print(f"epoch {i}")
new = next(loader)
img = new["image"].to(device)
img = img.reshape(img.shape[1], 1, *args["img_size"])
img_mask = img_mask = new["mask"]
img_mask = img_mask.to(device)
slice = np.random.choice([0, 1, 2, 3], p=[0.2, 0.3, 0.3, 0.2])
# diffuse over varying frequency values
output = diff.detection_A_fixedT(
unet, img[slice, ...].reshape(1, 1, *args["img_size"]), args,
img_mask[slice, ...].reshape(1, 1, *args["img_size"])
)
fig, subplots = plt.subplots(
6, 6, sharex=True, sharey=True, constrained_layout=False, figsize=(6, 6),
gridspec_kw={'wspace': 0, 'hspace': 0}, squeeze=False
)
tempplot = fig.add_subplot(111, frameon=False)
# plot images and set label
for freq in range(6):
for out_img in range(6):
if out_img == 3:
subplots[freq][out_img].imshow(
output[6 * freq + out_img].reshape(*output.shape[-2:]).cpu().numpy(),
cmap="hot"
)
else:
# subplots[brain][noise].imshow(output[12 * brain + noise].reshape(256, 256, 1), cmap="gray")
subplots[freq][out_img].imshow(
output[6 * freq + out_img].reshape(*output.shape[-2:]).cpu().numpy(),
cmap="gray"
)
subplots[freq][out_img].tick_params(
top=False, bottom=False, left=False, right=False,
labelleft=False, labelbottom=False
)
for i, val in enumerate(["$x_0$", "$x_{250}$", "Reconstruction", "Square Error", "Prediction", "Ground Truth"]):
subplots[0][i].set_xlabel(f"{val}", fontsize=6)
subplots[0][i].xaxis.set_label_position("top")
for i in range(6):
subplots[i][0].set_ylabel(f"$2^{i + 1}={2 ** (i + 1)}$", fontsize=6)
subplots[i][0].yaxis.set_label_position("left")
plt.tick_params(labelcolor='none', which='both', top=False, left=False, bottom=False, right=False)
plt.ylabel("Starting Frequency\n", fontsize=6)
temp = os.listdir(f"./final-outputs/ARGS={args['arg_num']}")
plt.savefig(
f'./final-outputs/ARGS={args["arg_num"]}/{new["filenames"][0][-9:-4]}-frequency-attempt'
f'={len(temp) + 1}.png'
)
plt.close('all')
def gauss_varyingT_outputs():
"""
generate figure for Gaussian diffusion with lambda as 250,500,750
:return:
"""
# init model and load checkpoint
args, output = load_parameters(device)
unet = UNetModel(args['img_size'][0], args['base_channels'], channel_mults=args['channel_mults'])
betas = get_beta_schedule(args['T'], args['beta_schedule'])
diff = GaussianDiffusionModel(
args['img_size'], betas, loss_weight=args['loss_weight'],
loss_type=args['loss-type'], noise=args["noise_fn"]
)
unet.load_state_dict(output["ema"])
unet.to(device)
unet.eval()
ano_dataset = dataset.AnomalousMRIDataset(
ROOT_DIR=f'{DATASET_PATH}', img_size=args['img_size'],
slice_selection="iterateKnown_restricted", resized=False
)
loader = dataset.init_dataset_loader(ano_dataset, args)
plt.rcParams['figure.dpi'] = 1000
for i in [f'./final-outputs/', f'./final-outputs/ARGS={args["arg_num"]}']:
if not os.path.exists("./final-outputs"):
os.makedirs(i)
# generate 20 figures
for i in range(20):
print(f"epoch {i}")
# select image and slice
new = next(loader)
img = new["image"].to(device)
img = img.reshape(img.shape[1], 1, *args["img_size"])
img_mask = new["mask"]
img_mask = img_mask.to(device)
slice = np.random.choice([0, 1, 2, 3], p=[0.2, 0.3, 0.3, 0.2])
# perform diffusion with lambda as 250, 500 and 750
output_250 = diff.forward_backward(
unet, img[slice, ...].reshape(1, 1, *args["img_size"]),
see_whole_sequence="whole",
# t_distance=5, denoise_fn=args["noise_fn"]
t_distance=250, denoise_fn=args["noise_fn"]
)
output_250_images, mse_threshold_250 = make_prediction(
img[slice, ...].reshape(1, 1, *args["img_size"]), output_250[-1].to(device),
img_mask[slice, ...].reshape(1, 1, *args["img_size"]), output_250[251 // 2].to(device)
)
output_500 = diff.forward_backward(
unet, img[slice, ...].reshape(1, 1, *args["img_size"]),
see_whole_sequence="whole",
# t_distance=5, denoise_fn=args["noise_fn"]
t_distance=500, denoise_fn=args["noise_fn"]
)
output_500_images, mse_threshold_500 = make_prediction(
img[slice, ...].reshape(1, 1, *args["img_size"]), output_500[-1].to(device),
img_mask[slice, ...].reshape(1, 1, *args["img_size"]), output_500[501 // 2].to(device)
)
output_750 = diff.forward_backward(
unet, img[slice, ...].reshape(1, 1, *args["img_size"]),
see_whole_sequence="whole",
# t_distance=5, denoise_fn=args["noise_fn"]
t_distance=750, denoise_fn=args["noise_fn"]
)
output_750_images, mse_threshold_750 = make_prediction(
img[slice, ...].reshape(1, 1, *args["img_size"]), output_750[-1].to(device),
img_mask[slice, ...].reshape(1, 1, *args["img_size"]), output_750[751 // 2].to(device)
)
# x_0,x_t,\hat{x}_0,se,se_threshold,ground truth
# output figure manually
temp = os.listdir(f"./final-outputs/ARGS={args['arg_num']}")
fig, subplots = plt.subplots(
3, 6, sharex=True, sharey=True, constrained_layout=False, figsize=(6, 3),
squeeze=False,
gridspec_kw={'wspace': 0, 'hspace': 0}
)
tempplot = fig.add_subplot(111, frameon=False)
for i in range(3):
subplots[i][0].imshow(img[slice, ...].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[0][1].imshow(output_250[251 // 2].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[0][2].imshow(output_250[-1].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[0][3].imshow(output_250_images[3].reshape(*args["img_size"]).cpu().numpy(), cmap="hot")
subplots[0][4].imshow(output_250_images[4].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[0][5].imshow(output_250_images[5].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[1][1].imshow(output_500[501 // 2].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[1][2].imshow(output_500[-1].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[1][3].imshow(output_500_images[3].reshape(*args["img_size"]).cpu().numpy(), cmap="hot")
subplots[1][4].imshow(output_500_images[4].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[1][5].imshow(output_500_images[5].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[2][1].imshow(output_750[751 // 2].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[2][2].imshow(output_750[-1].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[2][3].imshow(output_750_images[3].reshape(*args["img_size"]).cpu().numpy(), cmap="hot")
subplots[2][4].imshow(output_750_images[4].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
subplots[2][5].imshow(output_750_images[5].reshape(*args["img_size"]).cpu().numpy(), cmap="gray")
# set labels and save
for i in range(3):
for j in range(6):
subplots[i][j].tick_params(
top=False, bottom=False, left=False, right=False,
labelleft=False, labelbottom=False
)
for i, val in enumerate(["$x_0$", "$x_t$", "Reconstruction", "Square Error", "Prediction", "Ground Truth"]):
subplots[0][i].set_xlabel(f"{val}", fontsize=6)
subplots[0][i].xaxis.set_label_position("top")
for i, val in enumerate([250, 500, 750]):
subplots[i][0].set_ylabel(f"$x_{{{val}}}$", fontsize=6)
subplots[i][0].yaxis.set_label_position("left")
plt.tick_params(labelcolor='none', which='both', top=False, left=False, bottom=False, right=False)
plt.savefig(
f'./final-outputs/ARGS={args["arg_num"]}/{new["filenames"][0][-9:-4]}-Gauss-attempt'
f'={len(temp) + 1}.png'
)
plt.close('all')
def make_gan_outputs():
import Comparative_models.CE as CE
import detection
# Figure for context encoder model
args, output = load_parameters(device)
args["Batch_Size"] = 1
netG = CE.Generator(start_size=args['img_size'][0], out_size=args['inpaint_size'], dropout=args["dropout"])
# load params and dataset
netG.load_state_dict(output["generator_state_dict"])
netG.to(device)
netG.eval()
ano_dataset = dataset.AnomalousMRIDataset(
ROOT_DIR=f'{DATASET_PATH}', img_size=args['img_size'],
slice_selection="iterateKnown_restricted", resized=False
)
loader = dataset.init_dataset_loader(ano_dataset, args)
# init args
overlapSize = args['overlap']
input_cropped = torch.FloatTensor(args['Batch_Size'], 1, 256, 256)
input_cropped = input_cropped.to(device)
for i in [f'./final-outputs/', f'./final-outputs/ARGS={args["arg_num"]}']:
if not os.path.exists("./final-outputs"):
os.makedirs(i)
# output 22 images
for i in range(22):
predictions = []
# randomly select number of mri images per figure
rows = np.random.choice([1, 2, 3, 4, 5, 8], p=[0.3, 0.3, 0.1, 0.1, 0.15, 0.05])
print(f"epoch {i}, rows @ epoch: {rows}")
for k in range(rows):
print(k)
new = next(loader)
img = new["image"]
img = img.reshape(img.shape[1], 1, *args["img_size"])
img_mask = img_mask = new["mask"]
slice = np.random.choice([0, 1, 2, 3], p=[0.15, 0.35, 0.35, 0.15])
img_mask = img_mask[slice, ...].reshape(1, 1, *args["img_size"]).to(device)
img = img[slice, ...].reshape(1, 1, *args["img_size"]).to(device)
# x_cpu = new["image"]
# x = x_cpu.to(device)
# B,C,W,H
# reconstruct image
if args['type'] == 'sliding':
recon_image = detection.ce_sliding_window(img, netG, input_cropped, args)
else:
input_cropped.resize_(img.size()).copy_(img)
recon_image = input_cropped.clone()
with torch.no_grad():
input_cropped.resize_(img.size()).copy_(img)
input_cropped[:, 0,
args['img_size'][0] // 4 + overlapSize:
args['inpaint_size'] + args['img_size'][0] // 4 - overlapSize,