Caffe Study (a): CIFAR-10

Source: Internet
Author: User
Tags root directory

The previous time has been in the TensorFlow, now the internship company project needs to compare TensorFlow and Caffe in the image classification which better, so small I can now only put tensorflow aside, engage a caffe.

There are a lot of such resources on the net, but we write all the same, run up there are many did not write understand, in order to use later, at the same time convenient for beginners like me to learn, I will run the Caffe on the CIFAR-10 example of the process as far as possible to write a detailed point.

Let's briefly introduce CIFAR-10.

working with databases: CIFAR-10

60000 32x32 color Images 10 classes, 50000 training, 10000 tests

specific operation implementation:

(1) Get the database

#下面假定caffe的根目录是 caffe_root, download the dataset in the Terminal Input command:
CD  $CAFFE _root
./data/cifar10/get_cifar10.sh  # The script will download the binary Cifar and unzip it, and a lot of batch files will appear in the/data/cifar10
./examples/cifar10/create_cifar10.sh #运行后将会在examples中出现数据集. /cifar10_xxx_lmdb and data set image mean./mean.binaryprot


Above is a more general data download process, mainly divided into two steps: Download and conversion. However, for a novice, this hint at all do not know how to do, small series I do not understand, and then through their own constant attempts, finally fix. So a lot of things on the Internet or not reliable, or to rely on their own constantly trying. Here are the specific steps:


1) Run the get_cifar10.sh file.

Here, the file is located at Downloads/caffe-master/data/cifar10

Use the CD command to navigate to the location where the file is located:

Such as:

sgg@ubuntu:~$ CD Downloads
sgg@ubuntu:~/downloads$ cd caffe-master  #定位到根目录

Then execute:./get_cifar10.sh command, download data
sgg@ubuntu:~/downloads/caffe-master$./data/cifar10/get_cifar10.sh
#该脚本会下载二进制的cifar, and unzip, a lot of batch files will appear in the/data/cifar10

2) Run the create_cifar10.sh file.

Here, the file is located at Downloads/caffe-master/examples/cifar10

Because the create_cifar10.sh file runs only in the root directory, if you first locate the location to the folder where Create_cifar10 is located, running Create_cifar10 will report an error.

report The following error:

./create_cifar10.sh:13:./create_cifar10.sh:./build/examples/cifar10/convert_cifar_data.bin:not found


So execute the create_cifar10.sh file with the following statement. After running, the dataset will appear in examples./cifar10_xxx_lmdb and data set image mean./mean.binaryproto


sgg@ubuntu:~$ CD Downloads
sgg@ubuntu:~/downloads$ cd caffe-master
sgg@ubuntu:~/downloads/caffe-master$. examples/cifar10/create_cifar10.sh


Create_cifar10.sh's job is to convert the picture library into leveldb format and calculate the mean binary file

After running, the database file will appear in examples./CIFAR10-LEVELDB and database image mean binary files./mean.binaryproto


(2) Start training # $CAFFE _root for CAFFE root
cd $CAFFE _root
./examples/cifar10/train_quick.sh



Here's how:

sgg@ubuntu:~$ CD Downloads
sgg@ubuntu:~/downloads$ cd caffe-master
sgg@ubuntu:~/downloads/caffe-master$. examples/cifar10/train_quick.sh#./train_quick.sh  Command, training data



When the training is complete, we will get:

Cifar10_quick_iter_4000.caffemodel.h5

Cifar10_quick_iter_4000.solverstate.h5

At this point, we trained to get the model for the following classification


Issues that occur during the run:

After the training statement is executed, a prompt question appears:


sgg@ubuntu:~/downloads/caffe-master$./examples/cifar10/train_quick.sh
I0802 17:42:26.469802  5520 caffe.cpp:217] Using GPUs 0
F0802 17:42:26.470361  5520 common.cpp:66] cannot use GPU in cpu-only caffe:check mode .



This problem can still occur when the configuration Makefile.config has changed to Cpu-only, because its resolution mode is not changed to CPU in Cifar10_quick_solver.prototxt. The others find the corresponding solver.prototxt to make the changes.


(3) using a trained model to classify new data

Let's start with someone else's model classification: (Default imagenet model)

Python python/classify.py examples/images/cat.jpg foo


Let's specify our own model to classify:

Python python/classify.py--model_def examples/cifar10/cifar10_quick.prototxt--pretrained_model examples/cifar10/ Cifar10_quick_iter_4000.caffemodel.h5--center_only  examples/images/cat.jpg foo

The above phrase means using the Cifar10_quick.prototxt Network + CIFAR10_QUICK_ITER_4000.CAFFEMODEL.H5 model to classify the examples/images/cat.jpg images.

The default classify script does not directly output the results, but will input the results into the Foo file, not too intuitive, here I found a modified version of the Web, add some parameters, you can output the highest probability classification.

Replace python/classify.py, download address: http://download.csdn.net/detail/caisenchuan/9513196

This script adds two parameters, you can specify Labels_file, and then you can output the classification results directly:

Python python/classify.py--print_results--model_def examples/cifar10/cifar10_quick.prototxt--pretrained_model Examples/cifar10/cifar10_quick_iter_4000.caffemodel.h5--labels_file data/cifar10/cifar10_words.txt  --center _only  examples/images/cat.jpg foo

Output Result:

Loading file:examples/images/cat.jpg
classifying 1 inputs.
Predict 3 inputs.
Done in 0.02 S.
Predictions: [[0.03903743  0.00722749  0.04582177  0.44352672  0.01203315  0.11832549
   0.02335102  0.25013766  0.03541689  0.02512246]]
python/classify.py:176:futurewarning:sort ( Columns= ....) is deprecated, use sort_values (by= ...)
  Labels = labels_df.sort (' synset_id ') [' name '].values
[(' Cat ', ' 0.44353 '), (' Horse ', ' 0.25014 '), (' Dog ', ' 0.11833 ') ), (' Bird ', ' 0.04582 '), (' Airplane ', ' 0.03904 ')]
indicate the order and confidence of each classification
Saving results into Foo

Tips

Finally, summarize the relevant files used to train a network:

Cifar10_quick_solver.prototxt: Schema configuration, used to configure the number of iterations and other information, training directly call Caffe train specify this file, will start training

Cifar10_quick_train_test.prototxt: Training network configuration, used to set up the training network, the name of this file will be specified in the Solver.prototxt

CIFAR10_QUICK_ITER_4000.CAFFEMODEL.H5: A trained model, followed by the model to classify

CIFAR10_QUICK_ITER_4000.SOLVERSTATE.H5: It is also training, should be used to interrupt after the training of the file

Cifar10_quick.prototxt: Network for classification





Model Introduction

The models are described in Examples/cifar10/cifar10_quick_solver.prototxt and examples/cifar10/cifar10_quick_train_test.prototxt.

Model training is the execution of train_quick.sh




















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.