ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
53.41k stars 13.49k forks source link

how to implement SVM inplace of KNN in face recognition #477

Open 008karan opened 6 years ago

008karan commented 6 years ago

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)]

donaldxu commented 5 years ago

I think you can use predict_proba instead

Roosh27 commented 4 years ago

@ageitgey Can you please help on this?

lanzani commented 4 years ago

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:

Step 1:

Add the parameter "probability=True" svm_clf = SVC(gamma='auto', kernel='rbf', probability=True, C=20, ) svm_clf.fit(X, y)

Step 2:

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 👋