How to train an object classifier using our own images
1. Info:
I prepared two python scripts (retrain.py, predict.py) for this task.
- retrain.py: used to train the classifier.
- predict.py: used to load the trained model and test on new images.
2. Prepare training and testing data:
-
Training Data: Let’s assume that we have two classes, namely “cat” and “dog”. We just need to make sure that there are two sub-folders in “training_images” folder. Each sub-folder only consists of its own images.
|- training_images |- cat |- image_0.jpg |- image_1.jpg ... |- dog |- image_2.jpg |- image_3.jpg ...
-
Testing Data: All testing images are put in one folder, e.g. “testing_images”.
|- testing_images |- image_0.jpg |- image_1.jpg |- image_2.jpg |- image_3.jpg ...
3. Train a classifier
Assume that both “training_images” and “testing_images” are in this folder:
> ls
models/ predict.py retrain.py testing_images/ training_images/
Then, we can type the following command to starting training process:
> python retrain.py \
--bottleneck_dir=./bottlenecks \
--how_many_training_steps=50000 \
--model_dir=./inception \
--output_graph=./models/retrained_graph.pb \
--output_labels=./models/retrained_labels.txt \
--summaries_dir=./retrain_logs \
--validation_batch_size=5000 \
--image_dir=training_images # this is the folder of training data
After this training process finished, it saves the trained model in “./models”.
> ls models
retrained_graph.pb retrained_labels.txt
4. Predict new images using trained model
Type the following command in a terminal to run the testing code:
> python predict.py \
--models_folder='./models' \
--test_image_folder='./testing_images' \
--display_image=False