2016-09-15 20:43:39 +08:00
|
|
|
|
## Demo 3: Training a Classifier
|
|
|
|
|
|
|
|
|
|
Released by [Brandon Amos](http://bamos.github.io) on 2015-10-13.
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2015-11-01 20:52:46 +08:00
|
|
|
|
OpenFace's core provides a feature extraction method to
|
|
|
|
|
obtain a low-dimensional representation of any face.
|
2015-11-11 03:31:24 +08:00
|
|
|
|
[demos/classifier.py](https://github.com/cmusatyalab/openface/blob/master/demos/classifier.py)
|
|
|
|
|
shows a demo of how these representations can be
|
|
|
|
|
used to create a face classifier.
|
2015-11-01 20:52:46 +08:00
|
|
|
|
|
2015-11-11 03:31:24 +08:00
|
|
|
|
There is a distinction between training the deep neural network (DNN)
|
|
|
|
|
model for feature representation
|
|
|
|
|
and training a model for classifying people with the DNN model.
|
|
|
|
|
This shows how to use a pre-trained DNN model to train and use
|
|
|
|
|
a classification model.
|
|
|
|
|
|
|
|
|
|
## Creating a Classification Model
|
|
|
|
|
|
|
|
|
|
### 1. Create raw image directory.
|
|
|
|
|
Create a directory for your raw images so that images from different
|
|
|
|
|
people are in different subdirectories. The names of the labels or
|
|
|
|
|
images do not matter, and each person can have a different amount of images.
|
|
|
|
|
The images should be formatted as `jpg` or `png` and have
|
|
|
|
|
a lowercase extension.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ tree data/mydataset/raw
|
|
|
|
|
person-1
|
|
|
|
|
├── image-1.jpg
|
|
|
|
|
├── image-2.png
|
|
|
|
|
...
|
|
|
|
|
└── image-p.png
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
person-m
|
|
|
|
|
├── image-1.png
|
|
|
|
|
├── image-2.jpg
|
|
|
|
|
...
|
|
|
|
|
└── image-q.png
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2015-11-12 00:20:27 +08:00
|
|
|
|
### 2. Preprocess the raw images
|
2015-11-11 03:31:24 +08:00
|
|
|
|
Change `8` to however many
|
|
|
|
|
separate processes you want to run:
|
2016-01-13 21:36:16 +08:00
|
|
|
|
`for N in {1..8}; do ./util/align-dlib.py <path-to-raw-data> align outerEyesAndNose <path-to-aligned-data> --size 96 & done`.
|
2015-11-11 03:31:24 +08:00
|
|
|
|
|
2016-03-12 05:50:35 +08:00
|
|
|
|
If failed alignment attempts causes your directory to have too few images,
|
|
|
|
|
you can use our utility script
|
|
|
|
|
[./util/prune-dataset.py](https://github.com/cmusatyalab/openface/blob/master/util/prune-dataset.py)
|
|
|
|
|
to deletes directories with less than a specified number of images.
|
|
|
|
|
|
2015-11-12 00:20:27 +08:00
|
|
|
|
### 3. Generate Representations
|
2015-11-12 00:00:21 +08:00
|
|
|
|
`./batch-represent/main.lua -outDir <feature-directory> -data <path-to-aligned-data>`
|
|
|
|
|
creates `reps.csv` and `labels.csv` in `<feature-directory>`.
|
|
|
|
|
|
2015-11-12 00:20:27 +08:00
|
|
|
|
### 4. Create the Classification Model
|
2015-11-12 00:00:21 +08:00
|
|
|
|
Use `./demos/classifier.py train <feature-directory>` to produce
|
2015-11-11 03:31:24 +08:00
|
|
|
|
the classification model which is an SVM saved to disk as
|
|
|
|
|
a Python pickle.
|
|
|
|
|
|
|
|
|
|
Training uses [scikit-learn](http://scikit-learn.org) to perform
|
|
|
|
|
a grid search over SVM parameters.
|
|
|
|
|
For 1000's of images, training the SVMs takes seconds.
|
|
|
|
|
|
|
|
|
|
## Classifying New Images
|
2016-01-13 04:46:49 +08:00
|
|
|
|
We have released a `celeb-classifier.nn4.small2.v1.pkl` classification model
|
2015-11-11 03:31:24 +08:00
|
|
|
|
that is trained on about 6000 total images of the following people,
|
2015-11-01 20:52:46 +08:00
|
|
|
|
which are the people with the most images in our dataset.
|
|
|
|
|
Classifiers can be created with far less images per
|
|
|
|
|
person.
|
|
|
|
|
|
|
|
|
|
+ America Ferrera
|
|
|
|
|
+ Amy Adams
|
|
|
|
|
+ Anne Hathaway
|
|
|
|
|
+ Ben Stiller
|
|
|
|
|
+ Bradley Cooper
|
|
|
|
|
+ David Boreanaz
|
|
|
|
|
+ Emily Deschanel
|
|
|
|
|
+ Eva Longoria
|
|
|
|
|
+ Jon Hamm
|
|
|
|
|
+ Steve Carell
|
|
|
|
|
|
|
|
|
|
For an example, consider the following small set of images
|
|
|
|
|
the model has no knowledge of.
|
|
|
|
|
For an unknown person, a prediction still needs to be made, but
|
|
|
|
|
the confidence score is usually lower.
|
|
|
|
|
|
2015-12-25 00:07:19 +08:00
|
|
|
|
Run the classifier with:
|
2015-11-01 20:52:46 +08:00
|
|
|
|
|
|
|
|
|
```
|
2016-01-13 04:46:49 +08:00
|
|
|
|
./demos/classifier.py infer ./models/openface/celeb-classifier.nn4.small2.v1.pkl images/examples/{carell,adams,lennon}*
|
2015-11-01 20:52:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
| Person | Image | Prediction | Confidence |
|
|
|
|
|
|---|---|---|---|
|
2016-01-13 04:46:49 +08:00
|
|
|
|
| Carell | <img src='https://raw.githubusercontent.com/cmusatyalab/openface/master/images/examples/carell.jpg' width='200px'></img> | SteveCarell | 0.97 |
|
|
|
|
|
| Adams | <img src='https://raw.githubusercontent.com/cmusatyalab/openface/master/images/examples/adams.jpg' width='200px'></img> | AmyAdams | 0.81 |
|
|
|
|
|
| Lennon 1 (Unknown) | <img src='https://raw.githubusercontent.com/cmusatyalab/openface/master/images/examples/lennon-1.jpg' width='200px'></img> | SteveCarell | 0.50 |
|
|
|
|
|
| Lennon 2 (Unknown) | <img src='https://raw.githubusercontent.com/cmusatyalab/openface/master/images/examples/lennon-2.jpg' width='200px'></img> | DavidBoreanaz | 0.43 |
|
2015-12-13 02:43:30 +08:00
|
|
|
|
|
|
|
|
|
# Minimal Working Example to Extract Features
|
|
|
|
|
|
|
|
|
|
```
|
2015-12-13 02:45:54 +08:00
|
|
|
|
mkdir -p classify-test/raw/{lennon,clapton}
|
|
|
|
|
cp images/examples/lennon-* classify-test/raw/lennon
|
|
|
|
|
cp images/examples/clapton-* classify-test/raw/clapton
|
2016-01-13 21:36:16 +08:00
|
|
|
|
./util/align-dlib.py classify-test/raw align outerEyesAndNose classify-test/aligned --size 96
|
2015-12-13 02:45:54 +08:00
|
|
|
|
./batch-represent/main.lua -outDir classify-test/features -data classify-test/aligned
|
2015-12-13 02:43:30 +08:00
|
|
|
|
...
|
|
|
|
|
nImgs: 4
|
|
|
|
|
Represent: 4/4
|
|
|
|
|
```
|