-
Notifications
You must be signed in to change notification settings - Fork 0
/
car_classification.py
309 lines (237 loc) · 8.67 KB
/
car_classification.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
# -*- coding: utf-8 -*-
"""Car_Classification.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Yss-MRyIfZv-PVNuuVgqTELTBY_zxaXm
"""
from google.colab import drive
import os
import shutil
drive.flush_and_unmount()
# Define the path to the Colab workspace
workspace_path = '/content/'
shutil.rmtree(workspace_path, ignore_errors=True)
# Create the destination directory if it doesn't exist
os.makedirs(workspace_path, exist_ok=True)
# Mount Google Drive
drive.mount('/content/drive')
# Define the path to the zip file in Google Drive
zip_file_path = '/content/drive/MyDrive/Datasets/stanford_car_dataset_by_classes.zip'
# Copy the zip file from Google Drive to the Colab workspace
shutil.copy2(zip_file_path, workspace_path)
# Change the working directory to the destination path
os.chdir(workspace_path)
# Unzip the file
shutil.unpack_archive(os.path.basename(zip_file_path), workspace_path)
# Remove zip file
os.remove(os.path.basename(zip_file_path))
# Unmount Google Drive
drive.flush_and_unmount()
# Define the paths for the training and test datasets
TRAIN_DIR = "/content/stanford_car_dataset_by_classes/car_data/car_data/train"
TEST_DIR = "/content/stanford_car_dataset_by_classes/car_data/car_data/test"
COMBINED_DIR = "/content/stanford_car_dataset_by_classes/car_data/car_data/combined"
# Create the combined dataset directory
os.makedirs(COMBINED_DIR, exist_ok=True)
# Move the training dataset subdirectories to the combined dataset directory
for class_dir in os.listdir(TRAIN_DIR):
source_dir = os.path.join(TRAIN_DIR, class_dir)
destination_dir = os.path.join(COMBINED_DIR, class_dir)
shutil.copytree(source_dir, destination_dir)
# Copy the test dataset subdirectories to the combined dataset directory
for class_dir in os.listdir(TEST_DIR):
source_dir = os.path.join(TEST_DIR, class_dir)
destination_dir = os.path.join(COMBINED_DIR, class_dir)
shutil.copytree(source_dir, destination_dir, dirs_exist_ok=True)
from tensorflow import keras
import tensorflow as tf
import pandas as pd
import numpy as np
# tf.config.gpu_options.allow_growth = True
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
try:
session
except NameError:
session = None
if session is not None:
session.close()
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.applications.resnet50 import preprocess_input
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
IMG_SIZE = 256
BATCH_SIZE = 16
def count_no_of_files(directory):
count = sum([len(files) for r, d, files in os.walk(directory)])
print('File count in "' + directory + '":', count)
count_no_of_files(TRAIN_DIR)
count_no_of_files(TEST_DIR)
count_no_of_files(COMBINED_DIR)
datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
rotation_range=20,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
validation_split=0.1)
train_generator = datagen.flow_from_directory(
directory = COMBINED_DIR,
class_mode = "categorical",
seed = 1,
target_size = (IMG_SIZE,IMG_SIZE),
subset='training',
batch_size = BATCH_SIZE)
valid_generator = datagen.flow_from_directory(
directory = COMBINED_DIR,
seed = 1,
class_mode = "categorical",
target_size = (IMG_SIZE,IMG_SIZE),
subset='validation',
batch_size = BATCH_SIZE)
IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)
# # Create the base model from the pre-trained model MobileNet V2
base_model = tf.keras.applications.ResNet50(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
base_model.trainable = False
base_model.summary()
model = tf.keras.Sequential([
base_model,
tf.keras.layers.MaxPool2D(2,2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation = 'relu'),
tf.keras.layers.Dense(196, activation = 'softmax')
])
model.summary()
base_learning_rate = 0.0001
model.compile(Adam(learning_rate = base_learning_rate),
loss="categorical_crossentropy",
metrics=["accuracy"])
history = model.fit(train_generator,
validation_data=valid_generator,
epochs=10)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()),1])
plt.title('Training and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Cross Entropy')
plt.ylim([0,10.0])
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()
"""## Fine Tune the Model ##"""
base_model.trainable = True
# Let's take a look to see how many layers are in the base model
print("Number of layers in the base model: ", len(base_model.layers))
# Fine tune from this layer onwards
fine_tune_at = 60
# Freeze all the layers before the `fine_tune_at` layer
for layer in base_model.layers[:fine_tune_at]:
layer.trainable = False
model.compile(loss='categorical_crossentropy',
optimizer = tf.keras.optimizers.RMSprop(learning_rate = base_learning_rate/10),
metrics=['accuracy'])
model.summary()
IMG_SIZE = 256
BATCH_SIZE = 8
datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
rotation_range=20,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
validation_split=0.1)
train_generator = datagen.flow_from_directory(
directory = COMBINED_DIR,
class_mode = "categorical",
seed = 1,
target_size = (IMG_SIZE,IMG_SIZE),
subset='training',
batch_size = BATCH_SIZE)
valid_generator = datagen.flow_from_directory(
directory = COMBINED_DIR,
seed = 1,
class_mode = "categorical",
target_size = (IMG_SIZE,IMG_SIZE),
subset='validation',
batch_size = BATCH_SIZE)
history = model.fit(train_generator,
validation_data=valid_generator,
epochs=5)
train_generator.class_indices
drive.mount('/content/drive')
model_path = "/content/drive/My Drive/Model/car_classification_ResNet50_Preprocessed_fineTuned_allData.h5"
model.save(model_path)
drive.flush_and_unmount()
model_path = "/content/Model/car_classification_ResNet50_Preprocessed_fineTuned_allData.h5"
model.save(model_path)
del(model)
from tensorflow.keras.models import load_model
from google.colab import drive
drive.mount('/content/drive')
model_path = "/content/drive/My Drive/Model/car_classification_ResNet50_Preprocessed_fineTuned_allData.h5"
model = load_model(model_path)
drive.flush_and_unmount()
"""## Checking Model Sanity on Training Data"""
import cv2
path = TRAIN_DIR + "/Volkswagen Beetle Hatchback 2012/00019.jpg"
img = cv2.imread(path)
image_x = 256
image_y = 256
img = cv2.resize(img, (image_x, image_y))
img = np.array(img, dtype=np.float32)
img = np.reshape(img, (-1, image_x, image_y, 3))
image = mpimg.imread(path)
plt.imshow(image)
plt.show()
pred = model.predict(img)
print(np.argmax(pred))
# print(class_list[np.argmax(pred)])
car_label = list(filter(lambda x: train_generator.class_indices[x] == np.argmax(pred), train_generator.class_indices))[0]
print(car_label)
# print(labels[np.argmax(pred)])
# train_generator.class_indices
from sklearn.metrics import classification_report, confusion_matrix
datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
rotation_range=20,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
validation_split=0.9999999)
test_generator = datagen.flow_from_directory(
directory = TEST_DIR,
seed = 1,
class_mode = "categorical",
target_size = (IMG_SIZE,IMG_SIZE),
subset='validation',
batch_size = BATCH_SIZE)
# Make predictions on the test set
predictions = model.predict(test_generator)
# Get the class with the highest probability for each sample
y_pred = np.argmax(predictions, axis=1)
# Get the true classes
y_true = test_generator.classes
# Calculate the confusion matrix
cm = confusion_matrix(y_true, y_pred)
# Calculate precision, recall, and F1 score
report = classification_report(y_true, y_pred, target_names=class_names)
print("Confusion Matrix:")
print(cm)
print("Classification Report:")
print(report)