-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
251 lines (200 loc) · 7.17 KB
/
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
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
from transformers import get_cosine_schedule_with_warmup
from torch.optim import AdamW
import random
import os
import numpy as np
import pandas as pd
from torch.utils.data import Sampler, RandomSampler, SequentialSampler, DataLoader
import torch
from torch.utils.data import RandomSampler, SequentialSampler, DataLoader, WeightedRandomSampler
from torch import nn, optim
import importlib
import math
class OrderedDistributedSampler(Sampler):
def __init__(self, dataset, num_replicas=None, rank=None):
if num_replicas is None:
if not dist.is_available():
raise RuntimeError("Requires distributed package to be available")
num_replicas = dist.get_world_size()
if rank is None:
if not dist.is_available():
raise RuntimeError("Requires distributed package to be available")
rank = dist.get_rank()
self.dataset = dataset
self.num_replicas = num_replicas
self.rank = rank
self.num_samples = int(math.ceil(len(self.dataset) * 1.0 / self.num_replicas))
self.total_size = self.num_samples * self.num_replicas
print("TOTAL SIZE", self.total_size)
def __iter__(self):
indices = list(range(len(self.dataset)))
# add extra samples to make it evenly divisible
indices += indices[: (self.total_size - len(indices))]
assert len(indices) == self.total_size
# subsample
indices = indices[
self.rank * self.num_samples : self.rank * self.num_samples + self.num_samples
]
print(
"SAMPLES",
self.rank * self.num_samples,
self.rank * self.num_samples + self.num_samples,
)
assert len(indices) == self.num_samples
return iter(indices)
def __len__(self):
return self.num_samples
def sync_across_gpus(t, world_size):
torch.distributed.barrier()
gather_t_tensor = [torch.ones_like(t) for _ in range(world_size)]
torch.distributed.all_gather(gather_t_tensor, t)
return torch.cat(gather_t_tensor)
def set_seed(seed=1234):
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 = False
torch.backends.cudnn.benchmark = True
def worker_init_fn(worker_id):
np.random.seed(np.random.get_state()[1][0] + worker_id)
def get_model(cfg, ds):
Net = importlib.import_module(cfg.model).Net
net = Net(cfg)
return net
def create_checkpoint(cfg, model, optimizer, epoch, scheduler=None, scaler=None):
state_dict = model.state_dict()
if cfg.save_weights_only:
checkpoint = {"model": state_dict}
return checkpoint
checkpoint = {
"model": state_dict,
"optimizer": optimizer.state_dict(),
"epoch": epoch,
}
if scheduler is not None:
checkpoint["scheduler"] = scheduler.state_dict()
if scaler is not None:
checkpoint["scaler"] = scaler.state_dict()
return checkpoint
def get_dataset(df, cfg, mode='train'):
print(f"Loading {mode} dataset")
if mode == 'train':
dataset = get_train_dataset(df, cfg)
elif mode == 'test':
dataset = get_test_dataset(df, cfg)
else:
pass
return dataset
def get_dataloader(ds, cfg, mode='train'):
if mode == 'train':
dl = get_train_dataloader(ds, cfg)
elif mode =='test':
dl = get_test_dataloader(ds, cfg)
else:
pass
return dl
def get_train_dataset(train_df, cfg):
print("Loading train dataset")
train_dataset = cfg.CustomDataset(train_df, cfg, aug=cfg.train_aug, mode="train")
return train_dataset
def get_train_dataloader(train_ds, cfg):
if cfg.distributed:
sampler = torch.utils.data.distributed.DistributedSampler(
train_ds, num_replicas=cfg.world_size, rank=cfg.local_rank, shuffle=True, seed=cfg.seed
)
else:
try:
if cfg.random_sampler_frac > 0:
num_samples = int(len(train_ds) * cfg.random_sampler_frac)
sample_weights = train_ds.sample_weights
sampler = WeightedRandomSampler(sample_weights, num_samples= num_samples )
else:
sampler = None
except:
sampler = None
train_dataloader = DataLoader(
train_ds,
sampler=sampler,
shuffle=(sampler is None),
batch_size=cfg.batch_size,
num_workers=cfg.num_workers,
pin_memory=cfg.pin_memory,
collate_fn=cfg.tr_collate_fn,
drop_last=cfg.drop_last,
worker_init_fn=worker_init_fn,
)
print(f"train: dataset {len(train_ds)}, dataloader {len(train_dataloader)}")
return train_dataloader
def get_test_dataset(test_df, cfg):
print("Loading test dataset")
test_dataset = cfg.CustomDataset(test_df, cfg, aug=cfg.val_aug, mode="test")
return test_dataset
def get_test_dataloader(test_ds, cfg):
if cfg.distributed and cfg.eval_ddp:
sampler = OrderedDistributedSampler(
test_ds, num_replicas=cfg.world_size, rank=cfg.local_rank
)
else:
sampler = SequentialSampler(test_ds)
test_dataloader = DataLoader(
test_ds,
sampler=sampler,
batch_size=cfg.batch_size,
num_workers=cfg.num_workers,
pin_memory=cfg.pin_memory,
collate_fn=cfg.val_collate_fn,
worker_init_fn=worker_init_fn,
)
print(f"test: dataset {len(test_ds)}, dataloader {len(test_dataloader)}")
return test_dataloader
def get_optimizer(model, cfg):
params = model.parameters()
if cfg.optimizer == "AdamW_mixed":
params = [
{
"params": [
param for name, param in model.named_parameters() if "backbone" in name
],
"lr": cfg.lr[0],
},
{
"params": [
param for name, param in model.named_parameters() if not "backbone" in name
],
"lr": cfg.lr[1],
},
]
optimizer = AdamW(params, lr=cfg.lr[1], weight_decay=cfg.weight_decay)
elif cfg.optimizer == "AdamW":
optimizer = AdamW(params, lr=cfg.lr, weight_decay=cfg.weight_decay)
elif cfg.optimizer == "SGD":
optimizer = optim.SGD(
params,
lr=cfg.lr,
momentum=cfg.sgd_momentum,
nesterov=cfg.sgd_nesterov,
weight_decay=cfg.weight_decay,
)
return optimizer
def get_scheduler(cfg, optimizer, total_steps):
if cfg.schedule == "cosine":
scheduler = get_cosine_schedule_with_warmup(
optimizer,
num_warmup_steps=cfg.warmup * (total_steps // cfg.batch_size) // cfg.world_size,
num_training_steps=cfg.epochs * (total_steps // cfg.batch_size) // cfg.world_size,
num_cycles = cfg.num_cycles,
)
else:
scheduler = None
return scheduler
def get_data(cfg):
# setup dataset
print(f"reading {cfg.train_df}")
df = pd.read_csv(cfg.train_df)
if cfg.test:
test_df = pd.read_csv(cfg.test_df)
else:
test_df = None
return df, test_df