Caffe installation, compilation (including Cuda and CUDNN installation), and training to test your own data (Caffe using tutorials)

Source: Internet
Author: User
Tags constant shuffle

Caffe is a very clear and efficient deep learning framework, now has a lot of users, but also gradually formed their own community, the community can discuss related issues.

I began to look at the relevant content of deep learning to be able to use Caffe training to test their own data, see a lot of sites, tutorials and blogs, also took a lot of detours, the whole process to comb and summarize, in order to expect can be easily through this article can be easy to use Caffe training their data, Experience the fun of deep learning. (This article contains a lot of links, links have I read the detailed tutorials and explanations to facilitate the understanding of the people)



1. Installing and configuring the Caffe (including Cuda installation)

Caffe installation and compilation, the official website has been written very detailed. Official website Installation Link: Link as long as the official website's instructions step by step, generally will not have any problem. Also, the official website gives a link to the installation and configuration of Ubuntu,mac, and Windows environments. Conditions allow, or try to install and configure it under Ubuntu, for those unfamiliar with Ubuntu may be more troublesome, but I feel that in Linux installation, compilation, including the use of the back is more convenient.

One thing that needs to be explained in advance here is that in the official installation documentation, the following options are available for installation


OpenCV >= 2.4 including 3.0 IO Libraries:lmdb, LEVELDB (note:leveldb requires snappy) CuDNN for GPU Acceleration (v5)

The CUDNN is a CUDA-specific accelerator kit that accelerates the process of running deep learning on the GPU, so this is an installation, which saves time for running programs later.



2. Installation of CUDNN

Here is a separate emphasis on the installation of CUDNN (many of the online tutorials are problematic, resulting in subsequent compilation failures), CUDNN installation needs to be done after CUDA installation.

Cudnn need to download from Nvidia's official website, and need to register first, when you register will let you fill in some questionnaires, after registration is completed, select the correct version to download. I download here: Download CuDNN V5 (May), for CUDA 8.0 CuDNN v5 the Library for Linux version, then unzip, and put in the appropriate folder, and finally modify the permissions


<span style= "Background-color:rgb (240, 240, 240); >tar Xvzf cudnn-8.0-linux-x64-v4.tgz #这里要注意你下载的版本, need to unzip the corresponding version of the file you downloaded </span> unzip the folder name is Cuda
sudo cp cuda/ Include/cudnn.h/usr/local/cuda/include
sudo cp cuda/lib64/libcudnn*/usr/local/cuda/lib64
sudo chmod a+r/ usr/local/cuda/include/cudnn.h/usr/local/cuda/lib64/libcudnn*
Many of the online tutorials are generally given above these will not have, I installed, just carry out the above steps, in make test will error. The error message is roughly as follows:

Cxx/ld-o. Build_release/tools/upgrade_net_proto_text.bin. build_release/lib/libcaffe.so:undefined reference to Caffe::curandgeterrorstring (Curandstatus)
. build_release/lib/libcaffe.so:undefined reference to Caffe::baseconvolutionlayer::weight_gpu_gemm (double const*, Double const*, double*)
. build_release/lib/libcaffe.so:undefined reference to Caffe::baseconvolutionlayer::forward_gpu_bias (double*, Double const*)

Find a lot of information, finally found a solution on the stackoverflaw, I put the link here: Link

In fact, the method is, in the/usr/local/cuda/lib64 directory, do the following (the big God on the web said that the copy process destroys the soft connection, so you have to manually establish a soft connection)

sudo rm libcudnn.so.5 libcudnn.so
sudo ln-sf libcudnn.so.5 libcudnn.so
sudo ln-sf libcudnn.so.5.1.3 libcudnn.so .5
sudo ldconfig/usr/local/cuda/lib64

At this point, re-compile, run make all and do test, and do runtest.



3. Lenet Network with Caffe training on Mnist

Mnist is a data set of handwritten numbers, LeNet is a relatively simple deep learning network model.

This process, which is officially given to the tutorial, is attached to the link: link

But at that time I follow the step by step implementation, and finally can fully run the success, but the program is how to run a successful, how to run through the principle is what, I still do not understand.

In order to understand the specific execution of the procedure, I tried to use some of my own data for training and testing to deepen the understanding of caffe and deep learning.



4. Train and test your own data

Thank you for the great help this blog has given me: Click to open the link. Although the blog is very good, but there are some of the details of the place. I have done this blog based on the completion and supplement, as far as possible the whole process described in more detail.


1) Prepare the data

To train their own network, first of all to have picture data, early in the learning phase, in order to save time, first from the small data set to start training and learning more appropriate, blog blogger to a small data set of links, a total of 500 pictures, divided into buses, dinosaurs, elephants, flowers and horses five categories, each category 100, The download link for the data set is: Http://pan.baidu.com/s/1nuqlTnN

Numbering for the beginning of the 3,4,5,6,7, respectively, a class, each class of 100 pictures, with 80 as training, 20 as a test, the picture is placed in the Caffe directory under the data directory, that is, the training picture directory for Data/re/train, the test picture of the directory for the Data/re/ Test


2) Convert picture to Lmdb format

Caffe, you can not directly take jpg,png and other picture files for training, you need to convert the image format to Lmdb format to use. And Caffe provides the tools you need to convert the format, and we just have to write a few lines of code to get it done.

Create a new Myffle folder under the examples folder, which is used to store configuration files, script files, and Lmdb files, and then write a script file that Create_ Filelist.sh is used to generate Train.txt and test.txt two text files, which contain a list of pictures in a text file. Then edit this script file as follows:

#!/usr/bin/env sh
data=data/re/
my=examples/myfile

echo "Create train.txt ..."
rm-rf $MY/train.txt For
I in 3 4 5 6 7 
do
find $DATA/train-name $i *.jpg | cut-d '/'-f4-5 | sed "s/$/$i/" >> $MY/train.txt
done
echo "Create test.txt ..."
rm-rf $MY/test.txt for
i in 3 4 5 6 7 do
find $DATA/test-name $i *.jpg | Cut-d '/'-f4-5 | Sed "s/$/$i/" >> $MY/test.txt
do
echo "all done"

Then run the script file
sudo sh examples/myfile/create_filelist.sh
Then you can get train.txt and test.txt files, open to see basically know what this file is to do. (Just make a category tag for each picture)

(In fact, the above steps of the script file if you do not understand or feel inconvenient, write a file in C to generate Train.txt and test.txt can also.) I'm not familiar with script files myself, so I sometimes write them in C. )

Then write a script file that invokes the Convert_imageset command to convert the data format. (that is, the image format of the file converted to Lmdb format)

sudo vi examples/myfile/create_lmdb.sh

in which to insert

#!/usr/bin/env sh
my=examples/myfile

echo "Create train Lmdb ..."
RM-RF $MY/img_train_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_height=256 \
--resize _width=256 \
/home/xxx/caffe/data/re/\
$MY/train.txt \
$MY/img_train_lmdb

echo "Create test Lmdb ..."
rm-rf $MY/img_test_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_width=256 \
--resize_height=256 \
/home/xxx/caffe/data/re/\
$MY/test.txt \
$MY/img_test_lmdb

echo "all Done: "

The size of the picture is inconsistent, so the uniform conversion to the size of the 256x256, after the successful run, will be generated under the Examples/myfile two files, Img_train_lmdb and Img_val_lmdb, this two files is the image dataset converted files.


3) Calculate the mean and save

The picture minus the mean value retraining, can improve the training speed and precision. Therefore, this is generally the operation. (such as a very famous paper describing Alexnet, this step is also pre-operation)

Similarly, the operation Caffe minus the mean is also written, the direct call on the line, that is, the Compute_image_mean.cpp file

sudo build/tools/compute_image_mean examples/myfile/img_train_lmdb Examples/myfile/mean.binaryproto

Compute_image_mean with two parameters, the first parameter is the LMDB training data location, the second parameter sets the mean file name and save the path.
After successful operation, a mean.binaryproto mean file is generated under examples/myfile/.


4) Create a neural network model and write a configuration file

Here, using Caffe's own caffenet model for training, the model and the corresponding configuration files are copied, the operation is as follows:

sudo cp models/bvlc_reference_caffenet/solver.prototxt examples/myfile/
sudo cp models/bvlc_reference_caffenet/ Train_val.prototxt examples/myfile/

Solver.prototxt is a configuration file for the entire network, which is modified as follows:

NET: "Examples/myfile/train_val.prototxt"
test_iter:2
test_interval:50
base_lr:0.001
lr_policy: "Step"
gamma:0.1
stepsize:100
display:20
max_iter:500
momentum:0.9
weight_decay:0.005
# Snapshot Intermediate results
snapshot:500
snapshot_prefix: "Examples/myfile/results/my"
Solver_mode:gpu
About the configuration file parameters do not understand the place, you can see links: links

Then, modify the Train_val.protxt to change to the following form

Name: "Caffenet" Layer {name: "Data" type: "Data" Top: "Data" Top: "label" include {Phase:train} tra Nsform_param {mirror:true crop_size:227 mean_file: "Examples/myfile/mean.binaryproto"} # mean Pixel/ch     Annel-wise mean instead of mean image # Transform_param {# crop_size:227 # mean_value:104 # mean_value:117 #
    mean_value:123 # mirror:true #} data_param {source: "Examples/myfile/img_train_lmdb" batch_size:50 
  Backend:lmdb}} layer {name: "Data" type: "Data" Top: "Data" Top: "label" include {phase:test} Transform_param {mirror:false crop_size:227 mean_file: "Examples/myfile/mean.binaryproto"} # mean Pix El/channel-wise mean instead of mean image # Transform_param {# crop_size:227 # mean_value:104 # Mean_value : 117 # mean_value:123 # mirror:false #} data_param {source: "Examples/myfile/img_test_lmdb" Batch_si Ze:50 Backend:lmdb
  }} layer {name: "CONV1" type: "Convolution" bottom: "Data" Top: "Conv1" param {lr_mult:1 decay_mu
    Lt:1} param {lr_mult:2 decay_mult:0} convolution_param {num_output:96 kernel_size:11
      Stride:4 Weight_filler {type: "Gaussian" std:0.01} bias_filler {type: "constant" VALUE:0}}} layer {name: "RELU1" type: "ReLU" bottom: "conv1" Top: "CONV1"} layer {name: "Pool1" t ype: "Pooling" bottom: "conv1" Top: "Pool1" Pooling_param {Pool:max kernel_size:3 stride:2}} Lay
    ER {name: "Norm1" type: "LRN" bottom: "pool1" Top: "Norm1" Lrn_param {local_size:5 alpha:0.0001
    beta:0.75}} layer {name: "Conv2" type: "Convolution" bottom: "Norm1" Top: "Conv2" param {lr_mult:1
    Decay_mult:1} param {lr_mult:2 decay_mult:0} convolution_param {num_output:256 pad:2 Kernel_size:5 GROup:2 Weight_filler {type: "Gaussian" std:0.01} bias_filler {type: "Constant" Val UE:1}}} layer {name: "RELU2" type: "ReLU" bottom: "conv2" Top: "conv2"} layer {name: "Pool2" type : "Pooling" bottom: "conv2" Top: "Pool2" Pooling_param {Pool:max kernel_size:3 stride:2}} Layer {Name: "Norm2" type: "LRN" bottom: "pool2" Top: "Norm2" Lrn_param {local_size:5 alpha:0.0001 bet
    a:0.75}} layer {name: "Conv3" type: "Convolution" bottom: "Norm2" Top: "Conv3" param {lr_mult:1
    Decay_mult:1} param {lr_mult:2 decay_mult:0} convolution_param {num_output:384 pad:1
      Kernel_size:3 Weight_filler {type: "Gaussian" std:0.01} bias_filler {type: "constant" VALUE:0}}} layer {name: "RELU3" type: "ReLU" bottom: "conv3" Top: "conv3"} layer {name: "Conv4
"Type:" Convolution "  Bottom: "Conv3" Top: "conv4" param {lr_mult:1 decay_mult:1} param {lr_mult:2 decay_mult:0 } convolution_param {num_output:384 pad:1 kernel_size:3 group:2 weight_filler {type: "Gaussian" std:0.01} bias_filler {type: "Constant" value:1}}} layer {name: "Relu 4 "Type:" ReLU "bottom:" conv4 "Top:" conv4 "} layer {name:" conv5 "type:" Convolution "bottom:" conv4 "top : "Conv5" param {lr_mult:1 decay_mult:1} param {lr_mult:2 decay_mult:0} Convolution_para m {num_output:256 pad:1 kernel_size:3 group:2 weight_filler {type: ' Gaussian ' std:0 . Bias_filler} {type: "Constant" value:1}}} layer {name: "Relu5" type: "ReLU" bot 
    Tom: "Conv5" Top: "conv5"} layer {name: "POOL5" type: "Pooling" bottom: "conv5" Top: "Pool5" Pooling_param { Pool:max KerneL_size:3 Stride:2}} layer {name: "Fc6" type: "Innerproduct" bottom: "pool5" Top: "Fc6" param {LR 
    _mult:1 decay_mult:1} param {lr_mult:2 decay_mult:0} inner_product_param {num_output:4096 
    Weight_filler {type: ' Gaussian ' std:0.005} bias_filler {type: ' constant ' value:1 }}}} layer {name: "Relu6" type: "ReLU" bottom: "fc6" Top: "fc6"} layer {name: "DROP6" type: "Dropou T "bottom:" fc6 "Top:" Fc6 "Dropout_param {dropout_ratio:0.5}} layer {name:" FC7 "type:" Innerproduct
  "Bottom:" fc6 "Top:" Fc7 "param {lr_mult:1 decay_mult:1} param {lr_mult:2 decay_mult:0 } inner_product_param {num_output:4096 weight_filler {type: ' Gaussian ' std:0.005} bias _filler {type: ' Constant ' value:1}}} layer {name: ' relu7 ' type: ' ReLU ' bottom: ' fc7 ' top: "FC7"} layer {name:"DROP7" type: "Dropout" bottom: "fc7" Top: "Fc7" Dropout_param {dropout_ratio:0.5}} layer {name: "Fc8 "Type:" Innerproduct "bottom:" fc7 "Top:" Fc8 "param {lr_mult:1 decay_mult:1} param {Lr_mult : 2 decay_mult:0} inner_product_param {num_output:1000 Weight_filler {type: "Gaussian" St d:0.01} bias_filler {type: ' Constant ' value:0}}} ' layer {name: ' accuracy ' type: ' ACCU  Racy "bottom:" Fc8 "bottom:" Label "Top:" accuracy "include {phase:test}} layer {name:" Loss "type: "Softmaxwithloss" bottom: "Fc8" bottom: "Label" Top: "Loss"}

This file is actually defined the entire network model, each layer is a layer, you define in each level is the convolution layer or pool layer, the number of neurons and so on, the meaning of the specific parameters can be seen link: Click to open the link


5) Training and testing

Just enter the following command directly to complete it. (It is important to note that the entire process of training and testing is performed together, as the above Train_ Val.prototxt file train and test parameters are set up, so the program after training will be directly into the test data stage, in fact, can also be written as two separate file execution. In the Solver.prototxt, there is an act

Snapshot_prefix: "Examples/myfile/results/my"

This means that the weights you have trained are stored under the results folder, and if you want to use a trained model, you can simply call the file under Rusult to test it. )

The result of the final output,

Test net output #0: accuracy:0.95

In this line, it means that the correct rate of classification is 95% on the test data, using the network of your own training.





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.