关于python中cython的一个请教

疯狂的阿木木 发布于 2015/09/26 20:59
阅读 1K+
收藏 0

调试一个开源项目,处理setup.py时,总是有错误,困扰了一周多,希望有知道的朋友能给点提示,谢谢。

开源项目链接:https://github.com/seanbell/intrinsic

错误提示为:

相关的代码  setup.py

from distutils.core import setup
from distutils.extension import Extension
try:
    from Cython.Build import cythonize
    import numpy
except ImportError:
    print "You must have Cython >=0.17 and NumPy to build!"
    import sys
    sys.exit(1)

setup(
    ext_modules=cythonize(Extension(
        'krahenbuhl2013',
        sources=[
            'krahenbuhl2013.pyx',
            "src/densecrf.cpp",
            "src/labelcompatibility.cpp",
            "src/pairwise.cpp",
            "src/permutohedral.cpp",
            "src/unary.cpp",
            "src/util.cpp",
            "src/densecrf_wrapper.cpp",
        ],
        include_dirs=[
            numpy.get_include(),
            "include",
            "/usr/include/eigen3",
        ],
        language="c++",
    )),
)



krahenbuhl2013.pyx 代码如下:

# distutils: sources = src/densecrf_wrapper.cpp

cimport numpy as np

cdef extern from "include/densecrf_wrapper.h":
    cdef cppclass DenseCRFWrapper:
        DenseCRFWrapper(int, int) except +
        void set_unary_energy(float*)
        void add_pairwise_energy(float*, float*, int)
        void map(int, int*)
        int npixels()
        int nlabels()

cdef class DenseCRF:
    cdef DenseCRFWrapper *thisptr

    def __cinit__(self, int npixels, int nlabels):
        self.thisptr = new DenseCRFWrapper(npixels, nlabels)

    def __dealloc__(self):
        del self.thisptr

    def set_unary_energy(self, float[:, ::1] unary_costs):
        if (unary_costs.shape[0] != self.thisptr.npixels() or
                unary_costs.shape[1] != self.thisptr.nlabels()):
            raise ValueError("Invalid unary_costs shape")

        self.thisptr.set_unary_energy(&unary_costs[0, 0])

    def add_pairwise_energy(self, float[:, ::1] pairwise_costs,
                            float[:, ::1] features):
        if (pairwise_costs.shape[0] != self.thisptr.nlabels() or
                pairwise_costs.shape[1] != self.thisptr.nlabels()):
            raise ValueError("Invalid pairwise_costs shape")
        if (features.shape[0] != self.thisptr.npixels()):
            raise ValueError("Invalid features shape")

        self.thisptr.add_pairwise_energy(
            &pairwise_costs[0, 0],
            &features[0, 0],
            features.shape[1]
        )

    def map(self, int n_iters=10):
        import numpy as np
        labels = np.empty(self.thisptr.npixels(), dtype=np.int32)
        cdef int[::1] labels_view = labels
        self.thisptr.map(n_iters, &labels_view[0])
        return labels



加载中
0
hyperfox
hyperfox
cython能直接编译cpp文件吗?我们这做这个的都是先正常编c++出一个so库,然后再用cython去包装一下
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部
返回顶部
顶部