2017-05-16 38 views
-1

我想添加一個python層,它可以即時爲INfoGainLoss Layer計算概率矩陣H。我已經編寫了用於創建該矩陣的程序,方法是計算一個圖像的每個類的概率並將其保存到.binaryproto文件中。我真的很感激,如果你給我一點提示,我該如何編寫一個創建這個矩陣的python層並將其作爲第三個參數發送給InfoGainLoss層?我在這裏畫了一個示意圖,它是正確的嗎?如果是,如何爲此編寫python圖層?我已經在線閱讀了一些代碼,但仍然對setup,reshapeforward函數有困惑。如何創建即時python圖層來計算caffe中的類別權重?

[1]: https://i.stack.imgur.com/jNu 7a.png

回答

1

你的Python層非常相似的輸入層:它沒有反向傳播,這使得它易於實現。有關更多詳細信息,請參閱this thread

你的層期望"label"底部,並製作"H"矩陣作爲頂部:

層{ 名: 「classWeightH」 底部: 「標籤」 頂部: 「H」 類型: 「蟒」 python_param { 模塊:其中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 
+1

非常感謝您的支持和幫助。你總是樂於助人。 –

相關問題