-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining.py
More file actions
176 lines (147 loc) · 5.16 KB
/
training.py
File metadata and controls
176 lines (147 loc) · 5.16 KB
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
import pandas as pd
print('Setting UP')
import os
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.utils import class_weight
from tensorflow.python.keras.callbacks import EarlyStopping
from utils import *
batch_data_size = 32 # 32
steering_mapping = {'forward': 0, 'left': 1, 'right': 2}
image_data_directory = 'data_collected'
image_dataframe = import_data(image_data_directory)
print(image_dataframe.shape)
print(image_dataframe.head())
image_dataframe = balance_data(image_dataframe, display=True) # vis and balance
print(image_dataframe.head())
# preprocess
images_path, steering, steering_forward, steering_left, steering_right = load_data(
image_data_directory,
image_dataframe)
print(f' image_path :: {len(images_path)} steering :: {len(steering)} steering_forward :: {len(steering_forward)} '
f'steering_left :: {len(steering_left)} steering_right {len(steering_right)}')
# cv2.imshow('Test Image',cv2.imread(images_path[5]))
# cv2.waitKey(0)
# split: validation and train setup
data_frame = pd.DataFrame({
'images_path': images_path,
'steering': steering,
'steering_forward': steering_forward,
'steering_left': steering_left,
'steering_right': steering_right
})
X = data_frame[['images_path', 'steering_forward', 'steering_left', 'steering_right']]
y = data_frame[['steering_forward', 'steering_left', 'steering_right']]
print(X.head())
print(y.head())
xTrain, xVal, yTrain, yVal = train_test_split(
X,
y,
test_size=0.2,
random_state=10
)
print(f'total training Images: {len(xTrain)}')
print(f'total validation Images: {len(xVal)}')
# data generators (training and validation)
train_generator = tf.data.Dataset.from_generator(
lambda: data_generator(
xTrain['images_path'].values,
xTrain['steering_forward'].values,
xTrain['steering_left'].values,
xTrain['steering_right'].values,
batch_size=batch_data_size,
train_flag=True
),
output_signature=(
{
'image_input': tf.TensorSpec(shape=(None, 66, 200, 3), dtype=tf.float32),
'numerical_input': tf.TensorSpec(shape=(None, 3), dtype=tf.float32)
},
tf.TensorSpec(shape=(None, 3), dtype=tf.float32)
)
)
validation_generator = tf.data.Dataset.from_generator(
lambda: data_generator(
xVal['images_path'].values,
xVal['steering_forward'].values,
xVal['steering_left'].values,
xVal['steering_right'].values,
batch_size=batch_data_size,
train_flag=False
),
output_signature=(
{
'image_input': tf.TensorSpec(shape=(None, 66, 200, 3), dtype=tf.float32),
'numerical_input': tf.TensorSpec(shape=(None, 3), dtype=tf.float32)
},
tf.TensorSpec(shape=(None, 3), dtype=tf.float32)
)
)
# model
model = create_model()
print(model.summary())
# we have more forwards
class_weights = class_weight.compute_class_weight(
class_weight='balanced',
classes=np.unique(np.argmax(yTrain, axis=1)),
y=np.argmax(yTrain, axis=1)
)
class_weights_dictionary = {i: class_weights[i] for i in range(len(class_weights))}
early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
# train
history = model.fit(
train_generator,
steps_per_epoch=len(xTrain) // batch_data_size,
epochs=10,
validation_data=validation_generator,
validation_steps=len(xVal) // batch_data_size,
# class_weight=class_weights_dictionary,
# callbacks=[early_stopping]
)
# save model
model.save('model.keras')
print('Model Saved')
# results
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['Training', 'Validation'])
plt.title('Loss')
plt.xlabel('Epoch')
plt.show()
# Evaluate model
test_loss, test_accuracy = model.evaluate(validation_generator, steps=len(xVal) // batch_data_size)
# test_loss, test_mae, test_mse = model.evaluate(validation_generator, steps=len(xVal) // batch_data_size)
print(f'Test Loss: {test_loss}')
print(f'Test accuracy: {test_accuracy}')
# print(f'Test MAE: {test_mae}')
# print(f'Test MSE: {test_mse}')
#
# # Plot additional metrics
# plt.plot(history.history['mae'])
# plt.plot(history.history['val_mae'])
# plt.legend(['Training MAE', 'Validation MAE'])
# plt.title('Mean Absolute Error')
# plt.xlabel('Epoch')
# plt.show()
#
#
# plt.plot(history.history['mse'])
# plt.plot(history.history['val_mse'])
# plt.legend(['Training MSE', 'Validation MSE'])
# plt.title('Mean Squared Error')
# plt.xlabel('Epoch')
# plt.show()
# Test model
for j in range(0, 10):
images_path_0 = data_frame.iloc[j]['images_path']
steering_0 = data_frame.iloc[j]['steering']
steering_forward_0 = data_frame.iloc[j]['steering_forward']
steering_left_0 = data_frame.iloc[j]['steering_left']
steering_right_0 = data_frame.iloc[j]['steering_right']
prediction = test_model(model, images_path_0, steering_0,
steering_forward_0, steering_left_0, steering_right_0)
print(f'image {images_path_0} Recorded vs Prediction : {steering_0} : {prediction}')
# converter = tf.lite.TFLiteConverter.from_keras_model()
#