Caffe Extraction feature C + + interface __c++

Source: Internet
Author: User

First, the preface

The following is the use of Caffe C + + interface to extract a layer of eigenvector, for the next record, the two ways of time consuming basically the same.

Second, the way one

CaffeExFeat.h #ifndef caffeexfeat_h #define CAFFEEXFEAT_H #include "caffe/caffe.hpp" #include <string> #include

<vector> #include "opencv2/opencv.hpp" using namespace Caffe; Class Caffeexfeat {public:explicit caffeexfeat (std::string proto,std::string model,char* nameLayer,std::string MeanF
		 Ile,float scale=-1); Explicit caffeexfeat std::string proto,std::string model,char* namelayer,float v1=0.0,float v2=0.0,float v3=0.0,float
		 SCALE=-1);

		~caffeexfeat ();
		Double*extractfeat (const cv::mat& img);
	Double calsimilarity (const cv::mat& IMG1, const cv::mat& IMG2);
		private:unsigned int blob_id_;
		boost::shared_ptr< net<float> > net_;
		Cv::size Input_geometry_;
		int Num_channels_;
		Cv::mat Mean_;
		blob<float>* Input_blobs_;
		unsigned int featnum_;

		float Scale_;
	    void Init (std::string proto,std::string model,float scale);
	    void Getmeandata (std::string mean_file);
		void Getmeandata (float v1,float v2,float v3);unsigned int get_blob_index (char *query_blob_name);
		void Wrapinputlayer (std::vector<cv::mat>* input_channels);

void preprocess (const cv::mat& img,std::vector<cv::mat>* input_channels);

}; #endif
CaffeExFeat.cpp #include "CaffeExFeat.h" caffeexfeat::caffeexfeat (std::string proto,std::string model, char* Nlayer
	, std::string meanfile, float scale) {init (Proto,model,scale);
    Getmeandata (Meanfile);
Blob_id_ = Get_blob_index (Nlayer);
	} caffeexfeat::caffeexfeat (std::string proto,std::string model, char* nlayer,float v1,float v2,float-v3, float scale) {
	Init (Proto,model,scale);
    Getmeandata (V1,V2,V3);
Blob_id_ = Get_blob_index (Nlayer);
	} void Caffeexfeat::init (std::string proto,std::string model,float scale) {Scale_=scale;
	Phase Phase = TEST;
	Caffe::set_mode (CAFFE::CPU);
	net_ = boost::shared_ptr< net<float> > (New caffe::net<float> (proto, phase));

	Net_->copytrainedlayersfrom (model);
	Input_blobs_ = Net_->input_blobs () [0];
	Num_channels_ = Input_blobs_->channels ();
	Input_geometry_ = Cv::size (Input_blobs_->width (), Input_blobs_->height ()); Input_blobs_->reshape (1, Num_channels_,input_geometry_.height, input_geometry_.width); //Can you enter more than one diagram Net_->reshape (); Dimension change} caffeexfeat::~caffeexfeat () {} double* caffeexfeat::extractfeat (const cv::mat& img) {std::vector<
	Cv::mat> Input_channels;
	Wrapinputlayer (&input_channels);

    Preprocess (IMG, &input_channels);
	Net_->forwardprefilled ();
	boost::shared_ptr<blob<float> > Featblob = net_->blobs () [blob_id_];
    Featnum_ = Featblob->count ();
    
    const FLOAT *featdata = (const float *) Featblob->cpu_data ();
    double* out = new Double[featnum_];

	for (int k=0;k<featnum_;++k) out[k]=featdata[k];
return out; Double caffeexfeat::calsimilarity (const cv::mat& IMG1, const cv::mat& img2) {double* feat_1 = Extractfeat (img
	1);

	double* feat_2 = extractfeat (IMG2); Double sim = Cblas_ddot (featnum_,feat_1,1,feat_2,1)/(Std::sqrt (Cblas_ddot (featnum_,feat_1,1,feat_1,1)) *std::sqrt (
    Cblas_ddot (featnum_,feat_2,1,feat_2,1)));
	delete []feat_1;

	delete []feat_2;
Return SIM; } void Caffeexfeat::getmeandata(std::string mean_file)  
  {Blobproto Blob_proto;  
  
  Readprotofrombinaryfileordie (Mean_file.c_str (), &blob_proto);  
  * Convert from Blobproto to blob<float> * * blob<float> Mean_blob; Mean_blob.  
  
  Fromproto (Blob_proto); /* The format of the mean file is planar 32-bit float BGR or grayscale.  
  * * std::vector<cv::mat> channels;  
  float* data = Mean_blob.mutable_cpu_data (); for (int i = 0; i < Num_channels_ ++i) {/* Extract an individual channel. * * Cv::mat Channel (mean_blob.he  
    Ight (), Mean_blob.width (), CV_32FC1, data);  
    Channels.push_back (channel);  
  Data + + mean_blob.height () * Mean_blob.width (); }/* Merge the separate channels into a single image.  
  * * Cv::mat mean;  
  
  Cv::merge (channels, mean); /* Compute The global mean pixel value and create a mean image * filled with this value.  
  * * Cv::scalar Channel_mean = Cv::mean (mean); Mean_ = Cv::mat (Input_geometry_, Mean.type (), channel_mean);
	} void Caffeexfeat::getmeandata (float v1,float v2, float v3) {cv::scalar Channel_mean (v1,v2,v3);
Mean_ = Cv::mat (Input_geometry_,cv_32fc3,channel_mean);    
    } unsigned int caffeexfeat::get_blob_index (char *query_blob_name) {std::string str_query (query_blob_name);
    vector< string > const & blob_names = Net_->blob_names (); 
            for (unsigned int i = 0; I!= blob_names.size (); ++i) {if (str_query = Blob_names[i]) {
        return i;

} LOG (FATAL) << "Unknown blob name:" << str_query;} void Caffeexfeat::wrapinputlayer (std::vector<cv::mat>* input_channels) {blob<float>* Input_layer = net_-  
  
  >input_blobs () [0];  
  int width = input_layer->width ();  
  int height = input_layer->height ();  
  float* input_data = Input_layer->mutable_cpu_data (); for (int i = 0; i < input_layer->channels (); ++i) {Cv::mat channel (height, width, cv_32fc1, input_data);  
    Input_channels->push_back (channel);  
  Input_data + = width * height;   
  } void Caffeexfeat::p reprocess (const cv::mat& img,std::vector<cv::mat>* input_channels) {Cv::Mat sample;  
  if (img.channels () = = 3 && num_channels_ = 1) Cv::cvtcolor (IMG, sample, Cv_bgr2gray);  
  else if (img.channels () = = 4 && num_channels_ = 1) Cv::cvtcolor (IMG, sample, Cv_bgra2gray);  
  else if (img.channels () = = 4 && num_channels_ = 3) Cv::cvtcolor (IMG, sample, CV_BGRA2BGR);  
  else if (img.channels () = = 1 && num_channels_ = 3) Cv::cvtcolor (IMG, sample, CV_GRAY2BGR); 

  else sample = IMG;
  Cv::mat sample_resized;  
  if (Sample.size ()!= input_geometry_) cv::resize (sample, sample_resized, Input_geometry_);

  else sample_resized = sample;
  Cv::mat sample_float;  
  if (Num_channels_ = = 3) Sample_resized.convertto (Sample_float, CV_32FC3); else Sample_resized.convertto (sample_float, CV_32FC1);  
  Cv::mat sample_normalized;
  
  Cv::subtract (Sample_float, Mean_, sample_normalized);  
  if (scale_!=-1) {cv::multiply (Scale_, sample_normalized,sample_normalized);
} cv::split (sample_normalized, *input_channels); }

Third, mode two

#ifndef caffeexfeat_h #define CAFFEEXFEAT_H #include "caffe/caffe.hpp" #include <string> #include <vector> #

Include "OPENCV2/OPENCV.HPP" using namespace Caffe; Class Caffeexfeat {public:explicit caffeexfeat (std::string proto,std::string model,char* nameLayer,std::string MeanF
		 Ile,float scale=1.); Explicit caffeexfeat std::string proto,std::string model,char* namelayer,float v1=0.0,float v2=0.0,float v3=0.0,float
		 Scale=1.);

		~caffeexfeat ();
		Double*extractfeat (const cv::mat& img);
	Double calsimilarity (const cv::mat& IMG1, const cv::mat& IMG2);
		private:unsigned int blob_id;
		boost::shared_ptr< net<float> > Net;
		blob<float>* Input_blobs;
		int channel,width,height;
		unsigned int featnum;
		float *meandata;

		float Scale_;
	    void Init (std::string proto,std::string model,float scale);
	    void Getmeandata (std::string meanfile);
		void Getmeandata (float v1,float v2,float v3); unsigned int get_blob_index (char *query_blob_name);

}; #endif
#include "CaffeExFeat.h" caffeexfeat::caffeexfeat (std::string proto,std::string model, char* nlayer,std::string
	Meanfile,float scale) {init (Proto,model,scale);
    Getmeandata (Meanfile);
blob_id = Get_blob_index (Nlayer); } caffeexfeat::caffeexfeat (std::string proto,std::string model, char* nlayer,float v1,float v2,float-v3,float scale) {i
	NIT (Proto,model,scale);
    Getmeandata (V1,V2,V3);
blob_id = Get_blob_index (Nlayer);
	} void Caffeexfeat::init (std::string proto,std::string model,float scale) {Scale_=scale;
	Phase Phase = TEST;
	Caffe::set_mode (CAFFE::CPU);
	NET = boost::shared_ptr< net<float> > (New caffe::net<float> (proto, phase));

	Net->copytrainedlayersfrom (model);
	Input_blobs = Net->input_blobs () [0];
	Channel = Input_blobs->channels ();
	width = Input_blobs->width ();
Height = input_blobs->height ();

 } caffeexfeat::~caffeexfeat () {delete meandata;}
	double* caffeexfeat::extractfeat (const cv::mat& img) {Cv::mat resizedimg; Cv: Resize (img, resizedimg, cv::size (height,width));
	
	Zoom and go mean float *input_data = Input_blobs->mutable_cpu_data ();
		for (int h=0;h 
Main.cpp
#include "CaffeExFeat.h"

#include <iostream>

int main (int argc,char**argv)
{

    Caffe::globalinit (&ARGC,&ARGV);  Turn off log
    std::string proto = "***.prototxt";
    std::string model = "***.caffemodel";
	
    Caffeexfeat Exfeater (Proto,model, (char*) "Fc5", 127.5,127.5,127.5, 1./128);

 
    Cv::mat img_1 = cv::imread (path);  
  Double* feat = extractfeat (img_1);
   Delete[] feat;
   return 0;
}

Four, under the compilation

g++ CaffeExFeat.cpp main.cpp-o main-d cpu_only-i/home/trainer/jyang/caffe-intel/include-l Caffe-intel/build/lib-lcblas-lpthread-lcaffe-lboost_system-lglog ' pkg-config opencv--libs--cflags '


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.