Feature extraction of Caffe C++api

Source: Internet
Author: User

As a beginner of Caffe, the feature extraction from the official Python tutorial is useful; but about the use of C++API, find some information, said is not a perfect feature; project requirements, get imagenet image in Caffenet network structure of the FC7 layer characteristics.

Environment: Window7+caffe
Language: C + +
Objective: To test the characteristics of a layer of image and to save
Can directly execute the following command, (note that the official case is saved as LEVELDB format, the database format is not intuitive, parsing is also more trouble, directly saved as text form how good AH ~ Another 7 parameters Oh, directly understand the corresponding parameter information)

Extract_features.exe D:/caffe-windows/examples/_temp/caffe_reference_imagenet_model D:/caffe-windows/examples/_ Temp/imagenet_val.prototxt fc7 
d:/caffe-windows/examples/_temp/features_fc7 leveldb GPU

And then through the solution of the Extract_features project source extract_features.cpp to improve the code to change the area block as follows

 Ofstream *fout = new Ofstream[num_features];
    for (size_t i = 0; i < num_features; ++i) {LOG (INFO) << "Opening DataSet" << Dataset_names[i];
  BOOST::SHARED_PTR&LT;DB::D b> db (Db::getdb (Db_type));
   Db->open (dataset_names.at (i), db::new);
   Feature_dbs.push_back (DB);
    Boost::shared_ptr<db::transaction> Txn (Db->newtransaction ());
    Txns.push_back (TXN);
    String feat_file_name = dataset_names.at (i);
    Feat_file_name = Feat_file_name.append ("/"). Append (Blob_names[i]). Append (". txt");
  Fout[i].open (Feat_file_name);
  LOG (ERROR) << "Extacting Features";
  Datum Datum;
  Std::vector<int> image_indices (num_features, 0); for (int batch_index = 0; Batch_index < num_mini_batches; ++batch_index) {Feature_extraction_net->forward ()

    ;
        for (int i = 0; i < num_features ++i) {Const boost::shared_ptr<blob<dtype> > Feature_blob = Feature_extraction_net->blob_by_name (BloB_names[i]);
      A blob is a four-dimensional data: n*c*w*h, "fc7" Layer 50*1*1*4960, "conv1" layer 50*96*55*55,//"conv2" layer 50*256*27*27.
      int batch_size = Feature_blob->num (); 

      Batch_size is n in a blob, representing the number of Feature-map LOG (ERROR) << "total feature block quantity:" << batch_size*num_mini_batches;//50*10

      int dim_features = Feature_blob->count ()/batch_size;
      LOG (ERROR) << "dim_features:" << dim_features;//4096 const dtype* feature_blob_data;
       for (int n = 0; n < batch_size ++n) {//Datum.set_height (Feature_blob->height ());
       Datum.set_width (Feature_blob->width ());
       Datum.set_channels (Feature_blob->channels ());
       Datum.clear_data ();
        Datum.clear_float_data ();    

      Feature_blob_data = Feature_blob->cpu_data () + Feature_blob->offset (n); for (int d = 0; d < dim_features ++d) {//Datum.add_float_data (Feature_blob_data[d]);//write to text file, FC7 total 4 096-D if (n = = 0) {for (int d = 0; d < dim_features ++d) Fout[i] << Feature_blob_data[d]
        << "";

        }//String key_str = Caffe::format_int (Image_indices[i], 10);
       string out; CHECK (Datum.
       Serializetostring (&out));
       txns.at (i)->put (key_str, out);
       ++image_indices[i];
       If (Image_indices[i]% 1000 = = 0) {//txns.at (i)->commit ();
       txns.at (i). Reset (feature_dbs.at (i)->newtransaction ()); LOG (ERROR) << "extracted features of" << image_indices[i] <<//"Query images for FEA
       Ture blob "<< blob_names[i];


    }//for (int n = 0; n < batch_size; ++n) fout[i].close (); }//for (int i = 0; i < num_features ++i)}//for (int batch_index = 0; Batch_index < num_mini_batches; ++bat Ch_index)//write the last batch//for (int i = 0; i < num_features; ++i) {//if (Image_Indices[i]% 1000!= 0) {//txns.at (i)->commit (); }//LOG (ERROR) << "extracted features of" << image_indices[i] <<//"Query images for FEA
    Ture blob "<< blob_names[i];
  feature_dbs.at (i)->close (); //}

Code rewrite, the resulting results are documented features, you can continue the next project task ~ ~

Fc7.txt Save is 4096-dimensional float data ~
configuration file upload problems, not more than 60M, Halo ~ ~

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.