-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_utils.py
147 lines (115 loc) · 3.89 KB
/
training_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
import math
import random
from pathlib import Path
import os, gc
from typing import Literal
from fastai.vision.all import DataLoaders, Metric
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import WeightedRandomSampler, RandomSampler, DataLoader, Dataset
import numpy as np
def parameter_count(model):
pars = 0
for _, p in model.named_parameters():
pars += torch.prod(torch.tensor(p.shape))
return pars.item()
def loss(pred,target):
p = pred[target['mask'][:,:pred.shape[1]]]
y = target['react'][target['mask']].clip(0,1)
loss = F.l1_loss(p, y, reduction='none')
loss = loss[~torch.isnan(loss)].mean()
return loss
def seed_everything(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True # type: ignore
torch.backends.cudnn.benchmark = True # type: ignore
class MAE(Metric):
def __init__(self):
self.reset()
def reset(self):
self.x,self.y = [],[]
def accumulate(self, learn):
x = learn.pred[learn.y['mask'][:,:learn.pred.shape[1]]]
y = learn.y['react'][learn.y['mask']].clip(0,1)
self.x.append(x)
self.y.append(y)
@property
def value(self):
x,y = torch.cat(self.x,0),torch.cat(self.y,0)
loss = F.l1_loss(x, y, reduction='none')
loss = loss[~torch.isnan(loss)].mean()
return loss
class MAE_2A3(MAE):
def __init__(self):
super().__init__()
def accumulate(self, learn):
x = learn.pred[:, :, 0][learn.y['mask'][:,:learn.pred.shape[1]]]
y = learn.y['react'][:, :, 0][learn.y['mask']].clip(0,1)
self.x.append(x)
self.y.append(y)
class MAE_DMS(MAE):
def __init__(self):
super().__init__()
def accumulate(self, learn):
x = learn.pred[:, :, 1][learn.y['mask'][:,:learn.pred.shape[1]]]
y = learn.y['react'][:, :, 1][learn.y['mask']].clip(0,1)
self.x.append(x)
self.y.append(y)
def val_to(x, device="cuda"):
if isinstance(x, list):
return [val_to(z) for z in x]
return x.to(device)
def dict_to(x, device='cuda'):
return {k: val_to(x[k], device) for k in x}
def to_device(x, device='cuda'):
return tuple(dict_to(e,device) for e in x)
class DeviceDataLoader:
def __init__(self, dataloader, device='cuda'):
self.dataloader = dataloader
self.device = device
def __len__(self):
return len(self.dataloader)
def __iter__(self):
for batch in self.dataloader:
yield tuple(dict_to(x, self.device) for x in batch)
def get_dataloaders(
train_dataset: Dataset,
val_dataset: Dataset,
batch_size: int,
batch_count: int,
num_workers: int,
device: torch.device,
no_weights: bool = False,
) -> DataLoaders:
if no_weights:
sampler_train = RandomSampler(
train_dataset,
replacement = True,
num_samples = batch_size * batch_count
)
else:
sampler_train = WeightedRandomSampler(
weights = train_dataset.weights,
num_samples = batch_size * batch_count)
train_dataloader = DeviceDataLoader(
DataLoader(
dataset = train_dataset,
batch_size = batch_size,
sampler = sampler_train,
num_workers = num_workers,
persistent_workers=True),
device = device)
val_dataloader = DeviceDataLoader(
DataLoader(
dataset = val_dataset,
batch_size = batch_size,
num_workers = num_workers,
persistent_workers = True,
shuffle=False),
device = device)
return DataLoaders(train_dataloader, val_dataloader)