【问题标题】:Set caffe net parameters via string通过字符串设置 caffe net 参数
【发布时间】:2016-08-09 16:24:11
【问题描述】:

我想做的是:

我加密了“.prototxt”和“.caffemodel”文件,所以文件不可读,参数不可见。在我的程序中,我解密文件并将结果存储为字符串。但现在我需要在我的 caffe 网络中设置图层。

有没有一种方法可以使用我的字符串中的参数设置 caffe 网络层?训练网络中的层也一样吗?与下面的源代码相媲美的东西(我知道这个源代码行不通)?

shared_ptr<Net<float> > net_;
string modelString;
string trainedString;

//Decryption stuff

net_.reset(new Net<float>(modelString, TEST));
net_->CopyTrainedLayersFrom(trainedString);

非常感谢。

【问题讨论】:

    标签: encryption caffe


    【解决方案1】:

    您可以使用 NetParameter 类的 Protocol Buffer API 直接初始化 NetParameter 类(您需要包含 caffe/proto/caffe.pb.h):

    bool ParseFromString(const string& data);
    

    然后使用下面的构造函数来初始化一个 Net 类:

    explicit Net(const NetParameter& param, const Net* root_net = NULL);
    

    以及复制权重:

    void CopyTrainedLayersFrom(const NetParameter& param);
    

    需要注意的是,上述方法要求字符串变量包含二进制格式的 protobuffer 而不是文本格式。虽然 Caffe 输出的 caffemodel 已经是二进制格式,但您也必须将 prototxt 文件转换为二进制格式,但您可以使用 protoc 命令行程序结合 --encode 标志来完成。

    有关更多信息,我建议您查看 Protocol-Buffer 的网站:https://developers.google.com/protocol-buffers/

    【讨论】:

      【解决方案2】:

      从文本格式加载网络模型(不使用protoc转换)可以通过以下方式完成:

      #include <google/protobuf/text_format.h>
      // [...]
      NetParameter net_parameter;
      bool success = google::protobuf::TextFormat::ParseFromString(model, &net_parameter);
      if (success){
         net_parameter.mutable_state()->set_phase(TEST);
         net_.reset(new Net<float>(net_parameter));
      }
      

      【讨论】:

        猜你喜欢
        • 2017-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-12
        • 1970-01-01
        • 2012-03-21
        • 2012-03-11
        相关资源
        最近更新 更多