This repository has been archived by the owner on Oct 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_resnext.py
148 lines (138 loc) · 6.66 KB
/
train_resnext.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
import argparse
import logging
import os
import time
import mxnet as mx
from symbol_resnext import resnext
fname = time.strftime("%Y%m%d%H%M%S", time.localtime())
logging.basicConfig(level=logging.INFO,
filename='log/'+fname+'.log', filemode='w')
logger = logging.getLogger()
data_type = 'cifar10'
def multi_factor_scheduler(begin_epoch, epoch_size, step=[60, 75, 90], factor=0.1):
step_ = [epoch_size * (x-begin_epoch) for x in step if x-begin_epoch > 0]
return mx.lr_scheduler.MultiFactorScheduler(step=step_, factor=factor) if len(step_) else None
def main():
if (args.depth-2) % 9 == 0: # and args.depth >= 164:
per_unit = [(args.depth-2) / 9]
filter_list = [16, 64, 128, 256]
bottle_neck = True
# elif (args.depth-2) % 6 == 0 and args.depth < 164:
# per_unit = [(args.depth-2) / 6]
# filter_list = [16, 16, 32, 64]
# bottle_neck = False
else:
raise ValueError(
"no experiments done on detph {}, you can do it youself".format(args.depth))
units = per_unit*3
symbol = resnext(units=units, num_stage=3, filter_list=filter_list, num_class=args.num_classes, num_group=args.num_groups,
bottle_neck=bottle_neck, bn_mom=args.bn_mom, workspace=args.workspace, memonger=args.memonger)
kv = mx.kvstore.create(args.kv_store)
devs = mx.cpu() if args.gpus is None else [
mx.gpu(int(i)) for i in args.gpus.split(',')]
epoch_size = max(
int(args.num_examples / args.batch_size / kv.num_workers), 1)
begin_epoch = args.model_load_epoch if args.model_load_epoch else 0
if not os.path.exists("./model"):
os.mkdir("./model")
model_prefix = "model/resnext-{}-{}-{}".format(
data_type, args.depth, kv.rank)
checkpoint = mx.callback.do_checkpoint(model_prefix)
arg_params = None
aux_params = None
if args.retrain:
_, arg_params, aux_params = mx.model.load_checkpoint(
model_prefix, args.model_load_epoch)
if args.memonger:
import memonger
symbol = memonger.search_plan(
symbol, data=(args.batch_size, 3, 32, 32))
train = mx.io.ImageRecordIter(
path_imgrec = os.path.join(args.data_dir, "cifar10_train.rec"),
label_width = 1,
data_shape = (3, 32, 32),
num_parts = kv.num_workers,
part_index = kv.rank,
shuffle = True,
batch_size = args.batch_size,
rand_crop = True,
fill_value = 127, # only used when pad is valid
pad = 4,
rand_mirror = True,
)
val = mx.io.ImageRecordIter(
path_imgrec = os.path.join(args.data_dir, "cifar10_val.rec"),
label_width = 1,
data_shape = (3, 32, 32),
num_parts = kv.num_workers,
part_index = kv.rank,
batch_size = args.batch_size,
)
model = mx.mod.Module(
symbol = symbol,
data_names = ('data', ),
label_names = ('softmax_label', ),
context = devs,
)
model.fit(
train_data = train,
eval_data = val,
eval_metric = ['acc'],
epoch_end_callback = checkpoint,
batch_end_callback = mx.callback.Speedometer(args.batch_size, args.frequent),
kvstore = kv,
optimizer = 'nag',
optimizer_params = (('learning_rate', args.lr), ('momentum', args.mom), ('wd', args.wd), (
'lr_scheduler', multi_factor_scheduler(begin_epoch, epoch_size, step=[80], factor=0.1))),
initializer = mx.init.Xavier(rnd_type='gaussian', factor_type="in", magnitude=2),
arg_params = arg_params,
aux_params = aux_params,
allow_missing = True,
begin_epoch = begin_epoch,
num_epoch = args.end_epoch,
)
# logging.info("top-1 and top-5 acc is {}".format(model.score(X = val,
# eval_metric = ['acc', mx.metric.create('top_k_accuracy', top_k = 5)])))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="command for training resnext")
parser.add_argument('--gpus', type=str, default=None,
help='the gpus will be used, e.g "0,1,2,3"')
parser.add_argument('--data-dir', type=str, default='./data/cifar10/',
help='the input data directory')
parser.add_argument('--lr', type=float, default=0.1,
help='initialization learning reate')
parser.add_argument('--mom', type=float, default=0.9,
help='momentum for sgd')
parser.add_argument('--bn-mom', type=float, default=0.9,
help='momentum for batch normlization')
parser.add_argument('--wd', type=float, default=0.0001,
help='weight decay for sgd')
parser.add_argument('--batch-size', type=int, default=128,
help='the batch size')
parser.add_argument('--workspace', type=int, default=512,
help='memory space size(MB) used in convolution, if xpu '
' memory is oom, then you can try smaller vale, such as --workspace 256')
parser.add_argument('--depth', type=int, default=164,
help='the depth of resnext')
parser.add_argument('--num-groups', type=int, default=32,
help='the number of groups in convolution')
parser.add_argument('--num-classes', type=int, default=10,
help='the class number of your task')
parser.add_argument('--num-examples', type=int, default=50000,
help='the number of training examples')
parser.add_argument('--kv-store', type=str, default='device',
help='the kvstore type')
parser.add_argument('--model-load-epoch', type=int, default=0,
help='load the model on an epoch using the model-load-prefix')
parser.add_argument('--end-epoch', type=int, default=120,
help='training ends at this num of epoch')
parser.add_argument('--frequent', type=int, default=50,
help='frequency of logging')
parser.add_argument('--memonger', action='store_true', default=False,
help='true means using memonger to save momory, https://github.com/dmlc/mxnet-memonger')
parser.add_argument('--retrain', action='store_true', default=False,
help='true means continue training')
args = parser.parse_args()
logging.info(args)
main()