caffe在内存中初始化模型 c++

#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); //加载权重

   //网络已经初始化好了!

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值