-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
175 lines (135 loc) · 7.63 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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
# --------------------------------------------------------
# References:
# DeiT: https://github.com/facebookresearch/deit
# BEiT: https://github.com/microsoft/unilm/tree/master/beit
# --------------------------------------------------------
import os
import sys
import math
import argparse
import json
from pathlib import Path
import torch
print(torch.__version__)
import torch.backends.cudnn as cudnn
import torchvision.transforms as transforms
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader, DistributedSampler
from torch.nn.parallel import DistributedDataParallel as DDP
import models_mae
import util.misc as misc
from util.misc import NativeScalerWithGradNormCount as NativeScaler
def get_args_parser():
parser = argparse.ArgumentParser('MAE pre-training', add_help=False)
parser.add_argument('--batch_size_per_gpu', default=256, type=int, help='Batch size per GPU (effective batch size is batch_size_per_gpu * accum_iter * # gpus')
parser.add_argument('--epochs', default=1000, type=int)
parser.add_argument('--accum_iter', default=1, type=int, help='Accumulate gradient iterations (for increasing the effective batch size under memory constraints)')
parser.add_argument('--save_prefix', default='', type=str, help='Prefix for saving checkpoint and log files')
# Model parameters
parser.add_argument('--model', default='mae_vit_huge_patch14', type=str, help='Name of model to train')
parser.add_argument('--resume', default='', help='Resume from a checkpoint')
parser.add_argument('--input_size', default=224, type=int, help='Images input size')
parser.add_argument('--mask_ratio', default=0.8, type=float, help='Masking ratio (percentage of removed patches).')
parser.add_argument('--norm_pix_loss', action='store_true', help='Use (per-patch) normalized pixels as targets for computing loss (default: false)')
parser.add_argument('--compile', action='store_true', help='Whether to compile the model for improved efficiency (default: false)')
# Optimizer parameters
parser.add_argument('--weight_decay', type=float, default=0.05, help='Weight decay (default: 0.05)')
parser.add_argument('--lr', type=float, default=0.001, help='Learning rate (absolute lr)')
# Dataset parameters
parser.add_argument('--data_path', default='', type=str, help='dataset path')
parser.add_argument('--output_dir', default='./output_dir', help='path where to save, empty for no saving')
parser.add_argument('--device', default='cuda', help='device to use for training/testing')
parser.add_argument('--start_epoch', default=0, type=int, help='start epoch')
parser.add_argument('--num_workers', default=16, type=int)
parser.add_argument('--jitter_scale', default=[0.2, 1.0], type=float, nargs='+')
parser.add_argument('--jitter_ratio', default=[3.0/4.0, 4.0/3.0], type=float, nargs='+')
# distributed training parameters
parser.add_argument('--dist_url', default='env://', help='url used to set up distributed training')
return parser
def main(args):
misc.init_distributed_mode(args)
print('job dir: {}'.format(os.path.dirname(os.path.realpath(__file__))))
print("{}".format(args).replace(', ', ',\n'))
device = torch.device(args.device)
cudnn.benchmark = True
# simple augmentation pipeline
transform = transforms.Compose([
transforms.RandomResizedCrop(args.input_size, scale=args.jitter_scale, ratio=args.jitter_ratio, interpolation=3),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
dataset = ImageFolder(args.data_path, transform=transform)
sampler = DistributedSampler(dataset, num_replicas=misc.get_world_size(), rank=misc.get_rank(), shuffle=True)
data_loader = DataLoader(dataset, sampler=sampler, batch_size=args.batch_size_per_gpu, num_workers=args.num_workers, pin_memory=True, drop_last=True)
print('Number of iters per epoch:', len(data_loader))
# define the model
model = models_mae.__dict__[args.model](norm_pix_loss=args.norm_pix_loss)
model.to(device)
model_without_ddp = model
print(f"Model: {model_without_ddp}")
# optionally compile model
if args.compile:
model = torch.compile(model)
print(f"Model: {model_without_ddp}")
model = DDP(model, device_ids=[args.gpu]) # TODO: try FSDP
print(f"Model: {model_without_ddp}")
print(f"Number of params (M): {(sum(p.numel() for p in model_without_ddp.parameters() if p.requires_grad) / 1.e6)}")
# set wd as 0 for bias and norm layers
param_groups = misc.add_weight_decay(model_without_ddp, args.weight_decay, bias_wd=False)
optimizer = torch.optim._multi_tensor.AdamW(param_groups, lr=args.lr, betas=(0.9, 0.95), fused=True) # setting fused True for faster updates (hopefully)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=900, gamma=0.1) # can use any other scheduler here
loss_scaler = NativeScaler()
misc.load_model(args=args, model_without_ddp=model_without_ddp, optimizer=optimizer, loss_scaler=loss_scaler, optim_resume=True)
model.train(True)
metric_logger = misc.MetricLogger(delimiter=" ")
optimizer.zero_grad()
print("Starting MAE training!")
for epoch in range(args.start_epoch, args.epochs):
data_loader.sampler.set_epoch(epoch)
header = 'Epoch: [{}]'.format(epoch)
for it, (samples, _) in enumerate(metric_logger.log_every(data_loader, len(data_loader) // 1, header)):
samples = samples.to(device, non_blocking=True)
with torch.cuda.amp.autocast():
loss, _, _ = model(samples, mask_ratio=args.mask_ratio)
loss_value = loss.item()
if not math.isfinite(loss_value):
print("Loss is {}, stopping training".format(loss_value))
sys.exit(1)
loss = loss / args.accum_iter
loss_scaler(loss, optimizer, parameters=model.parameters(), update_grad=(it + 1) % args.accum_iter == 0)
if (it + 1) % args.accum_iter == 0:
optimizer.zero_grad()
torch.cuda.synchronize()
metric_logger.update(loss=loss_value)
lr = optimizer.param_groups[0]["lr"]
metric_logger.update(lr=lr)
# ============ writing logs + saving checkpoint ============
save_dict = {
'model': model_without_ddp.state_dict(),
'optimizer': optimizer.state_dict(),
'args': args,
'epoch': epoch,
'scaler': loss_scaler.state_dict(),
}
misc.save_on_master(save_dict, os.path.join(args.output_dir, args.save_prefix + '_checkpoint.pth'))
# gather the stats from all processes
metric_logger.synchronize_between_processes()
train_stats = {k: meter.global_avg for k, meter in metric_logger.meters.items()}
log_stats = {**{f'train_{k}': v for k, v in train_stats.items()}, 'epoch': epoch}
if misc.is_main_process():
with (Path(args.output_dir) / (args.save_prefix + "_log.txt")).open("a") as f:
f.write(json.dumps(log_stats) + "\n")
# start a fresh logger to wipe off old stats
metric_logger = misc.MetricLogger(delimiter=" ")
# increment lr scheduler
scheduler.step()
if __name__ == '__main__':
args = get_args_parser()
args = args.parse_args()
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
main(args)