How can i conduct performance tests in OpenCV Python to check;
- the time it takes to obtain a recognition result
- the false accept/false reject rate on the database test cases.
I am using the example eigenface method in OpenCV (from Phillip – https://github.com/bytefish/facerecognition_guide) and am just interested in the results. Would be greatful if someone could point me in the right direction/show examples.Perhaps there are some functions that i could make use of?
Validating OpenCV algorithms
introduction
First of all sorry that it took so long to reply, but there was simply no spare time left. Actually validating algorithms is a very interesting topic and it’s really not that hard. In this post I’ll show how to validate your algorithms (I’ll take the FaceRecognizer, because you’ve asked for it). As always in my posts I will show it with a full source code example, because I think it’s much easier to explain stuff by code.
So whenever people tell me “my algorithm performs bad”, I ask them:
My hope is, that this post will clear up some confusion and show how easy it is to validate algorithms. Because what I have learned from experimenting with computer vision and machine learning algorithms is:
All code in this post is put under BSD License, so feel free to use it for your projects.
validating algorithms
One of the most important tasks of any computer vision project is to acquire image data. You need to get the same image data as you expect in production, so you won’t have any bad experiences when going live. A very practical example: If you want to recognize faces in the wild, then it isn’t useful to validate your algorithms on images taken in a very controlled scenario. Get as much data as possible, because Data is king. That for the data.
Once you have got some data and you have written your algorithm, it comes to evaluating it. There are several strategies for validating, but I think you should start with a simple Cross Validation and go on from there, for informations on Cross Validation see:
Instead of implementing it all by ourself, we’ll make use of scikit-learn a great Open Source project:
It has a very good documentation and tutorials for validating algorithms:
So the plan is the following:
cv2.FaceRecognizerinto a scikit-learn estimator.cv2.FaceRecognizerwith a given validation and metric.Getting the image data right
First I’d like to write some words on the image data to be read, because questions on this almost always pop up. For sake of simplicity I have assumed in the example, that the images (the faces, persons you want to recognize) are given in folders. One folder per person. So imagine I have a folder (a dataset) call
images, with the subfoldersperson1,person2and so on:One of the public available datasets, that is already coming in such a folder structure is the AT&T Facedatabase, available at:
Once unpacked it is going to look like this (on my filesystem it is unpacked to
/home/philipp/facerec/data/at/, your path is different!):putting it together
So first of all we’ll define a method
read_imagesfor reading in the image data and labels:Reading in the image data then becomes as easy as calling:
Because some algorithms (for example Eigenfaces, Fisherfaces) require your images to be of equal size, I added a second parameter
sz. By passing the tuplesz, all of the images get resized. So the following call will resize all images in/path/to/some/folderto100x100pixels.:All classifiers in scikit-learn are derived from a
BaseEstimator, which is supposed to have afitandpredictmethod. Thefitmethod gets a list of samplesXand corresponding labelsy, so it’s trivial to map to the train method of thecv2.FaceRecognizer. Thepredictmethod also gets a list of samples and corresponding labels, but this time we’ll need to return the predictions for each sample:You can then choose between a large range of validation methods and metrics to test the
cv2.FaceRecognizerwith. You can find the available cross validation algorithms in sklearn.cross_validation:For estimating the recognition rate of the
cv2.FaceRecognizerI suggest using a Stratified Cross Validation. You may ask why anyone needs the other Cross Validation methods. Imagine you want to perform emotion recognition with your algorithm. What happens if your training set has images of the person you test your algorithm with? You will probably find the closest match to the person, but not the emotion. In these cases you should perform a subject-independent cross validation.Creating a Stratified k-Fold Cross Validation Iterator is very simple with scikit-learn:
And there’s a wide range of metrics we can choose from. For now I only want to know the precision of the model, so we import the callable function
sklearn.metrics.precision_score:Now we’ll only need to create our estimator and pass the
estimator,X,y,precision_scoreandcvtosklearn.cross_validation.cross_val_score, which calculates the cross validation scores for us:There’s a large amount of metrics available, feel free to choose another one:
So let’s put all this in a script!
validation.py
running the script
The above script will print out the precision scores for the Fisherfaces method. You simply need to call the script with the image folder:
conclusion
The conclusion is, that using Open Source projects makes your life really easy! There’s much to enhance for the example script. You probably want to add some logging, to see which fold you are in for example. But it’s a start for evaluating any metric you want, just read through the scikit-learn tutorials to see how to do it and adapt it to the above script.
I encourage everybody to play around with OpenCV Python and scikit-learn, because interfacing these two great projects is really, really easy as you can see.