-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
316 lines (225 loc) · 6.67 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
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
# ## Problem Statement:
#
#
# Build a Resnet50 CNN classifier to pridict scores of these breeds only:
# beagle, chihuahua, doberman, french_bulldog, golden_retriever, malamute, pug, saint_bernard, scottish_deerhound, tibetan_mastiff.
#
# Importing libraries :
# %%
import pandas as pd
import numpy as np
from numpy import save
import cv2
import matplotlib.pyplot as plt
from tqdm import tqdm
from PIL import Image
from random import randrange
import kaggle
from prettytable import PrettyTable
## KERAS
import tensorflow as tf
from sklearn.model_selection import train_test_split
from keras.utils import np_utils
from tensorflow import keras
from tensorflow.keras import layers
from keras.optimizers import SGD
from keras import applications
from mpl_toolkits.axes_grid1 import ImageGrid
from keras.models import Sequential,Model,load_model
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D,GlobalAveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.applications.resnet50 import ResNet50
from tensorflow.keras.activations import relu, softmax
#
# # 2. Reading Data
# %%
# Reading csv file with pandas
df = pd.read_csv('labels.csv')
df.shape
#
# The dataset has total of 10222 rows
#
# %%
# Creating custom dog breed list
dogs = ['beagle', 'chihuahua', 'doberman', 'french_bulldog', 'golden_retriever', 'malamute', 'pug', 'saint_bernard', 'scottish_deerhound', 'tibetan_mastiff']
images_dir = 'train/'
labels_dir = 'labels.csv'
# %%
custom_breed = df.loc[df.breed.isin(dogs)].reset_index(drop=True)
custom_breed.shape
#
# After filtering custom breeds we get a dataset of approx 850 rows.
#
# %%
custom_breed.head()
# %%
custom_breed.breed.nunique()
# %%
custom_breed['breed'].value_counts().sum
#
# The data has high number of images of scottish deerhound with 126 images, whereas golden retriver has lowest number of images with 64 only.
#
#
# 3. Displaying a random image of dog from the data.
#
# %%
random_dog = randrange(custom_breed.shape[0])
print(f'Random index location {random_dog}')
# %%
print('Dog Breed:',custom_breed.loc[random_dog,'breed'])
Image.open(images_dir + custom_breed.loc[random_dog, "id"] + ".jpg")
#
# # 4. Data preprocessing
#
# %%
# Reading images and converting each image into array and apending them to a list
image_list = []
label_list = []
for index, x in tqdm(custom_breed.iterrows()):
image = cv2.imread(images_dir + x['id'] + '.jpg')
image = cv2.resize(image, (300, 300))
image_list.append(image)
label_list.append(x['breed'])
# %%
print(f'No of Images: {len(image_list)} \nNo of Labels: {len(label_list)}')
# %%
# Convert list to array
data = np.array(image_list) #/ 255
data.shape
# %%
# Saving the data and labes into npy file on the system [Optional]
save('dog_images.npy', data)
save('labels.npy', label_list)
# %%
# Reading the files:
from numpy import load
photos = load('dog_images.npy')
labels = load('labels.npy')
print(photos.shape, labels.shape)
# %%
# Converting labels to numbers
from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical
label_encoder = LabelEncoder()
vec = label_encoder.fit_transform(labels)
target = to_categorical(vec)
target
#
#
# %%
x_train, x_test, y_train, y_test = train_test_split(photos,target, test_size=0.3)
# %%
# Checking the size of train and test data
x_train.shape, x_test.shape, y_train.shape, y_test.shape
# %%
num_class = y_test.shape[1]
#
# # 5.1 Building the Model
# %%
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import MaxPooling2D
# %%
# Importing ResNet50 from Keras
from tensorflow.keras.applications.resnet50 import ResNet50
# load model
model = Sequential()
# Adding Resnet50 with pretrained imagenet weights
img_height,img_width = 300,300
model.add(ResNet50(weights="imagenet", pooling=max, include_top=False, input_shape= (img_height,img_width,3)))
# 2nd layer as Dense for classification
model.add(GlobalAveragePooling2D())
model.add(Dropout(0.2))
model.add(Dense(10, activation = 'softmax'))
# Not training firstlayer
model.layers[0].trainable = False
# %%
# Defining sgd optimizer
rate = 0.01
sgd = SGD(lr=rate)
# %%
model.compile(optimizer= sgd, loss='categorical_crossentropy',metrics=['accuracy'])
model.summary()
# %%
# Traing the network for 10 epochs
model.fit(x_train, y_train, epochs = 10,batch_size=32, validation_split=0.3)
# %%
preds = model.evaluate(x_test, y_test)
print ("Loss = " , preds[0])
print ("Test Accuracy = ", preds[1])
# %%
y_pred_prob = model.predict(x_test)
# %%
y_pred = y_pred_prob.argmax(axis=-1)
y_pred = to_categorical(y_pred)
#
# # 5.2 Calculating Performace
# %%
# Calculating Performace metrics
from sklearn.datasets import make_circles
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import roc_auc_score
from sklearn.metrics import confusion_matrix
# %%
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='micro')
recall = recall_score(y_test, y_pred, average='micro')
f1 = f1_score(y_test, y_pred, average='micro')
auc = roc_auc_score(y_test, y_pred, average='micro')
# %%
from prettytable import PrettyTable
# %%
x = PrettyTable()
x.field_names = ["Metrics", "Score"]
x.add_row(["Accuracy", round(accuracy,3)])
x.add_row(["Precision", round(precision,3)])
x.add_row(["Recall", round(recall,3)])
x.add_row(["F1 Score", round(f1,3)])
x.add_row(["ROC-AUC Score", round(auc,3)])
print(x)
# %%
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
matrix = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
confusion_df = pd.DataFrame(matrix, index = dogs, columns = dogs)
plt.figure(figsize = (10,8))
sn.heatmap(confusion_df, annot=True)
# %%
# Saving our model for future use:
model.save('dog_breed_model.h5')
#
# **Conclusion**: Our model performs well on the given data with overall accuracy of 0.95
#
#
# 6 Testing on custom image data
#
#
# %%
def breed_predictor(image_file):
image = cv2.imread(image_file)
image = cv2.resize(image, (300, 300))
# dog_img = Image.open(image_file)
image = image.reshape(1,300,300,3)
result_prob = model.predict(image)
result = result_prob.argmax(axis=-1)
result = label_encoder.inverse_transform(result)
return f'Predicted Breed: {result[0]} with probability of {round(np.amax(result_prob)*100,2)}%'
# %%
file_n = 'gold_ret.jpg'
breed_predictor(file_n)
# %%
Image.open(file_n)
# %%
file_n = 'dobber.jpg'
breed_predictor(file_n)
# %%
Image.open(file_n)
# %%
file_n = 'saint_berni.jpg'
breed_predictor(file_n)
# %%
Image.open(file_n)