-
Notifications
You must be signed in to change notification settings - Fork 91
/
tune_fgvc.py
208 lines (178 loc) · 5.71 KB
/
tune_fgvc.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
"""
tune lr, wd for fgvc datasets and other datasets with train / val / test splits
"""
import os
import warnings
from time import sleep
from random import randint
from src.configs.config import get_cfg
from src.utils.file_io import PathManager
from train import train as train_main
from launch import default_argument_parser
warnings.filterwarnings("ignore")
def setup(args, lr, wd, check_runtime=True):
"""
Create configs and perform basic setups.
overwrite the 2 parameters in cfg and args
"""
cfg = get_cfg()
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
# setup dist
cfg.DIST_INIT_PATH = "tcp://{}:4000".format(os.environ["SLURMD_NODENAME"])
# overwrite below four parameters
lr = lr / 256 * cfg.DATA.BATCH_SIZE # update lr based on the batchsize
cfg.SOLVER.BASE_LR = lr
cfg.SOLVER.WEIGHT_DECAY = wd
# setup output dir
# output_dir / data_name / feature_name / lr_wd / run1
output_dir = cfg.OUTPUT_DIR
output_folder = os.path.join(
cfg.DATA.NAME, cfg.DATA.FEATURE, f"lr{lr}_wd{wd}"
)
# output_folder = os.path.splitext(os.path.basename(args.config_file))[0]
# train cfg.RUN_N_TIMES times
if check_runtime:
count = 1
while count <= cfg.RUN_N_TIMES:
output_path = os.path.join(output_dir, output_folder, f"run{count}")
# pause for a random time, so concurrent process with same setting won't interfere with each other. # noqa
sleep(randint(1, 5))
if not PathManager.exists(output_path):
PathManager.mkdirs(output_path)
cfg.OUTPUT_DIR = output_path
break
else:
count += 1
if count > cfg.RUN_N_TIMES:
raise ValueError(
f"Already run {cfg.RUN_N_TIMES} times for {output_folder}, no need to run more")
else:
# only used for dummy config file
output_path = os.path.join(output_dir, output_folder, f"run1")
cfg.OUTPUT_DIR = output_path
cfg.freeze()
return cfg
def finetune_main(args):
lr_range = [0.001, 0.0001, 0.0005, 0.005]
wd_range = [0.01, 0.001, 0.0001, 0.0]
for wd in wd_range:
for lr in lr_range:
# set up cfg and args
try:
cfg = setup(args, lr, wd)
except ValueError:
continue
train_main(cfg, args)
def finetune_rn_main(args):
lr_range = [
0.05, 0.025, 0.005, 0.0025
]
wd_range = [0.01, 0.001, 0.0001, 0.0]
for wd in wd_range:
for lr in lr_range:
# set up cfg and args
try:
cfg = setup(args, lr, wd)
except ValueError as e:
print(e)
continue
train_main(cfg, args)
def prompt_rn_main(args):
lr_range = [
0.05, 0.025, 0.01, 0.5, 0.25, 0.1,
1.0, 2.5, 5.
]
wd_range = [0.01, 0.001, 0.0001, 0.0]
for lr in sorted(lr_range, reverse=True):
for wd in wd_range:
# set up cfg and args
try:
cfg = setup(args, lr, wd)
except ValueError as e:
print(e)
continue
train_main(cfg, args)
def linear_main(args):
lr_range = [
50.0, 25., 10.0,
5.0, 2.5, 1.0,
0.5, 0.25, 0.1, 0.05
]
wd_range = [0.01, 0.001, 0.0001, 0.0]
for lr in lr_range:
for wd in wd_range:
# set up cfg and args
try:
cfg = setup(args, lr, wd)
except ValueError:
continue
train_main(cfg, args)
sleep(randint(1, 10))
def linear_mae_main(args):
lr_range = [
50.0, 25., 10.0,
5.0, 2.5, 1.0,
0.5, 0.25, 0.1, 0.05,
0.025, 0.005, 0.0025,
]
wd_range = [0.01, 0.001, 0.0001, 0.0]
for lr in lr_range:
for wd in wd_range:
# set up cfg and args
try:
cfg = setup(args, lr, wd)
except ValueError:
continue
train_main(cfg, args)
sleep(randint(1, 10))
def prompt_main(args):
lr_range = [
5.0, 2.5, 1.0,
50.0, 25., 10.0,
0.5, 0.25, 0.1,
]
wd_range = [0.01, 0.001, 0.0001, 0.0]
for lr in lr_range:
for wd in wd_range:
# set up cfg and args
try:
cfg = setup(args, lr, wd)
except ValueError:
continue
train_main(cfg, args)
sleep(randint(1, 10))
def prompt_main_largerrange(args):
lr_range = [
500, 1000, # for parralel-based prompt for stanford cars
250., 100.0, # for parralel-based prompt for stanford cars
]
wd_range = [0.0, 0.01, 0.001, 0.0001]
for lr in lr_range:
for wd in wd_range:
# set up cfg and args
try:
cfg = setup(args, lr, wd)
except ValueError:
continue
train_main(cfg, args)
sleep(randint(1, 10))
def main(args):
"""main function to call from workflow"""
if args.train_type == "finetune":
finetune_main(args)
elif args.train_type == "finetune_resnet":
finetune_rn_main(args)
elif args.train_type == "linear":
linear_main(args)
elif args.train_type == "linear_mae":
linear_mae_main(args)
elif args.train_type == "prompt":
prompt_main(args)
elif args.train_type == "prompt_resnet":
prompt_rn_main(args)
elif args.train_type == "prompt_largerrange" or args.train_type == "prompt_largerlr": # noqa
prompt_main_largerrange(args)
if __name__ == '__main__':
args = default_argument_parser().parse_args()
main(args)