You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/08/22 02:50:31 UTC

[GitHub] ghgggg commented on issue #12267: some errors when extracting feature with mxnet c++ api in windows10 in gpu mode

ghgggg commented on issue #12267: some errors when  extracting feature with mxnet c++ api in windows10 in gpu mode
URL: https://github.com/apache/incubator-mxnet/issues/12267#issuecomment-414890946
 
 
   @ankkhedia  the example is here;
   
   
   #include <iostream>
   #include <fstream>
   #include <map>
   #include <string>
   #include <vector>
   #include "mxnet-cpp/MxNetCpp.h"
   #include <opencv2\opencv.hpp>
   using namespace std;
   using namespace mxnet::cpp;
   /*The global context, change them if necessary*/
   
   enum Mode
   {
   	GPU,
   	CPU
   };
   // Context global_ctx(kCPU,0);
   namespace MxSpace{
   class FeatureExtractor {
   
   public:
   	
   	FeatureExtractor(std::string symbol_file, std::string params_file, std::string layer_name)
   	{
   		GetFeatureSymbol(layer_name, symbol_file);
   		LoadParameters(params_file);
   	}
   	//FeatureExtractor() {
   	//	/*prepare the model, fill the pretrained parameters, get the mean image*/
   	//	GetFeatureSymbol();
   	//	LoadParameters();
   	//	//GetMeanImg();
   	//}
   
   
   
   	NDArray Forward(cv::InputArray _img)
   	{
   		cv::Mat img = _img.getMat();
   		
   		img.convertTo(img, CV_32F);
   		img = (img - 127.5) / 128;
   		//NDArray input(Shape(1, img.channels(), img.rows, img.cols), ctx, false);	//
   		
   		NDArray input(Shape(1, img.channels(), img.rows, img.cols), Context(kGPU, 0), false);
   
   		NDArray temp(Shape(1, img.channels(), img.rows, img.cols), Context(kCPU, 0),false); 
   		//NDArray::WaitAll();
   		auto ptr = temp.GetData();
   		std::vector<cv::Mat> chs(3);
   		for (int i = img.channels() - 1; i >= 0; i--)
   		{
   			chs[img.channels() - i - 1] = cv::Mat(img.size(), CV_32FC1, (float*)ptr + i*img.size().area());
   		}
   		cv::split(img, chs);
   		//input = temp.Copy(ctx);
   		input.SyncCopyFromCPU(temp.GetData(), temp.Size());
   		NDArray::WaitAll();
   		return Extract(input);
   
   	}
   private:
   	/*the mean image, get from the pretrained model*/
   	NDArray mean_img;
   	/*the following two maps store all the paramters need by the model*/
   	map<string, NDArray> args_map;
   	map<string, NDArray> aux_map;
   	Symbol net;
   	Executor *executor;
   	Context ctx = Context(kGPU, 0);
   	bool isbind = false;
   	/*Get the feature layer we want to extract*/
   	void GetFeatureSymbol() {
   		/*
   		* use the following to check all the layers' names:
   		* */
   		
   		net=Symbol::Load("./model/resnet50-symbol.json").GetInternals();
   		for(const auto & layer_name:net.ListOutputs()){
   		LG<<layer_name;
   		}
   		
   		//net = Symbol::Load("./model/resnet50-symbol.json")
   		//	.GetInternals()["fc1_output"];			//
   	}
   	void SetMode(Mode mode , int device_id=0)
   	{
   		switch (mode)
   		{
   		case CPU:
   			ctx = Context(kCPU, device_id);
   			break;
   		case GPU:
   			ctx = Context(kGPU, device_id);
   			break;
   		default:
   			LG << "Unknown mode.";
   			break;
   		}
   	}
   	void GetFeatureSymbol(std::string layer_name , std::string symbol_file = "./model/resnet50-symbol.json")
   	{
   
   		net = Symbol::Load(symbol_file).GetInternals();	// return all output
   		net = layer_name.empty() ? net : net[layer_name];
   	}
   	/*Fill the trained paramters into the model, a.k.a. net, executor*/
   	void LoadParameters(std::string params_file = "./model/resnet50-0050.params")
   	{
   		map<string, NDArray> paramters;
   		NDArray::Load(params_file, 0, &paramters);
   		for (const auto &k : paramters) {
   			if (k.first.substr(0, 4) == "aux:") {
   				auto name = k.first.substr(4, k.first.size() - 4);
   				aux_map[name] = k.second.Copy(ctx);
   			}
   			if (k.first.substr(0, 4) == "arg:") {
   				auto name = k.first.substr(4, k.first.size() - 4);
   				args_map[name] = k.second.Copy(ctx);
   			}
   		}
   		/*WaitAll is need when we copy data between GPU and the main memory*/
   		NDArray::WaitAll();
   	}
   	void GetMeanImg() {
   		mean_img = NDArray(Shape(1, 3, 112, 112), ctx, false);
   		mean_img.SyncCopyFromCPU(
   			NDArray::LoadToMap("./model/mean_224.nd")["mean_img"].GetData(),
   			1 * 3 * 224 * 224);
   		NDArray::WaitAll();
   	}
   	
   	NDArray Extract(NDArray data)
   	{
   		
   		args_map["data"] = data;
   		
   		if (!isbind)
   		{
   			
   			executor = net.SimpleBind(ctx, args_map, map<string, 
   				NDArray>(), map<string, OpReqType>(), aux_map);
   			isbind = true;
   		}
   														
   		executor->Forward(false);
   		/*print out the features*/
   		NDArray array = executor->outputs[0].Copy(ctx);
   		std::cout << array.Size() << std::endl;
   		NDArray::WaitAll();
   		for (int i = 0; i < 1024; ++i) {
   			cout << array.At(0, i) << ",";
   		}
   		//cout << endl;
   		return array;
   	}
   public:
   	
   };
   
   
   };
   int main() {
   	/*
   	* get the data from a binary file ./img.data
   	* this file is generated by ./prepare_data_with_opencv
   	* it stores 2 pictures in NDArray format
   	*
   	*/
   	cv::Mat img = cv::imread("d:\\1.jpg");
   	cv::Mat mtest;
   	cv::resize(img, mtest, cv::Size(112, 112));
   	MxSpace::FeatureExtractor fe("./model/resnet50-symbol.json","./model/resnet50-0050.params","fc1_output");
   
   	fe.Forward(mtest);
   	//getchar();
   	system("pause");
   	return 0;
   }
   
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services