Caffe:如何获得Python层的相位?
Caffe: how to get the phase of a Python layer?
我在caffe中创建了一个"Python"
层"myLayer"
,并在网络中使用它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" google group 上发布了这个问题。如果那里弹出任何内容,我会更新。
正如 所指出的,caffe 现在将 phase
暴露给 python 层 class。这个新功能使这个答案有点多余。了解 caffe python 层中的 param_str
对于将其他参数传递给该层仍然很有用。
原回答:
据我所知,没有简单的方法可以获取相位。但是,可以将任意参数从 net prototxt 传递到 python。这可以使用 python_param
.
的 param_str
参数来完成
操作方法如下:
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
我在caffe中创建了一个"Python"
层"myLayer"
,并在网络中使用它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" google group 上发布了这个问题。如果那里弹出任何内容,我会更新。
正如 phase
暴露给 python 层 class。这个新功能使这个答案有点多余。了解 caffe python 层中的 param_str
对于将其他参数传递给该层仍然很有用。
原回答:
据我所知,没有简单的方法可以获取相位。但是,可以将任意参数从 net prototxt 传递到 python。这可以使用 python_param
.
的 param_str
参数来完成
操作方法如下:
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