-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
275 lines (204 loc) · 8.53 KB
/
main.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
from tensorflow import keras
from keras.models import load_model # TensorFlow is required for Keras to work
from PIL import ImageFile, Image, ImageOps # Install pillow instead of PIL
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
from keras.applications.mobilenet_v2 import MobileNetV2
from keras.preprocessing import image
import numpy as np
from keras import models
from keras import layers
from keras import optimizers
import os
import pickle
from sklearn.metrics import classification_report, confusion_matrix
import itertools
ImageFile.LOAD_TRUNCATED_IMAGES = True
VERSION = 2
image_size = 224
model_file_name = f'output/v{VERSION}/model.h5'
# Load the labels
class_names = open("data/labels.txt", "r").readlines()
n_classes = len(class_names)
def printAccuracyGraph(epochs, acc, val_acc):
plt.plot(epochs, acc, 'b', label='Training accuracy')
plt.plot(epochs, val_acc, 'r', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()
plt.savefig(f'output/v{VERSION}/AccuracyGraph.png')
plt.clf()
def printLossGraph(epochs, loss, val_loss):
plt.plot(epochs, loss, 'b', label='Training loss')
plt.plot(epochs, val_loss, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.savefig(f'output/v{VERSION}/LossGraph.png')
plt.clf()
def plot_confusion_matrix(cm, classes, normalize=True, title='Confusion matrix', cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.figure(figsize=(200,200))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
cm = np.around(cm, decimals=2)
cm[np.isnan(cm)] = 0.0
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.savefig(f'output/v{VERSION}/ConfusionGraph.png')
if not os.path.exists(model_file_name):
mobile_net_v2 = MobileNetV2(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3))
# Freeze the layers except the last 8 layers
for layer in mobile_net_v2.layers[:-8]:
layer.trainable = False
# Create the model
model = models.Sequential(name = "Dog_Breed_Classification")
# Add the vgg convolutional base model
model.add(mobile_net_v2)
model.add(layers.Dropout(0.5))
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1024, activation='relu')) # dense layer 2
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation='relu')) # dense layer 3
model.add(layers.Dropout(0.5))
model.add(layers.Dense(n_classes, activation='softmax')) # final layer with softmax activation
# Show a summary of the model. Check the number of trainable parameters
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
metrics=['accuracy'])
train_datagen = ImageDataGenerator(rescale=1./255,
zoom_range=0.2,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
vertical_flip=True)
validation_datagen = ImageDataGenerator(rescale=1./255)
# Change the batchsize according to your system RAM
train_batchsize = 100
val_batchsize = 10
image_size = 224
train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(image_size, image_size),
batch_size=train_batchsize,
class_mode='categorical')
validation_generator = validation_datagen.flow_from_directory(
'data/valid',
target_size=(image_size, image_size),
batch_size=val_batchsize,
class_mode='categorical',
shuffle=False)
# Train the model
history = model.fit(
train_generator,
steps_per_epoch=train_generator.samples/train_generator.batch_size ,
epochs=50,
validation_data=validation_generator,
validation_steps=validation_generator.samples/validation_generator.batch_size,
verbose=1)
# Save the model
model.save(model_file_name)
with open(f'output/v{VERSION}/trainHistoryDict', 'wb') as file_pi:
pickle.dump(history.history, file_pi)
# Show final result
history.history['accuracy'][-1]
# Plot the results
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
# Disable scientific notation for clarity
np.set_printoptions(suppress=True)
model = load_model(f"output/v{VERSION}/model.h5", compile=False)
with open(f'output/v{VERSION}/trainHistoryDict', 'rb') as handle:
history = pickle.load(handle)
model.summary()
# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
# Replace this with the path to your image
image = Image.open("test/dog.png").convert("RGB")
# resizing the image to be at least 224x224 and then cropping from the center
size = (224, 224)
image = ImageOps.fit(image, size, Image.LANCZOS)
# turn the image into a numpy array
image_array = np.asarray(image)
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
# Load the image into the array
data[0] = normalized_image_array
# Predicts the model
prediction = model.predict(data)
index = np.argmax(prediction)
class_name = class_names[index]
confidence_score = prediction[0][index]
# Print prediction and confidence score
print("Class:", class_name[2:], end="")
print("Confidence Score:", confidence_score)
test_batch_size = 1
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
directory='data/test',
target_size=(image_size, image_size),
batch_size=test_batch_size,
class_mode='categorical',
shuffle=False
)
if not os.path.exists(f'output/v{VERSION}/AccuracyGraph.png'):
print("Accuracy graph")
acc = history['accuracy']
val_acc = history['val_accuracy']
epochs = range(len(acc))
printAccuracyGraph(epochs, acc, val_acc)
if not os.path.exists(f'output/v{VERSION}/LossGraph.png'):
print("Loss graph")
loss = history['loss']
val_loss = history['val_loss']
epochs = range(len(loss))
printLossGraph(epochs, loss, val_loss)
if not os.path.exists(f'output/v{VERSION}/ConfusionGraph.png'):
print('Confusion Matrix')
Y_pred = model.predict(test_generator)
y_pred = np.argmax(Y_pred, axis=1)
cm = confusion_matrix(test_generator.classes, y_pred)
plot_confusion_matrix(cm, class_names, title='Confusion Matrix')
if not os.path.exists(f"output/v{VERSION}/ClassificationResults.csv"):
print("Classification Report")
Y_pred = model.predict(test_generator)
y_pred = np.argmax(Y_pred, axis=1)
report = classification_report(test_generator.classes, y_pred, output_dict=True)
df = pd.DataFrame(report).transpose()
df.to_csv(f"output/v{VERSION}/ClassificationResults.csv", encoding='utf-8', index=False)
if not os.path.exists(f"output/v{VERSION}/TFLITE/model.tflite"):
if not os.path.exists(f"output/v{VERSION}/TFLITE"):
os.makedirs(f"output/v{VERSION}/TFLITE")
print("Creating TFLite model")
TF_LITE_MODEL_FILE_NAME = f"output/v{VERSION}/TFLITE/model.tflite"
tf_lite_converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = tf_lite_converter.convert()
tflite_model_name = TF_LITE_MODEL_FILE_NAME
open(tflite_model_name, "wb").write(tflite_model)