This repository has been archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.py
1455 lines (1167 loc) · 50.2 KB
/
workflow.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
"""
Covalent-wrapped execution of a machine learning model for detecting
anomalous tissue in MRI scans of human brains.
This script is adapted from the following code:
https://github.com/mateuszbuda/brain-segmentation-pytorch
https://www.kaggle.com/code/mateuszbuda/brain-segmentation-pytorch/script
Input data can be downloaded here:
https://www.kaggle.com/datasets/mateuszbuda/lgg-mri-segmentation
Original author: Mateusz Buda (https://www.kaggle.com/mateuszbuda)
Adapted by: Ara Ghukasyan (https://github.com/araghukas) @ AgnostiqHQ
Adapted date: Jan 3, 2023
"""
import os
import random
import sys
from argparse import ArgumentParser, Namespace
from collections import OrderedDict
from dataclasses import dataclass, field
from datetime import datetime
from itertools import product
from pathlib import Path
from time import perf_counter
from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple
from zipfile import ZipFile
import boto3
import cloudpickle as pickle
import covalent as ct
import matplotlib.pyplot as plt
import numpy as np
import torch
from matplotlib.backends.backend_agg import FigureCanvasAgg
from skimage.exposure import rescale_intensity
from skimage.io import imread, imsave
from skimage.transform import rescale, resize, rotate
from torch import nn, optim
from torch.utils.data import DataLoader
from torchvision.transforms import Compose
_TIMESTAMP = datetime.now().strftime("%m-%d-%Y_%H-%M-%S-%f")
# ==============================================================================
# ADAPTED CODE
# ==============================================================================
def crop_sample(v, m):
"""threshold and resize volume-mask pair"""
v[v < np.max(v) * 0.1] = 0
z_projection = np.max(np.max(np.max(v, axis=-1), axis=-1), axis=-1)
z_nonzero = np.nonzero(z_projection)
z_min = np.min(z_nonzero)
z_max = np.max(z_nonzero) + 1
y_projection = np.max(np.max(np.max(v, axis=0), axis=-1), axis=-1)
y_nonzero = np.nonzero(y_projection)
y_min = np.min(y_nonzero)
y_max = np.max(y_nonzero) + 1
x_projection = np.max(np.max(np.max(v, axis=0), axis=0), axis=-1)
x_nonzero = np.nonzero(x_projection)
x_min = np.min(x_nonzero)
x_max = np.max(x_nonzero) + 1
v = v[z_min:z_max, y_min:y_max, x_min:x_max]
m = m[z_min:z_max, y_min:y_max, x_min:x_max]
return v, m
def pad_sample(v, m):
"""add padding to volume-mask pair"""
a = v.shape[1]
b = v.shape[2]
if a == b:
return v, m
diff = (max(a, b) - min(a, b)) / 2.0
if a > b:
padding = ((0, 0), (0, 0), (int(np.floor(diff)), int(np.ceil(diff))))
else:
padding = ((0, 0), (int(np.floor(diff)), int(np.ceil(diff))), (0, 0))
m = np.pad(m, padding, mode="constant", constant_values=0)
padding = padding + ((0, 0),)
v = np.pad(v, padding, mode="constant", constant_values=0)
return v, m
def resize_sample(v, m, size: int):
"""resize and volume and mask images"""
v_shape = v.shape
out_shape = (v_shape[0], size, size)
m = resize(m,
output_shape=out_shape,
order=0,
mode="constant",
cval=0,
anti_aliasing=False)
out_shape = out_shape + (v_shape[3],)
v = resize(v,
output_shape=out_shape,
order=2,
mode="constant",
cval=0,
anti_aliasing=False)
return v, m
def normalize_volume(v):
"""normalize the volume image by rescaling intensity"""
p10 = np.percentile(v, 10)
p99 = np.percentile(v, 99)
v = rescale_intensity(v, in_range=(p10, p99))
m = np.mean(v, axis=(0, 1, 2))
s = np.std(v, axis=(0, 1, 2))
v = (v - m) / s
return v
def normalize_mask(m):
"""normalize the mask image by dividing through with maximum value"""
mask_max = np.max(m)
if mask_max > 0:
return m / mask_max
return m
class Dataset:
"""
A pickle-able wrapper class for iterating over datasets.
Assumes image data is stored as PKL files.
"""
def __init__(self,
data_access_dict: Dict[str, Dict[str, Any]],
random_sampling: bool,
batch_size: int,
shuffle: bool,
drop_last: bool,
random_seed: Optional[int] = None):
"""Note: always random sampling; equal weights"""
self._random_sampling = random_sampling
self._batch_size = batch_size
self._shuffle = shuffle if shuffle else None
self._drop_last = drop_last
self._random_seed = random_seed # also use in `_init_data`
# initialize data
self._build_access_index(data_access_dict["volumes"])
self._init_data(self._get_iterator(data_access_dict["volumes"],
data_access_dict["masks"],
data_access_dict["weights"]))
def _build_access_index(self, volumes: Dict[str, List[Path]]) -> None:
slices = []
patients = []
for patient in sorted(volumes):
if len(volumes[patient]) == 0:
print(f"no volumes for {patient}")
continue
indices = []
for i, volume_path in enumerate(volumes[patient]):
idx = int(volume_path.name.rsplit("_", maxsplit=1)[1].split(".")[0])
patients.append(patient)
indices.append((i, idx))
slices.extend(indices)
self._slices = np.array(slices)
self._patients = np.array(patients)
def _get_iterator(self,
volumes: Dict[str, List[Path]],
masks: Dict[str, List[Path]],
weights: Dict[str, List[float]]) -> Iterator:
np.random.seed(self._random_seed)
# build flattened list of slice indices
rand_slices: List[Tuple[int, int]] = []
rand_patients: List[str] = []
for patient in np.unique(self._patients):
locs = self._patients == patient
indices = np.array([s for i, s in enumerate(self._slices) if locs[i]])
if self._random_sampling:
rand_idx = np.random.choice(range(indices.shape[0]),
size=indices.shape[0],
p=weights[patient])
indices = indices[rand_idx]
rand_patients.extend([patient] * indices.shape[0])
rand_slices.extend(indices)
# construct data store
data = []
for patient, index in zip(rand_patients, rand_slices):
data.append([str(volumes[patient][index[0]]),
str(masks[patient][index[0]])])
self._slices = np.array(rand_slices)
self._patients = np.array(rand_patients)
return iter(DataLoader(data, # type: ignore
num_workers=1,
batch_size=self._batch_size,
shuffle=self._shuffle,
drop_last=self._drop_last))
def _init_data(self, iterator):
"""populate the data store"""
self._data_store = []
x = next(iterator, None)
while x is not None:
self._data_store.append(x)
x = next(iterator, None)
self._length = len(self._data_store)
@property
def batch_size(self):
"""images batch size"""
return self._batch_size
@property
def slices(self):
"""1D array of slice indices"""
return np.array(self._slices)
@property
def patients(self):
"""1D array of patient ids"""
return np.array(self._patients)
@property
def patient_index(self):
"""2D array of (patient_id, slice_number)"""
return np.stack((self._patients, self._slices[:, 1])).T
def __getitem__(self, idx):
# tuples f `batch_size` many volume and matching mask files
volume_paths, mask_paths = self._data_store[idx]
vm_pairs = []
for vp, mp in zip(volume_paths, mask_paths):
with open(vp, "rb") as volume_file_obj:
v = pickle.load(volume_file_obj)
with open(mp, "rb") as mask_file_obj:
m = pickle.load(mask_file_obj)
vm_pairs.append((v, m))
vm_pairs = (np.stack([v[0] for v in vm_pairs]), np.stack([v[1] for v in vm_pairs]))
vm_pairs = (torch.from_numpy(vm_pairs[0].transpose(0, 3, 1, 2).astype(np.float32)),
torch.from_numpy(vm_pairs[1].transpose(0, 3, 1, 2).astype(np.float32)))
return vm_pairs
class Scale:
"""scaling transformation"""
def __init__(self, scale):
self.scale = scale
def __call__(self, sample):
image, mask = sample
img_size = image.shape[0]
scale = np.random.uniform(low=1.0 - self.scale, high=1.0 + self.scale)
image = rescale(image,
(scale, scale),
channel_axis=-1,
preserve_range=True,
mode="constant",
anti_aliasing=False)
mask = rescale(mask,
(scale, scale),
order=0,
channel_axis=-1,
preserve_range=True,
mode="constant",
anti_aliasing=False)
if scale < 1.0:
diff = (img_size - image.shape[0]) / 2.0
padding = ((int(np.floor(diff)), int(np.ceil(diff))),) * 2 + ((0, 0),)
image = np.pad(image, padding, mode="constant", constant_values=0)
mask = np.pad(mask, padding, mode="constant", constant_values=0)
else:
x_min = (image.shape[0] - img_size) // 2
x_max = x_min + img_size
image = image[x_min:x_max, x_min:x_max, ...]
mask = mask[x_min:x_max, x_min:x_max, ...]
return image, mask
class Rotate:
"""rotation transformation"""
def __init__(self, angle):
self.angle = angle
def __call__(self, sample):
image, mask = sample
angle = np.random.uniform(low=-self.angle, high=self.angle)
image = rotate(image,
angle,
resize=False,
preserve_range=True,
mode="constant")
mask = rotate(mask,
angle,
resize=False,
order=0,
preserve_range=True,
mode="constant")
return image, mask
class HorizontalFlip:
"""transformation that randomly flips the image"""
def __init__(self, flip_prob):
self.flip_prob = flip_prob
def __call__(self, sample):
image, mask = sample
if np.random.rand() > self.flip_prob:
return image, mask
image = np.fliplr(image).copy()
mask = np.fliplr(mask).copy()
return image, mask
def get_transform(scale: float, angle: float, flip_prob: float) -> Compose:
"""A composition of pre-defined image transforms"""
transform_list = []
if scale is not None:
transform_list.append(Scale(scale))
if angle is not None:
transform_list.append(Rotate(angle))
if flip_prob is not None:
transform_list.append(HorizontalFlip(flip_prob))
return Compose(transform_list)
class UNet(nn.Module):
"""Neural network definition"""
def __init__(self, in_channels=3, out_channels=1, init_features=32):
super().__init__()
features = init_features
self.encoder1 = UNet._block(in_channels, features, name="enc1")
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.encoder2 = UNet._block(features, features * 2, name="enc2")
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.encoder3 = UNet._block(features * 2, features * 4, name="enc3")
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.encoder4 = UNet._block(features * 4, features * 8, name="enc4")
self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2)
self.bottleneck = UNet._block(features * 8, features * 16, name="bottleneck")
self.upconv4 = nn.ConvTranspose2d(features * 16,
features * 8,
kernel_size=2,
stride=2)
self.decoder4 = UNet._block((features * 8) * 2, features * 8, name="dec4")
self.upconv3 = nn.ConvTranspose2d(features * 8,
features * 4,
kernel_size=2,
stride=2)
self.decoder3 = UNet._block((features * 4) * 2, features * 4, name="dec3")
self.upconv2 = nn.ConvTranspose2d(features * 4,
features * 2,
kernel_size=2,
stride=2)
self.decoder2 = UNet._block((features * 2) * 2, features * 2, name="dec2")
self.upconv1 = nn.ConvTranspose2d(features * 2,
features,
kernel_size=2,
stride=2)
self.decoder1 = UNet._block(features * 2, features, name="dec1")
self.conv = nn.Conv2d(in_channels=features,
out_channels=out_channels,
kernel_size=1)
def forward(self, x):
"""execute when called"""
enc1 = self.encoder1(x)
enc2 = self.encoder2(self.pool1(enc1))
enc3 = self.encoder3(self.pool2(enc2))
enc4 = self.encoder4(self.pool3(enc3))
bottleneck = self.bottleneck(self.pool4(enc4))
dec4 = self.upconv4(bottleneck)
dec4 = torch.cat((dec4, enc4), dim=1)
dec4 = self.decoder4(dec4)
dec3 = self.upconv3(dec4)
dec3 = torch.cat((dec3, enc3), dim=1)
dec3 = self.decoder3(dec3)
dec2 = self.upconv2(dec3)
dec2 = torch.cat((dec2, enc2), dim=1)
dec2 = self.decoder2(dec2)
dec1 = self.upconv1(dec2)
dec1 = torch.cat((dec1, enc1), dim=1)
dec1 = self.decoder1(dec1)
return torch.sigmoid(self.conv(dec1))
@staticmethod
def _block(in_channels, features, name):
od = OrderedDict([(name + "conv1",
nn.Conv2d(in_channels=in_channels,
out_channels=features,
kernel_size=3,
padding=1,
bias=False)),
(name + "norm1",
nn.BatchNorm2d(num_features=features)),
(name + "relu1",
nn.ReLU(inplace=True)),
(name + "conv2",
nn.Conv2d(in_channels=features,
out_channels=features,
kernel_size=3,
padding=1,
bias=False)),
(name + "norm2",
nn.BatchNorm2d(num_features=features)),
(name + "relu2",
nn.ReLU(inplace=True))])
return nn.Sequential(od)
class DiceLoss(nn.Module):
"""wrapper for dice loss"""
def __init__(self):
super().__init__()
self.smooth = 1.0
def forward(self, y_pred, y_true):
"""execute when called"""
assert y_pred.size() == y_true.size()
y_pred = y_pred[:, 0].contiguous().view(-1)
y_true = y_true[:, 0].contiguous().view(-1)
intersection = (y_pred * y_true).sum()
d = (2. * intersection + self.smooth)
d /= (y_pred.sum() + y_true.sum() + self.smooth)
return 1. - d
def gray2rgb(image):
"""converts a gray image to an rgb image"""
w, h = image.shape
image += np.abs(np.min(image))
image_max = np.abs(np.max(image))
if image_max > 0:
image /= image_max
ret = np.empty((w, h, 3), dtype=np.uint8)
ret[:, :, 2] = ret[:, :, 1] = ret[:, :, 0] = image * 255
return ret
def outline(image, mask, color):
"""draws an outline on the image from hatch in the mask"""
mask = np.round(mask)
yy, xx = np.nonzero(mask)
for y, x in zip(yy, xx):
if 0.0 < np.mean(mask[max(0, y - 1): y + 2, max(0, x - 1): x + 2]) < 1.0:
image[max(0, y): y + 1, max(0, x): x + 1] = color
return image
def dsc(y_pred, y_true):
"""basic dice loss function between two arrays"""
y_pred = np.round(y_pred).astype(int)
y_true = np.round(y_true).astype(int)
y_sum = (np.sum(y_pred) + np.sum(y_true))
loss = np.sum(y_pred[y_true == 1]) * 2.0
if y_sum > 0:
loss /= y_sum
return loss
def dsc_distribution(volumes):
"""dice loss function by patient"""
dsc_dict = {}
for p in volumes:
y_pred = volumes[p][1]
y_true = volumes[p][2]
dsc_dict[p] = dsc(y_pred, y_true)
return dsc_dict
def dsc_per_volume(validation_pred, validation_true, patient_index):
"""dice losses for each slice for a every patient"""
dsc_list = []
unique_patients = np.unique(patient_index[:, 0])
num_slices = [len(patient_index[patient_index == p]) for p in unique_patients]
index = 0
for nl in num_slices:
y_pred = np.array(validation_pred[index: index + nl])
y_true = np.array(validation_true[index: index + nl])
dsc_list.append(dsc(y_pred, y_true))
index += nl
return dsc_list
def postprocess_per_volume(input_list, pred_list, true_list, patient_index, patients):
"""input, predictions, and ground truth over all results for each patient"""
volumes = {}
unique_patients = np.unique(patients)
num_slices = [len(patient_index[patient_index == p]) for p in unique_patients]
index = 0
for p, sl in enumerate(num_slices):
volume_in = np.array(input_list[index: index + sl])
volume_pred = np.round(np.array(pred_list[index: index + sl])).astype(int)
volume_true = np.array(true_list[index: index + sl])
volumes[unique_patients[p]] = (volume_in, volume_pred, volume_true)
index += sl
return volumes
def plot_dsc(dsc_dist: dict, output_dir: Path) -> np.ndarray:
"""makes horizontal bar graph of dice coefficients by patient ID"""
y_positions = np.arange(len(dsc_dist))
dsc_dist = sorted(dsc_dist.items(), key=lambda x: x[1])
values = [x[1] for x in dsc_dist]
labels = [str(x[0]) for x in dsc_dist]
labels = ["_".join(l.split("_")[1:-1]) for l in labels]
fig = plt.figure(figsize=(12, 8))
canvas = FigureCanvasAgg(fig)
plt.barh(y_positions, values, align="center", color="skyblue")
plt.yticks(y_positions, labels)
plt.xticks(np.arange(0.0, 1.0, 0.1))
plt.xlim([0.0, 1.0])
plt.gca().axvline(np.mean(values), color="tomato", linewidth=2)
plt.gca().axvline(np.median(values), color="forestgreen", linewidth=2)
plt.xlabel("Dice coefficient", fontsize="x-large")
plt.gca().xaxis.grid(color="silver", alpha=0.5, linestyle="--", linewidth=1)
plt.tight_layout()
canvas.draw()
plt.close()
s, (width, height) = canvas.print_to_buffer()
# also create record file
with open(output_dir / "dsc_dist.csv", "w", encoding="utf8") as f:
f.write("patient_id,dsc\n")
for patient_id, _dsc in reversed(dsc_dist):
f.write(f"{patient_id}, {_dsc}\n")
f.write(f"mean, {sum(x[1] for x in dsc_dist) / len(dsc_dist)}\n")
return np.frombuffer(s, np.uint8).reshape((height, width, 4))
def get_access_dict(images_dir: Path,
extension: str,
split_fraction: float = 0.75) -> Dict[str, Dict[str, Dict[str, List[Any]]]]:
"""
Find all image data files with the given extension and assign for training/validation.
Returns a dictionary:
{
"primary": {
"volumes": volumes_dict_train,
"masks": masks_dict_train,
"weights": weights_dict_train
},
"secondary": {
"volumes": volumes_dict_valid,
"masks": masks_dict_valid,
"weights": weights_dict_valid
}
}
"""
# find all mask files
masks = []
for mask_path in images_dir.rglob(f"*_mask.{extension}"):
patient_id = mask_path.parent.name # ex: TCGA_HT_7693_19950520
mp = mask_path.relative_to(mask_path.parent.parent.parent)
masks.append((patient_id, mp))
# assign train/validation subsets
n_masks = len(masks)
n_train = int(split_fraction * n_masks)
indices = set(range(n_masks))
indices_train = set(random.sample(indices, n_train))
indices_valid = indices - indices_train
# convert to sorted lists of indices
indices_train = sorted(indices_train)
indices_valid = sorted(indices_valid)
# build mask and volume dictionaries by patient id for training
masks_dict_train: Dict[str, List[Path]] = {}
for patient_id, mp in [masks[i] for i in indices_train]:
if patient_id in masks_dict_train:
masks_dict_train[patient_id].append(mp)
else:
masks_dict_train[patient_id] = [mp]
volumes_dict_train: Dict[str, List[Path]] = {}
for patient_id, mask_paths in masks_dict_train.items():
volumes = []
for mask_path in mask_paths:
volumes.append(mask_path.parent / mask_path.name.replace("_mask", ""))
volumes_dict_train[patient_id] = volumes
# compute training image weights from hatch size on each mask
weights_dict_train: Dict[str, List[float]] = {}
for patient_id, mask_paths in masks_dict_train.items():
weights_dict_train[patient_id] = []
for mask_file in mask_paths:
# have to read volume to determine weight
if extension == "pkl":
with open(mask_file, "rb") as mask_file_obj:
arr = pickle.load(mask_file_obj)
weights = arr.sum(axis=0).sum(axis=0)
else:
arr = imread(mask_file, as_gray=True)
weights_dict_train[patient_id].append(arr.sum())
normalized_weights_dict_train: Dict[str, List[float]] = {}
for patient_id, weights in weights_dict_train.items():
sum_weights = sum(weights)
if sum_weights != 0:
normalized_weights_dict_train[patient_id] = np.array(weights) / sum_weights
else:
print(f"sum of weights is 0 for patient {patient_id}")
n_w = len(weights)
normalized_weights_dict_train[patient_id] = np.ones(n_w) / n_w
# build mask and volume dictionaries by patient id for validation
masks_dict_valid: Dict[str, List[Path]] = {}
for patient_id, mp in [masks[i] for i in indices_valid]:
if patient_id in masks_dict_valid:
masks_dict_valid[patient_id].append(mp)
else:
masks_dict_valid[patient_id] = [mp]
volumes_dict_valid: Dict[str, List[Path]] = {}
for patient_id, mask_paths in masks_dict_valid.items():
volumes = []
for mask_path in mask_paths:
volumes.append(mask_path.parent / mask_path.name.replace("_mask", ""))
volumes_dict_valid[patient_id] = volumes
# compute validation image weights from hatch size on each mask
weights_dict_valid: Dict[str, List[float]] = {}
for patient_id, mask_paths in masks_dict_valid.items():
weights_dict_valid[patient_id] = []
for mask_file in mask_paths:
# have to read volume to determine weight
if extension == "pkl":
with open(mask_file, "rb") as mask_file_obj:
arr = pickle.load(mask_file_obj)
weights = arr.sum(axis=0).sum(axis=0)
else:
arr = imread(mask_file, as_gray=True)
weights_dict_valid[patient_id].append(arr.sum())
normalized_weights_dict_valid: Dict[str, List[float]] = {}
for patient_id, weights in weights_dict_valid.items():
sum_weights = sum(weights)
if sum_weights != 0:
normalized_weights_dict_valid[patient_id] = np.array(weights) / sum_weights
else:
print(f"sum of weights is 0 for patient {patient_id}")
n_w = len(weights)
normalized_weights_dict_valid[patient_id] = np.ones(n_w) / n_w
return {
"primary": {
"volumes": volumes_dict_train,
"masks": masks_dict_train,
"weights": normalized_weights_dict_train
},
"secondary": {
"volumes": volumes_dict_valid,
"masks": masks_dict_valid,
"weights": normalized_weights_dict_valid
}
}
# ==============================================================================
# PARSER
# ==============================================================================
_PARSER = ArgumentParser(
prog="python workflow2.py",
description="dispatch the ML workflow using covalent and AWSBatch"
)
_PARSER.add_argument("-B",
help="list of batch sizes, number of images per step",
type=int,
nargs="+",
metavar="b",
default=[16])
_PARSER.add_argument("-E",
help="list of number of epochs",
type=int,
nargs="+",
metavar="e",
default=[10])
_PARSER.add_argument("-Z",
help="image sizes to resize images to",
type=int,
nargs="+",
metavar="e",
default=[112])
_PARSER.add_argument("-L",
help="learnings rate for Adam optimizer",
type=float,
nargs="+",
metavar="l",
default=[0.0001])
_PARSER.add_argument("-s",
help="scaling factor for `Scale` transformation",
type=float,
metavar="AUG_SCALE",
default=0.05)
_PARSER.add_argument("-g",
help="rotation angle for `Rotate` transformation",
type=float,
metavar="AUG_ANGLE",
default=15)
_PARSER.add_argument("-d",
help="data directory name",
type=str,
metavar="NAME",
default="data_full")
_PARSER.add_argument("--dispatch_id",
help="provide dispatch ID in attempt to retrieve or reconnect",
type=str,
metavar="ID",
default=None)
_PARSER.add_argument("--seed",
help="random seed to fix data selection and order",
type=int,
metavar="N",
default=None)
_PARSER.add_argument("--local",
help="run script locally, without covalent",
action="store_true",
default=False)
# ==============================================================================
# PATHS
# ==============================================================================
CWD = Path(os.environ["PWD"]).resolve()
OUTPUT_DIR = Path(f"outputs_{_TIMESTAMP}")
UNET_FILE = Path(f"unet_{_TIMESTAMP}.pt")
# ==============================================================================
# EXECUTOR
# ==============================================================================
BUCKET_NAME = "my-s3-bucket" # use same bucket for both executor and data
_EXECUTOR_KWARGS = {
"credentials": "~/.aws/credentials",
"region": "us-east-1",
"s3_bucket_name": BUCKET_NAME,
"batch_queue": "my-batch-queue",
"batch_job_log_group_name": "my-log-group",
}
BATCH_GPU_EXECUTOR = ct.executor.AWSBatchExecutor(
num_gpus=1,
vcpu=2,
memory=4,
time_limit=4 * 3600,
base_uri="docker.io/araghukas/covalent-executor-gpu:cuda-11.4.1",
**_EXECUTOR_KWARGS,
)
BATCH_CPU_EXECUTOR = ct.executor.AWSBatchExecutor(
num_gpus=0,
vcpu=2,
memory=4,
time_limit=2 * 3600,
** _EXECUTOR_KWARGS
)
# ==============================================================================
# HELPERS
# ==============================================================================
def _download_file(client, key: str, filename: str) -> None:
"""download from S3 bucket"""
print(f"downloading: s3://{BUCKET_NAME}/{key} -> {filename}")
client.download_file(Bucket=BUCKET_NAME,
Key=key,
Filename=filename)
def _upload_file(client, filename: str, key: str) -> None:
"""upload to S3 bucket"""
print(f"uploading: {filename} -> s3://{BUCKET_NAME}/{key}")
client.upload_file(Filename=filename,
Bucket=BUCKET_NAME,
Key=key)
def _download_and_unzip(client,
zip_filename: str) -> None:
"""download from S3 bucket and unzip"""
# download from s3 bucket
_download_file(client, zip_filename, zip_filename)
# extract contents
print(f"extracting from: {zip_filename}")
with ZipFile(zip_filename, "r") as zipped_file:
zipped_file.extractall()
def _zip_and_upload(client,
dir_path: Path,
key: str = "",
pattern: str = "*") -> None:
"""zip and upload to S3 bucket"""
# compress directory
print(f"compressing: {dir_path}")
with ZipFile(f"{dir_path.name}.zip", "x") as zipped_file:
for file_path in dir_path.rglob(pattern):
zipped_file.write(file_path, file_path.parent / file_path.name)
# upload to s3 bucket
key = f"{dir_path.name}.zip" if not key else key
_upload_file(client, filename=f"{dir_path.name}.zip", key=key)
def _preprocess(volumes_dict: Dict[str, List[Path]],
masks_dict: Dict[str, List[Path]],
image_size: int,
transform: Optional[Callable],
save_to_dir: Path) -> None:
"""pre-processes images into data arrays and pickle the data arrays"""
print(f"number of volume/mask pairs: {sum(len(v) for _,v in volumes_dict.items())}")
for patient_id in list(volumes_dict):
volume_paths = volumes_dict[patient_id]
mask_paths = masks_dict[patient_id]
if not (volume_paths and mask_paths):
print(f"ignored patient {patient_id}")
continue
print(f"preprocessing patient {patient_id} ({len(volume_paths)} slices)")
slice_numbers = []
vm_pairs = []
for vf, mf in zip(volume_paths, mask_paths):
slice_numbers.append(int(vf.name.rsplit("_", maxsplit=1)[1].split(".")[0]))
v = imread(vf)
m = imread(mf, as_gray=True)
vm_pairs.append((v, m))
vm_pairs = (
np.stack([v[0] for v in vm_pairs]),
np.stack([v[1] for v in vm_pairs])
)
vm_pairs = crop_sample(*vm_pairs)
vm_pairs = pad_sample(*vm_pairs)
vm_pairs = resize_sample(*vm_pairs, image_size)
vm_pairs = normalize_volume(vm_pairs[0]), normalize_mask(vm_pairs[1])
vm_pairs = vm_pairs[0], vm_pairs[1][..., np.newaxis]
if transform:
vs, ms = vm_pairs
for i in range(vs.shape[0]):
vs[i], ms[i] = transform((vs[i], ms[i]))
vm_pairs = vs, ms
# save images
patient_dir = save_to_dir / patient_id
patient_dir.mkdir()
for v_array, m_array, sn in zip(*vm_pairs, slice_numbers):
with open(patient_dir / f"{patient_id}_{sn}.pkl", "wb") as v_file:
pickle.dump(v_array, v_file)
with open(patient_dir / f"{patient_id}_{sn}_mask.pkl", "wb") as m_file:
pickle.dump(m_array, m_file)
def _get_device(assert_gpu: bool) -> torch.device:
"""create and return a `device` object that uses GPU backend if available"""
if torch.cuda.is_available():
device = torch.device("cuda:0")
elif assert_gpu:
raise RuntimeError("GPU backend not available.")
else:
print(">>WARNING!<< GPU backend not available.")
device = torch.device("cpu")
return device
def _get_unet_and_optimizer(device: torch.device,
learning_rate: float) -> Tuple[UNet, optim.Adam]:
"""initialize the model and associate it with a device and optimizer"""
unet = UNet()
unet.to(device)
optimizer = optim.Adam(unet.parameters(), lr=learning_rate)
return unet, optimizer
@dataclass
class InputPaths:
"""preprocessed image output paths container"""
train: Path
valid: Path
@dataclass(frozen=True)
class Params:
"""a container for hyperparameter values"""
batch_size: int
epochs: int
learning_rate: float
image_size: int
@dataclass