caffe configuration training for sphereFace face recognition

     This tutorial is an example of adding a new layer MarginInnerProduct of sphereFace based on the configuration of the Windows Microsoft version of caffe environment. Other new layers can also be used for reference. The author's open source face recognition source is at https://github.com/wy1iu/sphereface . In addition, the address of the Microsoft version of caffe is: https://github.com/Microsoft/caffe . Figure 1 shows the network structure file prototxt provided by the author for training.

                             

Figure 1 Trained network structure

1. File rewriting


    Download the author's entire code on github and unzip it. Find the caffe-shpereface version in the tools folder. In fact, this version is basically the same as the Microsoft version of caffe. The difference is the new layer and some other associated detail files.


1. Copy the source code: Find margin_inner_product_layer.cpp and margin_inner_product_layer.cu under "caffe-shpereface/src/caffe/include/caffe/layers" in the tools folder and copy them to C:\caffe\caffe-master\src\caffe\ In the layers directory (this is my Microsoft version of caffe source code directory), ".cu" is the cuda source code for GPU, and computers without GPU can not copy it. In addition, the dependent source code math_functions.cpp and math_functions.cu should also be copied to the corresponding directory. This mathematical calculation file is generally in the src/caffe/util directory, and the file location can also be searched.
2. Copy the header file: find margin_inner_product_layer.hpp under "caffe-sphereface/include/caffe/layers" under tools and copy it to C:\caffe\caffe-master\include\caffe\layers directory. In addition, the dependent header files math_functions.hpp and mkl_alternate.hpp also need to be copied to the corresponding directory, and the location can be searched.
3. Rewrite caffe.proto: find the "caffe.proto" file in the "caffe-sphereface\src\caffe\proto" directory under tools, open it with notepad++ or notepad, this file is very important, it defines all the support of caffe Layer parameters, type, name, ID, etc.
The place to be rewritten is in message LayerParameter {
                              ...
                              ...
                              }
and the bottom message MarginInnerProductParameter{
                              ...
                              ...
                              }
      find the "caffe.proto" file in the Microsoft version of caffe similar directory (my directory C:\caffe\caffe-master\src\caffe\proto) and open it , These two files are roughly the same, but there are actually many differences. If you replace caffe.proto directly with Microsoft's, there will be errors, and there will be many pitfalls, and there will be inexplicable errors in the subsequent compilation. After opening, find the position shown in Figure 2, see what the next suitable specified layer ID is pointed by the arrow, and then add parameters to the second one according to the first opened caffe.proto new layer parameters at the appropriate position below, As shown in the green line in Figure 3 added.

                      

Figure 2 layer parameter information

                            

Figure 3 Parameter ID specification

Then add some parameter ID descriptions of the new layer at the end of the file, which is also added according to the first one, as shown in the gray part of Figure 4.

                             

Figure 4 The last added part


2. Compile the source code


     Since a new layer is added in the first step, it is necessary to recompile the caffe item. The other 15 projects (if not added before) do not need to be recompiled, and the compilation time is long.

1. Open the Microsoft version of the caffe project in VS2013, and add a new source code to the libcaffe project, as shown in Figure 5.


                            

Figure 5 Source code containing the new layer

Compile the caffe project, right-click caffe, select regenerate, and wait for the compilation to complete. At this time, if an error is reported, see the cause of the error, which cpp file or header file is the problem, and then look for the problem with the Microsoft version of caffe. If it is successful, you can see that the compiled caffe.exe is larger than the original one, mine is 3.44M.

                              

Figure 6 Compiled caffe.exe

     At this point, you can use the new Layer as shown in Figure 7 below.

                                                                    


Figure 7 New layer


3. Training


      According to the network structure provided by sphereface, prepare your own data and train your own model.


                                  

Figure 8 spherefaceNet-20 training



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325593623&siteId=291194637