-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyAiTest.py
More file actions
258 lines (217 loc) · 8.2 KB
/
myAiTest.py
File metadata and controls
258 lines (217 loc) · 8.2 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
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
import matplotlib.pyplot as plt
import seaborn as sns
from skimage import io
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from sklearn.metrics import classification_report,confusion_matrix
import tensorflow as tf
import cv2
import os
import numpy as np
labels = ['No Traffic Circle', 'Approaching', 'Entering','Inside','Exiting']
img_size = 224
def get_data(data_dir):
data = []
for label in labels:
path = os.path.join(data_dir, label)
class_num = labels.index(label)
for img in os.listdir(path):
try:
img_arr = cv2.imread(os.path.join(path, img))[...,::-1] #convert BGR to RGB format
#img_arr = io.imread(os.path.join(path, img))
#resized_arr = cv2.resize(img_arr, (img_size, img_size)) # Reshaping images to preferred size
#data.append([resized_arr, class_num])
except Exception as e:
print(e)
#print(img)
return np.array(data)
#
#train = get_data('D:/Nishanth Polygence/RoundAboutData')
# val = get_data('D:/Nishanth Polygence')
data_dir = 'C:/RoundAboutDebug'
#Batch size originally 32
batch_size = 32
img_height = 224
img_width = 224
train = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset ="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
# l = [0,0,0,0,0]
# for num,i in enumerate(train):
# #print("This is i: ")
# #print(i)
# #print("This is the end of i")
# #print(i[0])
# #print(i[1])
# print(num)
# labelSet = i[1].numpy()
# length = len(labelSet)
# for n in range (0,length):
# if labelSet[n] == 0:
# #l.append("No Traffic Circle")
# l[0]+=1
# elif labelSet[n] == 1:
# #l.append("Approaching")
# l[1] += 1
# elif labelSet[n] == 2:
# #l.append("Entering")
# l[2] += 1
# elif labelSet[n] == 3:
# #l.append("Inside")
# l[3] += 1
# elif labelSet[n]== 4:
# #l.append("Exiting")
# l[4] += 1
# #sns.set_style('darkgrid')
# #sns.countplot(l)
# print(l)
# plt.figure(figsize=(10, 10))
# class_names = train.class_names
# for images, labels in train.take(1):
# for i in range(32):
# ax = plt.subplot(6, 6, i + 1)
# plt.imshow(images[i].numpy().astype("uint8"))
# plt.title(class_names[labels[i]])
# plt.axis("off")
# plt.show()
# plt.figure(figsize = (5,5))
# example_set = train.take(1)
# images = example_set[0]
# plt.imshow(images[0].numpy().astype("uint8"))
# plt.show()
#
# plt.imshow(train.take(1))
# plt.title(labels[train[0][1]])
#
# plt.figure(figsize = (5,5))
# plt.imshow(train[-1][0])
# plt.title(labels[train[-1][1]])
# print("plot incomplete")
# x_train = []
# y_train = []
# x_val = []
# y_val = []
#
# for feature, label in train:
# x_train.append(feature)
# y_train.append(label)
#
# for feature, label in val:
# x_val.append(feature)
# y_val.append(label)
# Normalize the data
# x_train = np.array(x_train) / 255
# x_val = np.array(x_val) / 255
#
# x_train.reshape(-1, img_size, img_size, 1)
# y_train = np.array(y_train)
#
# x_val.reshape(-1, img_size, img_size, 1)
# y_val = np.array(y_val)
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range = 30, # randomly rotate images in the range (degrees, 0 to 180)
zoom_range = 0.2, # Randomly zoom image
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip = True, # randomly flip images
vertical_flip=False,
validation_split = 0.2) # randomly flip images
train_generator = datagen.flow_from_directory(directory = data_dir, target_size=(224, 224), classes= ['No Traffic Circle', 'Approaching', 'Entering', 'Inside', 'Exiting'], class_mode='categorical', batch_size=32, shuffle=True)
# datagen.fit(train)
model = Sequential()
model.add(Conv2D(32,3,padding="same", activation="relu", input_shape=(224,224,3)))
model.add(MaxPool2D())
model.add(Conv2D(32, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Conv2D(64, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128,activation="relu"))
model.add(Dense(5, activation="softmax"))
model.summary()
opt = Adam(lr=0.000001)
model.compile(optimizer = opt , loss = 'categorical_crossentropy' , metrics = ['accuracy'])
history = model.fit(train_generator,epochs = 2)
# tf.keras.callbacks.EarlyStopping(
# monitor="val_loss",
# min_delta=0,
# patience=0,
# verbose=0,
# mode="auto",
# baseline=None,
# restore_best_weights=False
# )
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(500)
plt.figure(figsize=(15, 15))
plt.subplot(2, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(2, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
predictions = model.predict_classes(x_val)
predictions = predictions.reshape(1,-1)[0]
print(classification_report(y_val, predictions, target_names = ['No Traffic Circle(Class 0)','Approaching (Class 1)','Entering (Class2) ','Inside (Class3)','Exiting (Class4)']))
base_model = tf.keras.applications.MobileNetV2(input_shape = (224, 224, 3), include_top = False, weights = "imagenet")
base_model.trainable = False
model = tf.keras.Sequential([base_model,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(2, activation="softmax")
])
base_learning_rate = 0.00001
model.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate),
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(x_train,y_train,epochs = 500 , validation_data = (x_val, y_val))
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(500)
plt.figure(figsize=(15, 15))
plt.subplot(2, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(2, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
predictions = model.predict_classes(x_val)
predictions = predictions.reshape(1,-1)[0]
print(classification_report(y_val, predictions, target_names = ['No Traffic Circle(Class 0)','Approaching (Class 1)','Entering (Class2) ','Inside (Class3)','Exiting (Class4)']))
model.save("model.h5")
print("Saved model to disk")