带有可选参数的 Boost.Python 构造函数
Boost.Python constructor with optional arguments
我想使用 Boost.Python 为带有可选参数的 C++ 构造函数创建一个 Python 包装器。我希望 Python 包装器的行为如下:
class Foo():
def __init__(self, filename, phase, stages=None, level=0):
"""
filename -- string
phase -- int
stages -- optional list of strings
level -- optional int
"""
if stages is None:
stages = []
# ...
如何使用 Boost.Python 执行此操作?我不知道如何使用make_constructor
来做到这一点,而且我不知道如何使用raw_function
制作构造函数。有没有比这更好的文档?
我的具体问题是尝试向这两个构造函数添加两个可选参数(阶段和级别):
https://github.com/BVLC/caffe/blob/rc3/python/caffe/_caffe.cpp#L76-L96
多亏了丹的评论,我找到了一个有效的解决方案。我将在这里复制大部分内容,因为有一些关于如何从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)
我可以像这样使用它:
net = caffe.Net('network.prototxt', weights_file='weights.caffemodel',
phase=caffe.TEST, level=1, stages=['deploy'])
此处的拉取请求中提供了完整代码:https://github.com/BVLC/caffe/pull/3863
相关文章:
- 我应该包含什么来制作 boost.python 扩展?
- C++ 和 Boost.Python - 如何将变量公开给 python 并在循环中更新它?
- 如何将来自 Boost.Python 的map_indexing_suite与自定义而不是标准对象一起使用?
- Boost Python Numpy - 要初始化的未定义引用
- Boost.Python :C++模板类型匹配的嵌套命名空间
- Boost.Python 列出了所有公开的类和属性
- Boost.Python 在静态库方面失败
- 在cpp中使用boost-python的python代码是否进行动态内存分配
- Boost.Python 和导入 dll,"The specified module could not be found"
- 无法将__str__特殊方法与Boost::Python接口
- 编译时出现Boost.python链接错误
- 使用 Boost/Python 的未定义符号 - 复杂
- Boost.Python 和C++导入到 Python 3 时编译库错误
- 两个并发的 Python 进程可以在 Boost Python 中运行吗?
- 通过 Boost Python 将 Python 函数转换为 C++,用作回调
- 编译在 Python 代码内部调用的 C++ 代码时出错,使用 Boost Python
- Boost.Python.ArgumentError: World.set(World, str) 中的 Python
- 使用 boost python 从 c++ 为 python 中的类成员变量赋值
- Boost Python 不断链接 Python27 库
- 使用 python3 为 msvc 构建 boost python - 链接器错误