from fastai.vision.all import *
from fastai_datasets.all import *
FaceNet
facenet
facenet (pretrained=True)
A binary classifier matching pairs of facial images
FaceNetInceptionResnetV1
FaceNetInceptionResnetV1 (pretrained=True, classify=True)
Like pytorch_facenet
’s InceptionResnetV1
but accepts standard float tensors
Performance on LFW
Let’s start by quickly evaluating on the development view of LFW:
= LFWPairs().dev().dls()
dls = facenet()
face_matcher
face_matcher.fit_threshold(dls.train)= Learner(dls, face_matcher, metrics=accuracy)
learn learn.validate()
(#2) [0.22585052251815796,0.9919999837875366]
learn.show_results()
face_matcher.plot_distance_histogram(dls.valid)
We can also evaluate the test accuracy (as defined by LFW: 10-fold cross validation on the test view):
class FacenetCrossValidation(RepeatedExperiment):
def iteration(self):
self.model.fit_threshold(self.dls.train)
= Learner(self.dls, self.model, metrics=accuracy).validate()
stats return dict(zip(['loss', 'accuracy'], stats))
= FacenetCrossValidation(facenet('vggface2'), LFWPairs().test()).run() res
res.plot_stats()
print(as_percentage(res.stat_means['accuracy']))
99.35%
Performance on SLLFW
For a quick evaluation, let’s use the first cross-validation split (since SLLFW doesn’t have a dev view)
= SLLFWPairs().test()[0].dls()
dls = facenet()
face_matcher
face_matcher.fit_threshold(dls.train)= Learner(dls, face_matcher, metrics=accuracy)
learn learn.validate()
(#2) [0.36826473474502563,0.9466666579246521]
learn.show_results()
face_matcher.plot_distance_histogram(dls.valid)
And evaluating properly using the 10-fold cross validation:
= FacenetCrossValidation(facenet('vggface2'), SLLFWPairs().test()).run() res
res.plot_stats()
print(as_percentage(res.stat_means['accuracy']))
94.85%