-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_and_save_model.py
182 lines (142 loc) · 6.02 KB
/
train_and_save_model.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
import os
import numpy as np
import pandas as pd
import random
import cv2
import csv
import glob
from sklearn import model_selection
from keras import backend as K
from keras import models, optimizers
from keras.models import Sequential
from keras.layers import Conv2D, Cropping2D, Dense, Dropout, Flatten, Lambda, Activation, MaxPooling2D, Reshape
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical
from keras.callbacks import ModelCheckpoint
ROOT_PATH = './'
BATCH_SIZE = 4
EPOCHS = 20
NUM_CLASSES = 3
IMAGE_HEIGHT = 600
IMAGE_WIDTH = 800
IMAGE_CHANNEL = 3
BOTTOM_CROP = 178
MODEL_FILE_NAME = './real_tl_classifier.h5'
# check for GPU
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
if not os.path.exists('./traffic_light_train.csv'):
with open('traffic_light.csv', 'w', newline='') as csvfile:
mywriter = csv.writer(csvfile)
mywriter.writerow(['path', 'class', 'color'])
for myclass, directory in enumerate(['nolight', 'red', 'yellow', 'green']):
for filename in glob.glob('./data/real_training_data/{}/*.jpg'.format(directory)):
filename = '/'.join(filename.split('\\'))
mywriter.writerow([filename, myclass, directory])
print('CSV file created successfully')
else:
print('CSV already present')
def random_brightness(image):
# Convert 2 HSV colorspace from RGB colorspace
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Generate new random brightness
rand = random.uniform(0.3, 1.0)
hsv[:, :, 2] = rand*hsv[:, :, 2]
# Convert back to RGB colorspace
new_img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
return new_img
def zoom(image):
zoom_pix = random.randint(0, 10)
zoom_factor = 1 + (2*zoom_pix)/IMAGE_HEIGHT
image = cv2.resize(image, None, fx=zoom_factor,
fy=zoom_factor, interpolation=cv2.INTER_LINEAR)
top_crop = (image.shape[0] - IMAGE_HEIGHT)//2
# bottom_crop = image.shape[0] - top_crop - IMAGE_HEIGHT
left_crop = (image.shape[1] - IMAGE_WIDTH)//2
# right_crop = image.shape[1] - left_crop - IMAGE_WIDTH
image = image[top_crop: top_crop+IMAGE_HEIGHT,
left_crop: left_crop+IMAGE_WIDTH]
return image
# fuction to read image from file
def get_image(index, data, should_augment):
# Read image and appropiately traffic light color
image = cv2.imread(os.path.join(
ROOT_PATH, data['path'].values[index].strip()))
color = data['class'].values[index]
lucky = random.randint(0, 1)
too_lucky = random.randint(0, 1)
unlucky = random.randint(0, 1)
if should_augment:
if lucky == 1:
image = random_brightness(image)
if too_lucky == 1:
image = cv2.flip(image, 1)
if not unlucky == 1:
image = zoom(image)
return [image, color]
# generator function to return images batchwise
def generator(data, should_augment=False):
while True:
# Randomize the indices to make an array
indices_arr = np.random.permutation(data.count()[0])
for batch in range(0, len(indices_arr), BATCH_SIZE):
# slice out the current batch according to batch-size
current_batch = indices_arr[batch:(batch + BATCH_SIZE)]
# initializing the arrays, x_train and y_train
x_train = np.empty(
[0, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNEL], dtype=np.float32)
y_train = np.empty([0], dtype=np.int32)
for i in current_batch:
# get an image and its corresponding color for an traffic light
[image, color] = get_image(i, data, should_augment)
# Appending them to existing batch
x_train = np.append(x_train, [image], axis=0)
y_train = np.append(y_train, [color])
y_train = to_categorical(y_train, num_classes=NUM_CLASSES)
yield (x_train, y_train)
def get_model(time_len=1):
model = Sequential()
model.add(Cropping2D(cropping=((0, BOTTOM_CROP), (0, 0)),
input_shape=(IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNEL)))
model.add(Lambda(lambda x: x/127.5 - 1.))
model.add(Conv2D(32, 8, strides=(4, 4), padding="same", activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(32, 4, strides=(2, 2), padding="same", activation='relu'))
model.add(MaxPooling2D(2, 2))
# model.add(Conv2D(64, 5, strides=(2, 2), padding="same", activation='relu'))
model.add(Flatten())
model.add(Dropout(.35))
model.add(Dense(128, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(NUM_CLASSES))
# model.add(Lambda(lambda x: (K.exp(x) + 1e-4) / (K.sum(K.exp(x)) + 1e-4)))
model.add(Lambda(lambda x: K.tf.nn.softmax(x)))
model.compile(optimizer=Adam(lr=5e-4),
loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
return model
if __name__ == "__main__":
data = pd.read_csv(os.path.join('./traffic_light_train.csv'))
# Split data into random training and validation sets
d_train, d_valid = model_selection.train_test_split(data, test_size=.2)
train_gen = generator(d_train, True)
validation_gen = generator(d_valid, False)
model = get_model()
# checkpoint to save best weights after each epoch based on the improvement in val_loss
checkpoint = ModelCheckpoint(MODEL_FILE_NAME, monitor='val_loss', verbose=1,save_best_only=True, mode='min',save_weights_only=False)
callbacks_list = [checkpoint] #,callback_each_epoch]
print('Training started....')
history = model.fit_generator(
train_gen,
steps_per_epoch=len(d_train)//BATCH_SIZE,
epochs=EPOCHS,
validation_data=validation_gen,
validation_steps=len(d_valid)//BATCH_SIZE,
verbose=1,
callbacks=callbacks_list
)
# print("Saving model..")
# model.save("./tl_classifier_keras.h5")
# print("Model Saved successfully!!")
# Destroying the current TF graph to avoid clutter from old models / layers
K.clear_session()