microsoft / caffe

Caffe on both Linux and Windows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

caffe.exe exit with error on customized Python Layer

IsaacYangSLA opened this issue · comments

Tested on Windows 7 Prof. Release build of BVLC/Windows branch. PythonSupport is true. Either CPU only or GPU.

Run caffe.exe with proper PYTHONPATH (including PYTHON DLLs, Lib, Release\pycaffe and others).

caffe.exe train --solver=solver.prototxt

solver.prototxt points to net.prototxt.

When net.prototxt includes the following, among other layers,

layer {
   name: "blank_square"
   type: "Python"
   bottom: "scaled"
   top: "scaled"
   python_param {
     module: "digits_python_layers"
     layer: "BlankSquareLayer"
   }
}

And digits_python_layers.py is

import caffe
import random

class BlankSquareLayer(caffe.Layer):

    def setup(self, bottom, top):
        print('BSL setup')
        assert len(bottom) == 1,            'requires a single layer.bottom'
        assert bottom[0].data.ndim >= 3,    'requires image data'
        assert len(top) == 1,               'requires a single layer.top'

    def reshape(self, bottom, top):
        # Copy shape from bottom
        top[0].reshape(*bottom[0].data.shape)

    def forward(self, bottom, top):
        # Copy all of the data
        top[0].data[...] = bottom[0].data[...]
        # Then zero-out one fourth of the image
        height = top[0].data.shape[-2]
        width = top[0].data.shape[-1]
        h_offset = random.randrange(height/2)
        w_offset = random.randrange(width/2)
        top[0].data[...,
                h_offset:(h_offset + height/2),
                w_offset:(w_offset + width/2),
                ] = 0

    def backward(self, top, propagate_down, bottom):
        pass


The following code in layer_factory.py throws bp::error_already_set exception

  Py_Initialize();
  try {
    bp::object module = bp::import(param.python_param().module().c_str());
    bp::object layer = module.attr(param.python_param().layer().c_str())(param);
    return bp::extract<shared_ptr<PythonLayer<Dtype> > >(layer)();
  } catch (bp::error_already_set) {
    PyErr_Print();
    throw;
  }

The line that fails is module.attr(param.python_param().layer().c_str())(param);

On console, the error message is
TypeError: No to_python (by-value) converter found for C++ type: class caffe::LayerParameter

Same solver.prototxt works OK in the following code.

import caffe
solver = caffe.SGDSolver('solver.prototxt')
out = solver.solve()
print(out)

Caffe build in Linux also works OK.

I have the same question, and I am not clear how to figure this problem, could you please give me some advice?