簡體   English   中英

Caffe:如何獲得Python層的階段?

[英]Caffe: how to get the phase of a Python layer?

我在caffe中創建了一個"Python"圖層"myLayer" ,並在net train_val.prototxt使用它我插入這樣的圖層:

layer {
  name: "my_py_layer"
  type: "Python"
  bottom: "in"
  top: "out"
  python_param {
    module: "my_module_name"
    layer: "myLayer"
  }
  include { phase: TRAIN } # THIS IS THE TRICKY PART!
}

現在,我的圖層只參與網絡的TRAIN ing階段,
我怎么知道在我的圖層的setup功能?

class myLayer(caffe.Layer):
  def setup(self, bottom, top):
     # I want to know here what is the phase?!!
  ...

PS,
我在“Caffe Users”谷歌小組上發布了這個問題。 如果有什么東西在那里,我會更新。

正如galloguille所指出的,caffe現在將phase暴露給python層類。 這個新功能使這個答案有點多余。 了解caffe python層中用於將其他參數傳遞給圖層的param_str仍然很有用。

原始答案:

AFAIK沒有瑣碎的方式來獲得階段。 但是,可以將任意參數從net prototxt傳遞給python。 這可以用做param_str的參數python_param
以下是它的完成方式:

layer {
  type: "Python"
  ...
  python_param {
    ...
    param_str: '{"phase":"TRAIN","numeric_arg":5}' # passing params as a STRING

在python中,您可以在圖層的setup函數中獲得param_str

import caffe, json
class myLayer(caffe.Layer):
  def setup(self, bottom, top):
    param = json.loads( self.param_str ) # use JSON to convert string to dict
    self.phase = param['phase']
    self.other_param = int( param['numeric_arg'] ) # I might want to use this as well...

這是一個非常好的解決方法,但如果您只想將phase作為參數傳遞,那么現在您可以訪問階段作為圖層的屬性。 此功能僅在6天前合並https://github.com/BVLC/caffe/pull/3995

具體提交: https//github.com/BVLC/caffe/commit/de8ac32a02f3e324b0495f1729bff2446d402c2c

使用此新功能,您只需使用屬性self.phase 例如,您可以執行以下操作:

class PhaseLayer(caffe.Layer):
"""A layer for checking attribute `phase`"""

def setup(self, bottom, top):
    pass

def reshape(self, bootom, top):
    top[0].reshape()

def forward(self, bottom, top):
    top[0].data[()] = self.phase

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM