繁体   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