"Caffe C + + interface use instructions (c)" Ubuntu14.04 under the Caffe using the training model for classification of C + + interface use instructions (c) __c++

Source: Internet
Author: User

Ubuntu, the C + + classification interface uses the method, as follows:

This blog is a broadcast of the blog ...

The author realized that after using Caffe training model, how to call the model in the program is a problem that many friends pay attention to, therefore, the author intends to explain how to use C + + to call Caffe training model in the program, the following start body.

in your friends from GitHub download Caffe source code, in the source code has a example folder, in the example folder has a Cpp_classification folder, open it, There is a CPP file called classification, which is the interface that Caffe gives us to call the classification network for forward computing, to get the result of the classification, Let's first parse the Classification.cpp file, according to the Convention, the source code and the comments released first:

[CPP]  View Plain  copy #include  <caffe/caffe.hpp>   #ifdef  USE_OPENCV   # include <opencv2/core/core.hpp>   #include  <opencv2/highgui/highgui.hpp>    #include  <opencv2/imgproc/imgproc.hpp>   #endif   // USE_OPENCV    #include  <algorithm>   #include  <iosfwd>   #include  < memory>   #include  <string>   #include  <utility>   # include <vector>      #ifdef  USE_OPENCV   using namespace  caffe;  // nolint (build/namespaces)    using std::string;      /* pair  (label, confidence)  representing a prediction. */   TYPEDEF&NBSP;STD::p air<string, float> prediction;//Record the name of each class and the probability      // ClassifiEr is the constructor, mainly carries on the model initialization, reads the training complete model parameter, the mean value file and the label file    class classifier {    public:      classifier (const string& model_file,//model_file the Prototxt file path to record the network structure when testing the model)                 const string & trained_file,//trained_file Caffemodel file path           for training completed        const string& mean_file,//mean_file is the file path to record data set mean values, Data set mean files are typically formatted as binaryproto                 const string& label_file);//label_file is the file path for the record category label, the label is usually recorded in a TXT file, one line          std::vector<prediction> classify (const cv::mat& img, int  N&NBSP;=&NBSP;5);//classify function to carry out network prequel, get the probability that img belongs to each class        private:      void setmeaN (const string& mean_file);//setmean function is mainly to set the mean value, each test map input will be subtracted from the average operation, this mean value can be used in the model of the dataset image of the mean value          std::vector<float> predict (const cv::mat& img);// The Predict function is the main component of the classify function, and the IMG is sent to the network for forward propagation to get the final category         void wrapinputlayer (std::vector<cv::mat>* input_channels);//wrapinputlayer function puts an IMG channel (input_channels) into the input blob of the network          void preprocess (const cv::mat& img,                      std::vector <cv::mat>* input_channels);//preprocess function to separate input image img by Channel (input_channels)        private

Related Article

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.