cython双重免费或腐败

时间:2017-08-30 14:20:16

标签: python cython glibc

我最近在cython中编写了一个python函数来加速它。我不太了解C,看起来我忘了什么,因为我在执行代码时遇到错误。

这是我在histogram.pyx中编写的Cython函数代码:

# -*- coding: utf-8 -*-
import numpy as np
import pdb
import skimage.transform as tf
import cv2
from skimage.exposure import rescale_intensity as rsc
cimport numpy as np
cimport cython
from libc.math cimport floor,ceil

@cython.boundscheck(False)
@cython.wraparound(False)
def histogram(np.ndarray[char, ndim=2] image1,np.ndarray[char, ndim=2] image2, transfo):
    cdef int py,px
    cdef int max1 = np.amax(image1)
    cdef int max2 = np.amax(image2)
    cdef int lar = image1.shape[0]
    cdef int lon = image1.shape[1]
    cdef np.ndarray[np.float64_t, ndim=2] H = np.zeros((max2+1,max1+1),dtype = np.float64)
    cdef int larc = image2.shape[0]
    cdef int lonc = image1.shape[1]
    cdef np.ndarray[np.float64_t, ndim =3] transformed = tf.warp_coords(transfo,(larc,lonc))
    cdef double qy,qx,dy,dx,qytemp,qxtemp
    cdef int qx0,qy0,x,y,qxi,qyj
    cdef double w[4]
    cdef char rk,f
    cdef int utile = 2
    for py in range(larc):
        for px in range(lonc):
            f = image2[py,px]
            qy = transformed[0,py,px]
            qx = transformed[1,py,px]
            qxtemp = floor(qx)
            qytemp = floor(qy)
            qx0 =int(qx)
            qy0 =int(qy)
            dx = qx-qxtemp
            dy = qy-qytemp
            w[0]=(1-dx)*(1-dy)
            w[1]=(1-dx)*dy
            w[2]=dx*(1-dy)
            w[3]=dx*dy
            for x in [0,1]:
                for y in [0,1]:
                    qxi = qx0+x
                    qyj = qy0+y
                    if 0<=qxi and qxi<lon and 0<=qyj and qyj<lar:
                        rk= image1[qyj,qxi]
                        H[f,rk]+=w[y+2*x]
                    else:
                        H[f,0]+= w[y+2*x]    
    return H

似乎该功能有效。我用这个setup.py编译代码:

from distutils.core import setup
from Cython.Build import cythonize


setup(ext_modules = cythonize('histogram.pyx'))

然后我尝试在这个脚本中使用它:

from histogram import histogram
import numpy as np
import cv2
from skimage.exposure import rescale_intensity as rsc
import skimage.transform as tf
import pdb


def mutual_information(image1,image2,transfo):
    i =0
    h = histogram(image1,image2,transfo)
    f,r = np.shape(h)
    hf = np.sum(h,axis=1)
    hr = np.sum(h,axis=0)
    n = np.sum(h)
    somme = 0.
    hfr = np.nditer(h,flags=['multi_index'])
    while not hfr.finished:
        if hfr[0] != 0:
            i,j = hfr.multi_index 
            temp= hfr[0]*np.log2((n*hfr[0])/(hf[i]*hr[j]))
            somme+= temp
            #pdb.set_trace()
        hfr.iternext()
    i = somme
    pdb.set_trace()
    return i

img = cv2.imread('frame_18.png',cv2.IMREAD_GRAYSCALE)
im2 = cv2.imread('frame_18.png',cv2.IMREAD_GRAYSCALE)
p10, p90 = np.percentile(img, (10, 90))

img = rsc(img,in_range=(p10,p90))
im2 = rsc(im2,in_range=(p10,p90))
tform = tf.AffineTransform(translation=(+0.,0.),scale = (1.,1.),rotation =0.22,shear =0.0)

def mi(t):
    global img
    global im2
    tform = tf.AffineTransform(translation = (t[0],t[1]),scale=(t[2],t[3]),rotation =t[4],shear = t[5])
    x =-mutual_information(img,im2,tform)
    return x

h = histogram (img,im2,tform)
pdb.set_trace()
print(mi([0.,0.,1.,1.5,0.22,0.]))

当我调用函数直方图(image1,image2,transfo)并将其注册到局部变量时没有问题。 但是当我使用它然后退出(例如返回)时,我得到以下C错误(脚本仅在最后一个pdb.set_trace()崩溃,并且打印了mi函数的结果):

*** glibc detected *** python: double free or corruption (out): 0x0000000002077ff0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3110475dee]
/lib64/libc.so.6[0x3110478c80]
/cea/home/dev_tse/dev_tse/applications/python/packages/lib/python2.7/site-packages/numpy/core/multiarray.so(+0x1e2bf)[0x2ad8b632f2bf]
/cea/home/dev_tse/dev_tse/applications/python/packages/lib/python2.7/site-packages/numpy/core/multiarray.so(+0x211ce)[0x2ad8b63321ce]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x9b88b)[0x2ad8af1b888b]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x9b88b)[0x2ad8af1b888b]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x65c6a)[0x2ad8af182c6a]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x6288b)[0x2ad8af17f88b]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x852a0)[0x2ad8af1a22a0]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(_PyObject_GenericSetAttrWithDict+0xaa)[0x2ad8af1c06ca]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyObject_SetAttr+0x87)[0x2ad8af1c0147]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x1835)[0x2ad8af2245a5]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x8bc)[0x2ad8af22da3c]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x762a)[0x2ad8af22a39a]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x784a)[0x2ad8af22a5ba]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x8bc)[0x2ad8af22da3c]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalCode+0x19)[0x2ad8af22db39]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyRun_FileExFlags+0x8a)[0x2ad8af251f9a]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyRun_SimpleFileExFlags+0xd5)[0x2ad8af253375]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(Py_Main+0xc61)[0x2ad8af269921]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x311041ed1d]
python[0x400791]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:15 12798505                           /ccc/products1/ccc_python/RedHat-6-x86_64/2.7.12_2017.01.2/bin/python2.7
00600000-00601000 r--p 00000000 00:15 12798505                           /ccc/products1/ccc_python/RedHat-6-x86_64/2.7.12_2017.01.2/bin/python2.7
00601000-00602000 rw-p 00001000 00:15 12798505                           /ccc/products1/ccc_python/RedHat-6-x86_64/2.7.12_2017.01.2/bin/python2.7
00f88000-0269b000 rw-p 00000000 00:00 0                                  [heap]
3110000000-3110020000 r-xp 00000000 08:02 405704                         /lib64/ld-2.12.so
3110220000-3110221000 r--p 00020000 08:02 405704                         /lib64/ld-2.12.so
3110221000-3110222000 rw-p 00021000 08:02 405704                         /lib64/ld-2.12.so
3110222000-3110223000 rw-p 00000000 00:00 0 
3110400000-311058a000 r-xp 00000000 08:02 406012                         /lib64/libc-2.12.so
311058a000-311078a000 ---p 0018a000 08:02 406012                         /lib64/libc-2.12.so
311078a000-311078e000 r--p 0018a000 08:02 406012                         /lib64/libc-2.12.so
311078e000-3110790000 rw-p 0018e000 08:02 406012                         /lib64/libc-2.12.so
3110790000-3110794000 rw-p 00000000 00:00 0 
3110800000-3110883000 r-xp 00000000 08:02 406041                         /lib64/libm-2.12.so
3110883000-3110a82000 ---p 00083000 08:02 406041                         /lib64/libm-2.12.so
3110a82000-3110a83000 r--p 00082000 08:02 406041                         /lib64/libm-2.12.so
3110a83000-3110a84000 rw-p 00083000 08:02 406041                         /lib64/libm-2.12.so
3110c00000-3110c17000 r-xp 00000000 08:02 406022                         /lib64/libpthread-2.12.so
3110c17000-3110e17000 ---p 00017000 08:02 406022                         /lib64/libpthread-2.12.so
3110e17000-3110e18000 r--p 00017000 08:02 406022                         /lib64/libpthread-2.12.so
3110e18000-3110e19000 rw-p 00018000 08:02 406022                         /lib64/libpthread-2.12.so
3110e19000-3110e1d000 rw-p 00000000 00:00 0 
3111000000-3111002000 r-xp 00000000 08:02 406039                         /lib64/libdl-2.12.so
3111002000-3111202000 ---p 00002000 08:02 406039                         /lib64/libdl-2.12.so
3111202000-3111203000 r--p 00002000 08:02 406039                         /lib64/libdl-2.12.so
3111203000-3111204000 rw-p 00003000 08:02 406039                         /lib64/libdl-2.12.so
3111800000-3111807000 r-xp 00000000 08:02 406116                         /lib64/librt-2.12.so
3111807000-3111a06000 ---p 00007000 08:02 406116                         /lib64/librt-2.12.so
3111a06000-3111a07000 r--p 00006000 08:02 406116                         /lib64/librt-2.12.so
3111a07000-3111a08000 rw-p 00007000 08:02 406116                         /lib64/librt-2.12.so
3111c00000-3111c3a000 r-xp 00000000 08:02 406248                         /lib64/libreadline.so.6.0
3111c3a000-3111e3a000 ---p 0003a000 08:02 406248                         /lib64/libreadline.so.6.0
3111e3a000-3111e42000 rw-p 0003a000 08:02 406248                         /lib64/libreadline.so.6.0
3111e42000-3111e43000 rw-p 00000000 00:00 0 
3112000000-311201d000 r-xp 00000000 08:02 406143                         /lib64/libselinux.so.1
311201d000-311221c000 ---p 0001d000 08:02 406143                         /lib64/libselinux.so.1
311221c000-311221d000 r--p 0001c000 08:02 406143                         /lib64/libselinux.so.1
311221d000-311221e000 rw-p 0001d000 08:02 406143                         /lib64/libselinux.so.1
311221e000-311221f000 rw-p 00000000 00:00 0 
3112400000-3112416000 r-xp 00000000 08:02 406071                         /lib64/libresolv-2.12.so
3112416000-3112616000 ---p 00016000 08:02 406071                         /lib64/libresolv-2.12.so
3112616000-3112617000 r--p 00016000 08:02 406071                         /lib64/libresolv-2.12.so
3112617000-3112618000 rw-p 00017000 08:02 406071                         /lib64/libresolv-2.12.so
3112618000-311261a000 rw-p 00000000 00:00 0 
3118800000-3118803000 r-xp 00000000 08:02 406156                         /lib64/libcom_err.so.2.1
3118803000-3118a02000 ---p 00003000 08:02 406156                         /lib64/libcom_err.so.2.1
3118a02000-3118a03000 r--p 00002000 08:02 406156                         /lib64/libcom_err.so.2.1Abandon (core dumped)

经过一些研究,它似乎有效地来自np.ndarray h的记忆问题,但我无法找出它可能是什么。有人曾经遇到过同样的问题吗?我希望我已经提供了足够的细节,以便有人能够理解这个问题。

PS:抱歉我的英文不好!

1 个答案:

答案 0 :(得分:2)

让Boundschecking重新开始为我工作。我有一个类型的错误,我把char,但我应该使用unsigned char。随着这一点的增加,一切都在努力!

非常感谢你提示!!