Caffe Official website: http://caffe.berkeleyvision.org/
Initializing the network
1#include"caffe/caffe.hpp"2#include <string>3#include <vector>4 using namespaceCaffe;5 6 Char*proto ="H:\\models\\caffe\\deploy.prototxt";/*load the caffenet configuration*/7Phase Phase = TEST;/*or TRAIN*/8 Caffe::set_mode (CAFFE::CPU);9 //Caffe::set_mode (CAFFE::GPU);Ten //caffe::setdevice (0); One A //! Note: All of the net mentioned below is this net -boost::shared_ptr< net<float> > Net (Newcaffe::net<float> (proto, Phase));
Load a trained model
1 Char " H:\\models\\caffe\\bvlc_reference_caffenet.caffemodel " ; 2 Net->copytrainedlayersfrom (model);
Read image mean value
1 Char*mean_file ="H:\\models\\caffe\\imagenet_mean.binaryproto";2blob<float>Image_mean;3 Blobproto Blob_proto;4 Const float*mean_ptr;5UnsignedintNum_pixel;6 7 BOOLsucceed = Readprotofrombinaryfile (Mean_file, &Blob_proto);8 if(Succeed)9 {Ten Image_mean. Fromproto (Blob_proto); OneNum_pixel = Image_mean.count ();/*nchw=1x3x256x256=196608*/ AMean_ptr = (Const float*) Image_mean.cpu_data (); -}
Forward propagation network According to the specified data
1 //! Note:data_ptr points to data that has been processed (to mean, the long width and batch Size of the network input image)2 voidCaffe_forward (boost::shared_ptr< net<float> > & NET,float*data_ptr)3 {4blob<float>* input_blobs = Net->input_blobs () [0];5 Switch(Caffe::mode ())6 {7 CaseCAFFE::CPU:8memcpy (input_blobs->mutable_cpu_data (), Data_ptr,9 sizeof(float) * input_blobs->count ());Ten Break; One CaseCaffe::gpu: Acudamemcpy (input_blobs->mutable_gpu_data (), Data_ptr, - sizeof(float) * input_blobs->count (), cudamemcpyhosttodevice); - Break; the default: -LOG (FATAL) <<"Unknown Caffe mode."; - } -Net->forwardprefilled (); +}
Gets its index in the network based on the name of the feature layer
1 //! Note:net blob refers to the output data of each layer, that is, feature Maps2 //char *query_blob_name = "CONV1";3UnsignedintGet_blob_index (boost::shared_ptr< net<float> > & NET,Char*query_blob_name)4 {5STD::stringstr_query (query_blob_name); 6vector<string>Const& blob_names = net->blob_names ();7 for(unsignedinti =0; I! = Blob_names.size (); ++i)8 {9 if(Str_query = =Blob_names[i])Ten { One returni; A } - } -LOG (FATAL) <<"Unknown blob Name:"<<Str_query; the}
Read network specified feature layer data
1 //! Note: According to Caffenet's Deploy.prototxt file, there are 15 blobs in the net, from data up to prob2 Char*query_blob_name ="CONV1";/*data, Conv1, Pool1, Norm1, fc6, prob, etc*/3Unsignedintblob_id =Get_blob_index (NET, query_blob_name);4 5boost::shared_ptr<blob<float> > BLOB = net->blobs () [blob_id];6UnsignedintNum_data = Blob->count ();/*nchw=10x96x55x55*/7 Const float*blob_ptr = (Const float*) Blob->cpu_data ();
Gets its index in the network based on the name of the layer
1 //! Note:layer includes all layers of the neural network, for example, there are 23 layers of caffenet2 //char *query_layer_name = "CONV1";3UnsignedintGet_layer_index (boost::shared_ptr< net<float> > & NET,Char*query_layer_name)4 {5STD::stringstr_query (query_layer_name); 6vector<string>Const& layer_names = net->layer_names ();7 for(unsignedinti =0; I! = Layer_names.size (); ++i)8 {9 if(Str_query = =Layer_names[i])Ten { One returni; A } - } -LOG (FATAL) <<"Unknown layer Name:"<<Str_query; the}
Reading weight data for a specified layer
1 //! Note: BLOBs that are different from net are feature maps,layer blobs that refer to weight and bias of conv and FC layers2 Char*query_layer_name ="CONV1";3 Const float*weight_ptr, *bias_ptr;4Unsignedintlayer_id =Get_layer_index (NET, query_layer_name);5boost::shared_ptr<layer<float> > Layer = net->layers () [layer_id];6std::vector<boost::shared_ptr<blob<float> >> blobs = layer->blobs ();7 if(Blobs.size () >0)8 {9Weight_ptr = (Const float*) blobs[0]->Cpu_data ();TenBias_ptr = (Const float*) blobs[1]->Cpu_data (); One } A - //! Note: In training mode, read the gradient data for the specified layer, similar to this, the only difference is to change the cpu_data to Cpu_diff
Modify weight data for a layer
1 Const float* DATA_PTR;/*pointer to data to write, source data pointer*/2 float* Weight_ptr = NULL;/*A pointer to a layer of weights in the network, the target data pointer*/3UnsignedintData_size;/*amount of data to write*/4 Char*layer_name ="CONV1";/*the layer name that needs to be modified*/5 6Unsignedintlayer_id =Get_layer_index (NET, query_layer_name); 7boost::shared_ptr<blob<float> > Blob = net->layers () [Layer_id]->blobs () [0];8 9CHECK (data_size = = blob->count ());Ten Switch(Caffe::mode ()) One { A CaseCAFFE::CPU: -Weight_ptr = blob->Mutable_cpu_data (); - Break; the CaseCaffe::gpu: -Weight_ptr = blob->Mutable_gpu_data (); - Break; - default: +LOG (FATAL) <<"Unknown Caffe Mode"; - } +Caffe_copy (blob->count (), data_ptr, weight_ptr); A at //! Note: In training mode, manually modify the gradient data for the specified layer, similar to - //Mutable_cpu_data to Mutable_cpu_diff,mutable_gpu_data instead of Mutable_gpu_diff
Save a new model
1 Char " Bvlc_reference_caffenet_new.caffemodel " ; 2 Netparameter Net_param; 3 false ); 4 writeprototobinaryfile (Net_param, weights_file);
Caffe Using Tutorials