Caffe Deep Learning Framework Tutorial

Source: Internet
Author: User

This article source: http://suanfazu.com/t/caffe/281

The main purpose of this article is to save a link and suggest reading the original.

Caffe (convolutional Architecture for Fast Feature embedding) is a clear and efficient deep learning framework whose author is a PhD graduate from UC Berkeley and currently works for Google.

Caffe is a pure C++/cuda architecture that supports command line, Python, and MATLAB interfaces, and can be seamlessly switched directly between the CPU and GPU:

Caffe::set_mode (CAFFE::GPU);
Advantages of Caffe
    1. Get started: Models and corresponding optimizations are given in textual form rather than code.
      Caffe gives the model definition, optimization settings and pre-training weights for immediate access.
    2. Fast: Ability to run the best models and massive amounts of data.
      Using Caffe with CUDNN, test the alexnet model, and process each picture on K40 with only 1.17ms.
    3. Modularity: Easy to extend to new tasks and settings.
      You can use the various layer types provided by Caffe to define your own model.
    4. Openness: Public code and reference models are used for renditions.
    5. Community good: can participate in development and discussion through BSD-2.
Network definitions for Caffe

The network in Caffe is a collection of directed, non-circular graphs that can be defined directly:

Name: "Dummy-net" layers {<span><span>name: <span> "Data" ...</span></span></span> }layers {<span><span>name: <span> "conv" ... </span></span></span>}layers {< Span><span>name: <span> "Pool" ... </span></span></span>}layers {<span>< Span>name: <span> "loss" ... </span></span></span>}
The data and its derivatives flow between layers in the form of blobs. The definitions of each layer of Caffe are composed of 2 parts: Layer properties and layer parameters, such as
Name: "Conv1" Type:convolutionbottom: "Data" Top: "Conv1" convolution_param{    num_output:<span>20    Kernel_size:5    stride:1    weight_filler{        type: "<span style=" color: #c0504d; " >xavier</span> "    }}
The first 4 lines of this configuration file are layer properties, which define the layer name, layer type, and layer join structure (input blob and output blob), and the second half is the various layer parameters.

Blob

BLOBs are 4-dimensional arrays used to store data, such as

    • For data: Number*channel*height*width
    • For convolutional weights: output*input*height*width
    • For convolution offset: output*1*1*1
Training Network

The definition of network parameters is also very convenient, you can set the corresponding parameters.

Even calling the GPU operation requires only one sentence:

Solver_mode:gpu



Installation and configuration of Caffe

Caffe need to pre-install some dependencies, first of all cuda drivers. Both CentOS and Ubuntu have an open source Nouveau graphics driver (SuSE does not have this problem) and the Cuda driver does not install properly if not disabled. Take Ubuntu, for example, to introduce the treatment here, and of course there are other ways to deal with it.

Generate mnist-train-leveldb/and mnist-test-leveldb/to convert data into LEVELDB format:

Training Network:

# sudo vi/etc/modprobe.d/blacklist.conf# add one line: Blacklist nouveausudoapt-get--purge Remove xserver-xorg-video-nouveau< c1/> #把官方驱动彻底卸载: Sudoapt-get--purge Remove nvidia-*    #清除之前安装的任何NVIDIA驱动sudo service lightdm stop    #进命令行, Close Xserversudo Kill all Xorg

After the installation of Cuda, followed by Caffe official website Installation Guide Installation Blas, OpenCV, boost can be.

Caffe run and run Mnist try

Under the Caffe installation directory, get the mnist DataSet first:

CD Data/mnistsh get_mnist.sh

Generate mnist-train-leveldb/and mnist-test-leveldb/to convert data into LEVELDB format:

CD examples/lenet SH create_mnist.sh

Training Network:

SH train_lenet.sh



Let caffe generate data set to run directly on Theano

Regardless of the framework used for CNNs training, there are 3 data sets:

    • Training Set: for training networks
    • Validation Set: Test network accuracy for training
    • Test Set: For testing the final correct rate after network training is completed
The data generated by Caffe is divided into 2 formats: Lmdb and LEVELDB
    • They are key/value pairs (key/value pair) embedded database management System programming library.
    • While Lmdb memory consumption is 1.1 times times leveldb, Lmdb is 10% to 15% faster than LEVELDB, and more importantly LMDB allows multiple training models to read the same set of data simultaneously.
    • As a result, Lmdb replaces leveldb as the default dataset generation format for Caffe.
Installation of Google Protocol buffer

Protocol buffer is an XML-like automatic mechanism for serializing data.
First download the latest version in Protocol buffers:
Https://developers.google.com/protocol-buffers/docs/downloads
After decompression run:

./configure$ make$ make check$ make Installpip installprotobuf
Add a dynamic link library
Export Ld_library_path=/usr/local/lib: $LD _library_path
Installation of Lmdb
Pip Install Lmdb

To parse (parse) a protobuf type of data, first tell the computer what format is inside of the PROTOBUF data (what are the items, what data types are used to determine how many bytes are occupied, whether the items can be duplicated, repeated several times), Install PROTOBUF This module can use the PROTOBUF syntax to define these formats (this is the. proto file), and then use the PROTOC to compile the. proto file to generate the target file you need.
To define your own. proto file, read:
Https://developers.google.com/protocol-buffers/docs/proto?hl=zh-cn

Compiling the. Proto file
Protoc--proto_path=import_path--cpp_out=dst_dir--java_out=dst_dir--python_out=dst_dir Path/to/file.proto
--proto_path can also be abbreviated to-I is the. Proto path output path:--cpp_out to generate C + + available header files, respectively, ***.pb.h (including the Declaration Class) ***.pb.cc (contains the executable class), when used as long as the include " . pb.h "--java_out generate Java Available header file--python_out generate Python available header file, **_pb2.py, when used Import**_ pb2.py the last parameter is your. proto file full path.
Caffe (CNN, deep learning) Introduction

Caffe-----------convolution Architecture for Feature embedding (Extraction)

    1. What is Caffe?
      • CNN (Deep Learning) toolbox
      • C + + language architecture
      • Seamless CPU and GPU switching
      • Packaging for Python and MATLAB
      • However, decaf is only the CPU version.
    2. Why do you use Caffe?

      • The operation speed is fast. Some libraries used by the simple and friendly architecture:
      • Google Logging Library (glog): A C + + language application-level logging framework that provides C + +-style streaming operations and various helper macros.
      • LEBELDB (Data storage): A very efficient KV database implemented by Google, single process operation.
      • CBLAS Library (CPU version of the matrix operation)
      • Cublas Library (GPU version of the matrix operation)
    3. Caffe Architecture


    1. LEVELDB Construction of preprocessing image
      Input: Batch of images and labels (2 and 3)
      Output: Leveldb (4)
      The following information is included in the instruction:
      • Conver_imageset (to build leveldb running program)
      • train/(jpg or other format image in this directory)
      • Label.txt (image file name and its label information)
      • The name of the output Leveldb folder
      • CPU/GPU (Specifies whether to run code on the CPU or on the GPU)
    2. CNN Network configuration file

      • Imagenet_solver.prototxt (file with configuration for global parameters)
      • Imagenet.prototxt (Files that contain the configuration of the Training network)
      • Imagenet_val.prototxt (contains configuration file for test network)
Alexnet interpretation of Image classification model Caffe Deep learning

On the Imagenet Image Classification Challenge, Alex proposed the Alexnet network structure model won the 2012-term championship. In order to study the application of the CNN type DL network model in image classification, we can not escape the research alexnet, which is the classic model of CNN in image classification (after the DL fires up).

In the DL open source Implementation Caffe Model sample, it also gives the alexnet of the recurrence, the specific network configuration file is as follows Train_val.prototxt

Next this article will step by step to the network configuration structure of the various layers of the detailed interpretation (training phase):

The operation of various layers can be referred to Caffe layer catalogue for more explanation.

From the process of calculating the data flow of the model, the model parameters are probably 5kw+.

    1. CONV1 phase DFD (Data flow diagram):

    2. Conv2 phase DFD (Data flow diagram):

    3. Conv3 phase DFD (Data flow diagram):

    4. CONV4 phase DFD (Data flow diagram):

    5. CONV5 phase DFD (Data flow diagram):

    6. Fc6 phase DFD (Data flow diagram):

    7. FC7 phase DFD (Data flow diagram):

    8. Fc8 phase DFD (Data flow diagram):

0721 10:38:15.326920 4692 net.cpp:125] Top shape:256 3 227 227 (39574272) I0721 10:38:15.326971 4692 net.cpp:125] Top sh ape:256 1 1 1 (I0721) 10:38:15.326982 4692 net.cpp:156] data does not need backward computation. I0721 10:38:15.327003 4692 net.cpp:74] Creating Layer conv1i0721 10:38:15.327011 4692 net.cpp:84] conv1 <-dataI0721 10:38:15.327033 4692 net.cpp:110] conv1, conv1i0721 10:38:16.721956 4692 net.cpp:125] Top shape:256 96 55 55 (7434 2400) I0721 10:38:16.722030 4692 net.cpp:151] conv1 needs backward computation.  I0721 10:38:16.722059 4692 net.cpp:74] Creating Layer relu1i0721 10:38:16.722070 4692 net.cpp:84] RELU1 <-conv1i0721 10:38:16.722082 4692 net.cpp:98] relu1, CONV1 (in-place) I0721 10:38:16.722096 4692 net.cpp:125] Top shape:256 96 (74342400) I0721 10:38:16.722105 4692 net.cpp:151] relu1 needs backward computation. I0721 10:38:16.722116 4692 net.cpp:74] Creating Layer pool1i0721 10:38:16.722125 4692 net.cpp:84] pool1 <-conv1i0710:38:16.722133 4692 net.cpp:110] pool1, pool1i0721 10:38:16.722167 4692 net.cpp:125] Top shape:256 96 27 27 (1 7915904) I0721 10:38:16.722187 4692 net.cpp:151] pool1 needs backward computation.  I0721 10:38:16.722205 4692 net.cpp:74] Creating Layer norm1i0721 10:38:16.722221 4692 net.cpp:84] Norm1 <-pool1i0721 10:38:16.722234 4692 net.cpp:110] norm1, norm1i0721 10:38:16.722251 4692 net.cpp:125] Top shape:256 96 27 27 (179 15904) I0721 10:38:16.722260 4692 net.cpp:151] norm1 needs backward computation.  I0721 10:38:16.722272 4692 net.cpp:74] Creating Layer conv2i0721 10:38:16.722280 4692 net.cpp:84] conv2 <-norm1i0721 10:38:16.722290 4692 net.cpp:110] conv2, conv2i0721 10:38:16.725225 4692 net.cpp:125] Top shape:256 256 27 27 (47 775744) I0721 10:38:16.725242 4692 net.cpp:151] conv2 needs backward computation.  I0721 10:38:16.725253 4692 net.cpp:74] Creating Layer relu2i0721 10:38:16.725261 4692 net.cpp:84] RELU2 <-conv2i0721 10:38:16.725270 4692 NET.CPP:98] Relu2, Conv2 (in-place) I0721 10:38:16.725280 4692 net.cpp:125] Top shape:256 (47775744) I0721 10 : 38:16.725288 4692 net.cpp:151] relu2 needs backward computation.  I0721 10:38:16.725298 4692 net.cpp:74] Creating Layer pool2i0721 10:38:16.725307 4692 net.cpp:84] pool2 <-conv2i0721 10:38:16.725317 4692 net.cpp:110] pool2, pool2i0721 10:38:16.725329 4692 net.cpp:125] Top shape:256 256 13 13 (11 075584) I0721 10:38:16.725338 4692 net.cpp:151] pool2 needs backward computation.  I0721 10:38:16.725358 4692 net.cpp:74] Creating Layer norm2i0721 10:38:16.725368 4692 net.cpp:84] Norm2 <-pool2i0721 10:38:16.725378 4692 net.cpp:110] norm2, norm2i0721 10:38:16.725389 4692 net.cpp:125] Top shape:256 256 13 13 (11 075584) I0721 10:38:16.725399 4692 net.cpp:151] norm2 needs backward computation.  I0721 10:38:16.725409 4692 net.cpp:74] Creating Layer conv3i0721 10:38:16.725419 4692 net.cpp:84] conv3 <-norm2i0721 10:38:16.725427 4692 net.cpp:110] Conv3-conv3i0721 10:38:16.735193 4692 net.cpp:125] Top shape:256 384 (16613376) I0721 10:38:16.735213 4692 net.c PP:151] conv3 needs backward computation.  I0721 10:38:16.735224 4692 net.cpp:74] Creating Layer relu3i0721 10:38:16.735234 4692 net.cpp:84] RELU3 <-conv3i0721  10:38:16.735242 4692 net.cpp:98] relu3, Conv3 (in-place) I0721 10:38:16.735250 4692 net.cpp:125] Top shape:256 384 (16613376) I0721 10:38:16.735258 4692 net.cpp:151] RELU3 needs backward computation.  I0721 10:38:16.735302 4692 net.cpp:74] Creating Layer conv4i0721 10:38:16.735312 4692 net.cpp:84] conv4 <-conv3i0721 10:38:16.735321 4692 net.cpp:110] conv4, conv4i0721 10:38:16.743952 4692 net.cpp:125] Top shape:256 384 13 13 (16 613376) I0721 10:38:16.743988 4692 net.cpp:151] conv4 needs backward computation.  I0721 10:38:16.744000 4692 net.cpp:74] Creating Layer relu4i0721 10:38:16.744010 4692 net.cpp:84] Relu4 <-conv4i0721 10:38:16.744020 4692 net.cpp:98] Relu4-CONV4 (iN-place) I0721 10:38:16.744030 4692 net.cpp:125] Top shape:256 384 (16613376) I0721 10:38:16.744038 4692 net.cpp:15 1] relu4 needs backward computation.  I0721 10:38:16.744050 4692 net.cpp:74] Creating Layer conv5i0721 10:38:16.744057 4692 net.cpp:84] conv5 <-conv4i0721 10:38:16.744067 4692 net.cpp:110] conv5, conv5i0721 10:38:16.748935 4692 net.cpp:125] Top shape:256 256 13 13 (11 075584) I0721 10:38:16.748955 4692 net.cpp:151] conv5 needs backward computation.  I0721 10:38:16.748965 4692 net.cpp:74] Creating Layer relu5i0721 10:38:16.748975 4692 net.cpp:84] relu5 <-conv5i0721  10:38:16.748983 4692 net.cpp:98] relu5, CONV5 (in-place) I0721 10:38:16.748998 4692 net.cpp:125] Top shape:256 256 (11075584) I0721 10:38:16.749011 4692 net.cpp:151] relu5 needs backward computation.  I0721 10:38:16.749022 4692 net.cpp:74] Creating Layer pool5i0721 10:38:16.749030 4692 net.cpp:84] pool5 <-conv5i0721 10:38:16.749039 4692 net.cpp:110] pool5, pool5i0721 10:38:16.749050 4692 net.cpp:125] Top shape:256 6 6 (2359296) I0721 10:38:16.749058 4692 net.cpp:151] POOL5 needs BAC Kward computation. I0721 10:38:16.749074 4692 net.cpp:74] Creating Layer fc6i0721 10:38:16.749083 4692 net.cpp:84] fc6 <-pool5i0721 10: 38:16.749091 4692 net.cpp:110] fc6, fc6i0721 10:38:17.160079 4692 net.cpp:125] Top shape:256 4096 1 1 (1048576) I07 10:38:17.160148 4692 net.cpp:151] fc6 needs backward computation. I0721 10:38:17.160166 4692 net.cpp:74] Creating Layer relu6i0721 10:38:17.160177 4692 net.cpp:84] Relu6 <-fc6i0721 1 0:38:17.160190 4692 net.cpp:98] relu6, Fc6 (in-place) I0721 10:38:17.160202 4692 net.cpp:125] Top shape:256 4096 1 1 (1048576) I0721 10:38:17.160212 4692 net.cpp:151] relu6 needs backward computation. I0721 10:38:17.160222 4692 net.cpp:74] Creating Layer drop6i0721 10:38:17.160230 4692 net.cpp:84] DROP6 <-fc6i0721 1 0:38:17.160238 4692 net.cpp:98] drop6, Fc6 (in-place) I0721 10:38:17.160258 4692 net.cpp:1Top shape:256 4096 1 1 (1048576) I0721 10:38:17.160265 4692 net.cpp:151] drop6 needs backward computation. I0721 10:38:17.160277 4692 net.cpp:74] Creating Layer fc7i0721 10:38:17.160286 4692 net.cpp:84] fc7 <-fc6i0721 10:38  : 17.160295 4692 net.cpp:110] fc7, fc7i0721 10:38:17.342094 4692 net.cpp:125] Top shape:256 4096 1 1 (1048576) I0721 10:38:17.342157 4692 net.cpp:151] fc7 needs backward computation. I0721 10:38:17.342175 4692 net.cpp:74] Creating Layer relu7i0721 10:38:17.342185 4692 net.cpp:84] relu7 <-fc7i0721 1 0:38:17.342198 4692 net.cpp:98] relu7, Fc7 (in-place) I0721 10:38:17.342208 4692 net.cpp:125] Top shape:256 4096 1 1 (1048576) I0721 10:38:17.342217 4692 net.cpp:151] relu7 needs backward computation. I0721 10:38:17.342228 4692 net.cpp:74] Creating Layer drop7i0721 10:38:17.342236 4692 net.cpp:84] DROP7 <-fc7i0721 1 0:38:17.342245 4692 net.cpp:98] drop7, Fc7 (in-place) I0721 10:38:17.342254 4692 net.cpp:125] Top shape:256 4096 1 1 (1048576) I0721 10:38:17.342262 4692 net.cpp:151] DROP7 needs backward computation. I0721 10:38:17.342274 4692 net.cpp:74] Creating Layer fc8i0721 10:38:17.342283 4692 net.cpp:84] Fc8 <-fc7i0721 10:38 : 17.342291 4692 net.cpp:110] fc8, fc8i0721 10:38:17.343199 4692 net.cpp:125] Top shape:256 1 1 (5632) I0721 10:3 8:17.343214 4692 net.cpp:151] fc8 needs backward computation. I0721 10:38:17.343231 4692 net.cpp:74] Creating Layer lossI0721 10:38:17.343240 4692 net.cpp:84] loss <-fc8i0721 10: 38:17.343250 4692 net.cpp:84] loss <-labelI0721 10:38:17.343264 4692 net.cpp:151] loss needs backward computation. I0721 10:38:17.343305 4692 net.cpp:173] Collecting learning rate and Weight decay.i0721 10:38:17.343327 4692 net.cpp:166 ] Network initialization done. I0721 10:38:17.343335 4692 net.cpp:167] Memory required for Data 1073760256

CIFAR-10 Training and learning on the Caffe

Working with databases: CIFAR-10

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

Get ready

Run the following command at the terminal:

CD $CAFFE _ROOT/DATA/CIFAR10./GET_CIFAR10.SHCD $CAFFE _root/examples/cifar10./create_cifar10.sh

Where Caffe_root is Caffe-master's address on your machine.

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

Model

The CNN consists of a convolution layer, a pooling layer, a nonlinear transformation layer, and a local contrast normalized linear classifier at the top. The model is defined in Caffe_root/examples/cifar10 directory ' s cifar10_quick_train.prototxt and can be modified. In fact, the suffix is prototxt many are used to modify the configuration.

Training and testing

Training this model is very simple when we write good parameters to set the file Cifar10_quick_solver.prototxt and define the file Cifar10_quick_train.prototxt and Cifar10_quick_ After Test.prototxt, run train_quick.sh or enter the following command at the terminal:

CD $CAFFE _root/examples/cifar10./train_quick.sh

Yes, train_quick.sh is a simple script that shows the execution information and the training tool is train_net.bin,cifar10_quick_solver.prototxt as a parameter.

A message similar to the following appears: This is information about building a model

I0317 21:52:48.945710 2008298256 net.cpp:74] Creating Layer conv1i0317 21:52:48.945716 2008298256 net.cpp:84] Conv1 <- dataI0317 21:52:48.945725 2008298256 net.cpp:110] conv1, conv1i0317 21:52:49.298691 2008298256 net.cpp:125] Top Shap E:100 (3276800) I0317 21:52:49.298719 2008298256 net.cpp:151] conv1 needs backward computation.

Then:

0317 21:52:49.309370 2008298256 net.cpp:166] Network initialization done. I0317 21:52:49.309376 2008298256 net.cpp:167] Memory required for Data 23790808i0317 21:52:49.309422 2008298256 SOLVER.CP P:36] Solver scaffolding done. I0317 21:52:49.309447 2008298256 solver.cpp:47] solving Cifar10_quick_train

After that, the training begins.

I0317 21:53:12.179772 2008298256 solver.cpp:208] iteration, lr = 0.001i0317 21:53:12.185698 2008298256 solver.cpp:65] iteration, loss = 1.73643...i0317 21:54:41.150030 2008298256 solver.cpp:87] iteration, testing netI0317 21:54:47. 129461 2008298256 solver.cpp:114] Test score #0:0.5504i0317 21:54:47.129500 2008298256 solver.cpp:114] Test score #1:1.2 7805

Each of the 100 iterations shows the time of the training LR (learningrate), and loss (training loss function), once every 500 times, output score 0 (accuracy) and score 1 (Test loss function)

After 5,000 iterations, the correct rate is approximately 75%, and the parameters of the model are stored in the binary protobuf format in cifar10_quick_iter_5000

The model can then be used to run on the new data.

Other

In addition, changing the Cifar*solver.prototxt file can use CPU training,

# Solver MODE:CPU or GPUSOLVER_MODE:CPU

You can see the difference between CPU and GPU training.

Main data Source: Caffe Official website tutorial

Caffe Deep Learning Framework Tutorial

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.