Add a new DataLayer for Caffe

Source: Internet
Author: User
Target

When Deepid is used to realize face recognition with Caffe, the framework of network training is often this:

This means that the data in the Image list is arranged in pairs, alternating between class (Intra Class) classes (Inter Class). This can be directly used Imagedatalayer to obtain a uniform Batch. Now as long as the Loss Layer simple to make changes, the network has been able to train the normal, quite simple.

But the simple price is also quite obvious: the combination of the data class and the class has been fixed, want to use the new combination training, you have to rearrange a ImageList. The alternate arrangement between class data and class data in the left image is not conducive to the optimization of losslayer calculation. the new framework

So if you can integrate this step of data consolidation into DataLayer, you will get a greater degree of simplification. and additional functions can be added, such as randomly extracted classes within the class data, data extraction in the class of the combination of large differences, the data between the classes to extract a small difference in the combination, if the calculation speed allows.

So the new training framework should be the following, DataLayer names are all taken, called tripletdatalayer!

Tripletdatalayer will extract a Batch data in each iteration, and the data in Batch is divided into Idenities A, idenities P, idenities N three parts, representing the ANC in FaceNet 1 respectively. Hor, Positive, Negative. Anchor and Positive are combinations within classes, and anchor and Negative are combinations of classes, which are expressed later in this way. You can see that because the data in the Batch is orderly, the corresponding Loss Layer do not need label information (except SoftMax Loss).

the realization of Tripletdatalayer

The steps to add a new data layer for Caffe (take Tripletdatalayer as an example) are as follows: Define related parameters in Src/caffe/proto/caffe.proto add triplet_d under src/caffe/layers/directory Ata_layer.cpp Add the TRIPLET_DATA_LAYER.HPP under the include/caffe/layers/directory add test_triplet_data_layer.cpp under the Src/caffe/test directory ( Optional)

2nd, 3 steps is to realize Tripletdatalayer class, the most important. Then add the parameters that need to be customized to the Caffe.proto file. Complete these two steps accurately, add the datalayer layer of work even if completed. If you're unsure, it's a good idea to write a test case in the Src/caffe/test directory. Analysis Imagedatalayer

My implementation principle is to caffe the source code to do the least modification . The best reference for the existing DataLayer is imagedatalayer. It is a matter of course to analyze its implementation, and here is a very rough Imagedatalayer class diagram I have drawn:

As you can see, unlike ordinary Layer, Imagedatalayer does not need to implement the. cu file. Since the operation of the GPU has already been implemented in higher-level classes, the same data prefetching (the classic producer consumer problem, each prefetch of 3 Batch) has also been implemented in the high-level class. So we just rewrite the Load_batch () function. defining external parameters in Caffe.proto defines a message structure for a parameter

Modified on the basis of imagedataparameter. Although the choice of structure name is free, it is best to write in Caffe style, that is, the structure name corresponds to the class name. For PROTOBUF syntax, please Google yourself.

Message Tripletdataparameter {//Picture List of paths, can be file names or file paths Required String source = 1;

  Whether to filter the data based on the characteristics of the picture optional bool Use_feature = [default = False];

  The extension of the picture feature file, the feature file and the corresponding picture are located in the same directory Optional string feature_extension = [default = ". Feat"];

  If each person's picture number is unequal, let each Batch's data distribution be the same as the training set optional BOOL Batch_follow_distribution = [default = True];

  The directory where the picture is stored (all pictures are used in the same directory) Optional string root_folder = [default =];

  Batch Size,layer when initialized to a multiple of 3 optional UInt32 batch_size = 4 [default = 1];

  Randomly skip several data at startup, with Imagedatalayer slightly different optional uint32 Rand_skip = 7 [default = 0];
  The following parameters have exactly the same meaning as Imagedatalayer optional bool shuffle = 8 [default = False];
  Optional UInt32 new_height = 9 [default = 0];
  Optional UInt32 new_width = ten [default = 0];
  optional BOOL Is_color = [default = True];
  Optional float scale = 2 [default = 1];
  Optional String mean_file = 3;
  Optional UInt32 crop_size = 5 [default = 0]; optional BOOL Mirror = 6 [DefauLt = false]; }
to add the definition of a parameter in Layerparameter

Similarly, the name of the argument is related to the class name. Note that the identification number cannot be duplicated with the previous field.

Message Layerparameter {
  ...
  Optional Tripletdataparameter Triplet_data_param = A;

These parameters can then be read in the Tripletdatalayer class (caffe.pb.cc and Caffe.pb.h are automatically compiled during Caffe compilation). For example, if we want to read the Batch_size parameter in the configuration file, we can do this:

int batch_size_ = This->layer_param_.triplet_data_param (). Batch_size ();

Cond...

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.