-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeras-starter-kit-unet-train-on-full-dataset.py
1088 lines (886 loc) · 32.7 KB
/
keras-starter-kit-unet-train-on-full-dataset.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
#!/usr/bin/env python
# coding: utf-8
# # Keras starter kit [full training set, UNet]
# ## Note
#
# - 高い解像度でリサイズすることはprecisionの向上につながるため有効
# - seresnextでチャネル間の相関を見れるので有効
# - 文字の太さ・書き方に大きくバリアントがあるので、横の相関よりもdepthの相関の方が大事かもしれない
# - 深さに関して、隣り合う深さ同士に大きな変化はない
# - fpをfnよりも小さくしたい
# - valid_scoreはCFG.thd依存
# - valid_lossはCFG.loss1/loss2依存
# - encoder内でdepthをクロップしてバッチで繋げた方が精度が良い
# - maskに対しては有効ではないが、labelは的確なラベルを用いることで精度が向上
# - depthは22~34 or 24 ~ 36
# - 画像のサイズは大きい方が良いのか?(BUFFER / SHARED_HEIGHT)
# - 比率を同じにして試してみる
# - SHARED_HEIGHTをデカくするとデータがメモリに載らなさそうだった
# - NetのweightsにBatchNormはない方が良い
# - BUFFER:strideを160:96から160:80にしたら精度が落ちた
# - trainの精度は上がっていた
# - BUFFERに対してstrideが細かすぎると過学習に繋がっているのかもしれない
# - チャネル数を増やして入力画像解像度を上げた
# - 文字が見えなくなるよりも、高いthdを設定して、文字が大きく見えた方が良い(仮説)
# - 学習時にはstrideは大きくし、過学習を防ぐ。識別時にはstrideは小さくし見落としを減らす
# ## Setup
# In[1]:
import numpy as np
import torch
import torch.nn as nn
import wandb
import torchvision
import datetime
# import cupy
import albumentations as A
from albumentations.pytorch import ToTensorV2
import pytorch_lightning
import segmentation_models_pytorch as smp
import pytorch_lightning as pl
import pytorch_lightning.callbacks.model_checkpoint
import pytorch_lightning.plugins
from skimage.transform import resize as resize_ski
from pytorch_lightning.strategies.ddp import DDPStrategy
from pytorch_lightning.loggers import WandbLogger
from einops import rearrange, reduce, repeat
import torch.nn.functional as F
import segmentation_models_pytorch as smp
from segmentation_models_pytorch.decoders.unet.decoder import UnetDecoder, DecoderBlock
from timm.models.resnet import (
resnet10t,
resnet34d,
resnet50d,
resnet14t,
seresnext26d_32x4d,
seresnext50_32x4d,
)
import os
import torch.utils.data
from dataclasses import dataclass
from scipy.ndimage import distance_transform_edt
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
import glob
import time
import PIL.Image as Image
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import matplotlib.patches as patches
from sklearn.model_selection import KFold
from tqdm import tqdm
import cv2
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
@dataclass
class CFG:
# Data config
# DATA_DIR = '/kaggle/input/vesuvius-challenge-ink-detection/'
# DATA_DIR = '/home/fummicc1/codes/competitions/kaggle-ink-detection'
DATA_DIR = "/home/fummicc1/codes/Kaggle/kaggle-ink-detection"
BUFFER = 160 # Half-size of papyrus patches we'll use as model inputs
STRIDE = 96
# Z_LIST = list(range(0, 20, 5)) + list(range(22, 34)) # Offset of slices in the z direction
Z_LIST = list(range(24, 36))
# Z_LIST = list(range(0, 24, 8)) + list(range(24, 36, 2)) + list(range(36, 64, 10))
Z_DIM = len(
Z_LIST
) # Number of slices in the z direction. Max value is 64 - Z_START
SHARED_HEIGHT = 4480 # Max height to resize all papyrii
# Model config
BATCH_SIZE = 24
device = torch.device("cuda")
threshold = 0.5
num_workers = 8
exp = 1e-7
mask_padding = BUFFER
num_epochs = 30
lr = 5e-4
eta_min_lr = 1e-6
WANDB_NOTE = "augにノイズを追加. Netでバイリニアではなくバイキュービックを使用"
loss1_alpha = 0.5
loss1_beta = 0.5
loss1_weight = 0.5
loss2_alpha = 0.5
loss2_beta = 0.5
loss2_weight = 0.5
lr_scheduler = optim.lr_scheduler.CosineAnnealingWarmRestarts
loss1 = smp.losses.TverskyLoss(
smp.losses.BINARY_MODE,
log_loss=False,
from_logits=True,
smooth=1e-7,
alpha=loss1_alpha,
beta=loss1_beta,
)
loss2 = smp.losses.TverskyLoss(
smp.losses.BINARY_MODE,
log_loss=False,
from_logits=True,
smooth=1e-7,
alpha=loss2_alpha,
beta=loss2_beta,
)
use_new_label_mask = True
pretrained = True
def class2dict(c):
return {
attr: getattr(c, attr)
for attr in dir(c)
if not callable(getattr(c, attr)) and not attr.startswith("__")
}
# ## Load up the training data
# In[2]:
def resize(img):
current_height, current_width = img.shape
aspect_ratio = current_width / current_height
if CFG.SHARED_HEIGHT is None:
return img
# new_height = CFG.SHARED_HEIGHT
# pad_y = new_height - current_height
# if pad_y > 0:
# # 元画像が小さい場合は解像度を大きくしないでpaddingをつける
# img = np.pad(img, [(0, pad_y), (0, 0)], constant_values=0)
# else:
# 既に十分でかい場合はリサイズする
# 本当はpaddingしたいけど、メモリサイズが大きくなる
new_height = CFG.SHARED_HEIGHT
new_width = int(CFG.SHARED_HEIGHT * aspect_ratio)
new_size = (new_width, new_height)
# (W, H)の順で渡すが結果は(H, W)になっている
img = cv2.resize(img, new_size)
return img
def load_mask(split, index):
if index == "2a" or index == "2b":
mode = index[1]
index = "2"
img = cv2.imread(f"{CFG.DATA_DIR}/{split}/{index}/mask.png", 0) // 255
if index == "2":
h = 9456
if mode == "a":
img = img[h:, :]
elif mode == "b":
img = img[:h, :]
img = resize(img)
img = np.pad(img, 1, constant_values=0)
dist = distance_transform_edt(img)
img[dist <= CFG.mask_padding] = 0
img = img[1:-1, 1:-1]
return img
def load_labels(split, index):
if index == "2a" or index == "2b":
mode = index[1]
index = "2"
suffix = "_new" if CFG.use_new_label_mask else ""
img = cv2.imread(f"{CFG.DATA_DIR}/{split}/{index}/inklabels{suffix}.png", 0) // 255
if index == "2":
h = 9456
if mode == "a":
img = img[h:, :]
elif mode == "b":
img = img[:h, :]
img = resize(img)
return img
# In[3]:
# input shape: (H, W, C)
def rotate90(volume: np.ndarray, k=None, reverse=False):
if k:
volume = np.rot90(volume, k)
else:
volume = np.rot90(volume, 1 if not reverse else 3)
height = volume.shape[0]
width = volume.shape[1]
new_height = CFG.SHARED_HEIGHT
new_width = int(new_height * width / height)
if len(volume.shape) == 2:
return cv2.resize(volume, (new_width, new_height))
return resize_ski(volume, (new_height, new_width, volume.shape[2]))
# In[4]:
def load_volume(split, index):
if index == "2a" or index == "2b":
mode = index[1]
index = "2"
# Load the 3d x-ray scan, one slice at a time
all = sorted(glob.glob(f"{CFG.DATA_DIR}/{split}/{index}/surface_volume/*.tif"))
z_slices_fnames = [all[i] for i in range(len(all)) if i in CFG.Z_LIST]
assert len(z_slices_fnames) == CFG.Z_DIM
z_slices = []
for z, filename in tqdm(enumerate(z_slices_fnames)):
img = cv2.imread(filename, -1)
if index == "2":
h = 9456
if mode == "a":
img = img[h:, :]
elif mode == "b":
img = img[:h, :]
img = resize(img)
# img = (img / (2 ** 8)).astype(np.uint8)
img = img.astype(np.float32) // 255
z_slices.append(img)
return np.stack(z_slices, axis=-1)
# ## Create a dataset in the input volume
#
# In[5]:
def is_in_masked_zone(location, mask):
return mask[location[0], location[1]] > 0
# In[6]:
def generate_locations_ds(volume, mask, label=None, skip_zero=False):
is_in_mask_train = lambda x: is_in_masked_zone(x, mask)
# Create a list to store train locations
locations = []
# Generate train locations
volume_height, volume_width = volume.shape[:-1]
for y in range(CFG.BUFFER, volume_height - CFG.BUFFER, CFG.STRIDE):
for x in range(CFG.BUFFER, volume_width - CFG.BUFFER, CFG.STRIDE):
if (
skip_zero
and label is not None
and np.all(
label[
y - CFG.BUFFER // 2 : y + CFG.BUFFER // 2,
x - CFG.BUFFER // 2 : x + CFG.BUFFER // 2,
]
== 0
)
):
# print(f"skip location at (y: {y}, x: {x})")
continue
if is_in_mask_train((y, x)):
locations.append((y, x))
# Convert the list of train locations to a PyTorch tensor
locations_ds = np.stack(locations, axis=0)
return locations_ds
# ## Visualize some training patches
#
# Sanity check visually that our patches are where they should be.
# In[7]:
def extract_subvolume(location, volume):
global printed
y = location[0]
x = location[1]
subvolume = volume[
y - CFG.BUFFER : y + CFG.BUFFER, x - CFG.BUFFER : x + CFG.BUFFER, :
].astype(np.float32)
return subvolume
# ## SubvolumeDataset
# In[8]:
import torch
from torch.utils.data import Dataset, DataLoader
from sklearn.preprocessing import OneHotEncoder
from albumentations.core.transforms_interface import ImageOnlyTransform
class SubvolumeDataset(Dataset):
def __init__(
self,
locations,
volume,
labels,
buffer,
is_train: bool,
return_location: bool = False,
):
self.locations = locations
self.volume = volume
self.labels = labels
self.buffer = buffer
self.is_train = is_train
self.return_location = return_location
def __len__(self):
return len(self.locations)
def __getitem__(self, idx):
label = None
location = np.array(self.locations[idx])
y, x = location[0], location[1]
subvolume = extract_subvolume(location, self.volume)
if self.labels is not None:
label = self.labels[
y - self.buffer : y + self.buffer, x - self.buffer : x + self.buffer
]
label = np.stack([label], axis=-1)
# 段々meanは小さくなる
mean = np.array([0.45 - i / 100 for i in range(0, CFG.Z_DIM)]).reshape(-1, 1, 1)
# 段々stdは小さくなる
std = np.array([0.22 - i / 300 for i in range(0, CFG.Z_DIM)]).reshape(-1, 1, 1)
if self.is_train and label is not None:
transformed = A.Compose(
[
A.HorizontalFlip(p=0.4),
A.VerticalFlip(p=0.4),
A.RandomScale(p=0.4, scale_limit=0.4),
A.Transpose(p=0.4),
A.RandomRotate90(p=0.4),
A.ShiftScaleRotate(
p=0.5,
scale_limit=0.4,
),
A.OneOf(
[
# A.GaussNoise(var_limit=[5, 10]),
A.MotionBlur(blur_limit=(3, 5)),
A.MotionBlur(blur_limit=(7, 9)),
],
p=0.2,
),
# A.GridDistortion(num_steps=5, distort_limit=0.3, p=0.5),
# A.CoarseDropout(
# max_holes=1,
# max_width=int(CFG.BUFFER * 2 * 0.3),
# max_height=int(CFG.BUFFER * 2 * 0.3),
# mask_fill_value=0,
# p=0.5
# ),
A.GridDistortion(p=0.6),
# A.CoarseDropout(
# max_holes=1,
# max_width=int(self.buffer * 0.15),
# max_height=int(self.buffer * 0.15),
# mask_fill_value=0,
# p=0.5,
# ),
# A.GridDropout(p=0.15),
A.PadIfNeeded(
min_height=self.buffer * 2, min_width=self.buffer * 2
),
A.Resize(height=self.buffer * 2, width=self.buffer * 2),
]
)(image=subvolume, mask=label)
subvolume = transformed["image"]
label = transformed["mask"]
subvolume = np.transpose(subvolume, (2, 0, 1))
label = np.transpose(label, (2, 0, 1))
subvolume /= 255.0
subvolume = (subvolume - mean) / std
else:
if label is None:
subvolume = np.transpose(subvolume, (2, 0, 1))
subvolume /= 255.0
subvolume = (subvolume - mean) / std
else:
# print("subvolume in val dataset (before aug)", subvolume, file=open("before-val-aug.log", "w"))
subvolume = np.transpose(subvolume, (2, 0, 1))
label = np.transpose(label, (2, 0, 1))
subvolume /= 255.0
subvolume = (subvolume - mean) / std
# print("subvolume", subvolume)
if self.return_location:
return subvolume, location
return subvolume, label
# ## Visualize validation dataset patches
#
# Note that they are partially overlapping, since the stride is half the patch size.
# In[9]:
def visualize_dataset_patches(locations_ds, labels, mode: str, fold=0):
fig, ax = plt.subplots()
ax.imshow(labels)
for y, x in locations_ds:
patch = patches.Rectangle(
[x - CFG.BUFFER, y - CFG.BUFFER],
2 * CFG.BUFFER,
2 * CFG.BUFFER,
linewidth=2,
edgecolor="g",
facecolor="none",
)
ax.add_patch(patch)
plt.savefig(f"fold-{fold}-{mode}.png")
plt.show()
# ## Compute a trivial baseline
#
# This is the highest validation score you can reach without looking at the inputs.
# The model can be considered to have statistical power only if it can beat this baseline.
# ## Dataset check
# ## Model
# In[10]:
# ref - https://www.kaggle.com/competitions/vesuvius-challenge-ink-detection/discussion/397288
def fbeta_score(preds, targets, threshold, beta=0.5, smooth=1e-5):
preds_t = torch.where(preds > threshold, 1.0, 0.0).float()
y_true_count = targets.sum()
ctp = preds_t[targets == 1].sum()
cfp = preds_t[targets == 0].sum()
beta_squared = beta * beta
c_precision = ctp / (ctp + cfp + smooth)
c_recall = ctp / (y_true_count + smooth)
dice = (
(1 + beta_squared)
* (c_precision * c_recall)
/ (beta_squared * c_precision + c_recall + smooth)
)
return dice
# In[11]:
class SmpUnetDecoder(nn.Module):
def __init__(
self,
in_channel,
skip_channel,
out_channel,
):
super().__init__()
self.center = nn.Identity()
i_channel = [
in_channel,
] + out_channel[:-1]
s_channel = skip_channel
o_channel = out_channel
block = [
DecoderBlock(i, s, o, use_batchnorm=True, attention_type=None)
for i, s, o in zip(i_channel, s_channel, o_channel)
]
self.block = nn.ModuleList(block)
def forward(self, feature, skip):
d = self.center(feature)
decode = []
for i, block in enumerate(self.block):
s = skip[i]
d = block(d, s)
decode.append(d)
last = d
return last, decode
class Net(nn.Module):
def __init__(
self,
):
super().__init__()
self.output_type = ["inference", "loss"]
conv_dim = 64
encoder1_dim = [
conv_dim,
256,
512,
1024,
2048,
]
decoder1_dim = [
256,
128,
64,
64,
]
self.encoder1 = seresnext26d_32x4d(
pretrained=CFG.pretrained, in_chans=CFG.Z_DIM - 4
)
self.decoder1 = SmpUnetDecoder(
in_channel=encoder1_dim[-1],
skip_channel=encoder1_dim[:-1][::-1],
out_channel=decoder1_dim,
)
# -- pool attention weight
self.weight1 = nn.ModuleList(
[
nn.Sequential(
nn.Conv2d(dim, dim, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
)
for dim in encoder1_dim
]
)
self.logit1 = nn.Conv2d(decoder1_dim[-1], 1, kernel_size=1)
# --------------------------------
#
encoder2_dim = [64, 128, 256, 512] #
decoder2_dim = [
128,
64,
32,
]
self.encoder2 = resnet10t(pretrained=CFG.pretrained, in_chans=decoder1_dim[-1])
self.decoder2 = SmpUnetDecoder(
in_channel=encoder2_dim[-1],
skip_channel=encoder2_dim[:-1][::-1],
out_channel=decoder2_dim,
)
self.logit2 = nn.Conv2d(decoder2_dim[-1], 1, kernel_size=1)
def forward(self, batch):
v = batch
B, C, H, W = v.shape
vv = [v[:, i : i + CFG.Z_DIM - 4] for i in [0, 2, 4]]
K = len(vv)
x = torch.cat(vv, 0)
# x = v
# ----------------------
encoder = []
e = self.encoder1
x = e.conv1(x)
x = e.bn1(x)
x = e.act1(x)
encoder.append(x)
x = F.avg_pool2d(x, kernel_size=2, stride=2)
x = e.layer1(x)
encoder.append(x)
x = e.layer2(x)
encoder.append(x)
x = e.layer3(x)
encoder.append(x)
x = e.layer4(x)
encoder.append(x)
# print('encoder', [f.shape for f in encoder])
for i in range(len(encoder)):
e = encoder[i]
f = self.weight1[i](e)
_, c, h, w = e.shape
f = rearrange(f, "(K B) c h w -> B K c h w", K=K, B=B, h=h, w=w) #
e = rearrange(e, "(K B) c h w -> B K c h w", K=K, B=B, h=h, w=w) #
w = F.softmax(f, 1)
e = (w * e).sum(1)
encoder[i] = e
feature = encoder[-1]
skip = encoder[:-1][::-1]
last, decoder = self.decoder1(feature, skip)
logit1 = self.logit1(last)
logit1 = F.interpolate(
logit1, size=(H, W), mode="bicubic", align_corners=False, antialias=True
)
# ----------------------
x = last # .detach()
# x = F.avg_pool2d(x,kernel_size=2,stride=2)
encoder = []
e = self.encoder2
x = e.layer1(x)
encoder.append(x)
x = e.layer2(x)
encoder.append(x)
x = e.layer3(x)
encoder.append(x)
x = e.layer4(x)
encoder.append(x)
feature = encoder[-1]
skip = encoder[:-1][::-1]
last, decoder = self.decoder2(feature, skip)
logit2 = self.logit2(last)
logit2 = F.interpolate(
logit2, size=(H, W), mode="bicubic", align_corners=False, antialias=True
)
return logit1, logit2
# In[12]:
Net()
# In[13]:
tc = torch
def TTA(x: tc.Tensor, model: nn.Module):
# x.shape=(batch,c,h,w)
shape = x.shape
x = [x, *[tc.rot90(x, k=i, dims=(-2, -1)) for i in range(1, 4)]]
x = tc.cat(x, dim=0)
_, x = model(x)
x = x.reshape(4, shape[0], 1, *shape[2:])
x = [tc.rot90(x[i], k=-i, dims=(-2, -1)) for i in range(4)]
x = tc.stack(x, dim=0)
return x.mean(0)
# In[14]:
def dice_coef_torch(prob_preds, targets, beta=0.5, smooth=1e-5):
# No need to binarize the predictions
# prob_preds = torch.sigmoid(preds)
# flatten label and prediction tensors
prob_preds = prob_preds.view(-1).float()
targets = targets.view(-1).float()
intersection = (prob_preds * targets).sum()
dice = ((1 + beta**2) * intersection + smooth) / (
(beta**2) * prob_preds.sum() + targets.sum() + smooth
)
return dice
class Model(pl.LightningModule):
training_step_outputs = []
validation_step_outputs = []
test_step_outputs = [[], []]
def __init__(self, **kwargs):
super().__init__()
self.model = Net()
# self.loss1 = nn.BCEWithLogitsLoss(
# pos_weight=torch.tensor([0.7])
# )
# self.loss2 = nn.BCEWithLogitsLoss(
# pos_weight=torch.tensor([0.7])
# )
self.loss1 = CFG.loss1
self.loss2 = CFG.loss2
def forward(self, image, stage):
if stage != "train":
mask = TTA(image, self.model)
else:
mask = self.model(image)
return mask
def shared_step(self, batch, stage):
subvolumes, labels = batch
image, labels = subvolumes.float(), labels.float()
assert image.ndim == 4
h, w = image.shape[2:]
assert h % 32 == 0 and w % 32 == 0
# print("labels", labels.max(), labels.min())
assert labels.max() <= 1.0 and labels.min() >= 0
if stage == "train":
logit1, logit2 = self.forward(image, stage)
loss = CFG.loss1_weight * self.loss1(
logit1, labels
) + CFG.loss2_weight * self.loss2(logit2, labels)
elif stage == "valid":
logit2 = self.forward(image, stage)
loss = self.loss2(logit2, labels)
prob2 = torch.sigmoid(logit2)
pred_mask = (prob2 > CFG.threshold).float()
# print("pred_mask", pred_mask)
score = fbeta_score(pred_mask, labels, threshold=CFG.threshold)
tp, fp, fn, tn = smp.metrics.get_stats(
pred_mask.long(), labels.long(), mode="binary"
)
return {
"loss": loss,
"tp": tp,
"fp": fp,
"fn": fn,
"tn": tn,
"score": score,
}
def shared_epoch_end(self, outputs, stage):
# aggregate step metics
tp = torch.cat([x["tp"] for x in outputs])
fp = torch.cat([x["fp"] for x in outputs])
fn = torch.cat([x["fn"] for x in outputs])
tn = torch.cat([x["tn"] for x in outputs])
loss = torch.mean(torch.Tensor([x["loss"] for x in outputs]))
fbeta_score = torch.mean(torch.Tensor([x["score"] for x in outputs]))
per_image_iou = smp.metrics.iou_score(
tp, fp, fn, tn, reduction="micro-imagewise"
)
dataset_iou = smp.metrics.iou_score(tp, fp, fn, tn, reduction="micro")
metrics = {
f"{stage}_per_image_iou": per_image_iou,
f"{stage}_dataset_iou": dataset_iou,
f"{stage}_loss": 10000 if loss.item() == 0 else loss.item(),
f"{stage}_tp": tp.sum().int().item(),
f"{stage}_fp": fp.sum().int().item(),
f"{stage}_fn": fn.sum().int().item(),
f"{stage}_tn": tn.sum().int().item(),
f"{stage}_score": fbeta_score.item(),
}
self.log_dict(metrics, prog_bar=True, sync_dist=True)
def training_step(self, batch, batch_idx):
out = self.shared_step(batch, "train")
self.training_step_outputs.append(out)
return out
def on_train_epoch_end(self):
out = self.shared_epoch_end(self.training_step_outputs, "train")
self.training_step_outputs.clear()
return out
def validation_step(self, batch, batch_idx):
out = self.shared_step(batch, "valid")
self.validation_step_outputs.append(out)
return out
def on_validation_epoch_end(self):
out = self.shared_epoch_end(self.validation_step_outputs, "valid")
self.validation_step_outputs.clear()
return out
def test_step(self, batch, batch_idx):
global predictions_map, predictions_map_counts
patch_batch, loc_batch = batch
loc_batch = loc_batch.long()
patch_batch = patch_batch.float()
predictions = self.forward(patch_batch, "test")
predictions = predictions.sigmoid()
predictions = torch.permute(predictions, (0, 2, 3, 1)).squeeze(dim=-1)
predictions = predictions.cpu().numpy()
loc_batch = loc_batch.cpu().numpy()
self.test_step_outputs[0].extend(loc_batch)
self.test_step_outputs[1].extend(predictions)
return loc_batch, predictions
def on_test_epoch_end(self):
global predictions_map, predictions_map_counts
locs = np.array(self.test_step_outputs[0])
preds = np.array(self.test_step_outputs[1])
print("locs", locs.shape)
print("preds", preds.shape)
for (y, x), pred in zip(locs, preds):
predictions_map[
y - CFG.BUFFER : y + CFG.BUFFER, x - CFG.BUFFER : x + CFG.BUFFER
] += pred
predictions_map_counts[
y - CFG.BUFFER : y + CFG.BUFFER, x - CFG.BUFFER : x + CFG.BUFFER
] += 1
predictions_map /= predictions_map_counts + CFG.exp
def configure_optimizers(self):
optimizer = optim.AdamW(self.parameters(), lr=CFG.lr)
scheduler = CFG.lr_scheduler(
optimizer,
T_0=CFG.num_epochs,
T_mult=2,
eta_min=CFG.eta_min_lr,
)
return {
"optimizer": optimizer,
"lr_scheduler": scheduler,
}
# In[15]:
if __name__ == "__main__":
pytorch_lightning.seed_everything(seed=42)
torch.set_float32_matmul_precision("high")
masks = load_mask(split="train", index=1)
labels = load_labels(split="train", index=1)
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_title("mask.png")
ax1.imshow(masks, cmap="gray")
ax2.set_title("inklabels.png")
ax2.imshow(labels, cmap="gray")
plt.show()
mask_test_a = load_mask(split="test", index="a")
mask_test_b = load_mask(split="test", index="b")
mask_train_1 = load_mask(split="train", index="1")
labels_train_1 = load_labels(split="train", index="1")
mask_train_2a = load_mask(split="train", index="2a")
labels_train_2a = load_labels(split="train", index="2a")
mask_train_2b = load_mask(split="train", index="2b")
labels_train_2b = load_labels(split="train", index="2b")
mask_train_3 = load_mask(split="train", index="3")
labels_train_3 = load_labels(split="train", index="3")
print(f"mask_test_a: {mask_test_a.shape}")
print(f"mask_test_b: {mask_test_b.shape}")
print("-")
print(f"mask_train_1: {mask_train_1.shape}")
print(f"labels_train_1: {labels_train_1.shape}")
print("-")
print(f"mask_train_2a: {mask_train_2a.shape}")
print(f"labels_train_2a: {labels_train_2a.shape}")
print("-")
print(f"mask_train_2b: {mask_train_2b.shape}")
print(f"labels_train_2b: {labels_train_2b.shape}")
print("-")
print(f"mask_train_3: {mask_train_3.shape}")
print(f"labels_train_3: {labels_train_3.shape}")
fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4)
ax1.set_title("labels_train_1")
ax1.imshow(labels_train_1, cmap="gray")
ax2.set_title("labels_train_2a")
ax2.imshow(labels_train_2a, cmap="gray")
ax3.set_title("labels_train_2b")
ax3.imshow(labels_train_2b, cmap="gray")
ax4.set_title("labels_train_3")
ax4.imshow(labels_train_3, cmap="gray")
plt.tight_layout()
plt.show()
volume_train_1 = load_volume(split="train", index=1)
print(f"volume_train_1: {volume_train_1.shape}, {volume_train_1.dtype}")
volume_train_2a = load_volume(split="train", index="2a")
print(f"volume_train_2a: {volume_train_2a.shape}, {volume_train_2a.dtype}")
volume_train_2b = load_volume(split="train", index="2b")
print(f"volume_train_2b: {volume_train_2b.shape}, {volume_train_2b.dtype}")
volume_train_3 = load_volume(split="train", index=3)
print(f"volume_train_3: {volume_train_3.shape}, {volume_train_3.dtype}")
# volume = np.concatenate([volume_train_1, volume_train_2, volume_train_3], axis=1)
# volume = np.concatenate([volume_train_1, volume_train_2], axis=1)
# print(f"total volume: {volume.shape}")
# In[ ]:
k_folds = 4
kfold = KFold(n_splits=k_folds, shuffle=True)
data_list = [
(volume_train_2a, labels_train_2a, mask_train_2a),
(volume_train_1, labels_train_1, mask_train_1),
(volume_train_2b, labels_train_2b, mask_train_2b),
(volume_train_3, labels_train_3, mask_train_3),
]
predictions_map = None
predictions_map_counts = None
for fold, (train_data, val_data) in enumerate(kfold.split(data_list)):
print(f"FOLD {fold}")
print("--------------------------------")
print("train_data", train_data)
print("val_data", val_data)
one = data_list[train_data[0]]
two = data_list[train_data[1]]
three = data_list[train_data[2]]
train_volume = np.concatenate([one[0], two[0], three[0]], axis=1)
train_label = np.concatenate([one[1], two[1], three[1]], axis=1)
train_mask = np.concatenate([one[2], two[2], three[2]], axis=1)
val_volume, val_label, val_mask = data_list[val_data[0]]
train_locations_ds = generate_locations_ds(
train_volume, train_mask, train_label, skip_zero=True
)
val_location_ds = generate_locations_ds(val_volume, val_mask, skip_zero=False)
visualize_dataset_patches(train_locations_ds, train_label, "train", fold)
visualize_dataset_patches(val_location_ds, val_label, "val", fold)
# Init the neural network
model = Model()
wandb.finish()
# Initialize a trainer
now = datetime.datetime.now()
checkpoint_callback = pytorch_lightning.callbacks.ModelCheckpoint(
monitor="valid_score",
dirpath=f"best-results-{now}/",
mode="max",
filename="train-fold-" + str(fold) + "-{epoch:02d}-{valid_score:.3f}",
save_last=True,
)