Caffe source code Simple analysis--layer layer _caffe

Source: Internet
Author: User

Original from: http://www.shwley.com/index.php/archives/68/

Objective

To be honest, there are more layer layers in the Caffe, and the various abstractions look rather round. The official tutorial on layer is very clear, I based on this document, a simple picture, and then understand the convenience of some.

Layer.hpp

The header files associated with layer are:

COMMON_LAYERS.HPP
data_layers.hpp
layer.hpp
loss_layers.hpp
neuron_layers.hpp
vision_ Layers.hpp

Where ' layer.hpp is the abstract base class, others are inherited on its basis, that is, the remaining five header files and the five parts of the above figure. In the layer.hpp ' header file, these headers are included:

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"
#include " CAFFE/UTIL/DEVICE_ALTERNATE.HPP "

In DEVICE_ALTERNATE.HPP, macros are defined by #ifdef cpu_only to cancel the call to the GPU:

#define STUB_GPU (classname)
#define Stub_gpu_forward (classname, funcname)
#define Stub_gpu_backward ( ClassName, FuncName)

These three main parameters are in the layer:

Layerparameter LAYER_PARAM_;      This is the layer parameter stored in the Protobuf file
vector<share_ptr<blob<dtype>>> blobs_;        This store is the layer parameter, in the program use
vector<bool> param_propagate_down_;        This bool indicates whether the diff for each blob parameter is computed, i.e. propagation error

The build function of the Layer class explicit Layer (const layerparameter& param): LAYER_PARAM_ (param) Attempts to read the parameters from the Protobuf file. Its three main interfaces:

virtual void SetUp (const vector<blob<dtype>*>& Bottom, vector<blob<dtype>*>* top)
Inline Dtype Forward (const vector<blob<dtype>*>& Bottom, vector<blob<dtype>*>* top);
inline void Backward (const vector<blob<dtype>*>& top, const vector<bool>& Propagate_down, const <blob<dtype>*>* bottom);

The setup function needs to be implemented according to the actual parameter settings, initialization of various types of parameters, forward and backward corresponding forward calculation and reverse update, input unification is bottom, output is top, backward inside has a propagate_ The down parameter, which indicates whether the layer propagates the parameter in reverse.

In the implementation of Forward and backward, according to Caffe::mode () the corresponding operation, that is, using CPU or GPU for computing, two have implemented the corresponding interface forward_cpu, FORWARD_GPU and Backward_ CPU, BACKWARD_GPU, these interfaces are virtual, or according to the type of layer to do the corresponding calculation (note: Some layer do not have the implementation of GPU calculation, so the encapsulation of the CPU to add the calculation as a backup). In addition, the Toproto interface is implemented, and the layer parameters are written to the protocol buffer file. Data_layers.hpp

DATA_LAYERS.HPP This header file contains the following headers:

#include "boost/scoped_ptr.hpp"
#include "hdf5.h"
#include "leveldb/db.h"
#include "lmdb.h"

Include "CAFFE/BLOB.HPP"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/ Internal_thread.hpp "
#include" caffe/layer.hpp "
#include" caffe/proto/caffe.pb.h "

See HDF5, Leveldb, Lmdb, really is related to the specific data. Data_layer, as the input layer of the original data, is at the bottom of the whole network, it can read data from the database leveldb, Lmdb, can also read directly from the memory, can also read the data from HDF5, even the original image.

With regard to these databases, the introduction is as follows:

Leveldb is a high-performance key/value repository for Google, the simple call, the data is snappy compressed, is said to be much more efficient, can reduce disk I/O, specific examples can look at Wikipedia.

The Lmdb (Lightning memory-mapped database) is a key/value repository similar to LEVELDB, but the effect seems to be better, with "Ultra-fast,ultra-compact" on its home page. This awaits further study AH ~ ~

HDF (Hierarchical Data format) is a file format and corresponding library file designed to store and process high-volume scientific data, and the most popular version is HDF5, whose files contain two basic data objects: Group: Similar folders, can contain multiple datasets or subordinate groups; Datasets: data content, either multidimensional arrays or more complex data types.

The above content from Wikipedia, on the use can refer to [HDF5 Small test-tall object file format] (HDF5 Small test-tall multiple object file format), the follow-up will be studied in detail how to use.

The role of CAFFE/FILLER.HPP is to populate the initial parameters based on the definition of the layer when the network is initialized, and the following code is intuitive to fill the corresponding parameter according to the type specified by Fillerparameter.

//A function to get a specific filler from the specification given in//Fillerparameter.
Ideally this would is replaced by a factory pattern,//But we'll leave it this way for now. Template <typename dtype> filler<dtype>* getfiller (const fillerparameter& param) {const STD::STRING&AM P
  Type = Param.type ();
  if (type = = "Constant") {return new constantfiller<dtype> (param);
  else if (type = = "Gaussian") {return new gaussianfiller<dtype> (param);
  else if (type = = "Positive_unitball") {return new positiveunitballfiller<dtype> (param);
  else if (type = = "Uniform") {return new uniformfiller<dtype> (param);
  else if (type = = "Xavier") {return new xavierfiller<dtype> (param);
  else {CHECK (false) << "Unknown filler name:" << param.type ();
Return (filler<dtype>*) (NULL); }

INTERNAL_THREAD.HPP encapsulates the Pthread function, the inherited subclass can get a separate thread, the main role is in the calculation of the current batch of data, in the background to obtain a new batch of data.

About Data_layer, I have to pay attention to the basic picture of the mark. Neuron_layers.hpp

When data is entered, it is calculated, such as common sigmoid, Tanh, and so on, which are abstracted into neuron_ LAYERS.HPP inside the class Neuronlayer, this layer is only responsible for the specific calculation, so explicitly defined the input exactnumbottomblobs () and exactnumtopblobs () are constant 1, that is, enter a blob, Output a blob. Common_layers.hpp

Neruonlayer is only responsible for simple one-to-one computations, while the remaining complex calculations are all in the COMMON_LAYERS.HPP. Operations such as Argmaxlayer, Concatlayer, Flattenlayer, Softmaxlayer, Splitlayer, and Slicelayer are modified to add or subtract from blobs. Loss_layers.hpp

The previous data layer and common layer are intermediate computing layers, though it involves reverse propagation, but the source of the transmission comes from the Loss_layer, the most terminal of the network. This layer is to compute the error, so the input is 2 blobs, output 1 blobs. Vision_layers.hpp

Vision_layer is mainly the operation of image convolution, such as convolusion, pooling, LRN are inside, according to the official document, it is possible to output images, this depends on the specific implementation of the code. Inside has a im2col realization, see Caffe Author's explanation, mainly is in order to accelerate the convolution, this concrete is how realizes must study well ~ ~ After language

Combine official documents, add drawing and look code, finally to the whole layer layer has a basic understanding: data is responsible for input, vision is responsible for convolution related calculation, neuron and common responsible for the middle part of the data calculation, and loss is the last part, responsible for calculating the error of the reverse propagation. The concrete realization all in src/caffe/layers inside, slowly again research research.

In these abstract base-class header files, looks very tired, fortunately various searches, also can learn some techniques, for example, skillfully uses the macro definition to abbreviate c,c++ the code, uses the template method, will have the massive repetition interface and the parameter class abstract as a macro definition, achieves simplifies the code the goal.

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.