-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtrain.py
135 lines (113 loc) · 5.37 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
''' AdapNet: Adaptive Semantic Segmentation
in Adverse Environmental Conditions
Copyright (C) 2018 Abhinav Valada, Johan Vertens , Ankit Dhall and Wolfram Burgard
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 argparse
import datetime
import importlib
import os
import numpy as np
import re
import tensorflow as tf
import yaml
from dataset.helper import *
PARSER = argparse.ArgumentParser()
PARSER.add_argument('-c', '--config', default='config/cityscapes_train.config')
def train_func(config):
os.environ['CUDA_VISIBLE_DEVICES'] = config['gpu_id']
module = importlib.import_module('models.'+config['model'])
model_func = getattr(module, config['model'])
data_list, iterator = get_train_data(config)
resnet_name = 'resnet_v1_50'
global_step = tf.Variable(0, trainable=False, name='Global_Step')
with tf.variable_scope(resnet_name):
model = model_func(num_classes=config['num_classes'], learning_rate=config['learning_rate'],
decay_steps=config['max_iteration'], power=config['power'],
global_step=global_step)
images_pl = tf.placeholder(tf.float32, [None, config['height'], config['width'], 3])
labels_pl = tf.placeholder(tf.float32, [None, config['height'], config['width'],
config['num_classes']])
model.build_graph(images_pl, labels_pl)
model.create_optimizer()
config1 = tf.ConfigProto()
config1.gpu_options.allow_growth = True
sess = tf.Session(config=config1)
sess.run(tf.global_variables_initializer())
step = 0
total_loss = 0.0
t0 = None
ckpt = tf.train.get_checkpoint_state(os.path.dirname(os.path.join(config['checkpoint'],
'checkpoint')))
if ckpt and ckpt.model_checkpoint_path:
saver = tf.train.Saver(max_to_keep=1000)
saver.restore(sess, ckpt.model_checkpoint_path)
step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])+1
sess.run(tf.assign(global_step, step))
print 'Model Loaded'
else:
if 'intialize' in config:
reader = tf.train.NewCheckpointReader(config['intialize'])
var_str = reader.debug_string()
name_var = re.findall('[A-Za-z0-9/:_]+ ', var_str)
import_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
initialize_variables = {}
for var in import_variables:
if var.name.split(':')[0]+' ' in name_var:
initialize_variables[var.name.split(':')[0]] = var
saver = tf.train.Saver(initialize_variables)
saver.restore(save_path=config['intialize'], sess=sess)
print 'Pretrained Intialization'
saver = tf.train.Saver(max_to_keep=1000)
while 1:
try:
img, label = sess.run([data_list[0], data_list[1]])
img = img-mean
feed_dict = {images_pl: img, labels_pl: label}
loss_batch, _ = sess.run([model.loss, model.train_op],
feed_dict=feed_dict)
total_loss += loss_batch
if (step + 1) % config['save_step'] == 0:
saver.save(sess, os.path.join(config['checkpoint'], 'model.ckpt'), step)
if (step + 1) % config['skip_step'] == 0:
left_hours = 0
if t0 is not None:
delta_t = (datetime.datetime.now() - t0).seconds
left_time = (config['max_iteration'] - step) / config['skip_step'] * delta_t
left_hours = left_time/3600.0
t0 = datetime.datetime.now()
total_loss /= config['skip_step']
print '%s %s] Step %s, lr = %f ' \
% (str(datetime.datetime.now()), str(os.getpid()), step,
model.lr.eval(session=sess))
print '\t loss = %.4f' % (total_loss)
print '\t estimated time left: %.1f hours. %d/%d' % (left_hours, step,
config['max_iteration'])
print '\t', config['model']
total_loss = 0.0
step += 1
if step > config['max_iteration']:
saver.save(sess, os.path.join(config['checkpoint'], 'model.ckpt'), step-1)
print 'training_completed'
break
except tf.errors.OutOfRangeError:
print 'Epochs in dataset repeat < max_iteration'
break
def main():
args = PARSER.parse_args()
if args.config:
file_address = open(args.config)
config = yaml.load(file_address)
else:
print '--config config_file_address missing'
train_func(config)
if __name__ == '__main__':
main()