Open 008karan opened 6 years ago
I think you can use predict_proba instead
@ageitgey Can you please help on this?
Hi! In SVM there no something like "closest_distances", but you can calculate the "probability" (the prediction confidence). If you combine this with a threshold you can obtain the "Unknown label". To do so:
Add the parameter "probability=True"
svm_clf = SVC(gamma='auto', kernel='rbf', probability=True, C=20, )
svm_clf.fit(X, y)
When you do the prediction use:
svm_clf.predict_proba(face_encodings)
This function returns the value of confidence for each trained element.
I have done a fast implementation about this. It works with only one face for img, I've not tested it with multiple faces.
Try by yourself:
def predict_SVM(faces_encodings):
known_faces_names = ["face1", "face2"]
DIST_THRESH = 0.7
recognized_faces = []
face_distances = svm_clf.predict_proba(faces_encodings)
for i in range(len(faces_encodings)):
name = "N/A"
max_index = np.argmax(face_distances[i])
if face_distances[i][max_index] >= DIST_THRESH:
name = known_faces_names[max_index]
recognized_faces.append(name)
return recognized_faces
I suggest to read this: https://prateekvjoshi.com/2015/12/15/how-to-compute-confidence-measure-for-svm-classifiers/
It's very clear and explain how this works and also how the implementation works. Bye 👋
I tried to implement SVM in place of KNN but got stuck. In KNN we compare the distance from the nearest neighbor with a threshold value. How to do these in SVM? I made changes here.
svm_clf = SVC(gamma='auto', kernel='rbf', C=20) svm_clf.fit(X, y)
but don't know what changes i need to do here:
closest_distances= knn_clf.kneighbors(faces_encodings, n_neighbors=1)
is_recognized = [closest_distances[0][i][0] <= DIST_THRESH for i in range(len(X_faces_loc))]
return [(pred, loc) if rec else ("N/A", loc) for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_faces_loc, is_recognized)]