#include <caffe/caffe.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <string>
#include <utility>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <boost/property_tree/xml_parser.hpp>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
using namespace boost::property_tree;
using google::protobuf::io::FileInputStream;
using google::protobuf::io::FileOutputStream;
using google::protobuf::io::IstreamInputStream;
using google::protobuf::io::OstreamOutputStream;
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::ZeroCopyOutputStream;
using google::protobuf::io::CodedOutputStream;
using google::protobuf::Message;
using std::ifstream;
int main(int argc, char *argv[])
{
caffe::NetParameter proto;
string filename = "./train.caffemodel";
caffe::NetParameter net_txt;
string prototxt = "./deploy.prototxt";
//将模型文件读到string fileContent中,模拟内存中的模型文件
ifstream fin;
fin.open(filename);
string fileContent;
if (fin.is_open())
{
ostringstream temp;//字符串流,提供对于string对象的写的功能。
temp<<fin.rdbuf();//往字符串流中写
fileContent= temp.str();
fin.close();
}
//将网络描述文件读到string netContent中,模拟内存中的网络描述文件
ifstream tfin;
tfin.open(prototxt);
string netContent;
if (tfin.is_open())
{
ostringstream txttemp;//字符串流,提供对于string对象的写的功能。
txttemp<<tfin.rdbuf();//往字符串流中写
netContent= txttemp.str();
tfin.close();
}
//读取网络描述文件参数
istringstream filenet(netContent);
IstreamInputStream * me_input = new IstreamInputStream((std::istream *)(&filenet));
ZeroCopyInputStream* coded_input = me_input;
google::protobuf::TextFormat::Parse(coded_input, &net_txt);
//读取模型文件参数
istringstream netpare(fileContent);
IstreamInputStream * net_input = new IstreamInputStream((std::istream *)(&netpare));
CodedInputStream* coded_input_p = new CodedInputStream(net_input);
coded_input_p->SetTotalBytesLimit(INT_MAX, 536870912);
proto.ParseFromCodedStream(coded_input_p);
boost::shared_ptr<Net<float>> _net;
_net.reset(new Net<float>(net_txt)); //定义一个网络
_net->CopyTrainedLayersFrom(proto); //加载权重
//网络已经初始化好了!
}