BVLC / caffe

Caffe: a fast open framework for deep learning.
http://caffe.berkeleyvision.org/
Other
33.98k stars 18.72k forks source link

Can I load model in memory? #6944

Closed huyutao3550346 closed 4 years ago

huyutao3550346 commented 4 years ago

Hello Everyone. In the example of "classification.cpp", I saw a way to load model by : net_.reset(new Net(modelfile, TEST)); net->CopyTrainedLayersFrom(trained_file); But it load model by file path.

Now I package the models and want load model file and trained file by binary file.

what should I do?

I have a not so good idea: 1.build a temp folder 2.unpack the models to folder 3.load model by file path in folder 4.delete the temp folder

Is there any better idea? thank you.

troyliu0105 commented 4 years ago

Binary file? you mean "in memory"? trained_file is already binary file.

huyutao3550346 commented 4 years ago

@troyliu0105 Yes, load model in memory to be exact. I have found the way to load model in memory; there are the codes:

char* model_buffer;   //load model_file as stream
int model_length;      //model_file's length
char* trained_buffer; //load trained_file as stream 
int trained_length;    //trained_file's length

caffe::NetParameter net_txt;
caffe::NetParameter proto;

string model_str = string(model_buffer,model_buffer+model_length);
istringstream model_stream(model_str);
IstreamInputStream * me_input = new IstreamInputStream((std::istream *)(&model_stream));
ZeroCopyInputStream* coded_input = me_input;
google::protobuf::TextFormat::Parse(coded_input, &net_txt);
delete me_input;

string trained_str = string(trained_buffer,trained_buffer + trained_length);
istringstream trained_stream(trained_str);
IstreamInputStream * net_input = new IstreamInputStream((std::istream *)(&trained_stream));
CodedInputStream* coded_input_p = new CodedInputStream(net_input);
coded_input_p->SetTotalBytesLimit(INT_MAX, 536870912);
proto.ParseFromCodedStream(coded_input_p);
delete net_input;
delete coded_input_p;

net_.reset(new Net<float>(net_txt));
net_->CopyTrainedLayersFrom(proto);