-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMixDlibDnn.py
122 lines (104 loc) · 4.38 KB
/
MixDlibDnn.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
import numpy as np
import os
import pickle
from usol import usolUtil
from usol import deep
'''
def transToNpy(tempFeatures, temlpLabel):
features = np.zeros(shape=(tempFeatures.__len__(), 128))
i = 0
for fe in tempFeatures:
features[i] = fe;
i = i + 1
labels = np.zeros(shape=(temlpLabel.__len__(), labelCount))
labels = np_utils.to_categorical(temlpLabel)
return features, labels
'''
trainPath = '../dataset/pre224/people/train'
validationPath = '../dataset/pre224/people/validation'
#testPath = '../dataset/people/test'
day = '0426'
usolDlib = deep.usolDlib()
if os.path.isfile("trainXY" + day + ".npz"):
tr, va, te = np.load('trainXY' + day +'.npz'), np.load('validationXY' + day+ '.npz'), np.load('testXY' + day + '.npz')
trainFeatures, trainLabel = tr['x'], tr['y']
validationFeatures, validationLabel = va['x'], va['y']
#testFeatures, testLabel = va['x'], va['y']
else:
trainFeatures, trainLabel = usolDlib.generateXYFromDir(path=trainPath)
np.savez('trainXY' + day, x=trainFeatures, y=trainLabel)
validationFeatures, validationLabel = usolDlib.generateXYFromDir(path=validationPath)
np.savez('validationXY' + day, x=validationFeatures, y=validationLabel)
#testFeatures, testLabel = usolDlib.generateXYFromDir(path=testPath)
#np.savez('testXY' + day, x=testFeatures, y=testLabel)
#dlib.hit_enter_to_continue()
from keras import models
from keras import layers
from keras import optimizers
from keras.models import load_model
from keras.preprocessing.image import load_img
import matplotlib.pyplot as plt
from PIL import Image
import numpy
from usol import deep
idx2label = usolUtil.makeIndexToLabelFromDir(trainPath)
classCount = len(idx2label)
if os.path.isfile('dilb_based_model' + day + '.h5') == False:
model = models.Sequential()
model.add(layers.Dense(128, activation='relu', input_dim=128))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(classCount, activation='softmax'))
model.summary()
model.compile(optimizer=optimizers.RMSprop(lr=2e-4),
loss='categorical_crossentropy',
metrics=['acc'])
history = model.fit(trainFeatures,
trainLabel,
epochs=60,
batch_size=20,
validation_data=(validationFeatures, validationLabel))
score = model.evaluate(trainFeatures, trainLabel, batch_size = 20)
print("-----------------result!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-----------------")
print("%s : %2f%%" %(model.metrics_names[1], score[1]*100))
print("-----------------model save!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-----------------")
model.save('dilb_based_model' + day + '.h5')
myModel = load_model('dilb_based_model' + day + '.h5')
testFile = '{}/{}/{}'.format(validationPath, 'parknare', '000065.jpg')
original = load_img(testFile)
plt.imshow(original)
img = Image.open(testFile).convert('RGB')
img = numpy.array(img)
dets = usolDlib.detector(img, 1)
testF = usolDlib.extractFeatureFromImg(dets=dets, img=img)
predictions = myModel.predict_classes(testF[0].reshape(1, -1))
prob = myModel.predict(testF[0].reshape(1, -1))
print(idx2label[predictions[0]])
print(np.amax(prob[0]))
from sklearn.neighbors import KNeighborsClassifier
knn =None
if os.path.isfile('knn_model' + day + '.pkl'):
with open('knn_model' + day + '.pkl', 'rb') as f:
knn = pickle.load(f)
else:
print("-----------------knn fit & save!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-----------------")
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(trainFeatures, trainLabel)
model_score = knn.score(trainFeatures, trainLabel)
print(model_score)
with open('knn_model' + day + '.pkl', 'wb') as f:
pickle.dump(knn, f)
print("-----------------knn predict!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-----------------")
result = knn.predict(testF[0].reshape(1, -1))
print(idx2label[np.where(result[0]==1)[0][0]])
print('Finish training...')
print("--------------L2 distance!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!--------------")
i = 0
for f in validationFeatures:
if((validationLabel[i] == result[0]).all()):
faceDistance = np.linalg.norm(testF[0] - f, axis=None, ord=2)
if(faceDistance < 0.6):
print(i)
print(faceDistance)
print(idx2label[np.where(validationLabel[i]==1)[0][0]])
i = i +1
plt.show()