Caffe_manual/get_features.cpp

Source: Internet
Author: User

Caffe_manual/get_features.cpp

According to the feedback, specially wrote a CPP file, can be a forward, while completing multi-layer feature extraction, the final probability output and the top label output, see Get_features.cpp file:

/** * Usage:get_features.exe feat.prototxt H:\Models\Caffe\bvlc_reference_caffenet.caffemodel 6 Conv1,fc7,prob,argmax Conv1.dat,fc7.dat,prob.dat,argmax.dat GPU 0 for Feat.prototxt, see the following example:name: ' Caffenet ' state {PHA Se:test} layer {name: "Data" type: "ImageData" Top: "Data" Top: "label" Transform_param {m Irror:false crop_size:227 mean_file: "Imagenet_mean.binaryproto"} image_data_param {so Urce: "File_list.txt" Batch_size:1 new_height:256 new_width:256}} layer {name: "Conv 1 "type:" Convolution "bottom:" Data "Top:" Conv1 "Convolution_param {num_output:96 Kern El_size:11 Stride:4}} ################################################################################# # # # # # #some lines is ignored here for simplicity, complete them by yourself###### ######################################### ######################################## Layer {name: "Fc8" type: "Innerproduct" bottom: "fc7" Top: "Fc8" Inner_product_param {     num_output:1000}} layer {name: "Prob" type: "Softmax" bottom: "Fc8" Top: "prob"} layer { Name: "Argmax" type: "Argmax" bottom: "Prob" Top: "Argmax" Argmax_param {top_k:1}} for file _list.txt, see the following example:h:\data\ilsvrc2012\n01440764\n01440764_18.jpeg 0 H:\Data\ILSVRC2012\n0144076 4\n01440764_297.jpeg 0 H:\Data\ILSVRC2012\n01443537\n01443537_395.JPEG 1 H:\Data\ILSVRC2012\n01443537\n01443537_69 3.JPEG 1 H:\Data\ILSVRC2012\n01518878\n01518878_103.JPEG 9 H:\Data\ILSVRC2012\n01518878\n01518878_477.JPEG 9 How T o load features in Matlab? Use the following function, See:prob = sc_load (' Prob.dat '); function data = sc_load (filename, type) if ~exist (' type ', ' var ') | |     IsEmpty (type) type = ' single ';         End fid = fopen (filename, ' r '); rows = Fread (FID, 1, tYPE);     cols = Fread (FID, 1, type);         data = fread (FID, rows * cols, type);     Fclose (FID);     data = reshape (data, rows, cols);     Switch type case ' int32 ' Data = Int32 (data);     Case "single" data = single (data); End end***/#include<string>#include<vector>#include"boost/algorithm/string.hpp"#include"caffe/caffe.hpp"usingboost::shared_ptr;usingSTD::string;using namespaceCaffe;#defineMax_feat_num 16intMainintargcChar**argv) {    if(ARGC <6) {LOG (ERROR)<<"get_features proto_file model_file iterations blob_name1[,name2] save_name1[,name2]"<<"[Cpu/gpu] [Device ID]"; return 1; } Phase Phase=TEST; if(ARGC >=7&& strcmp (argv[6],"GPU") ==0) {Caffe::set_mode (CAFFE::GPU); intdevice_id =0; if(ARGC = =8) {device_id= Atoi (argv[7]);        } caffe::setdevice (device_id); LOG (ERROR)<<"Using GPU #"<<device_id; } Else{LOG (ERROR)<<"Using CPU";    Caffe::set_mode (CAFFE::CPU); } boost::shared_ptr<Net<float> >feature_net; Feature_net.reset (Newnet<float> (argv[1], phase)); Feature_net->copytrainedlayersfrom (argv[2]); intTotal_iter = Atoi (argv[3]); LOG (ERROR)<<"Running"<< Total_iter <<"iterations."; STD::stringFeature_blob_names (argv[4]); Std::vector&LT;STD::string>Blob_names; Boost::split (Blob_names, Feature_blob_names, boost::is_any_of (",")); STD::stringSave_file_names (argv[5]); Std::vector&LT;STD::string>File_names; Boost::split (File_names, Save_file_names, boost::is_any_of (",")); Check_eq (Blob_names.size (), File_names.size ())<<"The number of feature blob names and save file names must be equal"; size_t Num_features=blob_names.size ();  for(size_t i =0; i < num_features; i++) {CHECK (feature_net-Has_blob (Blob_names[i]))<<"Unknown feature blob name"<< Blob_names[i] <<"In the network"; } FILE*Fp[max_feat_num];  for(size_t i =0; i < num_features; i++) {Fp[i]= fopen (File_names[i].c_str (),"WB"); }     for(inti =0; i < Total_iter; ++i) {feature_net-forwardprefilled ();  for(intj =0; J < Num_features; ++j) {Constboost::shared_ptr<blob<float> > Feature_blob = feature_net->Blob_by_name (Blob_names[j]); floatNum_imgs = Feature_blob->num () *Total_iter; floatFeat_dim = Feature_blob->count ()/feature_blob->num (); Const float* Data_ptr = (Const float*) feature_blob->Cpu_data (); if(i = =0) {fwrite (&feat_dim,sizeof(float),1, Fp[j]); Fwrite (&num_imgs,sizeof(float),1, Fp[j]); } fwrite (Data_ptr,sizeof(float), feature_blob->count (), fp[j]); }    }     for(size_t i =0; i < num_features; i++) {fclose (fp[i]); }    return 0;}

Caffe_manual/get_features.cpp

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.