通过字符串设置Caffe网络参数

3
我想要做的是:
我加密了".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);

非常感谢您。
2个回答

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/

0

从文本格式加载网络模型(而不是使用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));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接