-
Notifications
You must be signed in to change notification settings - Fork 12
/
train.py
308 lines (252 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
'''
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
* By Junnan Li
* References: timm and beit
* https://github.com/rwightman/pytorch-image-models/tree/master/timm
* https://github.com/microsoft/unilm/blob/master/beit
'''
import argparse
import datetime
import numpy as np
import time
import torch
import torch.backends.cudnn as cudnn
import json
import os
from contextlib import suppress
import random
from pathlib import Path
from collections import OrderedDict
from ema import ModelEma
from optim_factory import create_optimizer, get_parameter_groups, LayerDecayValueAssigner
import utils
from utils import NativeScalerWithGradNormCount as NativeScaler
from build_dataset import build_dataset
from engine_self_training import train_one_epoch, evaluate
from model import clip_classifier
import warnings
warnings.filterwarnings("ignore")
def get_args():
parser = argparse.ArgumentParser('MUST training and evaluation script', add_help=False)
parser.add_argument('--batch_size', default=64, type=int)
parser.add_argument('--save_ckpt_freq', default=10, type=int)
parser.add_argument('--eval_freq', default=1, type=int)
# CLIP parameters
parser.add_argument("--template", default='templates.json', type=str)
parser.add_argument("--classname", default='classes.json', type=str)
parser.add_argument('--clip_model', default='ViT-B/16', help='pretrained clip model name')
parser.add_argument('--image_mean', default=(0.48145466, 0.4578275, 0.40821073))
parser.add_argument('--image_std', default=(0.26862954, 0.26130258, 0.27577711))
parser.add_argument('--input_size', default=224, type=int, help='images input size')
# training parameters
parser.add_argument("--train_config", default='train_configs.json', type=str, help='training configurations')
parser.add_argument('--mask', action='store_true')
parser.set_defaults(mask=True)
parser.add_argument('--model_ema_decay', type=float, default=0.9998, help='')
parser.add_argument('--model_ema_force_cpu', action='store_true', default=False, help='')
# Optimizer parameters
parser.add_argument('--opt', default='adamw', type=str, metavar='OPTIMIZER',
help='Optimizer (default: "adamw"')
parser.add_argument('--opt_eps', default=1e-8, type=float, metavar='EPSILON',
help='Optimizer Epsilon (default: 1e-8)')
parser.add_argument('--opt_betas', default=None, type=float, nargs='+', metavar='BETA',
help='Optimizer Betas (default: None, use opt default)')
parser.add_argument('--weight_decay', type=float, default=0.05,
help='weight decay (default: 0.05)')
parser.add_argument('--lr', type=float, default=1e-3, metavar='LR',
help='learning rate (default: 1e-3)')
parser.add_argument('--layer_decay', type=float, default=0.65)
parser.add_argument('--warmup_lr', type=float, default=1e-6, metavar='LR',
help='warmup learning rate (default: 1e-6)')
parser.add_argument('--min_lr', type=float, default=1e-6, metavar='LR',
help='lower lr bound for cyclic schedulers that hit 0')
parser.add_argument('--warmup_epochs', type=int, default=0, metavar='N',
help='epochs to warmup LR, if scheduler supports')
parser.add_argument('--warmup_steps', type=int, default=-1, metavar='N',
help='num of steps to warmup LR, will overload warmup_epochs if set > 0')
# Augmentation parameters
parser.add_argument('--train_crop_min', default=0.3, type=float)
parser.add_argument('--color_jitter', type=float, default=0, metavar='PCT')
parser.add_argument('--aa', type=str, default='rand-m9-mstd0.5-inc1', metavar='NAME',
help='Use AutoAugment policy. "v0" or "original". " + "(default: rand-m9-mstd0.5-inc1)'),
parser.add_argument('--train_interpolation', type=str, default='bicubic',
help='Training interpolation (random, bilinear, bicubic default: "bicubic")')
# Dataset parameters
parser.add_argument('--nb_classes', default=0, type=int, help='number of the classification types')
parser.add_argument('--dataset', default='imagenet', type=str, help='dataset name')
parser.add_argument('--output_dir', default='', help='path to save checkpoint and log')
parser.add_argument('--device', default='cuda',
help='device to use for training / testing')
parser.add_argument('--seed', default=0, type=int)
parser.add_argument('--resume', default='',
help='resume from checkpoint')
parser.add_argument('--auto_resume', action='store_true')
parser.set_defaults(auto_resume=True)
parser.add_argument('--start_epoch', default=0, type=int, metavar='N',
help='start epoch')
parser.add_argument('--eval', action='store_true',
help='Perform evaluation only')
parser.add_argument('--num_workers', default=10, type=int)
# distributed training parameters
parser.add_argument('--world_size', default=1, type=int,
help='number of distributed processes')
parser.add_argument('--local_rank', default=-1, type=int)
parser.add_argument('--dist_on_itp', action='store_true')
parser.add_argument('--dist_url', default='env://',
help='url used to set up distributed training')
parser.add_argument('--amp', action='store_true')
return parser.parse_args()
def main(args):
utils.init_distributed_mode(args)
train_configs = json.load(open(args.train_config,'r'))
train_config = train_configs[args.dataset+'_'+args.clip_model]
if not args.output_dir:
args.output_dir = os.path.join('output',args.dataset)
if args.mask:
args.output_dir = os.path.join(args.output_dir, "%s_mpatch%d_mratio%.1f_walign%.1f_tau%.1f_epoch%d_lr%.5f"%(args.clip_model[:5],train_config['mask_patch_size'],train_config['mask_ratio'],train_config['w_align'],train_config['conf_threshold'],train_config['epochs'], train_config['lr']))
else:
args.output_dir = os.path.join(args.output_dir, "%s_tau%.1f_epoch%d_lr%.5f"%(args.clip_model[:5],train_config['conf_threshold'],train_config['epochs'], train_config['lr']))
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
print(args)
device = torch.device(args.device)
# fix the seed for reproducibility
seed = args.seed + utils.get_rank()
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
cudnn.benchmark = True
# create image classifier from pretrained clip model
model = clip_classifier(args)
args.nb_classes = len(model.classnames)
dataset_train = build_dataset(is_train=True, args=args, train_config=train_config)
dataset_val = build_dataset(is_train=False, args=args)
if True: # args.distributed:
num_tasks = utils.get_world_size()
global_rank = utils.get_rank()
sampler_train = torch.utils.data.DistributedSampler(
dataset_train, num_replicas=num_tasks, rank=global_rank, shuffle=True
)
print("Sampler_train = %s" % str(sampler_train))
else:
sampler_train = torch.utils.data.RandomSampler(dataset_train)
sampler_val = torch.utils.data.SequentialSampler(dataset_val)
if global_rank == 0 and args.output_dir is not None:
os.makedirs(args.output_dir, exist_ok=True)
log_writer = utils.TensorboardLogger(log_dir=args.output_dir)
else:
log_writer = None
if args.output_dir and utils.is_main_process():
with open(os.path.join(args.output_dir, "log.txt"), mode="a", encoding="utf-8") as f:
f.write(json.dumps(dict(args._get_kwargs())) + "\n")
data_loader_train = torch.utils.data.DataLoader(
dataset_train, sampler=sampler_train,
batch_size=args.batch_size,
num_workers=args.num_workers,
pin_memory=True,
drop_last=True,
)
data_loader_val = torch.utils.data.DataLoader(
dataset_val, sampler=sampler_val,
batch_size=2*args.batch_size,
num_workers=args.num_workers,
pin_memory=True,
drop_last=False
)
model_ema = ModelEma(
model,
decay=args.model_ema_decay,
resume='')
print("Using EMA with decay = %.5f" % (args.model_ema_decay) )
model_without_ddp = model
n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("Model = %s" % str(model_without_ddp))
print('number of params:', n_parameters)
total_batch_size = args.batch_size * utils.get_world_size()
num_training_steps_per_epoch = len(data_loader_train)
args.lr = train_config['lr'] * total_batch_size / 256
args.min_lr = args.min_lr * total_batch_size / 256
args.epochs = train_config['epochs']
args.eval_freq = train_config['eval_freq']
print("LR = %.8f" % args.lr)
print("Batch size = %d" % total_batch_size)
print("Number of training examples = %d" % len(dataset_train))
num_layers = model_without_ddp.model.visual.transformer.layers
if args.layer_decay < 1.0:
assigner = LayerDecayValueAssigner(list(args.layer_decay ** (num_layers + 1 - i) for i in range(num_layers + 2)))
else:
assigner = None
if assigner is not None:
print("Assigned values = %s" % str(assigner.values))
if args.distributed:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
model_without_ddp = model.module
optimizer = create_optimizer(
args, model_without_ddp,
get_num_layer=assigner.get_layer_id if assigner is not None else None,
get_layer_scale=assigner.get_scale if assigner is not None else None)
if args.amp:
loss_scaler = NativeScaler()
amp_autocast = torch.cuda.amp.autocast
else:
loss_scaler = None
amp_autocast = suppress
lr_schedule_values = utils.cosine_scheduler(
args.lr, args.min_lr, args.epochs, num_training_steps_per_epoch,
warmup_epochs=args.warmup_epochs, warmup_steps=args.warmup_steps,
)
utils.auto_load_model(
args=args, model=model, model_without_ddp=model_without_ddp,
optimizer=optimizer, loss_scaler=loss_scaler, model_ema=model_ema)
if args.eval:
test_stats = evaluate(data_loader_val, model, device, args=args)
print(f"Accuracy of the network on the {len(dataset_val)} test images: {test_stats['acc1']:.1f}%")
exit(0)
print(f"Start training for {args.epochs} epochs")
start_time = time.time()
max_accuracy = 0.0
for epoch in range(args.start_epoch, args.epochs):
if args.distributed:
data_loader_train.sampler.set_epoch(epoch)
if log_writer is not None:
log_writer.set_step(epoch * num_training_steps_per_epoch)
train_stats = train_one_epoch(
model, args, train_config,
data_loader_train, optimizer, amp_autocast, device, epoch, loss_scaler,
log_writer=log_writer,
start_steps=epoch * num_training_steps_per_epoch,
lr_schedule_values=lr_schedule_values,
model_ema=model_ema,
)
if args.output_dir and utils.is_main_process() and (epoch + 1) % args.eval_freq == 0:
if (epoch + 1) % args.save_ckpt_freq == 0 or epoch + 1 == args.epochs:
utils.save_model(
args=args, model=model, model_without_ddp=model_without_ddp, optimizer=optimizer,
loss_scaler=loss_scaler, epoch=epoch, model_ema=model_ema)
test_stats = evaluate(data_loader_val, model, device, model_ema=model_ema, args=args)
print(f"Accuracy of the network on the {len(dataset_val)} test images: {test_stats['acc1']:.1f}%")
if max_accuracy < test_stats["acc1"]:
max_accuracy = test_stats["acc1"]
if args.output_dir:
utils.save_model(
args=args, model=model, model_without_ddp=model_without_ddp, optimizer=optimizer,
loss_scaler=loss_scaler, epoch="best", model_ema=model_ema)
print(f'Max accuracy: {max_accuracy:.2f}%')
if log_writer is not None:
log_writer.update(test_acc1=test_stats['acc1'], head="test", step=epoch)
log_writer.update(test_ema_acc1=test_stats['ema_acc1'], head="test", step=epoch)
log_writer.flush()
log_stats = {**{f'train_{k}': v for k, v in train_stats.items()},
**{f'test_{k}': v for k, v in test_stats.items()},
'epoch': epoch,
'n_parameters': n_parameters}
with open(os.path.join(args.output_dir, "log.txt"), mode="a", encoding="utf-8") as f:
f.write(json.dumps(log_stats) + "\n")
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
print('Training time {}'.format(total_time_str))
if __name__ == '__main__':
opts = get_args()
main(opts)