Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add arguments for PythonLayer #2767

Closed
wants to merge 1 commit into from
Closed

Conversation

semisight
Copy link

This pull request adds a way for PythonLayers (#1703) to accept arguments through prototxt files.

Instead of loading files or other processing in setup(), this allows one to pass parameters through the 'arguments' string in PythonParameter. The receiver can then process the string however they choose: load through json.loads(), comma separated values, etc.

This pull request is a simpler alternative to #2001, without exception handling changes.

Example class:

class MultLayer(caffe.Layer):
    def setup(self, bottom, top):
        self.mult_factor = int(self.arguments)

    def reshape(self, bottom, top):
        top[0].reshape(bottom[0].num, bottom[0].channels, bottom[0].height,
            bottom[0].width)

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

    def backward(self, top, propagate_down, bottom):
        if propagate_down[0]:
            bottom[0].diff[...] = self.mult_factor * top[0].diff

Example addition to prototxt:

layer {
  name: "python1"
  type: "Python"
  bottom: "prev1"
  top: "curr1"
  python_param {
    module: "multlayer"
    layer: "MultLayer"
    arguments: "10"
  }
}

@BlGene
Copy link
Contributor

BlGene commented Jul 15, 2015

A simple parameter string seems to be a good solution. I would still add a comment recommending a particular way of passing parameters ( JSON seems suitable ) as the default, just to minimize unnecessary work of dealing with different formats.

@longjon does this sufficiently solve the second last point of #1376?

@@ -248,6 +252,7 @@ BOOST_PYTHON_MODULE(_caffe) {
bp::return_internal_reference<>()))
.def("setup", &Layer<Dtype>::LayerSetUp)
.def("reshape", &Layer<Dtype>::Reshape)
.add_property("arguments", &Layer_arguments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is adding an arguments property to all Layers and not just PythonLayer.

@shelhamer
Copy link
Member

Thanks for contributing your own version of parameter passing to Python layers, but I'm closing this in favor of the original change by @tnarihi. The change here incorrectly introduces a new property for all layers.

See #2871 for the cherry-picking of the PythonLayer parameter string part of #2001 alone.

@shelhamer shelhamer closed this Aug 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants