Giter VIP home page Giter VIP logo

keras-finetuning's Introduction

Training an InceptionV3-based image classifier with your own dataset

Based on the Fine-tune InceptionV3 on a new set of classes example in https://keras.io/applications/

Dependencies

Very latest (>=1.0.8 from source) Keras, scipy, pillow. opencv2 is only used in the toy webcam app. See osx-install.sh for installation instructions on OS X.

Training

Structure your image files in the following directory hierarchy. Sub-sub directories are allowed and traversed:

data_dir/classname1/*.*
data_dir/classname2/*.*
...

It depends on the domain, but a few hundred images per class can already give good results.

Run the training:

python train.py data_dir model

The standard output provides information about the state of the training, and the current accuracy. Accuracy is measured on a random 20% validation set. During training, Keras outputs the accuracy on the augmented validation dataset (val_acc). After a training round, the validation accuracy on non-augmented data is printed.

The files 000.png 001.png etc. give a visual confusion matrix about the progress of the training. 000.png is created after the newly created dense layers were trained, and the rest during fine-tuning.

The model is saved in three files, named model.h5, model.json, model-labels.json.

Webcam integration

If you train the model with the labeled faces of your friends and relatives, you can test your classifier in a toy app.

python webcam.py model

This does face detection on the webcam stream, and tags the detected faces according to the neural model. It looks for the model* files in the current directory. The file haarcascade_frontalface_default.xml must also be there.

Webcam data is quite different from photos, so to let the model generalize, set heavy_augmentation = True in train.py. For other applications, heavy_augmentation = False might be preferable.

Apple Photos: a great source of training data for face recognition

OS X Photos users can find high quality training data in the Photos Libraries of that application. Mihály Köles and I have reverse engineered the database format of Photos, and the result is an easy-to-use tool for building your personalized face recognition training datasets from Photos Libraries:

bash collect-apple-photos.sh "$HOME/Pictures/Photos Library.photoslibrary" photos_library_dataset

The output of the above script is the photos_library_dataset directory that has exactly the right layout to be used as input for the training script:

python train.py photos_library_dataset model
python webcam.py model

Of course, very small label classes won't generalize well to unseen data. It might make sense to consolidate their contents into the generic unknown label class, which contains faces not yet labeled by Apple Photos:

mv photos_library_dataset/too_small_class/* photos_library_dataset/unknown
rmdir photos_library_dataset/too_small_class

If you simply remove the unknown directory from the dataset before training, that leads to a "closed world" model that assumes that everyone appearing on your webcam stream has his or her Photos label.

The Photos Library data layout

For those interested, here's a bit more information about the Photos data layout. (Look into the source code of collect-apple-photos.sh for all relevant detail.) *.photoslibrary/resources/modelresources/ contains the manually and semi-automatically tagged faces cropped from your photos, and the *.photoslibrary/database/Person.db and *.photoslibrary/database/ImageProxies.apdb sqlite3 databases describe the correspondence between persons and cropped photos. The relevant tables for our purposes are Person.RKFace, Person.RKPerson, and ImageProxies.RKModelResource.

keras-finetuning's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.