-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyperparameter_lr_xent_retrieval.py
183 lines (163 loc) · 7.7 KB
/
hyperparameter_lr_xent_retrieval.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
import train_imgreid_xent_cars196_regularizer as xent_script
import train_imgreid_xent_retrieval_vib as xent_script_vib
import sys
import argparse
from torchreid import models
import os.path as osp
import itertools
from torchreid.utils.logger import Logger
import pdb
from torchreid import data_manager
import math
import numpy as np
from tensorboardX import SummaryWriter
parser = argparse.ArgumentParser(description='HyperParameter Search')
parser.add_argument('-a', '--arch', type=str, default='resnet50', choices=models.get_names())
parser.add_argument('--root', type=str, default='/data/george-data/Dataset',
help="root path to data directory")
parser.add_argument('--gpu-devices', default='0', type=str,
help='gpu device ids for CUDA_VISIBLE_DEVICES')
parser.add_argument('-d', '--dataset', type=str, default='market1501',
choices=data_manager.get_names())
parser.add_argument('--optim', type=str, default='adam',
help="optimization algorithm (see optimizers.py)")
parser.add_argument('--suffix', default='', type=str,
help='suffix to add to directory')
parser.add_argument('--multiplier', default=1, type=int,
help='number of learning rate in multiple of 5')
parser.add_argument('--load-weights', type=str, default='',
help="load pretrained weights but ignores layers that don't match in size")
parser.add_argument('--train-batch', default=32, type=int,
help="train batch size")
parser.add_argument('--cuhk03-labeled', action='store_true',
help="use labeled images, if false, detected images are used (default: False)")
parser.add_argument('--cuhk03-classic-split', action='store_true',
help="use classic split by Li et al. CVPR'14 (default: False)")
parser.add_argument('--use-metric-cuhk03', action='store_true',
help="use cuhk03-metric (default: False)")
parser.add_argument('--use-smoothing', action='store_true',
help="use label smoothing (default: False)")
parser.add_argument('--confidence-penalty', action='store_true',
help="use confidence penalty (default: False)")
parser.add_argument("--crop-img", action='store_true',
help="Crop img based on BBox (default: False)")
def main(args):
src = '/data/george-data/log_resnet_smooth/hyperLearningRate/'
args = parser.parse_args(args)
root = ["--root", args.root]
arch_choice = ["-a", args.arch]
data_choice = ["-d", args.dataset]
optim_choice = ["--optim", args.optim]
epoch_choice = ["--max-epoch", "200"]
train_batch = ["--train-batch", str(args.train_batch)]
test_batch = ["--test-batch", "100"]
height_width = ["--height", "256", "--width", "256"]
label_smooth = []
if args.use_smoothing:
label_smooth = ["--label-smooth"]
confidence_penalty = []
if args.confidence_penalty:
confidence_penalty = ["--confidence-penalty"]
stepSize_choice = ["--stepsize", "20", "40"]
gpu_choice = ["--gpu-devices", args.gpu_devices]
weight_loading = ["--load-weights",args.load_weights]
eval_steps = ["--eval-step", "10"]
enable_scheduler = ["--scheduler","1"]
fixBase = ["--fixbase-epoch","6" ,"--fixbase-lr","0.0003"]
multiplier = args.multiplier
crop_img = []
if args.crop_img:
crop_img = ["--crop-img"]
cuhk03_choice = []
cuhk03_name = ""
if args.cuhk03_labeled:
cuhk03_choice.append('--cuhk03-labeled')
if cuhk03_name != "":
cuhk03_name = "_".join([cuhk03_name,"cuhk03Labeled"])
else:
cuhk03_name = cuhk03_name + "cuhk03Labeled"
if args.cuhk03_classic_split:
cuhk03_choice.append('--cuhk03-classic-split')
if cuhk03_name != "":
cuhk03_name = "_".join([cuhk03_name,"cuhk03ClassicSplit"])
else:
cuhk03_name = cuhk03_name + "cuhk03ClassicSplit"
if args.use_metric_cuhk03:
cuhk03_choice.append('--use-metric-cuhk03')
if cuhk03_name != "":
cuhk03_name = "_".join([cuhk03_name,"cuhk03Metric"])
else:
cuhk03_name = cuhk03_name + "cuhk03Metric"
#lr_search = ["0.0002","0.0003","0.00035","0.0004","0.0005","0.0006","0.0007","0.001"]
aa = math.log(0.0002, 10)
bb = math.log(0.001, 10)
log_lr_list = np.random.uniform(aa, bb, int(5*multiplier)).tolist()
lr_search = [10**xx for xx in log_lr_list]
# Beta
xent_list = np.random.uniform(0.9, 5, int(5)).tolist()*multiplier
#xent_list = [1,1,1,1,1]*multiplier
confidence_beta = np.random.uniform(0.01, 0.2, int(5)).tolist()*multiplier
saved_folders = []
final_results = []
writer_dict={}
writer = SummaryWriter(log_dir=osp.join(src,'Accuracy_vs_LR_xent',"_".join([args.arch,data_choice[1],optim_choice[1], "e"+epoch_choice[1], "b"+train_batch[1],"{}lr".format(5*multiplier),cuhk03_name,args.suffix]), 'tensorboard'))
dict_acc = {}
best_lr = -1
best_xent = -1
best_lr_acc = -1
best_arg = None
print("Learning Rates to train: {}".format(lr_search))
for idx,lr_elem in enumerate(lr_search):
try:
sys.stdout = sys.__stdout__
arg_list = []
lr_choice = ["--learning-rate", str(lr_elem)]
xent = xent_list[idx]
xent_choice = ["--lambda-xent", str(xent)]
if args.use_smoothing:
temp_txt = "LabelSmoothing"
elif args.confidence_penalty:
temp_txt = "confidencePenalty"+str(confidence_beta[idx])
elif "vib" in args.arch:
temp_txt = "vibBeta0.01"
folder_save = osp.join(src,'image_retrieval', "_".join([args.arch,data_choice[1],optim_choice[1], "e"+epoch_choice[1], "b"+train_batch[1],"lr"+lr_choice[1], "xentLambda"+str(xent),'NotPretrained',cuhk03_name,temp_txt,args.suffix]))
if args.confidence_penalty:
confidence_penalty = confidence_penalty + ["--confidence-beta", str(confidence_beta[idx])]
save_dir = ["--save-dir",folder_save]
saved_folders.append(folder_save)
arg_list.extend(root+ arch_choice+data_choice+optim_choice+epoch_choice+train_batch+test_batch+height_width+stepSize_choice+gpu_choice+eval_steps+enable_scheduler+lr_choice+label_smooth+save_dir+fixBase+xent_choice+weight_loading+cuhk03_choice+ confidence_penalty+crop_img)
if "vib" in args.arch:
arg_list.extend(["--beta", "0.01"])
best_rank, best_epoch = xent_script_vib.main(arg_list)
else:
best_rank, best_epoch = xent_script.main(arg_list)
dict_acc[lr_elem] = (xent,best_rank, best_epoch)
writer.add_scalars(
'Acc vs LR',
dict(rank_1= best_rank,
epoch = best_epoch),
int(lr_search.index(lr_elem)))
writer.add_scalars(
'LR_Xent',
dict(learning_rate= lr_elem,
xent_lambda = xent),
int(lr_search.index(lr_elem)))
if best_lr_acc< best_rank:
best_lr = lr_elem
best_xent = xent
best_lr_acc = best_rank
best_arg = arg_list
print(dict_acc)
except Exception as inst:
print(type(inst)) # the exception instance
print(inst.args) # arguments stored in .args
print(inst)
pass
print("#################################")
print("Best Learning rate and xent lambda:")
print("Learning Rate: {}".format(best_lr))
print("Lambda Xent: {}".format(best_xent))
print("Best Rank1: {}".format(best_lr_acc))
print("Argument list: {}".format(best_arg))
if __name__ == '__main__':
main(sys.argv[1:])