2017-08-30 4 views
1

Ich habe vor kurzem eine Python-Funktion in Cython, um es zu beschleunigen codiert. Ich kenne C nicht wirklich gut und es scheint, dass ich etwas vergessen habe, da ich einen Fehler bekomme, wenn ich meinen Code ausführe.Cython doppelt frei oder Korruption

Das ist mein Cython Funktionscode writted in histogram.pyx:

# -*- 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 

Es scheint, dass die Funktion arbeitet. Ich kompilieren den Code mit diesem setup.py:

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


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

Und dann versuche ich es in diesem Skript zu verwenden:

Es gibt kein Problem, wenn ich die Funktion Histogramm (Bild1, Bild2 nennen, transfo) und registrieren Sie es in einer lokalen Variablen. Aber wenn ich es benutze und dann (mit einer Rückkehr zum Beispiel), bekomme ich den folgenden C-Fehler (das Skript stürzt nur bei der letzten pdb.set_trace(), und das Ergebnis der mi-Funktion wird gedruckt):

*** 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) 

Nach einigen Untersuchungen scheint es effektiv, dass es von einem Gedächtnis gestion Problem für die np.darray h kommt, aber ich konnte nicht herausfinden, was es sein könnte. Hat jemand einmal das gleiche Problem? Ich hoffe, dass ich genug Details habe, damit jemand das Problem versteht.

PS: Sorry für mein schlechtes Englisch!

+0

hi @Dadedidododu, dein Skript ist groß tbh. Können Sie versuchen, nur einen Auszug Ihres Fehlercodes zu geben, pls? –

+0

kann das helfen? https://stackoverflow.com/a/37713234/2572645 –

+0

Kann es nicht sein, aber einfache Sache zu versuchen ist, boundschecking wieder an. – chrisb

Antwort

2

Boundschecking für mich funktioniert wieder. Ich hatte einen Fehlertyp, ich legte char, aber ich sollte mit unsigned char arbeiten. Mit diesem Zusatz arbeitet alles an !!

Vielen Dank für Ihre Tipps !!

Verwandte Themen