-
Notifications
You must be signed in to change notification settings - Fork 5
/
tune_fgvc.py
278 lines (245 loc) · 8.07 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
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
"""
tune lr, wd for fgvc datasets and other datasets with train / val / test splits, should find the best results among 5 runs manually
"""
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")
# make small changes
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 # comment here
# 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
if 'P_VK' in cfg.MODEL.TRANSFER_TYPE:
P_NUM = cfg.MODEL.P_VK.NUM_TOKENS_P
VK_NUM = cfg.MODEL.P_VK.NUM_TOKENS
SHARED = cfg.MODEL.P_VK.SHARE_PARAM_KV
INIT = cfg.MODEL.P_VK.ORIGIN_INIT
SHARED_ACC = cfg.MODEL.P_VK.SHARED_ACCROSS
if SHARED == True:
marker = 1
else:
marker = 0
if INIT == 0:
init = 0
elif INIT == 1:
init = 1
else:
init = 2
if SHARED_ACC == True:
shared_acc = 1
else:
shared_acc = 0
# print(f"_P{P_NUM}_VK{VK_NUM}_SHARED_{marker}_INIT_{init}_ACC_{shared_acc}")
Data_Name_With_PVK = cfg.DATA.NAME + f"_P{P_NUM}_VK{VK_NUM}_SHARED_{marker}_INIT_{init}_ACC_{shared_acc}"
# setup output dir
# output_dir / data_name / feature_name / lr_wd / run1
output_dir = cfg.OUTPUT_DIR
if 'P_VK' in cfg.MODEL.TRANSFER_TYPE:
output_folder = os.path.join(
Data_Name_With_PVK, cfg.DATA.FEATURE, f"lr{lr}_wd{wd}"
)
else:
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
print('Should run times:', cfg.RUN_N_TIMES)
print('Current time', count)
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 QKV_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 QKV_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)
elif args.train_type == "QKV" or "P_VK":
QKV_main(args)
# elif args.train_type == "QKV_resnet":
# prompt_rn_main(args)
elif args.train_type == "QKV_largerrange" or args.train_type == "QKV_largerlr" or args.train_type == "P_VK_largerrange" or args.train_type == "P_VK_largerlr": # noqa
QKV_main_largerrange(args)
if __name__ == '__main__':
args = default_argument_parser().parse_args()
main(args)