forked from mindspore-lab/mindcv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
309 lines (276 loc) · 12.4 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
''' Model training pipeline '''
import os
import logging
import mindspore as ms
import random
import numpy as np
from mindspore import nn, Tensor
from mindspore import FixedLossScaleManager, Model
from mindspore.communication import init, get_rank, get_group_size
import mindspore.dataset.transforms as transforms
from mindcv.models import create_model
from mindcv.data import create_dataset, create_transforms, create_loader
from mindcv.loss import create_loss
from mindcv.optim import create_optimizer
from mindcv.scheduler import create_scheduler
from mindcv.utils import StateMonitor, AllReduceSum, TrainOneStepWithEMA
from mindcv.utils.random import set_seed
from config import parse_args
# TODO: arg parser already has a logger
logger = logging.getLogger('train')
logger.setLevel(logging.INFO)
h1 = logging.StreamHandler()
formatter1 = logging.Formatter('%(message)s',)
logger.addHandler(h1)
h1.setFormatter(formatter1)
def train(args):
''' main train function'''
ms.set_context(mode=args.mode)
if args.distribute:
init()
device_num = get_group_size()
rank_id = get_rank()
ms.set_auto_parallel_context(device_num=device_num,
parallel_mode='data_parallel',
gradients_mean=True)
else:
device_num = None
rank_id = None
set_seed(args.seed, rank_id)
# create dataset
dataset_train = create_dataset(
name=args.dataset,
root=args.data_dir,
split=args.train_split,
shuffle=args.shuffle,
num_samples=args.num_samples,
num_shards=device_num,
shard_id=rank_id,
num_parallel_workers=args.num_parallel_workers,
download=args.dataset_download,
num_aug_repeats=args.aug_repeats)
if args.num_classes is None:
num_classes = dataset_train.num_classes()
else:
num_classes = args.num_classes
# create transforms
transform_list = create_transforms(
dataset_name=args.dataset,
is_training=True,
image_resize=args.image_resize,
scale=args.scale,
ratio=args.ratio,
hflip=args.hflip,
vflip=args.vflip,
color_jitter=args.color_jitter,
interpolation=args.interpolation,
auto_augment=args.auto_augment,
mean=args.mean,
std=args.std,
re_prob=args.re_prob,
re_scale=args.re_scale,
re_ratio=args.re_ratio,
re_value=args.re_value,
re_max_attempts=args.re_max_attempts
)
target_transform = transforms.OneHot(num_classes) if args.loss == 'BCE' else None
# load dataset
loader_train = create_loader(
dataset=dataset_train,
batch_size=args.batch_size,
drop_remainder=args.drop_remainder,
is_training=True,
mixup=args.mixup,
cutmix=args.cutmix,
cutmix_prob=args.cutmix_prob,
num_classes=num_classes,
transform=transform_list,
target_transform=target_transform,
num_parallel_workers=args.num_parallel_workers,
)
if args.val_while_train:
dataset_eval = create_dataset(
name=args.dataset,
root=args.data_dir,
split=args.val_split,
num_shards=device_num,
shard_id=rank_id,
num_parallel_workers=args.num_parallel_workers,
download=args.dataset_download)
transform_list_eval = create_transforms(
dataset_name=args.dataset,
is_training=False,
image_resize=args.image_resize,
crop_pct=args.crop_pct,
interpolation=args.interpolation,
mean=args.mean,
std=args.std
)
loader_eval = create_loader(
dataset=dataset_eval,
batch_size=args.batch_size,
drop_remainder=False,
is_training=False,
transform=transform_list_eval,
target_transform=target_transform,
num_parallel_workers=args.num_parallel_workers,
)
# validation dataset count
eval_count = dataset_eval.get_dataset_size()
if args.distribute:
all_reduce = AllReduceSum()
eval_count = all_reduce(Tensor(eval_count, ms.int32))
else:
loader_eval = None
num_batches = loader_train.get_dataset_size()
# Train dataset count
train_count = dataset_train.get_dataset_size()
if args.distribute:
all_reduce = AllReduceSum()
train_count = all_reduce(Tensor(train_count, ms.int32))
# create model
network = create_model(model_name=args.model,
num_classes=num_classes,
in_channels=args.in_channels,
drop_rate=args.drop_rate,
drop_path_rate=args.drop_path_rate,
pretrained=args.pretrained,
checkpoint_path=args.ckpt_path,
use_ema=args.use_ema)
num_params = sum([param.size for param in network.get_parameters()])
# create loss
loss = create_loss(name=args.loss,
reduction=args.reduction,
label_smoothing=args.label_smoothing,
aux_factor=args.aux_factor)
# create learning rate schedule
lr_scheduler = create_scheduler(num_batches,
scheduler=args.scheduler,
lr=args.lr,
min_lr=args.min_lr,
warmup_epochs=args.warmup_epochs,
warmup_factor=args.warmup_factor,
decay_epochs=args.decay_epochs,
decay_rate=args.decay_rate,
milestones=args.multi_step_decay_milestones,
num_epochs=args.epoch_size,
lr_epoch_stair=args.lr_epoch_stair,
num_cycles=args.num_cycles,
cycle_decay=args.cycle_decay)
# resume training if ckpt_path is given
if args.ckpt_path != '' and args.resume_opt:
opt_ckpt_path = os.path.join(args.ckpt_save_dir, f'optim_{args.model}.ckpt')
else:
opt_ckpt_path = ''
# create optimizer
#TODO: consistent naming opt, name, dataset_name
if args.use_ema:
optimizer = create_optimizer(network.trainable_params(),
opt=args.opt,
lr=lr_scheduler,
weight_decay=args.weight_decay,
momentum=args.momentum,
nesterov=args.use_nesterov,
filter_bias_and_bn=args.filter_bias_and_bn,
checkpoint_path=opt_ckpt_path,
eps=args.eps)
else:
optimizer = create_optimizer(network.trainable_params(),
opt=args.opt,
lr=lr_scheduler,
weight_decay=args.weight_decay,
momentum=args.momentum,
nesterov=args.use_nesterov,
filter_bias_and_bn=args.filter_bias_and_bn,
loss_scale=args.loss_scale,
checkpoint_path=opt_ckpt_path,
eps=args.eps)
# Define eval metrics.
if num_classes >= 5:
eval_metrics = {'Top_1_Accuracy': nn.Top1CategoricalAccuracy(),
'Top_5_Accuracy': nn.Top5CategoricalAccuracy()}
else:
eval_metrics = {'Top_1_Accuracy': nn.Top1CategoricalAccuracy()}
# init model
if args.use_ema:
net_with_loss = nn.WithLossCell(network, loss)
if args.dynamic_loss_scale:
loss_scale_manager = nn.DynamicLossScaleUpdateCell(loss_scale_value=args.loss_scale, scale_factor=2,
scale_window=1000)
else:
loss_scale_manager = nn.FixedLossScaleUpdateCell(loss_scale_value=args.loss_scale)
ms.amp.auto_mixed_precision(net_with_loss, amp_level=args.amp_level)
net_with_loss = TrainOneStepWithEMA(net_with_loss, optimizer, scale_sense=loss_scale_manager,
use_ema=args.use_ema, ema_decay=args.ema_decay)
eval_network = nn.WithEvalCell(network, loss, args.amp_level in ["O2", "O3", "auto"])
model = Model(net_with_loss, eval_network=eval_network, metrics=eval_metrics, eval_indexes=[0, 1, 2])
else:
if args.dynamic_loss_scale:
loss_scale_manager = ms.amp.DynamicLossScaleManager(init_loss_scale=args.loss_scale, scale_factor=2,
scale_window=1000)
else:
loss_scale_manager = FixedLossScaleManager(loss_scale=args.loss_scale, drop_overflow_update=False)
model = Model(network, loss_fn=loss, optimizer=optimizer, metrics=eval_metrics, amp_level=args.amp_level,
loss_scale_manager=loss_scale_manager)
# callback
# save checkpoint, summary training loss
# recorad val acc and do model selection if val dataset is availabe
begin_epoch = 0
if args.ckpt_path != '':
if args.ckpt_path != '':
begin_step = optimizer.global_step.asnumpy()[0]
begin_epoch = args.ckpt_path.split('/')[-1].split('-')[1].split('_')[0]
begin_epoch = int(begin_epoch)
summary_dir = f"./{args.ckpt_save_dir}/summary"
assert (args.ckpt_save_policy != 'top_k' or args.val_while_train == True), \
"ckpt_save_policy is top_k, val_while_train must be True."
state_cb = StateMonitor(model, summary_dir=summary_dir,
dataset_val=loader_eval,
val_interval=args.val_interval,
metric_name=list(eval_metrics.keys()),
ckpt_dir=args.ckpt_save_dir,
ckpt_save_interval=args.ckpt_save_interval,
best_ckpt_name=args.model + '_best.ckpt',
rank_id=rank_id,
device_num=device_num,
log_interval=args.log_interval,
keep_checkpoint_max=args.keep_checkpoint_max,
model_name=args.model,
last_epoch=begin_epoch,
ckpt_save_policy=args.ckpt_save_policy)
callbacks = [state_cb]
# log
if rank_id in [None, 0]:
logger.info(f"-" * 40)
logger.info(f"Num devices: {device_num if device_num is not None else 1} \n"
f"Distributed mode: {args.distribute} \n"
f"Num training samples: {train_count}")
if args.val_while_train:
logger.info(f"Num validation samples: {eval_count}")
logger.info(f"Num classes: {num_classes} \n"
f"Num batches: {num_batches} \n"
f"Batch size: {args.batch_size} \n"
f"Auto augment: {args.auto_augment} \n"
f"Model: {args.model} \n"
f"Model param: {num_params} \n"
f"Num epochs: {args.epoch_size} \n"
f"Optimizer: {args.opt} \n"
f"LR: {args.lr} \n"
f"LR Scheduler: {args.scheduler}")
logger.info(f"-" * 40)
if args.ckpt_path != '':
logger.info(f"Resume training from {args.ckpt_path}, last step: {begin_step}, last epoch: {begin_epoch}")
else:
logger.info('Start training')
model.train(args.epoch_size, loader_train, callbacks=callbacks, dataset_sink_mode=args.dataset_sink_mode)
if __name__ == '__main__':
args = parse_args()
# data sync for cloud platform if enabled
if args.enable_modelarts:
import moxing as mox
args.data_dir = f'/cache/{args.data_url}'
mox.file.copy_parallel(src_url= os.path.join(args.data_url, args.dataset) , dst_url= args.data_dir)
# core training
train(args)
if args.enable_modelarts:
mox.file.copy_parallel(src_url= args.ckpt_save_dir, dst_url=args.train_url)