Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

neural network - Caffe: how to get the phase of a Python layer?

I created a "Python" layer "myLayer" in caffe, and use it in the net train_val.prototxt I insert the layer like this:

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!
}

Now, my layer only participates in the TRAINing phase of the net,
how can I know that in my layer's setup function??

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

PS,
I posted this question on "Caffe Users" google group as well. I'll udpdate if anything pops there.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As pointed out by galloguille, caffe is now exposing phase to the python layer class. This new feature makes this answer a bit redundant. Still it is useful to know about the param_str in caffe python layer for passing other parameters to the layer.

Original answer:

AFAIK there is no trivial way of getting the phase. However, one can pass arbitrary parameters from the net prototxt to python. This can be done using the param_str parameters of the python_param.
Here's how it's done:

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

In python, you get param_str in the layer's setup function:

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...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...