-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_utils.py
173 lines (137 loc) · 5.13 KB
/
train_utils.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
#!/usr/bin/env python3
# Author: Armit
# Create Time: 2024/01/13
from shutil import copy2
from torch.optim import SGD, Adam, AdamW
from torch.optim.lr_scheduler import CosineAnnealingLR, OneCycleLR
from torch.utils.data import Dataset, DataLoader
from lightning import LightningModule, Trainer
from models.utils import * # init torch
from utils import *
IS_FOR_ENSEMBLE = True
DTYPE = 'bf16-mixed'
MOMENTUM = 0.9
class ImageDataset(Dataset):
def __init__(self, dp:Path, split:str='train', split_ratio:float=0.7, transform:Callable=None):
super().__init__()
fps = get_test_fps(dp)
truth = load_truth()
assert len(fps) == len(truth)
nlen = len(fps)
cp = round(nlen * split_ratio)
if split == 'train':
fps = fps[:cp]
truth = truth[:cp]
elif split == 'val':
fps = fps[cp:]
truth = truth[cp:]
elif split in ['all', '']:
pass
self.fps = fps
self.truth = truth
self.transform = transform
def __len__(self):
return len(self.fps)
def __getitem__(self, idx:int):
y = self.truth[idx]
img = load_img(self.fps[idx])
if self.transform:
img = self.transform(img)
return img, 1 - y # swap 0-1
def get_dataloaders(batch_size:int, split_ratio:float, transform:Callable) -> Tuple[DataLoader, DataLoader]:
kwargs = {
'batch_size': batch_size,
'pin_memory': True,
'num_workers': 2,
'persistent_workers': True,
}
trainset = ImageDataset(DATA_PATH, 'train', split_ratio, transform=transform)
trainloader = DataLoader(trainset, **kwargs)
validset = ImageDataset(DATA_PATH, 'valid', split_ratio, transform=transform)
validloader = DataLoader(validset, **kwargs)
print('trainset:', len(trainset), 'trainloader:', len(trainloader))
print('validset:', len(validset),'validloader:', len(validloader))
return trainloader, validloader
class ImageDatasetForEnsemble(Dataset):
def __init__(self, dp:Path, idx:int, split:str='train', transform:Callable=None):
super().__init__()
assert idx in [0, 1, 2]
TRAIN_RATIO = 0.3
OVERLAP_RATIO = 0.1
VALID_RATIO = 0.4
all_fps = get_test_fps(dp)
all_truth = load_truth()
assert len(all_fps) == len(all_truth)
nlen = len(all_fps)
cp = round(nlen * VALID_RATIO)
# first 60% as trainset, but assign 30% to each model with 10% pairwise overlap
train_fps, train_truth = all_fps[:cp], all_truth[:cp]
# last 40% as common validtest
valid_fps, valid_truth = all_fps[:-cp], all_truth[:-cp]
if split in ['val', 'valid']:
fps = valid_fps
truth = valid_truth
elif split == 'train':
cp_start = round(nlen * max(0.0, idx * TRAIN_RATIO - OVERLAP_RATIO))
cp_end = cp_start + round(nlen * TRAIN_RATIO)
fps = (train_fps + train_fps)[cp_start:cp_end]
truth = (train_truth + train_truth)[cp_start:cp_end]
else: raise ValueError(split)
self.fps = fps
self.truth = truth
self.transform = transform
def __len__(self):
return len(self.fps)
def __getitem__(self, idx:int):
y = self.truth[idx]
img = load_img(self.fps[idx])
if self.transform:
img = self.transform(img)
return img, 1 - y # swap 0-1
def get_dataloaders_for_ensemble(batch_size:int, idx:int, transform:Callable) -> Tuple[DataLoader, DataLoader]:
kwargs = {
'batch_size': batch_size,
'pin_memory': True,
'num_workers': 2,
'persistent_workers': True,
}
trainset = ImageDatasetForEnsemble(DATA_PATH, idx, 'train', transform=transform)
trainloader = DataLoader(trainset, **kwargs)
validset = ImageDatasetForEnsemble(DATA_PATH, idx, 'valid', transform=transform)
validloader = DataLoader(validset, **kwargs)
print('trainset:', len(trainset), 'trainloader:', len(trainloader))
print('validset:', len(validset),'validloader:', len(validloader))
return trainloader, validloader
class LitModel(LightningModule):
def __init__(self, model:nn.Module):
super().__init__()
self.model = model
def make_trainable_params(self):
raise NotImplementedError
def configure_optimizers(self):
return SGD(self.make_trainable_params(), momentum=MOMENTUM)
def training_step(self, batch:Tuple[Tensor], batch_idx:int) -> Tensor:
x, y = batch
logits = self.model(x)
loss = F.cross_entropy(logits, y)
if batch_idx % 10 == 0:
self.log("train_loss", loss)
return loss
def infer_step(self, batch:Tuple[Tensor]) -> Tuple[Tensor, float]:
x, y = batch
logits = self.model(x)
loss = F.cross_entropy(logits, y)
pred = torch.argmax(logits, dim=-1)
acc = torch.sum(y == pred).item() / len(y)
return loss, acc
def validation_step(self, batch:Tuple[Tensor], batch_idx:int):
loss, acc = self.infer_step(batch)
if batch_idx % 10 == 0:
self.log_dict({'val_loss': loss.item(), 'val_acc': acc})
def test_step(self, batch:Tuple[Tensor], batch_idx:int):
loss, acc = self.infer_step(batch)
if batch_idx % 10 == 0:
self.log_dict({'test_loss': loss.item(), 'test_acc': acc})
def save_npz_weights(model:LitModel, src:Path, dst:Path):
np.savez(dst / 'model.npz', **{k: v.cpu().numpy() for k, v in model.state_dict().items()})
copy2(src / 'model.json', dst / 'model.json')