-
Notifications
You must be signed in to change notification settings - Fork 0
/
train-app.py
334 lines (238 loc) · 8.36 KB
/
train-app.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
'''
This module is set to train 3D CNN model and save weights for further usage in test-app
'''
# In[1]
import numpy as np
import cv2
import tensorflow as tf
import os
import pandas as pd
import matplotlib.image as img
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
import csv
from model import Conv3DModel
# In[2]
# Setting gestures labels
gestures = [
"Forward",
"Back",
"Turn Right",
"Turn Left",
"No Command"
]
# In[3]
# SKIP IF CREATED
# Rename tool, not necessary
import os
directory = 'dataset/' # Your directory
counter = 0
for root, dirs, files in os.walk(directory):
for d in dirs:
new_name = '{:03d}'.format(counter) # name formatting
os.rename(os.path.join(root, d), os.path.join(root, new_name)) # renaming
counter += 1
print('Your folders have been renamed.')
# In[4]
# SKIP IF CREATED
# Script to create a labeled csv file
list_iterator = 0
with open('dataset/comparison/labels_filtered.csv', mode='w', newline='') as csvfile:
fieldnames = ['label', 'id']
writer = csv.DictWriter(csvfile, delimiter=";", fieldnames=fieldnames)
writer.writeheader()
for i in range(114):
writer.writerow({"label": str(gestures[list_iterator]), "id": str(i)})
list_iterator += 1
if list_iterator == 3:
list_iterator = 0
# In[5]
# Setting up CSV labels as targets for training
temporal_variable = pd.read_csv('dataset/comparison/labels_filtered.csv', header=None, sep=';').to_dict()
training_targets_dict = temporal_variable[0]
temporal_variable = pd.read_csv('dataset/comparison/labels_validation.csv', header=None, sep=';').to_dict()
validation_targets_dict = temporal_variable[0]
temporal_variable = None
print(training_targets_dict[0])
print(validation_targets_dict[0])
print(len(training_targets_dict))
print(len(validation_targets_dict))
# In[6]
# Setting up directories of data
training_path = 'dataset/comparison/filter3/'
validation_path = 'dataset/comparison/validation2/'
training_directories = os.listdir(training_path)
validation_directories = os.listdir(validation_path)
print(len(training_directories))
print(len(validation_directories))
# In[7]
target_frame_number = 30 # Get 30 frames from each folder
# Get equal frame count in each folder
def unify_frames(path):
frames = os.listdir(path)
frames_count = len(frames)
if target_frame_number > frames_count:
# if folder contains fewer frames than needed, it duplicates last frame
frames += [frames[-1]] * (target_frame_number - frames_count)
elif target_frame_number < frames_count:
# if there's too many frames in the folder, it just takes 30 of them
frames = frames[0:target_frame_number]
return frames
# In[8]
# Resizing frames
def resize_frame(frame):
frame = img.imread(frame)
frame = cv2.resize(frame, (64, 64))
return frame
# In[9]
# Frame processing to return gray image
def rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140])
# In[10]
# Adjusting training data
counter_training = 0 # number for training, will be used in further modules
new_frames_training = [] # contains training data
training_targets = [] # array compare frames to labels
for directory in training_directories:
new_frame = []
frames = unify_frames(training_path + directory)
if len(frames) == target_frame_number:
for frame in frames:
frame = resize_frame(training_path + directory + '/' + frame)
new_frame.append(rgb2gray(frame))
if len(new_frame) == 15:
new_frames_training.append(new_frame)
training_targets.append(gestures.index(training_targets_dict[int(directory)]))
counter_training += 1
new_frame = []
# In[11]
# Adjust validation data
counter_validation = 0
new_frames_validation = []
validation_targets = []
for directory in validation_directories:
new_frame = []
frames = unify_frames(validation_path + directory)
if len(frames) == target_frame_number:
for frame in frames:
frame = resize_frame(validation_path + directory + '/' + frame)
new_frame.append(rgb2gray(frame))
if len(new_frame) == 15:
new_frames_validation.append(new_frame)
validation_targets.append(gestures.index(validation_targets_dict[int(directory)]))
counter_validation += 1
new_frame = []
# In[12]
# Correctness check
print(len(new_frames_training))
print(len(training_targets))
training_targets[0:20]
print(len(new_frames_validation))
print(len(validation_targets))
validation_targets[0:20]
# In[13]
# RAM cleansing
def release_list(array):
del array[:]
del array
# In[14]
# Convert training data to numbers
training_data = np.array(new_frames_training[0:counter_training], dtype=np.float32)
# In[15]
# Convert validation data to numbers
validation_data = np.array(new_frames_validation[0:counter_validation], dtype=np.float32)
# In[16]
# Releasing RAM
release_list(new_frames_validation)
release_list(new_frames_training)
# In[17]
# Training data normalization
print('old mean', training_data.mean())
scaler = StandardScaler()
scaled_images = scaler.fit_transform(training_data.reshape(-1, 15 * 64 * 64))
print('new mean', scaled_images.mean())
scaled_images_training = scaled_images.reshape(-1, 15, 64, 64, 1)
print(scaled_images_training.shape)
# In[18]
# Validation data normalization
print('old mean', validation_data.mean())
scaler = StandardScaler()
scaled_images_two = scaler.fit_transform(validation_data.reshape(-1, 15*64*64))
print('new mean', scaled_images_two.mean())
scaled_images_validation = scaled_images_two.reshape(-1, 15, 64, 64, 1)
print(scaled_images_validation.shape)
# In[19]
# Creating instance of 3D CNN model
model = Conv3DModel()
# In[20]
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy()
# Setting up model
model.compile(loss=loss_fn,
optimizer=tf.keras.optimizers.Adam(),
metrics = ['accuracy'])
# In[21]
# Finalizing both train and validation data
x_train = np.array(scaled_images_training) # Input train data
y_train = np.array(training_targets) # Target train data
x_val = np.array(scaled_images_validation) # Input validation set
y_val = np.array(validation_targets) # Target validation set
# In[22]
print(x_train.shape)
print(y_train.shape)
print(x_val.shape)
print(y_val.shape)
# In[23]
checkpoint_path = 'model_weights/cp-{epoch:04d}.ckpt'
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, monitor="val_loss", verbose=1, save_best_only=True)
early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
patience=20,
verbose=1,
mode='min')
# In[24]
# Well, let's start training
history = model.fit(x_train, y_train,
callbacks = [cp_callback],
validation_data=(x_val, y_val),
batch_size=32,
epochs=120)
# In[25]
# Save all metrics
import json
metrics = history.history
train_loss = metrics['loss']
train_accuracy = metrics['accuracy']
val_loss = metrics['val_loss']
val_accuracy = metrics['val_accuracy']
with open("model_weights/full4.json", "w+") as f:
json.dump({'train_loss' : train_loss,
'validation_loss': val_loss,
'train_accuracy': train_accuracy,
'validation_accuracy': val_accuracy}, f)
# In[26]
from numpy import arange
# Data visualisation for loss function
epochs = range(1, len(train_loss) + 1)
plt.plot(epochs, train_loss, label='Training Loss')
plt.plot(epochs, val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
# Set the tick locations
plt.xticks(arange(0, len(train_loss) + 1, 10))
# Display the plot
plt.legend(loc='best')
plt.show()
# In[27]
# Data visualisation for accuracies
epochs = range(1, len(train_accuracy) + 1)
plt.plot(epochs, train_accuracy, label='Training Accuracy')
plt.plot(epochs, val_accuracy, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
# Set the tick locations
plt.xticks(arange(0, len(train_accuracy) + 1, 10))
# Display the plot
plt.legend(loc='best')
plt.show()