【问题标题】:How to create on-the-fly python layer for calculating class weights in caffe?如何创建即时python层来计算caffe中的类权重?
【发布时间】:2017-10-15 22:17:56
【问题描述】:

我想添加一个 python 层,用于即时计算 H 的概率矩阵 INfoGainLoss Layer。我已经编写了通过计算一个图像的每一类的概率并将其保存到.binaryproto 文件中来创建这个矩阵的程序。如果你给我一些提示,我真的很感激我如何编写一个 python 层来创建这个矩阵并将它作为第三个参数发送到 InfoGainLoss 层?我在这里画了一个示意图,对吗?如果是,如何为此编写python层?我已经在网上阅读了一些代码,但仍然对setupreshapeforward 函数感到困惑。

7a.png

【问题讨论】:

    标签: python machine-learning neural-network deep-learning caffe


    【解决方案1】:

    您的 Python 层与输入层非常相似:它没有反向传播,因此很容易实现。详情请见this thread

    您的层需要"label" 底部,并生成"H" 矩阵作为顶部:

    层{ 名称:“classWeightH” 底部:“标签” 顶部:“H” 类型:“Python” python_param { module: # python 代码所在的文件名 层:“classWeightHLayer” } }

    python 代码应该类似于:

    import sys, os, numpy as np
    sys.path.insert(0, os.environ['CAFFE_ROOT']+'/python')
    import caffe
    class classWeightHLayer(caffe.Layer):
      def setup(self,bottom,top):
        assert len(bottom)==1, "expecting exactly one input"
        assert len(top)==1, "producing exactly one output"
        # you might want to get L - the number of labels as a parameter...
    
      def reshape(self,bottom,top):
        top[0].reshape(1,1,L,L) # reshape the output to the size of H
    
      def forward(self,bottom,top):         
        labels = bottom[0].data
        H = np.zeros((1,1,L,L), dtype='f4') 
        # do your magic here...
        top[0].data[...] = H    
    
      def backward(self, top, propagate_down, bottom):
        # no back-prop for input layers
        pass
    

    【讨论】:

    • 非常感谢您在这里的所有支持和帮助。你总是乐于助人。
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 2017-03-29
    • 1970-01-01
    • 2015-07-31
    • 2018-01-20
    • 2017-11-28
    相关资源
    最近更新 更多