-
Notifications
You must be signed in to change notification settings - Fork 24
/
grid_tools.py
183 lines (125 loc) · 6.19 KB
/
grid_tools.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
from math import *
from protos import pipeline_pb2
from google.protobuf import text_format
from sklearn.model_selection import ParameterGrid
def get_config_type(model_config, train_config):
model_type = model_config.WhichOneof('model')
optimizer_type = train_config.optimizer.WhichOneof('optimizer')
learning_rate_type = ''
if optimizer_type == 'rms_prop_optimizer':
learning_rate_type = get_learning_rate(train_config.optimizer.rms_prop_optimizer)
elif optimizer_type == 'momentum_optimizer':
learning_rate_type = get_learning_rate(train_config.optimizer.momentum_optimizer)
elif optimizer_type == 'adam_optimizer':
learning_rate_type = get_learning_rate(train_config.optimizer.adam_optimizer)
return model_type, optimizer_type, learning_rate_type
def get_learning_rate(optimizer):
return optimizer.learning_rate.WhichOneof('learning_rate')
def set_rssd_hyperparameter(rssd, config):
if 'angles' in config.keys():
rssd.anchor_generator.rssd_anchor_generator.angles[:] = config['angles']
if 'aspect_ratios' in config.keys():
rssd.anchor_generator.rssd_anchor_generator.aspect_ratios[:] = config['aspect_ratios']
if 'max_scale' in config.keys():
rssd.anchor_generator.rssd_anchor_generator.max_scale = config['max_scale']
if 'min_scale' in config.keys():
rssd.anchor_generator.rssd_anchor_generator.max_scale = config['min_scale']
def set_ssd_hyperparameter(ssd, config):
if 'aspect_ratios' in config.keys():
ssd.anchor_generator.ssd_anchor_generator.aspect_ratios[:] = config['aspect_ratios']
if 'max_scale' in config.keys():
ssd.anchor_generator.ssd_anchor_generator.max_scale = config['max_scale']
if 'min_scale' in config.keys():
ssd.anchor_generator.ssd_anchor_generator.max_scale = config['min_scale']
def set_faster_rcnn_hyperparameter(faster_rcnn, config):
# TODO(SeungHyunJeon): implementation faster rcnn hyperparameter
pass
def set_rms_hyperparameter(rms, lr_type, config):
if 'momentum_optimizer_value' in config.keys():
rms.momentum_optimizer_value = config['momentum_optimizer_value']
if 'decay' in config.keys():
rms.decay = config['decay']
if 'epsilon' in config.keys():
rms.epsilon = config['epsilon']
set_learning_rate_hyperparameter(rms.learning_rate, lr_type, config)
def set_momentum_hyperparameter(momentum, lr_type, config):
# TODO(SeungHyunJeon): implementation momentum hyperparameter
pass
def set_adam_hyperparameter(adam, lr_type, config):
# TODO(SeungHyunJeon): implementation adam hyperparameter
pass
def set_learning_rate_hyperparameter(lr, lr_type, config):
if lr_type == 'constant_learning_rate':
pass
elif lr_type == 'exponential_decay_learning_rate':
if 'init_learning_rate' in config.keys():
lr.exponential_decay_learning_rate.initial_learning_rate = config['init_learning_rate']
if 'decay_steps' in config.keys():
lr.exponential_decay_learning_rate.decay_steps = config['decay_steps']
if 'decay_factor' in config.keys():
lr.exponential_decay_learning_rate.decay_factor = config['decay_factor']
elif lr_type == 'manual_step_learning_rate':
pass
def set_hyperparameter(model_type, optimizer_type, learning_rate_type, model_config, train_config, config):
if model_type == 'rssd':
set_rssd_hyperparameter(model_config.rssd, config)
elif model_type == 'ssd':
set_ssd_hyperparameter(model_config.ssd, config)
elif model_type == 'faster_rcnn':
set_ssd_hyperparameter(model_config.faster_rcnn, config)
if optimizer_type == 'rms_prop_optimizer':
set_rms_hyperparameter(train_config.optimizer.rms_prop_optimizer, learning_rate_type, config)
elif optimizer_type == 'momentum_optimizer':
set_momentum_hyperparameter(train_config.optimizer.momentum_optimizer, learning_rate_type, config)
elif optimizer_type == 'adam_optimizer':
set_adam_hyperparameter(train_config.optimizer.adam_optimizer, learning_rate_type, config)
def parse_config(argv):
argc = len(argv)
config = {}
for i in range(1, argc):
if argv[i][:2] == '--':
continue
key, value = argv[i].split('=')
value = list(map(float, value.split(',')))
config[key] = value
return config
def build_config(config):
if 'angle' in config.keys():
angles = []
for step in config['angle']:
angle = [radians(x) for x in range(-90, 91, int(step))]
angles.append(angle)
config['angles'] = angles
config.pop('angle', None)
def main(argv):
def get_configs_from_pipeline_file():
"""Reads training configuration from a pipeline_pb2.TrainEvalPipelineConfig.
Reads training config from file specified by pipeline_config_path flag.
Returns:
model_config: model_pb2.DetectionModel
train_config: train_pb2.TrainConfig
input_config: input_reader_pb2.InputReader
"""
pipeline_config = pipeline_pb2.TrainEvalPipelineConfig()
with tf.gfile.GFile(FLAGS.pipeline_config_path, 'r') as f:
text_format.Merge(f.read(), pipeline_config)
model_config = pipeline_config.model
train_config = pipeline_config.train_config
input_config = pipeline_config.train_input_reader
return model_config, train_config, input_config
flags = tf.app.flags
flags.DEFINE_string('pipeline_config_path', 'configs/ssd_inception_v2_hsrc2016_rbbox.config',
'Path to a pipeline_pb2.TrainEvalPipelineConfig config '
'file. If provided, other configs are ignored')
FLAGS = flags.FLAGS
config = parse_config(argv)
build_config(config)
configs = ParameterGrid(config)
model_config, train_config, input_config = get_configs_from_pipeline_file()
model_type, optimizer_type, learning_rate_type = get_config_type(model_config, train_config)
set_hyperparameter(model_type, optimizer_type, learning_rate_type, model_config, train_config, configs[0])
print(model_config)
if __name__ == '__main__':
import sys
import tensorflow as tf
tf.app.run(argv=sys.argv)