Understanding Caffe's implementation from the code level to enable customization of new features

Xue Yunfeng, the author of this article, is mainly engaged in the research of video image algorithms, and is a researcher of deep learning algorithm in Zhejiang Jieshang Vision Technology Co., Ltd.

It is believed that many small partners use Caffe's deep learning framework for a long time like me. They also hope to understand Caffe's implementation from the code level to implement customization of new functions. This article will analyze the Caffe source code from the perspective of the overall architecture and the underlying implementation.

1. Caffe overall architecture

The Caffe framework has five main components, Blob, Solver, Net, Layer, and Proto. The structure diagram is shown in Figure 1 below. Solver is responsible for deep network training. Each Solver contains a training network object and a test network object. Each network consists of several Layers. Each Layer's input and output Feature map is represented as Input Blob and Output Blob. Blob is the structure that Caffe actually stores data. It is a matrix of indefinite dimensions. It is generally used in Caffe to represent a straightened four-dimensional matrix. The four dimensions correspond to the Batch Size (N) and the number of channels of the Feature Map (C). Feature Map Height (H) and Width (W). Proto is based on Google's Protobuf open source project, is a XML-like data exchange format, users only need to define the data members of the object by format, can achieve object serialization and deserialization in multiple languages, used in Caffe Structure definition, storage, and reading of network models

Understanding Caffe's implementation from the code level to enable customization of new features

2.Blob analysis

The following describes the basic data storage class blob in Caffe. The Blob uses the SyncedMemory class for data storage. The data member data_ points to the memory or memory block where the data is actually stored. The shape_ stores the dimension information of the current blob, and the diff_ holds the gradient information for the reverse transfer. In Blobs, it is not only a four-dimensional form of num, channel, height, and width. It is an indefinite dimensional data structure that stores data and stores it in a single vector-type shape_variable. Each dimension has its dimensions. Can be changed at will.

Let's take a look at the key functions of the Blob, data_at this function can read the data stored in this class, diff_at can be used to read back the error back. By the way, give a hint, try to use data_at (const vector & index) to find the data. Reshape function can modify the blob storage size, count is used to return the number of stored data. The BlobProto class is responsible for the serialization of Blob data into Caffe's model.

3. Factory mode description

Next, we introduce a design pattern, the Factory Pattern, which is used in the creation of Solver and Layer objects in Caffe. First, look at the UML class diagram of the factory pattern:

Understanding Caffe's implementation from the code level to enable customization of new features

Just like the Factory generates the same function but different models, these products implement the same OperaTIon. Many people read the code of the factory pattern, and this question arises why the new one does not come out, so that the new one seems to have no problem. Imagine the following situation, because the name of the code refactoring class has changed, or the constructor parameter has changed (increase or decrease parameters). And you have N at the new class in your code. If you don't use the factory again, you can only change one by one. The role of the factory model is to allow users to reduce their understanding of the product itself and reduce the difficulty of use. If you use the factory, you only need to modify the implementation of the method that creates the concrete object of the factory class, and other code will not be affected.

For example, write code is not hungry to work overtime to eat supper, McDonald's chicken wings and KFC chicken wings are MM love to eat, although the taste is different, but whether you take MM to McDonald's or KFC, just talk to the waiter "To four chicken wings" on the line. McDonald's and KFC are factories that produce chicken wings.

4.Solver analysis

Next, switch back to the topic, and let's take a look at how the Solver optimization object is implemented in Caffe. The SolverRegistry class is the above factory class that we see. It is responsible for giving us an optimization algorithm product. The external only needs to define the data and network structure, and it can optimize itself.

Solver* CreateSolver(const SolverParameter& param) This function is the operation of CreateProduct in factory mode. The SolverRegistry factory class in Caffe can provide us with 6 kinds of products (optimization algorithm):

Understanding Caffe's implementation from the code level to enable customization of new features

The function of these six kinds of products is to realize the network parameter update, but the implementation is not the same. Let's take a look at their usage process. Of course, these products are similar to the OperaTIon in the Product class above. Each Solver will inherit the Solve and Step functions. The only thing that is unique to each Solver is that the contents of the ApplyUpdate function are not the same and the interfaces are the same. This is also consistent with us. The previously mentioned products produced by the factory have the same function and the details are different. For example, most rice cookers have the function of cooking rice, but each type of rice cooker cooking method may have different heating methods. There are also three-dimensional heating of the heating of the chassis. Wait. Next we look at the key functions in Solver.

The Solve function flowchart in Solver is as follows:

Understanding Caffe's implementation from the code level to enable customization of new features

Step function flowchart in Solver class:

Understanding Caffe's implementation from the code level to enable customization of new features

The key point in the Solver is to call the Sovle function and the Step function. You only need to compare the two implementations of the Solver class. To understand the above two flow charts, you can understand the Caffe training execution process.

5.Net class parsing

After analyzing Solver, we analyze some key operations of the Net class. This is the deep network object we created using Proto. This class is responsible for forward and reverse delivery of deep networks. The following is the Net class initialization method NeTInit function call flow:

Understanding Caffe's implementation from the code level to enable customization of new features

Simple analysis of key functions in Net's class:

1.ForwardBackward : Forward and Backward are called in sequence.

2.ForwardFromTo (int start, int end) : Performs forward transfer from the start layer to the end layer, using a simple for loop call.

3.BackwardFromTo (int start, int end) : Similar to the previous ForwardFromTo function, the reverse transfer from the start layer to the end layer is called.

The ToProto function completes the serialization of the network to a file, and it calls the ToProto function of each layer in a loop.

6.Layer analysis

Layer is the basic unit of Net, such as a convolutional layer or a Pooling layer. This section will introduce the implementation of the Layer class.

(1) Layer's inheritance structure

Understanding Caffe's implementation from the code level to enable customization of new features

(2) Creation of Layer

Similar to the way that Solver is created, the creation of Layer is also in the factory mode. Here are a few macro functions:

The REGISTER_LAYER_CREATOR is responsible for placing the function that creates the layer into the LayerRegistry.

Understanding Caffe's implementation from the code level to enable customization of new features

Let's take a look at the generated macro REGISTER_LAYER_CLASS for most of the functions created by the layer. We can see that the macro function is relatively simple. We use the type as part of the function name so that we can create a create function and place the create function in the LayerRegistry.

Understanding Caffe's implementation from the code level to enable customization of new features

REGISTER_LAYER_CREATOR(type, Creator_##type##Layer)

This code is in the split_layer.cpp file

REGISTER_LAYER_CLASS(Split).

In this way, we will give you an example after replacing the type, refer to the following code.

Of course, the creation function here seems to be a direct call, not involving some of the problems of our previous factory pattern. Is the class of all layers the same? Of course not, we look closely at the convolution class.

The convolutional layer does not create a function, of course not, convolutional layer creation function in LayerFactory.cpp, screenshots for everyone to see, the specific code is as follows:

Understanding Caffe's implementation from the code level to enable customization of new features

There are corresponding declarations for the creation functions of these two types of Layers. Here directly shows that in addition to the layer with cudnn, other layers are created using the first method to create functions, and the layer with cudnn achieve the second method to create the function.

(3) Initialization of Layer

After the introduction, we create a look at when several functions in the layer are called.

The key function Setup this function is called in the NeTInit in the previous flowchart, the code is as follows:

Understanding Caffe's implementation from the code level to enable customization of new features

This way, during the entire Layer initialization process, CheckBlobCounts is called first, then LayerSetUp, followed by Reshape, and finally SetLossWeights. In this way, we have a better understanding of the life cycle of Layer initialization.

(4) Introduction to other functions of Layer

Layer's Forward function and Backward function complete the network forward and reverse transfer, these two functions must be implemented in their own implementation of the new layer. Among them, Backward will modify the diff_ of the blob in the bottom, so that the error direction conduction is completed.

7. Introduction to Protobuf

The Caffe.proto file in Caffe is responsible for the construction of the entire Caffe network, and is responsible for the storage and reading of Caffemodel. The following uses an example to illustrate how Protobuf works.

Use the protobuffer tool to store 512 dimensional image features:

1.message writing: new txt file suffix name changed to proto, write your own message is as follows, and into the decompressed protobuff folder;

Understanding Caffe's implementation from the code level to enable customization of new features

Among them, dwFaceFeatSize represents the number of feature points; pfFaceFeat represents face features.

2. Open the windows command window (cmd.exe) ----> cd space, copy and paste protobuff file path into ------> enter;

3. Enter the command protoc *.proto --cpp_out=. --------->enter

4. You can see that the files "*.pb.h" and "*.pb.cpp" are generated in the folder, indicating success.

5. The following can be integrated with your own code:

(1) Create your own project, add "*.pb.h" and "*.pb.cpp" files to your project, and write #include" *.pb.h"

(2) According to the library tutorial to configure the library on it.

VS Protobuf distribution method:

Solution ----> Right-Click Project Name ----> Properties

Understanding Caffe's implementation from the code level to enable customization of new features

Use protobuf to package the following code:

Understanding Caffe's implementation from the code level to enable customization of new features

(1) Caffe's model serialization

BlobProto is actually a Blob serialized Proto class, which is used by the Caffe model file. Net calls each layer's Toproto method, and each layer's Toproto method calls the Blob class's ToProto method so that the entire model is serialized into proto. Finally, as long as this proto inherits the object of the message class serialized to the file to complete the model to write the file. When Caffe packages a model, it simply calls the WriteProtoToBinaryFile function. The contents of this function are as follows:

At this point, Caffe's serialization model was completed.

(2) A brief description of Proto.txt

The construction of the Caffe network and Solver's parameter definitions are all done with this type of file. Calling ReadProtoFromTextFile during Net build reads all network parameters. Then call the above process to build the entire caffe network. This file determines how to use what each blob in the existing caffe model is for. If there is no such file caffe model file will not be able to use, because the model only stores a variety of blob data, only The value of float, and how to split this data is determined by the prototxt file.

Caffe's architecture uses a reflection mechanism in the framework to dynamically create a layer to build Net. Protobuf essentially defines a graph, a reflection mechanism is formed by the macro and a map structure, and then a factory pattern is used to create various layers. Of course, different from the general definition of the configuration using xml or json, the project's wording using the proto file assembly components.

to sum up

The above is an overview of the Caffe code architecture, hoping to help the buddy find the key to opening the customized Caffe gate. The author of this article hopes to initiate a discussion with peers who wish to understand Caffe and the underlying implementation of the deep learning framework.

Condenser Evaporator

Refrigerator Condenser Evaporator

1. Main material:
Rolling welded steel pipe: OD 4.76 to 8
Wall thickness:0.5- 0.7
Low carbon steel wire OD 1.0 to 1.8
Bracket:
Steel plate (SPCC) thickness: 0.6 to 1.5
Steel plate (SPCC) thickness: 0.3 to 0.4

2.Structure:
Flat type of Wire On Tube Condenser used at the back
Bended or spiral type of wire on tube condenser used at the bottom
Wrapped type of tube embedded on plate

3.Technical ability:
Wire pitch: ≥ 5mm

4.Performance:
Surface with electrophoresis coating to prevent the corrosion
Inner cleanliness can meet the requirements of CFC and R134a cooling system
Can satisfy the cooling capability requirements

Condenser Evaporator,Evaporator And Condenser,Refrigerator Evaporator,Compressor Condenser Evaporator

FOSHAN SHUNDE JUNSHENG ELECTRICAL APPLIANCES CO.,LTD. , https://www.junshengcondenser.com