Current location - Recipe Complete Network - Complete cookbook of home-style dishes - How to call a well-trained caffemodel?
How to call a well-trained caffemodel?
Folder, and then write a main function to call this class, such as: copy, save to caffe/examples/myproject/net _ operator.hpp, and then write a main.cpp in the same directory, and # include "net _ operator.hpp" in the main function. You can use this class: conststringnet _ prototxt =/prototxt file of your network, with absolute path, the following is the same as conststringpre _ trained _ file = ""; //this. Caffemodel file you trained, conststringimg _ path =///mirror path to be tested//Create NetOperator object net operator _ operator (net _ prototxt, pre _ trained _ file); blob * blob = net _ operator . process image(img _ path); //blob gets the output result of the last layer. As for how to store data in blob, it depends on official website's definition. After writing the main.cpp, make it in the caffe directory. Then it will compile the file you wrote, corresponding to the generated executable file. For example, as I wrote above, after make, a main.bin will be generated in the caffe/build/examples/myproject folder. Just execute this file. Because the generated executable file is not directly in the code directory, I suggest that the path you write should be an absolute path. In addition, if you want to get the output of the last layer, you need to modify the return value of the processImage function and get the required blob through the member variable net_ of the NetOperator. For example, if there is a blob named "label", you can use net _->; Blob_by_name("label "), of course, you get the type of shared _ ptr>, and you will know how it is different from ordinary pointers by searching boostshared_ptr. Next, paste the code: # include # include # include # include # include # include # including namespace cafe; //NOLINT(build/namespaces) uses STD::string;; class net operator { public:net operator(const string & amp; net _ proto txt); net operator(const string & amp; net_prototxt,conststring & amptrained _ file); ~ net operator(){ } int batch _ size(){ return batch _ size _; } Blob * process image(const string & amp; img_path,boolis _ color = true); blob * process images(const vector & amp; img_paths,boolis _ color = true); private:voidcreateNet(conststring & amp; net _ proto txt); //readtheimageandstoreitinthiidxpositionfimagesintheblovodireadimagetoblob(conststring & amp; img_path,intidx=0,boolis _ color = true); shared _ ptr & gtnet _; cv::size input _ geometry _; intbatch _ size _intnum _ channels _Blob * input _ blob _TransformationParametertransform _ param _; Shared _ ptr> data converter; Blobtransformed _ data _}; net operator::net operator(const string & amp; net _ proto txt){ create net(net _ proto txt); } net operator::net operator(const string & amp; net_prototxt,conststring & amptrained _ file){ create net(net _ proto txt); net _-& gt; CopyTrainedLayersFrom(trained _ file); } void net operator::create net(const string & amp; net _ proto txt){ # ifdefCPU _ only Caffe::set _ mode(Caffe::CPU); # else Caffe::set _ mode(Caffe::GPU); #endifnet_。 reset(newNet(net_prototxt,TEST)); CHECK _ EQ(net _-& gt; num_inputs(), 1)num_outputs(), 1)* input _ layer = net _-& gt; input _ blobs()[0]; Batch _ size _ = input layer->; num(); num _ channels _ = input _ layer->; Channel (); CHECK(num _ channels _ = = 3 | | num _ channels _ = = 1)width(),input _ layer-& gt; height()); //reshapestheoutputshapetofthedatatransformervectortop _ shape(4); top _ shape[0]= 1; top _ shape[ 1]= num _ channels _; Top _ shape[2]= Enter the geometry. Height; Top _ shape[3]= Enter the geometry. Width; This-> transformed_data_。 Top _ shape; } Blob * net operator::processImage(const string & amp; img_path,boolis _ color){//reshapethenetforteinputinput _ blob _ = net _-& gt; input _ blobs()[0]; input _ blob _-& gt; Reshape( 1, quantity channel _, input geometry _. Height, enter the geometry. Width); net _-& gt; shape(); readImageToBlob(img_path,0,is _ color); net _-& gt; forward prefilled(); return net _-& gt; output _ blobs()[0]; } Blob * net operator::process images(const vector & amp; img_paths,boolis _ color){ intimg _ num = img _ paths . size(); //reshapethenetfortheinputinput _ blob _ = net _-& gt; input _ blobs()[0]; input _ blob _-& gt; Reshape(img_num,num_channels_,input_geometry_。 Height, enter the geometry. Width); net _-& gt; shape(); for(inti = 0; iforward prefilled(); return net _-& gt; output _ blobs()[0]; } void net operator::readImageToBlob(const string & amp; img_path,intidx,boolis _ color){//readtheimageandresizetotargetsizecv::Matimg; intcv_read_flag=(is_color? CV _ load _ image _ color: CV _ load _ image _ gray scale); cv::Matcv _ img _ origin = cv::im read(img _ path,cv _ read _ flag); If (! cv _ img _ origin . data){ LOG(ERROR)0){ cv::resize(cv _ img _ origin,img,input _ geometry _); } else { img = cv _ img _ origin}//transformtheimagetoablusingdata transformer//createadatatransformer using defaulttransformationparameter(not transformation)data _ transformer _。 reset(new data transformer(transform _ param _,TEST)); Data converter _->; InitRand(); //settheoutputofdatatransformertoidximageoftheinputblobintoffset = input _ blob _-& gt; Offset (idx); This-> transformed_data_。 set _ CPU _ data(input _ blob _-& gt; mutable _ CPU _ data()+offset); //transformtheinputimagedata _ transformer _-& gt; Transform (img & amp; (this-> transformed _ data _);