Train your own picture data using Caffe's CIFAR10 network model

Source: Internet
Author: User

Since I am involved in a license plate recognition system project, I plan to use the Deep Learning Library Caffe to identify the license plate characters. Starting with Caffe, I'm going to use each of the network models in the example first, and of course the violent use is not going to have a good result--| | | , so here is just a sample of the network model using the steps, the accuracy of the final test for the moment no matter!

First, the picture database source

The image I used was a character image that was split in the character segmentation module of the project, grayscale and normalized to 32*64, and a sample sample of the character picture is as follows:

Create your own Data folder

Set up your own Data folder mine under the./caffe/data/directory, and create the train folder and the test folder under the Mine folder (because only to familiarize yourself with the CIFAR10 network model, to reduce the length of the training, only A, B is selected, C Three character samples for training and testing). The train folder is used to store training samples, and the test folder is used for testing samples.

Then, put the training sample picture you have processed in the./caffe/data/mine/train/folder below, the test sample is placed under the./caffe/data/mine/test/folder.

Train:

Test:

Writing Train.txt and Test.txt text

(1)--train.txt : The path name for the training picture (relative to the./caffe/data/mine/train/) and the category label, one picture at a line, as shown below:

(2)--test.txt : The path name of the test sample (relative to the./caffe/data/mine/test/) and the category label, one row represents a picture, as shown in:

Second, convert the picture data to LEVELDB format data

Remove the caffe.cpp from the project in the original Caffe project, add the Convert_imageset.cpp in the Tools folder to the project, build the. exe under./caffe/bin/after compiling, and rename it to Convert_ Imageset.exe.

Then, in the root directory (mine is F:\caffe\), write a batch bat file named Converttrain2ldb.bat, which is designed to convert the data format in the training set to the LEVELDB format:

After running, the Mtrainldb folder is generated under data/mine/, and the contents of the folder are as follows:

When the size of your generated. ldb file is 0KB or very small, it should be that your bat file is wrong.

In the same vein, convert the test set to a LEVELDB file:

Calculate the mean value of the image

Remove the caffe.cpp from the project in the original Caffe project, add the Compute_image_mean.cpp in the Tools folder to the project, build the. exe under./caffe/bin/after compiling, and rename it to Compute_image_ Mean.cpp.exe.

Write a batch bat file in the root directory named Computemean.bat, which is designed to calculate the mean value of the image and generate the mean file:

After running, generate the mean file under data/mine/. As shown below:

IV. Create a network model, write a configuration file, write a training script, validate a test set create a network model

Under./data/mine to create a folder Train-val, under the./examples/cifar10/folder Cifar10_quick_train_ Copy the Test.prototxt network model configuration file to the folder below as follows:

And make the following changes:

Name"Cifar10_quick"Layer {name:"Cifar"Type:"Data"Top:"Data"Top:"label"include {Phase:train} transform_param {//"1th Place to modify" below is the path to the mean file, changing to the path of your own mean fileMean_file:"Data/mine/train_mean.binaryproto"} data_param {//"where the 2nd block changes" is changed to the directory where the training sample was generated [Note: Training sample database]Source"Data/mine/mtrainldb"batch_size: -    //"3rd place to modify" because we don't have a lot of training samples, we can read 50 images at a time.Backend:leveldb}} Layer {name:"Cifar"Type:"Data"Top:"Data"Top:"label"include {phase:test} transform_param {//"4th place to modify" below is the path to the mean file, changing to the path of your own mean fileMean_file:"Data/mine/train_mean.binaryproto"} data_param {//"where the 5th block changes" is changed to the same directory as the database where the test sample was generated [Note: Test sample database]Source"Data/mine/mtestldb"batch_size: -//"6th place to modify" since we have only 150 test samples, we can read 50 images at a time.Backend:leveldb}} Layer {name:"CONV1"Type:"convolution"Bottom:"Data"Top:"CONV1"param {lr_mult:1} param {lr_mult:2the middle of the ellipsis ... layer {name:"IP2"Type:"innerproduct"Bottom:"ip1"Top:"IP2"param {lr_mult:1} param {lr_mult:2} inner_product_param {num_output:3         //"7th place to modify" We are now 3 classification problem, so the second full connection layer is changed to 3Weight_filler {type:"Gaussian"std:0.1} bias_filler {type:"constant"}}}layer {name:"accuracy"Type:"accuracy"Bottom:"IP2"Bottom:"label"Top:"accuracy"include {phase:test}}layer {name:"Loss"Type:"Softmaxwithloss"Bottom:"IP2"Bottom:"label"Top:"Loss"}

Writing a hyper-parameter configuration file

Similarly, copy the Cifar10_quick_solver.prototxt Hyper-parameter profile to./data/mine/train-val, making the following modifications:

//"1" to the directory of your own network model configuration fileNet"Data/mine/train-val/cifar10_quick_train_test.prototxt"//"2" prediction stage iteration number, set to 3, because Batch_size is set to 50, so you can overwrite 150 test set pictureTest_iter:3//"3" 50 times per iteration, one testTest_interval: -//the "4" weight learning rate, in fact, is in the reverse propagation phase, the weight of each adjustment amount of the degreeBASE_LR:0.001Momentum:0.9Weight_decay:0.004//"5" throughout the process, we use a fixed learning rate and can also try to use variable learning ratesLr_policy:"fixed"//"6" because you want to observe the changes in each training, set the iteration to display the contents onceDisplay1//"7" sets the maximum number of iterations to 4000Max_iter:4000//"8" outputs one result per iteration 1000 timesSnapshot +//"9" output formatSnapshot_format:hdf5//prefix for "10" output fileSnapshot_prefix:"Data/mine/cifar10_quick"//"11" is CPU-bound.Solver_mode:cpu

Writing Training Scripts

In the root directory, create a new batch bat file named Trainmine_usecifar10.bat, as shown in the following:

After running, call the Cifar10_quick_solver.prototxt in the Train-val folder to begin training, as shown in the training process:

...

After the training is completed, a training model in the HDF5 format is generated in the specified directory:

Validating test Sets

Write the batch bat file under the root directory named Testmine_usecifar10.bat:

After you double-click Run, start testing the data in the test set, and the output is as follows:

Can see, some test batch accuracy rate is 1, some batch accuracy is 0,accuracy part in addition to these two numbers, no other numbers, so I reckoned will have a strange, but still do not know where the problem, and later to understand!

Above.

Train your own picture data using Caffe's CIFAR10 network model

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.