-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_feature_extractor.py
112 lines (88 loc) · 3.63 KB
/
train_feature_extractor.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
import pickle
import time
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
import numpy as np
from scipy.misc import imread
from alexnet import AlexNet
driving_log = pd.read_csv('training_data/driving_log.csv')
nb_classes = len(driving_log)
epochs = 10
batch_size = 128
car_images = []
steering_angles = []
def process_car_image():
list = []
for i in range(nb_classes):
image_center_path = driving_log.ix[i][0]
im_center = imread(image_center_path).astype(np.float32)
im_center = im_center - np.mean(im_center)
image_left_path = driving_log.ix[i][1]
im_left = imread(image_left_path).astype(np.float32)
im_left = im_left - np.mean(im_left)
image_right_path = driving_log.ix[i][2]
im_right = imread(image_right_path).astype(np.float32)
im_right = im_right - np.mean(im_right)
list.extend([im_center, im_left, im_right])
yield list
def process_steering_angle():
list = []
for i in range(nb_classes):
steering_center = driving_log.ix[i][3]
steering_correction = 0.2
steering_left = steering_center + steering_correction
steering_right = steering_center - steering_correction
list.extend([steering_center, steering_left, steering_right])
yield list
car_image_processor = process_car_image()
steering_angle_processor = process_steering_angle()
for i in range(nb_classes):
car_images = (next(car_image_processor))
steering_angles = (next(steering_angle_processor))
X_train, X_val, y_train, y_val = train_test_split(car_images, steering_angles, test_size=0.33, random_state=0)
features = tf.placeholder(tf.float32, (None, 32, 32, 3))
labels = tf.placeholder(tf.int64, None)
resized = tf.image.resize_images(features, (227, 227))
# Returns the second final layer of the AlexNet model,
# this allows us to redo the last layer for the traffic signs
# model.
fc7 = AlexNet(resized, feature_extract=True)
fc7 = tf.stop_gradient(fc7)
shape = (fc7.get_shape().as_list()[-1], nb_classes)
fc8W = tf.Variable(tf.truncated_normal(shape, stddev=1e-2))
fc8b = tf.Variable(tf.zeros(nb_classes))
logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels)
loss_op = tf.reduce_mean(cross_entropy)
opt = tf.train.AdamOptimizer()
train_op = opt.minimize(loss_op, var_list=[fc8W, fc8b])
init_op = tf.global_variables_initializer()
preds = tf.arg_max(logits, 1)
accuracy_op = tf.reduce_mean(tf.cast(tf.equal(preds, labels), tf.float32))
def eval_on_data(X, y, sess):
total_acc = 0
total_loss = 0
for offset in range(0, X.shape[0], batch_size):
end = offset + batch_size
X_batch = X[offset:end]
y_batch = y[offset:end]
loss, acc = sess.run([loss_op, accuracy_op], feed_dict={features: X_batch, labels: y_batch})
total_loss += (loss * X_batch.shape[0])
total_acc += (acc * X_batch.shape[0])
return total_loss/X.shape[0], total_acc/X.shape[0]
with tf.Session() as sess:
sess.run(init_op)
for i in range(epochs):
# training
X_train, y_train = shuffle(X_train, y_train)
t0 = time.time()
for offset in range(0, X_train.shape[0], batch_size):
end = offset + batch_size
sess.run(train_op, feed_dict={features: X_train[offset:end], labels: y_train[offset:end]})
val_loss, val_acc = eval_on_data(X_val, y_val, sess)
print("Epoch", i+1)
print("Time: %.3f seconds" % (time.time() - t0))
print("Validation Loss =", val_loss)
print("Validation Accuracy =", val_acc)
print("")