-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCM_.py
215 lines (195 loc) · 8.26 KB
/
CM_.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
import numpy as np
import tensorflow as tf
import os
from tqdm import tqdm
from tensorflow import keras
from tensorflow.keras.models import Sequential,Model
from tensorflow.keras.layers import ZeroPadding2D,Convolution2D,MaxPooling2D
from tensorflow.keras.layers import Dense,Dropout,Softmax,Flatten,Activation,BatchNormalization
from tensorflow.keras.preprocessing.image import load_img,img_to_array
from tensorflow.keras.applications.imagenet_utils import preprocess_input
import tensorflow.keras.backend as K
# import the necessary packages
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from imutils.video import VideoStream
import numpy as np
import imutils
import time
import cv2
import os
from keras.engine import Model
from keras_vggface.vggface import VGGFace
## required for semi-hard triplet loss:
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.framework import dtypes
import tensorflow as tf
import os
#mport dlib
import cv2
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential,Model
from tensorflow.keras.layers import ZeroPadding2D,Convolution2D,MaxPooling2D
from tensorflow.keras.layers import Dense,Dropout,Softmax,Flatten,Activation,BatchNormalization
from tensorflow.keras.preprocessing.image import load_img,img_to_array
from tensorflow.keras.applications.imagenet_utils import preprocess_input
import tensorflow.keras.backend as K
print("Loading Model")
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(224,224, 3)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(4096, (7, 7), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(4096, (1, 1), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(2622, (1, 1)))
model.add(Flatten())
model.add(Activation('softmax'))
model.load_weights('vgg_face_weights.h5')
model.summary()
vgg_face=Model(inputs=model.layers[0].input,outputs=model.layers[-2].output)
classifier_model=tf.keras.models.load_model('colab_Best_Weights_VGG16_.h5')
person_rep={0:'Abdullah',1: 'Adnan',2: 'Baber', 3:'I.Khan',4: 'mark zakr',5: 'Salman',6: 'Tahir', 7:'Trump',8: 'Zaman',9: 'Zardari'}
DIRECTORY = r"Test_data"
filenames =os.listdir (".")
CATEGORIES = os.listdir(DIRECTORY)
print(CATEGORIES)
# grab the list of images in our dataset directory, then initialize
# the list of data (i.e., images) and class images
data = []
labels = []
for category in CATEGORIES:
path = os.path.join(DIRECTORY, category)
plst = os.listdir(path)
for img in tqdm(plst, desc='Loading image of : ' + category):
img_path = os.path.join(path, img)
image = load_img(img_path, target_size=(224, 224))
data.append(image)
labels.append(category)
print('labels are : ', labels)
print('labels length is : ',len(labels))
pred_list=[]
for imgi in data:
crop_img = img_to_array(imgi)
crop_img = np.expand_dims(crop_img, axis=0)
crop_img = preprocess_input(crop_img)
img_encode = vgg_face(crop_img)
# Make Predictions
embed = K.eval(img_encode)
person = classifier_model.predict(embed)
#print(max(person)*100)
pred_list.append(person_rep[np.argmax(person)])
# if max(max(person))>0.90:
# name = person_rep[np.argmax(person)]
# # cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
# # img = cv2.putText(img, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255), 2, cv2.LINE_AA)
# # img = cv2.putText(img, str(np.max(person)), (right, bottom + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1,
# # cv2.LINE_AA)
# persons_in_img.append(name)
print('Predictions are : ',pred_list)
print('Predictions length : ',len(pred_list))
import pandas as pd
print('---------------------------Test CM-----------------------------')
y_train_actu = pd.Series(labels, name='Y_Test')
y_train_pred = pd.Series(pred_list, name='Y_Predicted')
# df_train_confusion = pd.crosstab(y_train_actu, y_train_pred)
# print(df_train_confusion)
# #print('Accuracy on training dataset : ',acc_train)
#
# import matplotlib.pyplot as plt
# def plot_confusion_matrix(df_confusion, title='Confusion matrix', cmap=plt.cm.gray_r):
# plt.matshow(df_confusion, cmap=cmap) # imshow
# #plt.title(title)
# plt.colorbar()
# tick_marks = np.arange(len(df_confusion.columns))
# plt.xticks(tick_marks, df_confusion.columns, rotation=45)
# plt.yticks(tick_marks, df_confusion.index)
# #plt.tight_layout()
# plt.ylabel(df_confusion.index.name)
# plt.xlabel(df_confusion.columns.name)
# plt.show()
#
# #plot_confusion_matrix(df_train_confusion)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix
def cm_analysis(y_true, y_pred, labels, ymap=None, figsize=(10,10)):
"""
Generate matrix plot of confusion matrix with pretty annotations.
The plot image is saved to disk.
args:
y_true: true label of the data, with shape (nsamples,)
y_pred: prediction of the data, with shape (nsamples,)
filename: filename of figure file to save
labels: string array, name the order of class labels in the confusion matrix.
use `clf.classes_` if using scikit-learn models.
with shape (nclass,).
ymap: dict: any -> string, length == nclass.
if not None, map the labels & ys to more understandable strings.
Caution: original y_true, y_pred and labels must align.
figsize: the size of the figure plotted.
"""
if ymap is not None:
y_pred = [ymap[yi] for yi in y_pred]
y_true = [ymap[yi] for yi in y_true]
labels = [ymap[yi] for yi in labels]
cm = confusion_matrix(y_true, y_pred, labels=labels)
cm_sum = np.sum(cm, axis=1, keepdims=True)
cm_perc = cm / cm_sum.astype(float) * 100
annot = np.empty_like(cm).astype(str)
nrows, ncols = cm.shape
for i in range(nrows):
for j in range(ncols):
c = cm[i, j]
p = cm_perc[i, j]
if i == j:
s = cm_sum[i]
annot[i, j] = '%.1f%%\n%d/%d' % (p, c, s)
elif c == 0:
annot[i, j] = ''
else:
annot[i, j] = '%.1f%%\n%d' % (p, c)
cm = pd.DataFrame(cm, index=labels, columns=labels)
cm.index.name = 'Actual'
cm.columns.name = 'Predicted'
fig, ax = plt.subplots(figsize=figsize)
sns.heatmap(cm, annot=annot, fmt='', ax=ax)
#plt.savefig(filename)
plt.show()
cm_analysis(y_train_actu, y_train_pred, CATEGORIES, ymap=None, figsize=(10,10))