-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain.py
398 lines (342 loc) · 13.3 KB
/
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
#
# Copyright (C) 2023 Apple Inc. All rights reserved.
#
"""Modification of Pytorch ImageNet training code to handle additional datasets."""
import argparse
import os
import random
import shutil
import warnings
import yaml
import copy
from typing import List, Dict, Any
import torch
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torch.optim
import torch.multiprocessing as mp
import torch.utils.data
import torch.utils.data.distributed
from trainers import get_trainer
from utils import CosineLR
from data import (
download_dataset,
get_dataset,
get_dataset_num_classes,
get_dataset_size,
)
from dr.data import ReinforcedDataset
import logging
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.INFO)
best_acc1 = 0
def parse_args() -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description="PyTorch Training")
parser.add_argument(
"--config", type=str, required=False, help="Path to a yaml config file."
)
args = parser.parse_args()
return args
def get_trainable_parameters(
model: torch.nn.Module,
weight_decay: float,
no_decay_bn_filter_bias: bool,
*args,
**kwargs
) -> List[Dict[str, List[torch.nn.Parameter]]]:
"""Return trainable model parameters excluding biases and normalization layers.
Args:
model: The Torch model to be trained.
weight_decay: The weight decay coefficient.
no_decay_bn_filter_bias: If True exclude biases and normalization layer params.
Returns:
A list with two dictionaries for parameters with and without weight decay.
"""
with_decay = []
without_decay = []
for _, param in model.named_parameters():
if param.requires_grad and len(param.shape) == 1 and no_decay_bn_filter_bias:
# biases and normalization layer parameters are of len 1
without_decay.append(param)
elif param.requires_grad:
with_decay.append(param)
param_list = [{"params": with_decay, "weight_decay": weight_decay}]
if len(without_decay) > 0:
param_list.append({"params": without_decay, "weight_decay": 0.0})
return param_list
def get_optimizer(
model: torch.nn.Module, config: Dict[str, Any]
) -> torch.optim.Optimizer:
"""Initialize an optimizer with parameters of a model.
Args:
model: The model to be trained.
config: A dictionary of optimizer hyperparameters and configurations. The
configuration should at least have a `name`.
Returns:
A Torch optimizer.
"""
optim_name = config["name"]
params = get_trainable_parameters(
model,
weight_decay=config.get("weight_decay", 0.0),
no_decay_bn_filter_bias=config.get("no_decay_bn_filter_bias", False),
)
if optim_name == "sgd":
optimizer = torch.optim.SGD(
params=params,
lr=config["lr"],
momentum=config["momentum"],
nesterov=config.get("nesterov", True),
)
elif optim_name == "adamw":
optimizer = torch.optim.AdamW(
params=params,
lr=config["lr"],
betas=(config["beta1"], config["beta2"]),
)
else:
raise NotImplementedError
return optimizer
def main(args) -> None:
"""Train a model with the configurations specified in given arguments."""
if args.config is not None:
# Read parameters from yaml config for local run
yaml_path = args.config
with open(yaml_path, "r") as file:
config = yaml.safe_load(file).get("parameters")
dataset = config.get("dataset", "imagenet")
config["num_classes"] = get_dataset_num_classes(dataset)
# Print args before training
logging.info("Training args: {}".format(config))
if config["seed"] is not None:
random.seed(config["seed"])
torch.manual_seed(config["seed"])
cudnn.deterministic = True
cudnn.benchmark = False
warnings.warn(
"You have chosen to seed training. "
"This will turn on the CUDNN deterministic setting, "
"which can slow down your training considerably! "
"You may see unexpected behavior when restarting "
"from checkpoints."
)
ngpus_per_node = torch.cuda.device_count()
if config["gpu"] is not None:
warnings.warn(
"You have chosen a specific GPU. This will completely "
"disable data parallelism."
)
ngpus_per_node = 1
if config["dist_url"] == "env://" and config["world_size"] == -1:
config["world_size"] = int(os.environ["WORLD_SIZE"])
config["distributed"] = (
config["world_size"] > 1 or config["multiprocessing_distributed"]
)
if config["download_data"]:
config["data_path"] = download_dataset(config["data_path"], config["dataset"])
if config["multiprocessing_distributed"]:
# Since we have ngpus_per_node processes per node, the total world_size
# needs to be adjusted accordingly
config["world_size"] = ngpus_per_node * config["world_size"]
# Use torch.multiprocessing.spawn to launch distributed processes: the
# main_worker process function
mp.spawn(
main_worker,
nprocs=ngpus_per_node,
args=(ngpus_per_node, copy.deepcopy(config)),
)
else:
# Simply call main_worker function
main_worker(config["gpu"], ngpus_per_node, copy.deepcopy(config))
def main_worker(gpu: int, ngpus_per_node: int, config: Dict[str, Any]) -> None:
"""Train a model with a single process. In distributed training, run on one GPU."""
global best_acc1
config["gpu"] = gpu
if config["gpu"] is not None:
logging.info("Use GPU: {} for training".format(config["gpu"]))
if config["distributed"]:
if config["dist_url"] == "env://" and config["rank"] == -1:
config["rank"] = int(os.environ["RANK"])
if config["multiprocessing_distributed"]:
# For multiprocessing distributed training, rank needs to be the
# global rank among all the processes
config["rank"] = config["rank"] * ngpus_per_node + gpu
dist.init_process_group(
backend=config["dist_backend"],
init_method=config["dist_url"],
world_size=config["world_size"],
rank=config["rank"],
)
# Disable logging on all workers except rank=0
if config["multiprocessing_distributed"] and config["rank"] % ngpus_per_node != 0:
logging.basicConfig(
format="%(asctime)s %(message)s", level=logging.WARNING, force=True
)
# Initialize a trainer class that handles different models of training such as
# standard training (ERM), Knowledge distillation, and Dataset Reinforcement
trainer = get_trainer(config)
# Create the model to train. Also create and load the teacher model for KD
model = trainer.get_model()
# Print number of model parameters
logging.info(
"Number of model parameters: {}".format(
sum(p.numel() for p in model.parameters() if p.requires_grad)
)
)
# Define loss function (criterion) and optimizer
criterion = trainer.get_criterion()
logging.info("Criterion: {}".format(criterion))
# Define optimizer
optimizer = get_optimizer(model, config["optim"])
logging.info("Optimizer: {}".format(optimizer))
dataset_size = get_dataset_size(config["dataset"])
# Compute warmup and total iterations on each gpu
warmup_length = (
config["optim"]["warmup_length"]
* dataset_size
// config["batch_size"]
// ngpus_per_node
)
total_steps = (
config["epochs"] * dataset_size // config["batch_size"] // ngpus_per_node
)
lr_scheduler = CosineLR(
optimizer,
warmup_length=warmup_length,
total_steps=total_steps,
lr=config["optim"]["lr"],
end_lr=config["optim"].get("end_lr", 0.0),
)
# Resume from a checkpoint
if config["resume"]:
if os.path.isfile(config["resume"]):
logging.info("=> loading checkpoint '{}'".format(config["resume"]))
if config["gpu"] is None:
checkpoint = torch.load(config["resume"])
else:
# Map model to be loaded to specified single gpu.
loc = "cuda:{}".format(config["gpu"])
checkpoint = torch.load(config["resume"], map_location=loc)
config["start_epoch"] = checkpoint["epoch"]
best_acc1 = checkpoint["best_acc1"]
if hasattr(model, "module"):
model.module.load_state_dict(checkpoint["state_dict"])
else:
model.load_state_dict(checkpoint["state_dict"])
optimizer.load_state_dict(checkpoint["optimizer"])
lr_scheduler.load_state_dict(checkpoint["scheduler"])
logging.info(
"=> loaded checkpoint '{}' (epoch {})".format(
config["resume"], checkpoint["epoch"]
)
)
else:
logging.info("=> no checkpoint found at '{}'".format(config["resume"]))
# Data loading code
logging.info("Instantiating dataset.")
train_dataset, val_dataset = get_dataset(config)
logging.info("Training Dataset: {}".format(train_dataset))
logging.info("Validation Dataset: {}".format(val_dataset))
if config["trainer"] == "DR":
# Reinforce dataset
train_dataset = ReinforcedDataset(
train_dataset,
rdata_path=config["reinforce"]["data_path"],
config=config["reinforce"],
num_classes=config["num_classes"],
)
if config["distributed"]:
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
val_sampler = torch.utils.data.distributed.DistributedSampler(
val_dataset, shuffle=False, drop_last=True
)
else:
train_sampler = None
val_sampler = None
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=config["batch_size"],
shuffle=(train_sampler is None),
num_workers=config["workers"],
pin_memory=config["pin_memory"],
persistent_workers=config['persistent_workers'],
sampler=train_sampler,
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=config["batch_size"],
shuffle=False,
num_workers=config["workers"],
pin_memory=config["pin_memory"],
persistent_workers=config['persistent_workers'],
sampler=val_sampler,
)
if config["evaluate"]:
# Evaluate a pretrained model without training
trainer.validate(val_loader, model, criterion, config)
return
# Evaluate a pretrained teacher before training
trainer.validate_pretrained(val_loader, model, criterion, config)
# Start training
for epoch in range(config["start_epoch"], config["epochs"]):
if config["distributed"]:
train_sampler.set_epoch(epoch)
# Train for one epoch
train_metrics = trainer.train(
train_loader,
model,
criterion,
optimizer,
epoch,
config,
lr_scheduler,
)
# Evaluate on validation set
val_metrics = trainer.validate(val_loader, model, criterion, config)
val_acc1 = val_metrics["val_accuracy@top1"]
# remember best acc@1 and save checkpoint
is_best = val_acc1 > best_acc1
best_acc1 = max(val_acc1, best_acc1)
if not config["multiprocessing_distributed"] or (
config["multiprocessing_distributed"]
and config["rank"] % ngpus_per_node == 0
):
metrics = dict(train_metrics)
metrics.update(val_metrics)
if isinstance(model, torch.nn.DataParallel) or isinstance(
model, torch.nn.parallel.DistributedDataParallel
):
model_state_dict = model.module.state_dict()
else:
model_state_dict = model.state_dict()
is_save_epoch = (epoch + 1) % config.get("save_freq", 1) == 0
is_save_epoch = is_save_epoch or ((epoch + 1) == config["epochs"])
save_checkpoint(
{
"epoch": epoch + 1,
"config": config,
"state_dict": model_state_dict,
"best_acc1": best_acc1,
"optimizer": optimizer.state_dict(),
"scheduler": lr_scheduler.state_dict(),
},
is_best,
is_save_epoch,
config["artifact_path"],
)
def save_checkpoint(state, is_best, is_save_epoch, artifact_path) -> None:
"""Save checkpoint and update the best model."""
fname = os.path.join(artifact_path, "checkpoint.pth.tar")
torch.save(state, fname)
if is_save_epoch:
checkpoint_fname = os.path.join(
artifact_path, "checkpoint_{}.pth.tar".format(state["epoch"])
)
shutil.copyfile(fname, checkpoint_fname)
if is_best:
best_model_fname = os.path.join(artifact_path, "model_best.pth.tar")
shutil.copyfile(fname, best_model_fname)
if __name__ == "__main__":
args = parse_args()
main(args)