【问题标题】:Adding a custom python layer in caffe2在 caffe2 中添加自定义 python 层
【发布时间】:2018-01-26 03:29:07
【问题描述】:

在 caffe 中添加 python 层相当简单(创建一个继承自 caffe.layer 的子类并添加四个基本方法,如 herehere 所述。但是,在 caffe2 中添加自定义 python 层不是对我来说很简单。有人可以解释一下在 caffe2 中添加 python 层的过程吗?

【问题讨论】:

标签: caffe pycaffe caffe2


【解决方案1】:

首先,您必须将新层实现为 Python 类,如示例中所示。在这种情况下,它只以相反的顺序输出输入张量:

class ReverseOrderOp(object):
    def forward(self, inputs, outputs):
        blob_out = outputs[0]

        blob_out.reshape(inputs[0].shape)
        blob_out.data[...] = inputs[0].data[::-1]

然后,您可以使用 model.net.Python 将新层添加到模型中:

model = ModelHelper(name="test")

l = np.asarray([0,1,2,3])
workspace.FeedBlob('l', l.astype(np.float32))

model.net.Python(ReverseOrderOp().forward)(
    ['l'], ['out'], name='ReverseOrder'
)
workspace.RunNetOnce(model.net)
print(workspace.FetchBlob('out'))

输出应该是[ 3. 2. 1. 0.]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2014-08-25
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多