-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlb0-68-one-fold-stacked-unet-train.py
1023 lines (772 loc) · 24.6 KB
/
lb0-68-one-fold-stacked-unet-train.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
# In[ ]:
#!/usr/bin/env python
# coding: utf-8
# for dicsussion, refer to https://www.kaggle.com/competitions/vesuvius-challenge-ink-detection/discussion/407972#2272189
# In[19]:
# In[ ]:
# import sys
# sys.path.append('/kaggle/input/ink-00/my_lib')
# sys.path.append('/kaggle/input/pretrainedmodels/pretrainedmodels-0.7.4')
# sys.path.append('/kaggle/input/efficientnet-pytorch/EfficientNet-PyTorch-master')
# sys.path.append('/kaggle/input/timm-pytorch-image-models/pytorch-image-models-master')
# sys.path.append('/kaggle/input/segmentation-models-pytorch/segmentation_models.pytorch-master')
# sys.path.append('/kaggle/input/einops/einops-master')
# In[20]:
# In[ ]:
import hashlib
import numpy as np
import pandas as pd
from dotdict import dotdict
from time import time
# In[ ]:
from collections import defaultdict
from glob import glob
import PIL.Image as Image
Image.MAX_IMAGE_PIXELS = 10000000000 # Ignore PIL warnings about large images
# In[ ]:
import cv2
# In[ ]:
import torch
import torch.nn as nn
import torch.nn.functional as F
# In[ ]:
from einops import rearrange, reduce, repeat
import segmentation_models_pytorch as smp
from segmentation_models_pytorch.decoders.unet.decoder import UnetDecoder, DecoderBlock
from timm.models.resnet import resnet10t, resnet34d
# In[ ]:
import numpy as np
import torch
import torch.nn as nn
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.plugins
from skimage.transform import resize as resize_ski
from pytorch_lightning.strategies.ddp import DDPStrategy
from pytorch_lightning.loggers import WandbLogger
import os
# In[ ]:
from scipy.ndimage import distance_transform_edt
# In[ ]:
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
# In[ ]:
import matplotlib
import matplotlib.pyplot as plt
# In[21]:
# In[ ]:
def time_to_str(time):
h = time // 3600
m = (time % 3600) // 60
s = time % 60
return f"{h:02f}, {m:02f}, {s:02f}"
# In[22]:
# In[ ]:
class Config(object):
mode = [
"train", #
# 'test', 'skip_fake_test',
]
crop_fade = 56
crop_size = 384
crop_depth = 5
infer_fragment_z = [28, 37]
threshold = 0.5
lr = 1e-4
batch_size = 32
num_workers = 8
epochs = 20
# In[ ]:
CFG = Config()
CFG.is_tta = True # True
# In[ ]:
if "train" in CFG.mode:
CFG.stride = CFG.crop_size // 2
if "test" in CFG.mode:
CFG.stride = CFG.crop_size // 2
# In[ ]:
def cfg_to_text():
d = Config.__dict__
text = [
f"\t{k} : {v}"
for k, v in d.items()
if not (k.startswith("__") and k.endswith("__"))
]
d = CFG.__dict__
text += [
f"\t{k} : {v}"
for k, v in d.items()
if not (k.startswith("__") and k.endswith("__"))
]
return "CFG\n" + "\n".join(text)
# In[ ]:
# dataset ##
# --
# In[ ]:
def do_binarise(m, threshold=0.5):
m = m - m.min()
m = m / (m.max() + 1e-7)
m = (m > threshold).astype(np.float32)
return m
# In[ ]:
def read_data(fragment_id, z0=CFG.infer_fragment_z[0], z1=CFG.infer_fragment_z[1]):
volume = []
start_timer = time.time()
for i in range(z0, z1):
v = np.array(
Image.open(f"{data_dir}/{fragment_id}/surface_volume/{i:02d}.tif"),
dtype=np.uint16,
)
v = (v >> 8).astype(np.uint8)
# v = (v / 65535.0 * 255).astype(np.uint8)
volume.append(v)
print(
f"\r @ read_data(): volume{fragment_id} {time_to_str(time.time() - start_timer)}",
end="",
flush=True,
)
# print('')
volume = np.stack(volume, -1)
height, width, depth = volume.shape
# print(f'fragment_id={fragment_id} volume: {volume.shape}')
# ---
mask = cv2.imread(f"{data_dir}/{fragment_id}/mask.png", cv2.IMREAD_GRAYSCALE)
mask = do_binarise(mask)
if "train" in CFG.mode:
ir = cv2.imread(f"{data_dir}/{fragment_id}/ir.png", cv2.IMREAD_GRAYSCALE)
label = cv2.imread(
f"{data_dir}/{fragment_id}/inklabels.png", cv2.IMREAD_GRAYSCALE
)
ir = ir / 255
label = do_binarise(label)
if "test" in CFG.mode:
ir = None
label = None
d = dotdict(
fragment_id=fragment_id,
volume=volume,
ir=ir,
label=label,
mask=mask,
)
return d
# In[ ]:
def read_data1(fragment_id):
if fragment_id == "2a":
y = 9456
d = read_data("2")
d = dotdict(
fragment_id="2a",
volume=d.volume[:y],
ir=d.ir[:y],
label=d.label[:y],
mask=d.mask[:y],
)
elif fragment_id == "2b":
y = 9456
d = read_data("2")
d = dotdict(
fragment_id="2b",
volume=d.volume[y:],
ir=d.ir[y:],
label=d.label[y:],
mask=d.mask[y:],
)
else:
d = read_data(fragment_id)
return d
# In[ ]:
def load_mask(split, index):
img = cv2.imread(f"{data_dir}/{split}/{index}/mask.png", 0) // 255
return img
def load_labels(split, index):
img = cv2.imread(f"{data_dir}/{split}/{index}/inklabels.png", 0) // 255
return img
# In[ ]:
# 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[ ]:
def run_check_data():
d = read_data1(valid_id[0]) # valid_id[0]
print("")
print("fragment_id:", d.fragment_id)
print("volume:", d.volume.shape, d.volume.min(), d.volume.max())
print("mask :", d.mask.shape, d.mask.min(), d.mask.max())
if "train" in CFG.mode:
print("ir :", d.ir.shape, d.ir.min(), d.ir.max())
print("label :", d.label.shape, d.label.min(), d.label.max())
# un_check_data()
# ref - https://www.kaggle.com/competitions/vesuvius-challenge-ink-detection/discussion/397288
# In[ ]:
# In[ ]:
def extract(location, volume):
global printed
x = location[0]
y = location[1]
subvolume = volume[y : y + CFG.crop_size, x : x + CFG.crop_size, :].astype(
np.float32
)
return subvolume
# In[ ]:
# In[ ]:
import torch
from torch.utils.data import Dataset, DataLoader
from sklearn.preprocessing import OneHotEncoder
# In[ ]:
from albumentations.core.transforms_interface import ImageOnlyTransform
# In[ ]:
def run_check_net():
height, width = CFG.crop_size, CFG.crop_size
depth = CFG.infer_fragment_z[1] - CFG.infer_fragment_z[0]
batch_size = 3
# In[ ]:
# x = np.arange(0,3)
# y = np.arange(0,4)
# x,y = np.meshgrid(x,y)
# xy = np.stack([x,y],-1).reshape(-1,2)
# x, y, xy
# In[ ]:
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])
x, y = location[0], location[1]
subvolume = extract(location, self.volume)
if self.labels is not None:
label = self.labels[y : y + self.buffer * 2, x : x + self.buffer * 2]
label = np.stack([label], axis=-1)
if self.is_train and label is not None:
transformed = A.Compose(
[
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.Transpose(p=0.5),
A.RandomScale(p=0.5),
A.RandomRotate90(p=0.5),
A.ShiftScaleRotate(p=0.5),
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 - 0.45) / 0.225
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 - 0.45) / 0.225
d = {
"volume": subvolume,
"label": label,
}
if len(subvolume.shape) < 3:
print("shhape 2 location:", location)
elif subvolume.shape[1] != CFG.crop_size or subvolume.shape[2] != CFG.crop_size:
print("location()", location)
return d
# In[ ]:
def collate_fn(batch):
keys = batch[0].keys()
collated_batch = {}
for key in keys:
if batch[0][key] is not None:
collated_batch[key] = torch.stack(
[torch.from_numpy(sample[key]) for sample in batch]
)
else:
collated_batch[key] = None
return collated_batch
# In[ ]:
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,
64,
128,
256,
512,
]
decoder1_dim = [
256,
128,
64,
64,
]
self.encoder1 = resnet34d(pretrained=False, in_chans=CFG.crop_depth)
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=False, 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.crop_depth]
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)
# ----------------------
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="bilinear", align_corners=False, antialias=True
)
logit1 = F.interpolate(
logit1, size=(H, W), mode="bilinear", align_corners=False, antialias=True
)
output = {
"logit1": logit1,
"logit2": logit2,
}
return output
# In[ ]:
# #### infer here !!!!<br>
# https://gist.github.com/janpaul123/ca3477c1db6de4346affca37e0e3d5b0
# In[ ]:
def mask_to_rle(mask):
m = mask.reshape(-1)
# m = np.where(mask > threshold, 1, 0).astype(np.uint8)
s = np.array((m[:-1] == 0) & (m[1:] == 1))
e = np.array((m[:-1] == 1) & (m[1:] == 0))
s_index = np.where(s)[0] + 2
e_index = np.where(e)[0] + 2
length = e_index - s_index
rle = " ".join(map(str, sum(zip(s_index, length), ())))
return rle
# In[ ]:
# In[ ]:
def metric_to_text(ink, label, mask):
text = []
p = ink.reshape(-1)
t = label.reshape(-1)
pos = np.log(np.clip(p, 1e-7, 1))
neg = np.log(np.clip(1 - p, 1e-7, 1))
bce = -(t * pos + (1 - t) * neg).mean()
text.append(f"bce={bce:0.5f}")
mask_sum = mask.sum()
# print(f'{threshold:0.1f}, {precision:0.3f}, {recall:0.3f}, {fpr:0.3f}, {dice:0.3f}, {score:0.3f}')
text.append("p_sum th prec recall fpr dice score")
text.append("-----------------------------------------------")
for threshold in [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]:
p = ink.reshape(-1)
t = label.reshape(-1)
p = (p > threshold).astype(np.float32)
t = (t > 0.5).astype(np.float32)
tp = p * t
precision = tp.sum() / (p.sum() + 0.0001)
recall = tp.sum() / t.sum()
fp = p * (1 - t)
fpr = fp.sum() / (1 - t).sum()
beta = 0.5
# 0.2*1/recall + 0.8*1/prec
score = (
beta * beta / (1 + beta * beta) * 1 / recall
+ 1 / (1 + beta * beta) * 1 / precision
)
score = 1 / score
dice = 2 * tp.sum() / (p.sum() + t.sum())
p_sum = p.sum() / mask_sum
# print(fold, threshold, precision, recall, fpr, score)
text.append(
f"{p_sum:0.2f}, {threshold:0.2f}, {precision:0.3f}, {recall:0.3f}, {fpr:0.3f}, {dice:0.3f}, {score:0.3f}"
)
text = "\n".join(text)
return text
# In[ ]:
# In[ ]:
def make_infer_mask():
s = CFG.crop_size
f = CFG.crop_fade
x = np.linspace(-1, 1, s)
y = np.linspace(-1, 1, s)
xx, yy = np.meshgrid(x, y)
d = 1 - np.maximum(np.abs(xx), np.abs(yy))
d1 = np.clip(d, 0, f / s * 2)
d1 = d1 / d1.max()
infer_mask = d1
return infer_mask
# In[ ]:
# In[ ]:
class Model(pl.LightningModule):
training_step_outputs = []
validation_step_outputs = []
test_step_outputs = [[], []]
def __init__(self):
super().__init__()
self.model = Net()
self.loss1 = nn.BCEWithLogitsLoss(pos_weight=0.5)
self.loss2 = nn.BCEWithLogitsLoss(pos_weight=0.5)
def forward(self, image, stage):
mask = self.model(image)
return mask
def shared_step(self, batch, stage):
subvolumes, labels = batch["volume"], batch["label"]
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
segmentation_out = self.forward(image, stage)
loss = self.loss1(segmentation_out["logit1"], labels) + self.loss2(
segmentation_out["logit2"], labels
)
prob = segmentation_out["logit2"].sigmoid()
score = fbeta_score(prob, labels, CFG.threshold)
pred_mask = (prob > CFG.threshold).float()
tp, fp, fn, tn = smp.metrics.get_stats(
pred_mask.long(), labels.long(), mode="binary"
)
m = {
"loss": loss,
"tp": tp,
"fp": fp,
"fn": fn,
"tn": tn,
"score": score,
}
return m
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]))
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": 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(),
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):
pass
def on_test_epoch_end(self):
pass
def configure_optimizers(self):
optimizer = optim.AdamW(self.parameters(), lr=CFG.lr)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
mode="min",
factor=0.05,
patience=5,
)
return {
"optimizer": optimizer,
"lr_scheduler": {"scheduler": scheduler, "monitor": "valid_loss"},
}
# In[ ]:
# In[ ]:
def train_one(d, val_d):
net = Model()
# get coord
crop_size = CFG.crop_size
stride = CFG.stride
H, W, D = d.volume.shape
##pad #assume H,W >size
px, py = W % stride, H % stride
if (px != 0) or (py != 0):
px = stride - px
py = stride - py
pad_volume = np.pad(d.volume, [(0, py), (0, px), (0, 0)], constant_values=0)
pad_label = np.pad(d.label, [(0, py), (0, px)], constant_values=0)
else:
pad_volume = d.volume
pad_label = d.label
pH, pW, _ = pad_volume.shape
x = np.arange(0, pW - crop_size + 1, stride)
y = np.arange(0, pH - crop_size + 1, stride)
x, y = np.meshgrid(x, y)
xy = np.stack([x, y], -1).reshape(-1, 2)
print("H,W,pH,pW,len(xy)", H, W, pH, pW, len(xy))
val_H, val_W, val_D = val_d.volume.shape
##pad #assume H,W >size
val_px, val_py = val_W % stride, val_H % stride
if (val_px != 0) or (val_py != 0):
val_px = stride - val_px
val_py = stride - val_py
pad_val_volume = np.pad(
val_d.volume, [(0, val_py), (0, val_px), (0, 0)], constant_values=0
)
pad_val_label = np.pad(
val_d.label, [(0, val_py), (0, val_px)], constant_values=0
)
else:
pad_val_volume = val_d.volume
pad_val_label = val_d.label
val_pH, val_pW, _ = pad_val_volume.shape
val_x = np.arange(0, val_pW - crop_size + 1, stride)
val_y = np.arange(0, val_pH - crop_size + 1, stride)
val_x, val_y = np.meshgrid(val_x, val_y)
val_xy = np.stack([val_x, val_y], -1).reshape(-1, 2)
print(
"val_H,val_W,val_pH,val_pW,len(val_xy)",
val_H,
val_W,
val_pH,
val_pW,
len(val_xy),
)
train_ds = SubvolumeDataset(
locations=xy,
volume=pad_volume,
labels=pad_label,
buffer=crop_size // 2,
is_train=True,
)
val_ds = SubvolumeDataset(
val_xy,
pad_val_volume,
pad_val_label,
crop_size // 2,
is_train=False,
)
# Define data loaders for training and testing data in this fold
train_loader = torch.utils.data.DataLoader(
train_ds,
batch_size=CFG.batch_size,
num_workers=CFG.num_workers,
shuffle=True,
collate_fn=collate_fn,
)
val_loader = torch.utils.data.DataLoader(
val_ds,
batch_size=CFG.batch_size,
num_workers=CFG.num_workers,
shuffle=False,
collate_fn=collate_fn,
)
trainer = pl.Trainer(
max_epochs=CFG.epochs,
accelerator="gpu",
devices="0,1,2,3",
logger=WandbLogger(name=f"2.5d-stack-unet-{datetime.datetime.now()}"),
# strategy='ddp_find_unused_parameters_true',
)
trainer.fit(
net,
train_loader,
val_loader,
)
# In[ ]:
if __name__ == "__main__":
print(cfg_to_text())
if "train" in CFG.mode:
data_dir = "/home/fummicc1/codes/competitions/kaggle-ink-detection/train"
valid_id = [