forked from mit-han-lab/proxylessnas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagenet_arch_search.py
209 lines (184 loc) · 8.75 KB
/
imagenet_arch_search.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
# ProxylessNAS: Direct Neural Architecture Search on Target Task and Hardware
# Han Cai, Ligeng Zhu, Song Han
# International Conference on Learning Representations (ICLR), 2019.
import argparse
from models import ImagenetRunConfig
from nas_manager import *
from models.super_nets.super_proxyless import SuperProxylessNASNets
# ref values
ref_values = {
'flops': {
'0.35': 59 * 1e6,
'0.50': 97 * 1e6,
'0.75': 209 * 1e6,
'1.00': 300 * 1e6,
'1.30': 509 * 1e6,
'1.40': 582 * 1e6,
},
# ms
'mobile': {
'1.00': 80,
},
'cpu': {},
'gpu8': {},
}
parser = argparse.ArgumentParser()
parser.add_argument('--path', type=str, default=None)
parser.add_argument('--gpu', help='gpu available', default='0,1,2,3')
parser.add_argument('--resume', action='store_true')
parser.add_argument('--debug', help='freeze the weight parameters', action='store_true')
parser.add_argument('--manual_seed', default=0, type=int)
""" run config """
parser.add_argument('--n_epochs', type=int, default=120)
parser.add_argument('--init_lr', type=float, default=0.025)
parser.add_argument('--lr_schedule_type', type=str, default='cosine')
# lr_schedule_param
parser.add_argument('--dataset', type=str, default='imagenet', choices=['imagenet'])
parser.add_argument('--train_batch_size', type=int, default=256)
parser.add_argument('--test_batch_size', type=int, default=1000)
parser.add_argument('--valid_size', type=int, default=50000)
parser.add_argument('--opt_type', type=str, default='sgd', choices=['sgd'])
parser.add_argument('--momentum', type=float, default=0.9) # opt_param
parser.add_argument('--no_nesterov', action='store_true') # opt_param
parser.add_argument('--weight_decay', type=float, default=4e-5)
parser.add_argument('--label_smoothing', type=float, default=0.1)
parser.add_argument('--no_decay_keys', type=str, default=None, choices=[None, 'bn', 'bn#bias'])
parser.add_argument('--model_init', type=str, default='he_fout', choices=['he_fin', 'he_fout'])
parser.add_argument('--init_div_groups', action='store_true')
parser.add_argument('--validation_frequency', type=int, default=1)
parser.add_argument('--print_frequency', type=int, default=10)
parser.add_argument('--n_worker', type=int, default=32)
parser.add_argument('--resize_scale', type=float, default=0.08)
parser.add_argument('--distort_color', type=str, default='normal', choices=['normal', 'strong', 'None'])
""" net config """
parser.add_argument('--width_stages', type=str, default='24,40,80,96,192,320')
parser.add_argument('--n_cell_stages', type=str, default='4,4,4,4,4,1')
parser.add_argument('--stride_stages', type=str, default='2,2,2,1,2,1')
parser.add_argument('--width_mult', type=float, default=1.0)
parser.add_argument('--bn_momentum', type=float, default=0.1)
parser.add_argument('--bn_eps', type=float, default=1e-3)
parser.add_argument('--dropout', type=float, default=0)
# architecture search config
""" arch search algo and warmup """
parser.add_argument('--arch_algo', type=str, default='grad', choices=['grad', 'rl'])
parser.add_argument('--warmup_epochs', type=int, default=40)
""" shared hyper-parameters """
parser.add_argument('--arch_init_type', type=str, default='normal', choices=['normal', 'uniform'])
parser.add_argument('--arch_init_ratio', type=float, default=1e-3)
parser.add_argument('--arch_opt_type', type=str, default='adam', choices=['adam'])
parser.add_argument('--arch_lr', type=float, default=1e-3)
parser.add_argument('--arch_adam_beta1', type=float, default=0) # arch_opt_param
parser.add_argument('--arch_adam_beta2', type=float, default=0.999) # arch_opt_param
parser.add_argument('--arch_adam_eps', type=float, default=1e-8) # arch_opt_param
parser.add_argument('--arch_weight_decay', type=float, default=0)
parser.add_argument('--target_hardware', type=str, default=None, choices=['mobile', 'cpu', 'gpu8', 'flops', None])
""" Grad hyper-parameters """
parser.add_argument('--grad_update_arch_param_every', type=int, default=5)
parser.add_argument('--grad_update_steps', type=int, default=1)
parser.add_argument('--grad_binary_mode', type=str, default='full_v2', choices=['full_v2', 'full', 'two'])
parser.add_argument('--grad_data_batch', type=int, default=None)
parser.add_argument('--grad_reg_loss_type', type=str, default='mul#log', choices=['add#linear', 'mul#log'])
parser.add_argument('--grad_reg_loss_lambda', type=float, default=1e-1) # grad_reg_loss_params
parser.add_argument('--grad_reg_loss_alpha', type=float, default=0.2) # grad_reg_loss_params
parser.add_argument('--grad_reg_loss_beta', type=float, default=0.3) # grad_reg_loss_params
""" RL hyper-parameters """
parser.add_argument('--rl_batch_size', type=int, default=10)
parser.add_argument('--rl_update_per_epoch', action='store_true')
parser.add_argument('--rl_update_steps_per_epoch', type=int, default=300)
parser.add_argument('--rl_baseline_decay_weight', type=float, default=0.99)
parser.add_argument('--rl_tradeoff_ratio', type=float, default=0.1)
if __name__ == '__main__':
args = parser.parse_args()
torch.manual_seed(args.manual_seed)
torch.cuda.manual_seed_all(args.manual_seed)
np.random.seed(args.manual_seed)
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
os.makedirs(args.path, exist_ok=True)
# build run config from args
args.lr_schedule_param = None
args.opt_param = {
'momentum': args.momentum,
'nesterov': not args.no_nesterov,
}
run_config = ImagenetRunConfig(
**args.__dict__
)
# debug, adjust run_config
if args.debug:
run_config.train_batch_size = 256
run_config.test_batch_size = 256
run_config.valid_size = 256
run_config.n_worker = 0
width_stages_str = '-'.join(args.width_stages.split(','))
# build net from args
args.width_stages = [int(val) for val in args.width_stages.split(',')]
args.n_cell_stages = [int(val) for val in args.n_cell_stages.split(',')]
args.stride_stages = [int(val) for val in args.stride_stages.split(',')]
args.conv_candidates = [
'3x3_MBConv3', '3x3_MBConv6',
'5x5_MBConv3', '5x5_MBConv6',
'7x7_MBConv3', '7x7_MBConv6',
]
super_net = SuperProxylessNASNets(
width_stages=args.width_stages, n_cell_stages=args.n_cell_stages, stride_stages=args.stride_stages,
conv_candidates=args.conv_candidates, n_classes=run_config.data_provider.n_classes, width_mult=args.width_mult,
bn_param=(args.bn_momentum, args.bn_eps), dropout_rate=args.dropout
)
# build arch search config from args
if args.arch_opt_type == 'adam':
args.arch_opt_param = {
'betas': (args.arch_adam_beta1, args.arch_adam_beta2),
'eps': args.arch_adam_eps,
}
else:
args.arch_opt_param = None
if args.target_hardware is None:
args.ref_value = None
else:
args.ref_value = ref_values[args.target_hardware]['%.2f' % args.width_mult]
if args.arch_algo == 'grad':
from nas_manager import GradientArchSearchConfig
if args.grad_reg_loss_type == 'add#linear':
args.grad_reg_loss_params = {'lambda': args.grad_reg_loss_lambda}
elif args.grad_reg_loss_type == 'mul#log':
args.grad_reg_loss_params = {
'alpha': args.grad_reg_loss_alpha,
'beta': args.grad_reg_loss_beta,
}
else:
args.grad_reg_loss_params = None
arch_search_config = GradientArchSearchConfig(**args.__dict__)
elif args.arch_algo == 'rl':
from nas_manager import RLArchSearchConfig
arch_search_config = RLArchSearchConfig(**args.__dict__)
else:
raise NotImplementedError
print('Run config:')
for k, v in run_config.config.items():
print('\t%s: %s' % (k, v))
print('Architecture Search config:')
for k, v in arch_search_config.config.items():
print('\t%s: %s' % (k, v))
# arch search run manager
arch_search_run_manager = ArchSearchRunManager(args.path, super_net, run_config, arch_search_config)
# resume
if args.resume:
try:
arch_search_run_manager.load_model()
except Exception:
from pathlib import Path
home = str(Path.home())
warmup_path = os.path.join(
home, 'Workspace/Exp/arch_search/%s_ProxylessNAS_%.2f_%s/warmup.pth.tar' %
(run_config.dataset, args.width_mult, width_stages_str)
)
if os.path.exists(warmup_path):
print('load warmup weights')
arch_search_run_manager.load_model(model_fname=warmup_path)
else:
print('fail to load models')
# warmup
if arch_search_run_manager.warmup:
arch_search_run_manager.warm_up(warmup_epochs=args.warmup_epochs)
# joint training
arch_search_run_manager.train(fix_net_weights=args.debug)