forked from ruiminshen/yolo-tf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·175 lines (156 loc) · 9.14 KB
/
train.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
"""
Copyright (C) 2017, 申瑞珉 (Ruimin Shen)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import argparse
import configparser
import importlib
import shutil
import time
import inspect
import multiprocessing
import tensorflow as tf
import tensorflow.contrib.slim as slim
import utils.data
def summary_scalar(config):
try:
reduce = eval(config.get('summary', 'scalar_reduce'))
for t in utils.match_tensor(config.get('summary', 'scalar')):
name = t.op.name
if len(t.get_shape()) > 0:
t = reduce(t)
tf.logging.warn(name + ' is not a scalar tensor, reducing by ' + reduce.__name__)
tf.summary.scalar(name, t)
except (configparser.NoSectionError, configparser.NoOptionError):
tf.logging.warn(inspect.stack()[0][3] + ' disabled')
def summary_image(config):
try:
for t in utils.match_tensor(config.get('summary', 'image')):
name = t.op.name
channels = t.get_shape()[-1].value
if channels not in (1, 3, 4):
t = tf.expand_dims(tf.reduce_sum(t, -1), -1)
tf.summary.image(name, t, config.getint('summary', 'image_max'))
except (configparser.NoSectionError, configparser.NoOptionError):
tf.logging.warn(inspect.stack()[0][3] + ' disabled')
def summary_histogram(config):
try:
for t in utils.match_tensor(config.get('summary', 'histogram')):
tf.summary.histogram(t.op.name, t)
except (configparser.NoSectionError, configparser.NoOptionError):
tf.logging.warn(inspect.stack()[0][3] + ' disabled')
def summary(config):
summary_scalar(config)
summary_image(config)
summary_histogram(config)
def get_optimizer(config, name):
section = 'optimizer_' + name
return {
'adam': lambda learning_rate: tf.train.AdamOptimizer(learning_rate, config.getfloat(section, 'beta1'), config.getfloat(section, 'beta2'), config.getfloat(section, 'epsilon')),
'adadelta': lambda learning_rate: tf.train.AdadeltaOptimizer(learning_rate, config.getfloat(section, 'rho'), config.getfloat(section, 'epsilon')),
'adagrad': lambda learning_rate: tf.train.AdagradOptimizer(learning_rate, config.getfloat(section, 'initial_accumulator_value')),
'momentum': lambda learning_rate: tf.train.MomentumOptimizer(learning_rate, config.getfloat(section, 'momentum')),
'rmsprop': lambda learning_rate: tf.train.RMSPropOptimizer(learning_rate, config.getfloat(section, 'decay'), config.getfloat(section, 'momentum'), config.getfloat(section, 'epsilon')),
'ftrl': lambda learning_rate: tf.train.FtrlOptimizer(learning_rate, config.getfloat(section, 'learning_rate_power'), config.getfloat(section, 'initial_accumulator_value'), config.getfloat(section, 'l1_regularization_strength'), config.getfloat(section, 'l2_regularization_strength')),
'gd': lambda learning_rate: tf.train.GradientDescentOptimizer(learning_rate),
}[name]
def main():
model = config.get('config', 'model')
logdir = utils.get_logdir(config)
if args.delete:
tf.logging.warn('delete logging directory: ' + logdir)
shutil.rmtree(logdir, ignore_errors=True)
cachedir = utils.get_cachedir(config)
with open(os.path.join(cachedir, 'names'), 'r') as f:
names = [line.strip() for line in f]
width = config.getint(model, 'width')
height = config.getint(model, 'height')
cell_width, cell_height = utils.calc_cell_width_height(config, width, height)
tf.logging.warn('(width, height)=(%d, %d), (cell_width, cell_height)=(%d, %d)' % (width, height, cell_width, cell_height))
yolo = importlib.import_module('model.' + model)
paths = [os.path.join(cachedir, profile + '.tfrecord') for profile in args.profile]
num_examples = sum(sum(1 for _ in tf.python_io.tf_record_iterator(path)) for path in paths)
tf.logging.warn('num_examples=%d' % num_examples)
with tf.name_scope('batch'):
image_rgb, labels = utils.data.load_image_labels(paths, len(names), width, height, cell_width, cell_height, config)
with tf.name_scope('per_image_standardization'):
image_std = tf.image.per_image_standardization(image_rgb)
batch = tf.train.shuffle_batch((image_std,) + labels, batch_size=args.batch_size,
capacity=config.getint('queue', 'capacity'), min_after_dequeue=config.getint('queue', 'min_after_dequeue'),
num_threads=multiprocessing.cpu_count()
)
global_step = tf.contrib.framework.get_or_create_global_step()
builder = yolo.Builder(args, config)
builder(batch[0], training=True)
with tf.name_scope('total_loss') as name:
builder.create_objectives(batch[1:])
total_loss = tf.losses.get_total_loss(name=name)
variables_to_restore = slim.get_variables_to_restore(exclude=args.exclude)
with tf.name_scope('optimizer'):
try:
decay_steps = config.getint('exponential_decay', 'decay_steps')
decay_rate = config.getfloat('exponential_decay', 'decay_rate')
staircase = config.getboolean('exponential_decay', 'staircase')
learning_rate = tf.train.exponential_decay(args.learning_rate, global_step, decay_steps, decay_rate, staircase=staircase)
tf.logging.warn('using a learning rate start from %f with exponential decay (decay_steps=%d, decay_rate=%f, staircase=%d)' % (args.learning_rate, decay_steps, decay_rate, staircase))
except (configparser.NoSectionError, configparser.NoOptionError):
learning_rate = args.learning_rate
tf.logging.warn('using a staionary learning rate %f' % args.learning_rate)
optimizer = get_optimizer(config, args.optimizer)(learning_rate)
tf.logging.warn('optimizer=' + args.optimizer)
train_op = slim.learning.create_train_op(total_loss, optimizer, global_step,
clip_gradient_norm=args.gradient_clip, summarize_gradients=config.getboolean('summary', 'gradients'),
)
if args.transfer:
path = os.path.expanduser(os.path.expandvars(args.transfer))
tf.logging.warn('transferring from ' + path)
init_assign_op, init_feed_dict = slim.assign_from_checkpoint(path, variables_to_restore)
def init_fn(sess):
sess.run(init_assign_op, init_feed_dict)
tf.logging.warn('transferring from global_step=%d, learning_rate=%f' % sess.run((global_step, learning_rate)))
else:
init_fn = lambda sess: tf.logging.warn('global_step=%d, learning_rate=%f' % sess.run((global_step, learning_rate)))
summary(config)
tf.logging.warn('tensorboard --logdir ' + logdir)
slim.learning.train(train_op, logdir, master=args.master, is_chief=(args.task == 0),
global_step=global_step, number_of_steps=args.steps, init_fn=init_fn,
summary_writer=tf.summary.FileWriter(os.path.join(logdir, args.logname)),
save_summaries_secs=args.summary_secs, save_interval_secs=args.save_secs
)
def make_args():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', nargs='+', default=['config.ini'], help='config file')
parser.add_argument('-t', '--transfer', help='transferring model from a .ckpt file')
parser.add_argument('-e', '--exclude', nargs='+', help='exclude variables while transferring')
parser.add_argument('-p', '--profile', nargs='+', default=['train', 'val'])
parser.add_argument('-s', '--steps', type=int, default=None, help='max number of steps')
parser.add_argument('-d', '--delete', action='store_true', help='delete logdir')
parser.add_argument('-b', '--batch_size', default=8, type=int, help='batch size')
parser.add_argument('-o', '--optimizer', default='adam')
parser.add_argument('-n', '--logname', default=time.strftime('%Y-%m-%d_%H-%M-%S'), help='the name for TensorBoard')
parser.add_argument('-g', '--gradient_clip', default=0, type=float, help='gradient clip')
parser.add_argument('-lr', '--learning_rate', default=1e-6, type=float, help='learning rate')
parser.add_argument('--seed', type=int, default=None)
parser.add_argument('--summary_secs', default=30, type=int, help='seconds to save summaries')
parser.add_argument('--save_secs', default=600, type=int, help='seconds to save model')
parser.add_argument('--level', help='logging level')
parser.add_argument('--master', default='', help='master address')
parser.add_argument('--task', type=int, default=0, help='task ID')
return parser.parse_args()
if __name__ == '__main__':
args = make_args()
config = configparser.ConfigParser()
utils.load_config(config, args.config)
if args.level:
tf.logging.set_verbosity(args.level.upper())
main()