davidsandberg / facenet

Face recognition using Tensorflow
MIT License
13.74k stars 4.8k forks source link

How to set a threshold for a realtime recognition #752

Open skyWalker1997 opened 6 years ago

skyWalker1997 commented 6 years ago

Hello!I have already learned some principle of face-net and how your code work. But I have some question about the classifier.In the classifier.py and a threshold question . Classifier, it used SVC classifier.And when I looking for "predict_proba" I find a probability option.So I looking for some code about libsvm_sparse.libsvm_sparse_predict_proba. But I couldn`t find any code about how this probability work out. I just supposed it was a reflection of distance which refer to the input to each class.(?) So when I worked with a recognition program(based on your real time recogntion),it came out a quesiton.I trained 6 classes and because the principle,when a untrained input was recognized,facenet will found the most close class. To avoid this problem,I have two solutions. First is to set a "others" class, which contained ,for example,50 different people(DIFFERENT,because I want any un trained input was more close to the class "other" than classes I trained.In fact I used a Chinese face dataset called cas-peal - The PEAL Face Database. I used 300 pics for train and 300 pics for test(not a reasonable propotion),all of those pics are different from each other),and 10 pics for each man i want to train.After test by the test dataset ,i works perfect,all of those 300 test pics signed to be "other" class.But it will not work when i chose some one who looks like the trained class.I wondered if i will works when the other class was more strong like contain 10000 different pics which was not in the people i want to recognized. Second is to set a threshold to the propability/score the SVC figured. But how to calculate this threshold is the question.I read some papers and learned about F1,ROC.But I when i make test, all of test pics was recognized correct. Is that my test data are short or there is another way out to set this threshold?

priyakansal commented 6 years ago

Hi ,

I am also looking for setting up the threshold. I do not want to recognize the person who is not there in my train set....even as other, so while inferring any person.... I want to set a threshold....have you got ..how to set the threshold?

please share if you set the theshold

thuoctran commented 6 years ago

Hi you just need to set a threshold before it returns the face id.

rain2008204 commented 6 years ago

how do it?

thuoctran commented 6 years ago

you can check it in face.py and use this function: best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices] if best_class_probabilities > threshold: return self.class_names[best_class_indices[0]]

priyakansal commented 6 years ago

Thanks @thuoctran.

skyWalker1997 commented 6 years ago

@thuoctran yep.I just set a threshold by some specific ways.And it work well.In the same time, I set a "untrained" class, which helped a lot.

priyakansal commented 6 years ago

hi,

@skyWalker1997 Can you please tell how you set that "untrained class"?

skyWalker1997 commented 6 years ago

@rain2008204 I figure out the threshold by trained a lot of classes.And each class contain more than 100 pics.Then, i chose the lowest probabilities of each class and figure an average value.Which is the threshold.But I dont think its a statistical methods with theoretical support.

skyWalker1997 commented 6 years ago

@priyakansal I used CASIA-Face dataset which contains many of chinese faces. In "Untrained" class ,each pic represent one man.And this man is not in the range you want to recognized

priyakansal commented 6 years ago

@skyWalker1997 I am working on my own dataset. So, should I add this "Untrained folder" with some images not in my range of recognition in my training image directory while training the classifier? eg. Training image directory should be structured as follows:

Person A A1.jpg A2.jpg . . . An.jpg

Person B B1.jpg B2.jpg . . . Bn.jpg

UNTRAINED any random image 1.jpg any random image 2.jpg . . . .

skyWalker1997 commented 6 years ago

@priyakansal yes that what i mean.

priyakansal commented 6 years ago

@skyWalker1997 thanks a lot !!!

skyWalker1997 commented 6 years ago

@priyakansal make sure the untrained class contains enough pics.And I set a threshold to add a pledge to the final result.

priyakansal commented 6 years ago

@skyWalker1997 that means the double assurance while recognizing a face ? one is introducing some untrained persons and second is setting a threshold also.

priyakansal commented 6 years ago

@rain2008204 we may also consider the highest probability of each mis recognized class.

skyWalker1997 commented 6 years ago

@priyakansal Yes,some of my workmates think either set “untrained” or "threshold" will work.Both pledge is not necessary.

priyakansal commented 6 years ago

@skyWalker1997 Got it !!! Thanks for the quick response everytime

rain2008204 commented 6 years ago

@priyakansal @skyWalker1997 thanks a lot. another question, we use MTCNN to detect the faces,so which size(20x20???),you will recognize them . I also set a threshold (20X20).

thuoctran commented 6 years ago

hi, I have a question. What is the difference between Classifier training of inception resnet v1 & Train a classifier on your own dataset? As I understand, the train_softmax.py is used for training embeddings and we will use those embeddings for classifier (SVM) model. I'm not certain and would also like a more thorough explanations. Thank you in advance :)

rain2008204 commented 6 years ago

@thuoctran you are right.

thuoctran commented 6 years ago

@rain2008204 but I don't understand, for example, when I add my own pictures and run the classifier.py, how can the new person features add to 128-D feature vectors without retrain the whole model?

rain2008204 commented 6 years ago

@thuoctran I can't get your idea, the new person features 128-D feature vectors can be generated by the inception-resnet-v1 network, you can tell me in chinese.

susmith98 commented 5 years ago

Hi !! Can anyone please explain me the concept of "untrained" class mentioned above. Thanks in advance

priyakansal commented 5 years ago

@susmith98 the "untrained class" here means that you can add one more category of totally random faces as "unknown category" to train the model. So when you will use this model to classify the unknown face (which you have not trained) , it will classify that face as unknown. I hope it would help you.

deepak-neva commented 5 years ago

how is it one shot learning if we have to apply an SVC on the embeddings?