-
Notifications
You must be signed in to change notification settings - Fork 14
/
train.py
157 lines (134 loc) · 4.99 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
import argparse
import os
import tensorflow as tf
from model import Nivdia_Model
import reader
FLAGS = None
def batch_eval(target, data, x_image, y, keep_prob, batch_size, sess):
value = 0
batch_num = (data.num_expamles + batch_size - 1) // batch_size
for i in range(batch_num):
batch_x, batch_y = data.next_batch(batch_size, shuffle=False)
res = sess.run(
target, feed_dict={
x_image: batch_x,
y: batch_y,
keep_prob: 1.0
})
value += res * len(batch_x)
return value / data.num_expamles
def train():
x_image = tf.placeholder(tf.float32, [None, 66, 200, 3])
y = tf.placeholder(tf.float32, [None, 1])
keep_prob = tf.placeholder(tf.float32)
model = Nivdia_Model(x_image, y, keep_prob, FLAGS)
# dataset reader
dataset = reader.Reader(FLAGS.data_dir, FLAGS)
saver = tf.train.Saver()
with tf.Session() as sess:
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train',
sess.graph)
# initialize all varibales
sess.run(tf.global_variables_initializer())
min_validation_loss = float('Inf')
# restore model
if not FLAGS.disable_restore:
path = tf.train.latest_checkpoint(FLAGS.model_dir)
if not (path is None):
saver.restore(sess, path)
# validation
min_validation_loss = batch_eval(
model.loss, dataset.validation, x_image, y, keep_prob,
FLAGS.batch_size, sess)
print('Restore model from', path)
for i in range(FLAGS.max_steps):
batch_x, batch_y = dataset.train.next_batch(FLAGS.batch_size)
# train model
if i % 100 == 99: # Record execution stats
run_options = tf.RunOptions(
trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
summary, _ = sess.run(
[merged, model.optimization],
feed_dict={x_image: batch_x,
y: batch_y,
keep_prob: 0.8},
options=run_options,
run_metadata=run_metadata)
train_writer.add_run_metadata(run_metadata, 'step%03d' % i)
else:
summary, _ = sess.run(
[merged, model.optimization],
feed_dict={
x_image: batch_x,
y: batch_y,
keep_prob: 0.8
})
train_writer.add_summary(summary, i)
# validation
validation_loss = batch_eval(model.loss, dataset.validation,
x_image, y, keep_prob,
FLAGS.batch_size, sess)
if (validation_loss < min_validation_loss):
min_validation_loss = validation_loss
saver.save(sess, os.path.join(FLAGS.model_dir, "model.ckpt"))
if i % FLAGS.print_steps == 0:
loss = sess.run(
model.loss,
feed_dict={
x_image: batch_x,
y: batch_y,
keep_prob: 1.0
})
print("Step", i, "train_loss: ", loss, "validation_loss: ",
validation_loss)
train_writer.close()
def main():
if tf.gfile.Exists(FLAGS.log_dir):
tf.gfile.DeleteRecursively(FLAGS.log_dir)
tf.gfile.MakeDirs(FLAGS.log_dir)
if not tf.gfile.Exists(FLAGS.model_dir):
tf.gfile.MakeDirs(FLAGS.model_dir)
train()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--max_steps',
type=int,
default=20000,
help='Number of steps to run trainer')
parser.add_argument(
'--print_steps',
type=int,
default=100,
help='Number of steps to print training loss')
parser.add_argument(
'--learning_rate',
type=float,
default=1e-4,
help='Initial learning rate')
parser.add_argument(
'--batch_size', type=int, default=500, help='Train batch size')
parser.add_argument(
'--data_dir',
type=str,
default=os.path.join('.', 'driving_dataset'),
help='Directory of data')
parser.add_argument(
'--log_dir',
type=str,
default=os.path.join('.', 'logs'),
help='Directory of log')
parser.add_argument(
'--model_dir',
type=str,
default=os.path.join('.', 'saved_model'),
help='Directory of saved model')
parser.add_argument(
'--disable_restore',
type=int,
default=0,
help='Whether disable restore model from model directory')
FLAGS, unparsed = parser.parse_known_args()
main()