-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
172 lines (150 loc) · 5.95 KB
/
classifier.py
File metadata and controls
172 lines (150 loc) · 5.95 KB
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
#!/usr/bin/env python2
# MODIFIED from original apache license (2017):
#
# Example to classify faces.
# Brandon Amos
# 2015/10/11
#
# Copyright 2015-2016 Carnegie Mellon University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Modified significantly from source
import time
start = time.time()
import argparse
import cv2
import os
import pickle
from os.path import isdir, join
from operator import itemgetter
import numpy as np
import pandas as pd
import openface
from sklearn.preprocessing import LabelEncoder
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from random import sample
np.set_printoptions(precision=2)
fileDir = os.path.dirname(os.path.realpath(__file__))
modelDir = os.path.join(fileDir, 'models')
dlibModelDir = os.path.join(modelDir, 'dlib')
openfaceModelDir = os.path.join(modelDir, 'openface')
dlibFacePredictor = os.path.join(dlibModelDir,"shape_predictor_68_face_landmarks.dat")
networkModel = os.path.join(openfaceModelDir,'nn4.small2.v1.t7')
workDir = "data/"
classifierFile = "{}/classifier_%02d.pkl".format(workDir)
imgDim = 96
#Decision forest parameters
sampleRatio = .7
minFacesPerPerson = 5
unknownThreshold = .5
questionableThreshold = .7
trainingRunning = False
needsToRun = False
class Classifier:
def __init__(self, ensembleSize = 30):
self.trainingRunning = False
self.needsToRun = False
self.ensembleSize = ensembleSize
def train(self, recognizerMutex):
"""Queues an ensemble classifier train process, locking/using the mutex
during saving"""
if self.trainingRunning:
print "Deferring run"
self.needsToRun = True
return
self.trainingRunning = True
try:
os.remove("faces/cache.t7")
except:
pass
os.system("batch-represent/main.lua -outDir data/ -data faces/")
images = "faces/"
cuda = True
align = openface.AlignDlib(dlibFacePredictor)
net = openface.TorchNeuralNet(networkModel, imgDim=imgDim, cuda=cuda)
workDir="data/"
fname = "{}/labels.csv".format(workDir)
labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]
labels = map(itemgetter(1),
map(os.path.split,
map(os.path.dirname, labels))) # Get the directory.
fname = "{}/reps.csv".format(workDir)
embeddings = pd.read_csv(fname, header=None).as_matrix()
keyPairs = {}
for label, embedding in zip(labels, embeddings):
if label in keyPairs:
keyPairs[label].append(embedding)
else:
keyPairs[label]=[embedding]
for i in range(1,1+self.ensembleSize):
subSample = {label:sample(embeddings,min(len(embeddings),minFacesPerPerson,int(round(sampleRatio*len(embeddings))))) for label, embeddings in keyPairs.items()}
labelsSample, embeddingsSample = [], []
for label, embeddings in subSample.items():
for embedding in embeddings:
labelsSample.append(label)
embeddingsSample.append(embedding)
le = LabelEncoder().fit(labelsSample)
labelsNum = le.transform(labelsSample)
nClasses = len(le.classes_)
clf = SVC(C=1, kernel='linear', probability=True)#GaussianNB()#(C=1, kernel='linear', probability=True)
clf.fit(embeddingsSample, labelsNum)
recognizerMutex.append(" ")
with open(classifierFile%i, 'w') as f:
pickle.dump((le, clf), f)
recognizerMutex.pop()
print "Setting trainingRunning to false", self.trainingRunning, self.needsToRun
self.trainingRunning = False
if self.needsToRun:
print "Rerunning train from defer"
self.train(recognizerMutex)
needsToRun=False
def infer(self, reps, recognizerMutex):
"""
Identifies faces in reps with accuracy ratings. Returns the found faces
with indices corresponding and with the confidences corresponding to the
faces.
"""
listOfResults = [{} for i in reps]
totalPredicted = [0 for i in reps]
for i in range(1,1+self.ensembleSize):
recognizerMutex.append(" ")
try:
with open(classifierFile%i, 'r') as f:
(le, clf) = pickle.load(f)
except Exception:
self.ensembleSize-=1
continue
recognizerMutex.pop()
persons = []
confidences = []
for ind, rep in enumerate(reps):
try:
rep = rep.reshape(1, -1)
except:
print "One of the detected faces is invalid"
continue
predictions = clf.predict_proba(rep).ravel()
maxI = np.argmax(predictions)
name = le.inverse_transform(maxI)
print predictions[maxI],
#if predictions[maxI]>1.0/(len(predictions)-1):
listOfResults[ind][name] = 1+ listOfResults[ind].get(name,0)
totalPredicted[ind]+=1
print ""
for i in range(len(reps)):
selected = max(listOfResults[i].items(),key=lambda a:a[1])
confidence = float(selected[1])/max(1,totalPredicted[i])#Prevent 0/0 in bad classifier case
persons.append(selected[0])
confidences.append(confidence)
return (persons, confidences)