【问题标题】:Caffe - Use ".caffemodel " file with Python->C++ file translated layersCaffe - 使用带有 Python->C++ 文件翻译层的“.caffemodel”文件
【发布时间】:2017-10-14 03:44:32
【问题描述】:

我有一个.prototxt 文件和一个.caffemodel 文件,用于我在py-caffe 中构建和训练的网络,使用自行开发的Python 层

后来,我为 C++ 版本的 Caffe 实现了相同的层。

问题是是否可以只更改 .prototxt 文件中的层类型(从 Python 到 C++ 的),同时使用相同的已经训练好的 .caffemodel 文件,以便加载和使用训练好的网络,这次是 C++ 层?

【问题讨论】:

  • 你的图层有内部参数吗?也就是说,您在"Python" 层实现中是否使用了self.blobs.add_blob()
  • 我没有。这是否意味着我可以实现与 Python 相同的 C++ 方法?实际上这是合乎逻辑的,因为该方案是在.prototxt 文件中定义的,而不是在层实现中
  • 如果你没有内部参数,那么层对.caffemodel的加载没有影响,你应该可以将它从python转换为cpp。我想你最好有一个 GPU 实现(.cu 实现...)
  • 如果在prototxt 文件中,我的python 层定义中有python_param 子句,会不会有问题?
  • 在prototxt文件中有layer的参数完全没有问题。查看其他层的 c++ 实现,了解 prtototxt 参数在 c++ 代码中是如何处理的。然后您可以考虑是要为您的网络定义参数还是使用python_param 并从prototxt 中用c++ 读取它。

标签: caffe pycaffe


【解决方案1】:

是的,当然可以。如果你有层参数,你应该在 caffe.proto 中添加一个新的 layer_param 消息,并在你的 cpp 层中相应地读取它们。
例如,PVANet caffe repository 具有原始 Python 提议层的实现。
正如您在caffe.proto 中看到的那样,添加了一条新消息来保存 Python 层的参数 -
// 存储 ProposalLayer 使用的参数的消息

message ProposalParameter {
  optional uint32 feat_stride = 1 [default = 16];
  optional uint32 base_size = 2 [default = 16];
  optional uint32 min_size = 3 [default = 16];
  repeated float ratio = 4;
  repeated float scale = 5;
  optional uint32 pre_nms_topn = 6 [default = 6000];
  optional uint32 post_nms_topn = 7 [default = 300];
  optional float nms_thresh = 8 [default = 0.7];
}

原始训练/测试prototxt包含以下层:

layer {
  name: 'proposal'
  type: 'Python'
  bottom: 'rpn_cls_prob_reshape'
  bottom: 'rpn_bbox_pred'
  bottom: 'im_info'
  top: 'rpn_rois'
  top: 'rpn_scores'
  include { phase: TRAIN }
  python_param {
    module: 'rpn.proposal_layer'
    layer: 'ProposalLayer'
    param_str: "{'feat_stride': 16, 'ratios': [0.333, 0.5, 0.667, 1, 1.5, 2, 3], 'scales': [2, 3, 5, 9, 16, 32]}"
  }
}

当新的 cpp 看起来像这样:

layer {
  name: 'proposal'
  type: 'proposal'
  bottom: 'rpn_cls_prob_reshape'
  bottom: 'rpn_bbox_pred'
  bottom: 'im_info'
  top: 'rpn_rois'
  top: 'rpn_scores'
  include { phase: TRAIN }
  proposal_param {
    feat_stride: 16
    ratios: 0.333, 0.5, 0.667, 1, 1.5, 2, 3
    scales: 2, 3, 5, 9, 16, 32
  }
}

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-20
    • 2018-02-15
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2021-01-11
    相关资源
    最近更新 更多