-
Notifications
You must be signed in to change notification settings - Fork 92
/
tune_vtab.py
297 lines (250 loc) · 9.39 KB
/
tune_vtab.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
#!/usr/bin/env python3
"""
major actions here for training VTAB datasets: use val200 to find best lr/wd, and retrain on train800val200, report results on test
"""
import glob
import numpy as np
import os
import torch
import warnings
import random
from time import sleep
from random import randint
import src.utils.logging as logging
from src.configs.config import get_cfg
from src.data import loader as data_loader
from src.engine.evaluator import Evaluator
from src.engine.trainer import Trainer
from src.models.build_model import build_model
from src.utils.file_io import PathManager
from launch import default_argument_parser, logging_train_setup
warnings.filterwarnings("ignore")
DATA2CLS = {
'caltech101': 102,
'cifar(num_classes=100)': 100,
'dtd': 47,
'oxford_flowers102': 102,
'oxford_iiit_pet': 37,
'patch_camelyon': 2,
'sun397': 397,
'svhn': 10,
'resisc45': 45,
'eurosat': 10,
'dmlab': 6,
'kitti(task="closest_vehicle_distance")': 4,
'smallnorb(predicted_attribute="label_azimuth")': 18,
'smallnorb(predicted_attribute="label_elevation")': 9,
'dsprites(predicted_attribute="label_x_position",num_classes=16)': 16,
'dsprites(predicted_attribute="label_orientation",num_classes=16)': 16,
'clevr(task="closest_object_distance")': 6,
'clevr(task="count_all")': 8,
'diabetic_retinopathy(config="btgraham-300")': 5
}
def find_best_lrwd(files, data_name):
t_name = "val_" + data_name
best_lr = None
best_wd = None
best_val_acc = -1
for f in files:
try:
results_dict = torch.load(f, "cpu")
epoch = len(results_dict) - 1
val_result = results_dict[f"epoch_{epoch}"]["classification"][t_name]["top1"]
val_result = float(val_result)
except Exception as e:
print(f"Encounter issue: {e} for file {f}")
continue
if val_result == best_val_acc:
frag_txt = f.split("/run")[0]
cur_lr = float(frag_txt.split("/lr")[-1].split("_wd")[0])
cur_wd = float(frag_txt.split("_wd")[-1])
if best_lr is not None and cur_lr < best_lr:
# get the smallest lr to break tie for stability
best_lr = cur_lr
best_wd = cur_wd
best_val_acc = val_result
elif val_result > best_val_acc:
best_val_acc = val_result
frag_txt = f.split("/run")[0]
best_lr = float(frag_txt.split("/lr")[-1].split("_wd")[0])
best_wd = float(frag_txt.split("_wd")[-1])
return best_lr, best_wd
def setup(args, lr, wd, final_runs, run_idx=None, seed=None):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg()
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.SEED = seed
# create the clsemb_path for this dataset, only support vitb-sup experiments
if cfg.DATA.FEATURE == "sup_vitb16_imagenet21k":
cfg.MODEL.PROMPT.CLSEMB_PATH = os.path.join(
cfg.MODEL.PROMPT.CLSEMB_FOLDER, "{}.npy".format(cfg.DATA.NAME))
if not final_runs:
cfg.RUN_N_TIMES = 1
cfg.MODEL.SAVE_CKPT = False
cfg.OUTPUT_DIR = cfg.OUTPUT_DIR + "_val"
lr = lr / 256 * cfg.DATA.BATCH_SIZE # update lr based on the batchsize
cfg.SOLVER.BASE_LR = lr
cfg.SOLVER.WEIGHT_DECAY = wd
else:
cfg.RUN_N_TIMES = 5
cfg.MODEL.SAVE_CKPT = False
# find the best lr and best wd
files = glob.glob(f"{cfg.OUTPUT_DIR}_val/{cfg.DATA.NAME}/{cfg.DATA.FEATURE}/*/run1/eval_results.pth")
lr, wd = find_best_lrwd(files, cfg.DATA.NAME)
cfg.OUTPUT_DIR = cfg.OUTPUT_DIR + "_finalfinal"
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}"
)
# train cfg.RUN_N_TIMES times
if run_idx is None:
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:
output_path = os.path.join(output_dir, output_folder, f"run{run_idx}")
if not PathManager.exists(output_path):
PathManager.mkdirs(output_path)
cfg.OUTPUT_DIR = output_path
else:
raise ValueError(
f"Already run run-{run_idx} for {output_folder}, no need to run more")
cfg.freeze()
return cfg
def get_loaders(cfg, logger, final_runs=False):
# support two training paradims:
# 1) train / val / test, using val to tune
# 2) train / val: for imagenet
if not final_runs:
logger.info("Loading training data...")
train_loader = data_loader.construct_train_loader(cfg)
logger.info("Loading validation data...")
val_loader = data_loader.construct_val_loader(cfg)
# not really nessecary to check the results of test set.
test_loader = None
else:
logger.info("Loading training data...")
train_loader = data_loader.construct_trainval_loader(cfg)
# not really nessecary to check the results of val set, but the trainer class does not support no-validation loader yet # noqa
logger.info("Loading validation data...")
val_loader = data_loader.construct_val_loader(cfg)
logger.info("Loading test data...")
test_loader = data_loader.construct_test_loader(cfg)
return train_loader, val_loader, test_loader
def train(cfg, args, final_runs):
# clear up residual cache from previous runs
if torch.cuda.is_available():
torch.cuda.empty_cache()
# main training / eval actions here
# fix the seed for reproducibility
if cfg.SEED is not None:
torch.manual_seed(cfg.SEED)
np.random.seed(cfg.SEED)
random.seed(0)
# setup training env including loggers
logging_train_setup(args, cfg)
logger = logging.get_logger("visual_prompt")
train_loader, val_loader, test_loader = get_loaders(
cfg, logger, final_runs)
logger.info("Constructing models...")
model, cur_device = build_model(cfg)
logger.info("Setting up Evalutator...")
evaluator = Evaluator()
logger.info("Setting up Trainer...")
trainer = Trainer(cfg, model, evaluator, cur_device)
if train_loader:
trainer.train_classifier(train_loader, val_loader, test_loader)
# save the evaluation results
torch.save(
evaluator.results,
os.path.join(cfg.OUTPUT_DIR, "eval_results.pth")
)
else:
print("No train loader presented. Exit")
def get_lrwd_range(args):
if args.train_type == "finetune":
lr_range = [0.001, 0.0001, 0.0005, 0.005]
wd_range = [0.01, 0.001, 0.0001, 0.0]
elif args.train_type == "finetune_resnet":
lr_range = [
0.0005, 0.00025,
0.5, 0.25, 0.05, 0.025, 0.005, 0.0025,
]
wd_range = [0.01, 0.001, 0.0001, 0.0]
elif args.train_type == "linear":
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]
elif args.train_type == "linear_mae":
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]
elif args.train_type == "prompt":
lr_range = [
5.0, 2.5, 1.0,
50.0, 25., 10.0,
0.5, 0.25, 0.1, 0.05
]
wd_range = [0.01, 0.001, 0.0001, 0.0]
elif args.train_type == "prompt_largerlr":
lr_range = [
500, 1000, 250., 100.0,
]
wd_range = [0.01, 0.001, 0.0001, 0.0]
elif args.train_type == "prompt_resnet":
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]
return lr_range, wd_range
def main(args):
"""main function to call from workflow"""
# tuning lr and wd first:
lr_range, wd_range = get_lrwd_range(args)
for lr in sorted(lr_range, reverse=True):
for wd in sorted(wd_range, reverse=True):
try:
cfg = setup(args, lr, wd, final_runs=False)
except ValueError:
# already ran
continue
train(cfg, args, final_runs=False)
# final run 5 times with fixed seed
random_seeds = [42, 44, 82, 100, 800]
for run_idx, seed in enumerate(random_seeds):
try:
cfg = setup(
args, 0.1, 0.1, final_runs=True, run_idx=run_idx+1, seed=seed)
except ValueError:
# already ran
continue
train(cfg, args, final_runs=True)
if __name__ == '__main__':
args = default_argument_parser().parse_args()
main(args)