感谢Dan的评论,我发现一个可行的解决方案。因为有关于如何从bp::object
提取对象的一些有趣花絮,等我会复制大部分在这里
// Net constructor
shared_ptr<Net<Dtype> > Net_Init(string param_file, int phase,
const int level, const bp::object& stages,
const bp::object& weights_file) {
CheckFile(param_file);
// Convert stages from list to vector
vector<string> stages_vector;
if (!stages.is_none()) {
for (int i = 0; i < len(stages); i++) {
stages_vector.push_back(bp::extract<string>(stages[i]));
}
}
// Initialize net
shared_ptr<Net<Dtype> > net(new Net<Dtype>(param_file,
static_cast<Phase>(phase), level, &stages_vector));
// Load weights
if (!weights_file.is_none()) {
std::string weights_file_str = bp::extract<std::string>(weights_file);
CheckFile(weights_file_str);
net->CopyTrainedLayersFrom(weights_file_str);
}
return net;
}
BOOST_PYTHON_MODULE(_caffe) {
bp::class_<Net<Dtype>, shared_ptr<Net<Dtype> >, boost::noncopyable >("Net",
bp::no_init)
.def("__init__", bp::make_constructor(&Net_Init,
bp::default_call_policies(), (bp::arg("network_file"), "phase",
bp::arg("level")=0, bp::arg("stages")=bp::object(),
bp::arg("weights_file")=bp::object())))
}
生成的签名是:
__init__(boost::python::api::object, std::string network_file, int phase,
int level=0, boost::python::api::object stages=None,
boost::python::api::object weights_file=None)
而且我可以用它喜欢: https://github.com/BVLC/caffe/pull/3863
莫非:
上拉请求可在这里的完整代码你在构造函数def中使用[args](http://www.boost.org/doc/libs/1_55_0/libs/python/doc/v2/args.html#args-spec)类?这应该让你生成'make_constructor'所用的关键字表达式。 –
有关如何使[原始构造函数](https://wiki.python.org/moin/boost.python/HowTo#A.22Raw.22_constructor)位于Python Wiki上的说明。如果在make_constructor(&Net_Init,default_call_policies(),(arg(“param_file”),arg(“phase”),arg(“stages”)= object(),arg(“level”)= 0))''中设置 –
, - 阶段是对象,所以你可以处理None。 –