Caffe:ImageNetTutorial
lastmod 2025-02-14created 2015-10-1631 edits
Notes on how to train and test with Caffe.
The terms
- Training images: List of images for training. Stored in
train.txtalong with label information. - Validation images: List of images and labels for validation. Stored in
val.txtalong with label information. - deploy:
- solver:
Preparing the data
Get the image data from ImageNet. The obtained TEXT file containing the list of URLs can be downloaded as follows.
#!/bin/sh
WORKING=$PWD
URLS_FILE=$WORKING/fall11_urls.txt
DEST=$WORKING/train
if [[ ! -d $DEST ]]; then
mkdir $DEST
fi
cd $DEST
COUNT=`wc -l $URLS_FILE | awk '{ print $1 }'`
INDEX=0
echo "Total count: $COUNT"
echo "Download start!"
for path in $(cat $URLS_FILE); do
let "INDEX = INDEX + 1"
echo "Download: $INDEX/$COUNT"
wget --timeout=10 --tries=2 $path
done
echo "Download end."
Place the training images (train) and validation images (val) on disk as follows.
/path/to/imagenet/train/n01440764/n01440764_10026.JPEG
/path/to/imagenet/val/ILSVRC2012_val_00000001.JPEG
Also, download the auxiliary data for training using the script below. (For reference, it is about 17M in size)
When it finishes, the following data is created in the Caffe root directory. 1
det_synset_words.txt
imagenet_mean.binaryproto
synset_words.txt
synsets.txt
test.txt
train.txt
val.txt
imagenet.bet.pickle
The training list (train.txt) and validation list (val.txt) files contain the list of files along with each file's label. For the definitions of the labels, see the synset_words.txt file.
Optionally, at the user's choice, you can resize the images in advance.2
The reason this is not done explicitly is that you can benefit from resizing the images in parallel using mapreduce.3
Open the examples/imagenet/create_imagenet.sh file, set the paths of the training folder and validation folder (TRAIN_DATA_ROOT and VAL_DATA_ROOT respectively), and apply RESIZE=true if you did not resize the images in advance.
| Caveats when editing the |
| You must put a path separator ( |
You can easily create the Level DB by running the examples/imagenet/create_imagenet.sh file. Note that when using this script, the locations of the train.txt and val.txt files may differ, so double-check the script.
| Caveats before running |
| Before running the
|
Computing the image mean
The model requires the mean image from each image.4 (Why?; this needs to be looked into.) 5 The mean can be computed as follows.
As a result, the data/ilsvrc12/imagenet_mean.binaryproto file is created.
What is it about?
- Stackoverflow: Is it necessary to apply mean-subtraction on FC7 features of an CNN before feeding into L-SVM?
- Caffe Users group: What exactly is the meanfile do?
The following answer was posted.
It computes the mean pixel by pixel across the whole data-set (look at the compute_image_mean script). So say you had a constant background in the top half of all your images, that would all become zero'd out from mean subtraction.
Mean subtraction (mean-subtraction) needs to be looked into.
Defining the model
First, an explanation of the reference implementation is needed. 6
The network definition follows models/bvlc_reference_caffenet/train_val.prototxt. If you need to modify it, open that file. Looking at the include sections in that file, you will find the following.
-
include { phase: TRAIN } -
include { phase: TEST }
These differ in their input and output layers.
Input layer differences
Data for the training (TRAIN) network is randomly mirrored (Randomly mirrors) from the examples/imagenet/ilsvrc12_train_leveldb file.
Data for the test (TEST) network is obtained from the examples/imagenet/ilsvrc12_val_leveldb file, and random mirroring is not performed (does not perform random mirroring).
Output layer differences
Both networks output a softmax_loss layer.
During training, it computes the loss function and initializes the backpropagation (Initialize the backpropagation).7
The test network has a second output layer that outputs the accuracy on the data (Test set).
In the process of training, the test network will occasionally be instantiated and tested on the test set, producing lines like Test score #0: xxx and Test score #1: xxx. In this case score 0 is the accuracy (which will start around 1/1000 = 0.001 for an untrained network) and score 1 is the loss (which will start around 7 for an untrained network).
Plans
Set up the protocol buffer (Protocol buffer) for the Solver.
- We will run in batches of 256, and run a total of 450,000 iterations (about 90 epochs).
- For every 1,000 iterations, we test the learned net on the validation data.
- We set the initial learning rate to 0.01, and decrease it every 100,000 iterations (about 20 epochs).
- Information will be displayed every 20 iterations.
- The network will be trained with momentum 0.9 and a weight decay of 0.0005.
- For every 10,000 iterations, we will take a snapshot of the current status.
The above is implemented in models/bvlc_reference_caffenet/solver.prototxt.
| The explanation was long |
|
Starting ImageNet training
You can start training with the command below.
To change the number of Forward & Backward Iterations, change the max_iter property in the models/bvlc_reference_caffenet/solver.prototxt file.
Tip
If you want to check the computation time, run the following.
Resume Training?
If a power outage occurs due to war or an earthquake or training is canceled, you can restart it.8 Run it as follows.
$ ./build/tools/caffe train --solver=models/bvlc_reference_caffenet/solver.prototxt --snapshot=models/bvlc_reference_caffenet/caffenet_train_iter_10000.solverstate
Next
Once training is complete, perform classification (Classifying) using the Python interface: Caffe:ImageNetClassifying
Sample
See the following files.
-
./examples/imagenet/create_imagenet.sh -
./examples/imagenet/make_imagenet_mean.sh -
./examples/imagenet/train_imagenet.sh
All in one script
A script that does everything in one go is as follows.
#!/bin/sh
DATE_FORMAT=`date +%Y%m%d_%H-%M-%S`
LOG_FILE=brewing_imagenet-$DATE_FORMAT.log
echo "LOGFILE: $LOG_FILE"
echo "Create ImageNet."
./examples/imagenet/create_imagenet.sh >& ${LOG_FILE}.1
if [[ "$?" != "0" ]]; then; exit 1; fi
echo "Make ImageNet mean."
./examples/imagenet/make_imagenet_mean.sh >& ${LOG_FILE}.2
if [[ "$?" != "0" ]]; then; exit 1; fi
echo "Train CaffeNet."
./examples/imagenet/train_caffenet.sh >& ${LOG_FILE}.3
if [[ "$?" != "0" ]]; then; exit 1; fi
echo "Done."
exit 0
Use files
The list of files needed to follow the tutorial above is as follows. If possible, copy them to a separate directory before proceeding.
-
data/ilsvrc12/directory.- Text files such as
train.txt,val.txt. -
imagenet_mean.binaryproto: a file generated along the way.
- Text files such as
examples/imagenet/ directory.- Script files such as
create_imagenet.sh. -
ilsvrc12_train_lmdb/,ilsvrc12_val_lmdb/directories (DB files storing the image data)
models/bvlc_reference_caffenet/ directory.-
*.solverstateand*.caffemodelfiles. (intermediate saved data and model files) - Text files such as
solver.prototxt,train_val.prototxt.
tools/ directory. (contains the executables) Custom all in one script
A sample file containing the above is as follows. (Compiled on CentOS6.)
-
Caffe_copy.tar.gz(OLD VERSION)
An all-in-one script using run.sh is uploaded below. (Compiled on Ubuntu.)
-
Caffe_train.tar.gz
Troubleshooting
std::length_error
Using make_imagenet_mean.sh produces the following error.
[your@server caffe]$ ./examples/imagenet/make_imagenet_mean.sh
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create
./examples/imagenet/make_imagenet_mean.sh: line 10: 17802 Aborted (core dumped) $TOOLS/compute_image_mean $EXAMPLE/ilsvrc12_train_lmdb $DATA/imagenet_mean.binaryproto
Done.
In this case, the images may not have been read properly when running the previous step, the create_imagenet.sh script.
[your@server caffe]$ ./examples/imagenet/create_imagenet.sh
Creating train lmdb...
I1020 13:51:01.576401 17461 convert_imageset.cpp:82] Shuffling data
I1020 13:51:01.744885 17461 convert_imageset.cpp:85] A total of 2 images.
I1020 13:51:01.745170 17461 db_lmdb.cpp:23] Opened lmdb examples/imagenet/ilsvrc12_train_lmdb
E1020 13:51:01.745205 17461 io.cpp:80] Could not open or find file /home/your/Project/caffe/examples/imagenet/testnofire.jpeg
E1020 13:51:01.745221 17461 io.cpp:80] Could not open or find file /home/your/Project/caffe/examples/imagenet/testfire.jpg
Creating val lmdb...
I1020 13:51:01.811265 17463 convert_imageset.cpp:82] Shuffling data
I1020 13:51:01.954166 17463 convert_imageset.cpp:85] A total of 2 images.
I1020 13:51:01.954432 17463 db_lmdb.cpp:23] Opened lmdb examples/imagenet/ilsvrc12_val_lmdb
E1020 13:51:01.954473 17463 io.cpp:80] Could not open or find file /home/your/Project/caffe/examples/imagenet/valfire.jpg
E1020 13:51:01.954481 17463 io.cpp:80] Could not open or find file /home/your/Project/caffe/examples/imagenet/valnofire.jpeg
Done.
As shown, re-check the output of the create_imagenet.sh script.
The case above is one where the file paths were wrong.
See also
Favorite site
- Caffe: Brewing ImageNet
- How to Setup Caffe to Use Other Datasets by Modifying imagenet Example
- Blog.naver.com/sssmate1 Caffe_Tutorial
Guide
- Any simple example? #550
- CS231n Caffe Tutorial 9
- Train and Test LeNet on your own dataset
- Caffe tutorial 10
- Stackoverflow: How to train a caffe model?
- Caffe: Decaf, but Better (explanation of Caffe Layers)
References
-
Hidden files starting with
.are excluded from the list. ↩ -
The
convertexecutable comes with ImageMagick. ↩ -
Original text: The model requires us to subtract the image mean from each image ↩
-
Implemented in
tools/compute_image_mean.cpp. ↩ -
See Advances in Neural Information Processing Systems 25 (NIPS 2012) ↩
-
The loss computed during calculation is simply reported (Simply reported). ↩
-
Snapshots are created at intervals during training. ↩
-
CS231n_Caffe_Tutorial.pdf ↩
-
Caffe_tutorial.pdf ↩