从子目录

时间:2017-04-17 13:34:30

标签: python cython

我正在进行一些对象检测,我正在尝试使用yolo对象检测的darkflow代码,该代码可以自述,如自述文件中所述。但是,为了使程序文件夹更清洁,我试图将darkflow程序的所有文件和文件夹分组到一个父文件夹中。 当我这样做时,我将所有导入功能更新为darkflow.<sub folder>,但是当我使用cyton文件运行程序时出现错误。 cython_utils文件夹中的每个文件都已更新,以容纳额外的父目录,并在更新后重新编译,如自述文件中所述。错误是

File "/media/GIT/objdetect/darkflow/net/yolo/test.py", line 7, in <module>        
from darkflow.cython_utils.cy_yolo_findboxes import yolo_box_constructor 
File "cython_utils/cy_yolo_findboxes.pyx", line 1, in init cython_utils.cy_yolo_findboxes (cy_yolo_findboxes.c:18758)
ImportError: No module named 'cython_utils'

我尝试使用可靠路径来查找文件,以及finding the parent directory automatically但都没有用。

当我编译cython_utils文件夹时,我会收到一些警告,(完整输出在下面进一步发布)

Warning: Extension name 'nms' does not match fully qualified name 'cython_utils.nms' of 'nms.pyx'
Compiling nms.pyx because it changed.

如何使用cython编译的文件夹添加父文件夹?

这是我第一次处理cython模块,我认为只需更改每个模块的路径位置然后重新编译。

注意:如果我注释掉导入行以及使用代码的位置,则程序完成且没有错误。

例如我想要的。

project/
   -> darkflow/
         -> cfg/
             -> other subfiles
         -> cython_utils/
             -> build/
                 -> temp.linux-x86_64-3.5/
                     -> cy_yolo2_findboxes.o
                     -> cy_yolo_findboxes.o
                     -> nms.o
             -> cy_yolo_findboxes.c
             -> cy_yolo_findboxes.pyx
             -> other files
         -> dark/
             -> other subfiles
             .
             .
             .
   -> main.py

什么有效。

project/
     -> cfg/
         -> other subfiles
     -> cython_utils/
         -> build/
             -> temp.linux-x86_64-3.5/
                 -> cy_yolo2_findboxes.o
                 -> cy_yolo_findboxes.o
                 -> nms.o
             -> cy_yolo_findboxes.c
             -> other files
         -> dark/
             .
             .
             .
     -> main.py

编译cython

python3 setup.py build_ext --inplace

Warning: Extension name 'nms' does not match fully qualified name 'cython_utils.nms' of 'nms.pyx'
Compiling nms.pyx because it changed.
[1/1] Cythonizing nms.pyx
running build_ext
building 'nms' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c nms.c -o build/temp.linux-x86_64-3.5/nms.o
In file included from /usr/include/python3.5m/numpy/ndarraytypes.h:1777:0,
                 from /usr/include/python3.5m/numpy/ndarrayobject.h:18,
                 from /usr/include/python3.5m/numpy/arrayobject.h:4,
                 from nms.c:447:
/usr/include/python3.5m/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it by " \
  ^
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/nms.o -lm -o /media/GIT/objdetect/darkflow/cython_utils/nms.cpython-35m-x86_64-linux-gnu.so
Warning: Extension name 'cy_yolo2_findboxes' does not match fully qualified name 'cython_utils.cy_yolo2_findboxes' of 'cy_yolo2_findboxes.pyx'
Compiling cy_yolo2_findboxes.pyx because it changed.
[1/1] Cythonizing cy_yolo2_findboxes.pyx
running build_ext
building 'cy_yolo2_findboxes' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c cy_yolo2_findboxes.c -o build/temp.linux-x86_64-3.5/cy_yolo2_findboxes.o
In file included from /usr/include/python3.5m/numpy/ndarraytypes.h:1777:0,
                 from /usr/include/python3.5m/numpy/ndarrayobject.h:18,
                 from /usr/include/python3.5m/numpy/arrayobject.h:4,
                 from cy_yolo2_findboxes.c:447:
/usr/include/python3.5m/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it by " \
  ^
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/cy_yolo2_findboxes.o -lm -o /media/GIT/objdetect/darkflow/cython_utils/cy_yolo2_findboxes.cpython-35m-x86_64-linux-gnu.so
Warning: Extension name 'cy_yolo_findboxes' does not match fully qualified name 'cython_utils.cy_yolo_findboxes' of 'cy_yolo_findboxes.pyx'
Compiling cy_yolo_findboxes.pyx because it changed.
[1/1] Cythonizing cy_yolo_findboxes.pyx
running build_ext
building 'cy_yolo_findboxes' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c cy_yolo_findboxes.c -o build/temp.linux-x86_64-3.5/cy_yolo_findboxes.o
In file included from /usr/include/python3.5m/numpy/ndarraytypes.h:1777:0,
                 from /usr/include/python3.5m/numpy/ndarrayobject.h:18,
                 from /usr/include/python3.5m/numpy/arrayobject.h:4,
                 from cy_yolo_findboxes.c:447:
/usr/include/python3.5m/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it by " \
  ^
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/cy_yolo_findboxes.o -lm -o /media/GIT/objdetect/darkflow/cython_utils/cy_yolo_findboxes.cpython-35m-x86_64-linux-gnu.so

代码

包含原始cython_utils文件夹 cy_yolo2_findboxes.pyxcy_yolo2_findboxes.pyxnms.pxdnms.pyxsetup.py

darkflow /净/ YOLO /的 test.py

from darkflow.utils.im_transform import imcv2_recolor, imcv2_affine_trans
from darkflow.utils.box import BoundBox, box_iou, prob_compare
import numpy as np
import cv2
import os
from darkflow.cython_utils.cy_yolo_findboxes import yolo_box_constructor  # Error on this line

darkflow / cython_utils /的 cy_yolo_findboxes.pyx

import numpy as np
cimport numpy as np
cimport cython
ctypedef np.float_t DTYPE_t
from libc.math cimport exp
import os, sys
from darkflow.utils.box import BoundBox
from nms cimport NMS



@cython.cdivision(True)
@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False)  # turn off negative index wrapping for entire function
def yolo_box_constructor(meta,np.ndarray[float] net_out, float threshold):

    cdef:
        float sqrt
        int C,B,S
        int SS,prob_size,conf_size
        int grid, b
        int class_loop


    sqrt =  meta['sqrt'] + 1
    C, B, S = meta['classes'], meta['num'], meta['side']
    boxes = []
    SS        =  S * S # number of grid cells
    prob_size = SS * C # class probabilities
    conf_size = SS * B # confidences for each grid cell

    cdef:
        float [:,::1] probs =  np.ascontiguousarray(net_out[0 : prob_size]).reshape([SS,C])
        float [:,::1] confs =  np.ascontiguousarray(net_out[prob_size : (prob_size + conf_size)]).reshape([SS,B])
        float [: , : ,::1] coords =  np.ascontiguousarray(net_out[(prob_size + conf_size) : ]).reshape([SS, B, 4])
        float [:,:,::1] final_probs = np.zeros([SS,B,C],dtype=np.float32)


    for grid in range(SS):
        for b in range(B):
            coords[grid, b, 0] = (coords[grid, b, 0] + grid %  S) / S
            coords[grid, b, 1] = (coords[grid, b, 1] + grid // S) / S
            coords[grid, b, 2] =  coords[grid, b, 2] ** sqrt
            coords[grid, b, 3] =  coords[grid, b, 3] ** sqrt
            for class_loop in range(C):
                probs[grid, class_loop] = probs[grid, class_loop] * confs[grid, b]
                #print("PROBS",probs[grid,class_loop])
                if(probs[grid,class_loop] > threshold ):
                    final_probs[grid, b, class_loop] = probs[grid, class_loop]


    return NMS(np.ascontiguousarray(final_probs).reshape(SS*B, C) , np.ascontiguousarray(coords).reshape(SS*B, 4))

darkflow / cython_utils /的 nms.pxd

import numpy as np
cimport numpy as np
cimport cython
ctypedef np.float_t DTYPE_t
from libc.math cimport exp
from darkflow.utils.box import BoundBox


cdef NMS(float[:, ::1] , float[:, ::1] )

darkflow / cython_utils /的 nms.pyx

import numpy as np
cimport numpy as np
cimport cython
from libc.math cimport exp
from darkflow.utils.box import BoundBox

darkflow / cython_utils /的 setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy
import os

if os.name =='nt' :

    ext_modules=[
        Extension("nms",
                sources=["nms.pyx"],
                #libraries=["m"] # Unix-like specific
                    include_dirs=[numpy.get_include()]
        )
    ]

    ext_modules_yolo2=[
        Extension("cy_yolo2_findboxes",
                  sources=["cy_yolo2_findboxes.pyx"],
                  #libraries=["m"] # Unix-like specific
                  include_dirs=[numpy.get_include()]
        )
    ]

    ext_modules_yolo=[
        Extension("cy_yolo_findboxes",
                  sources=["cy_yolo_findboxes.pyx"],
                  #libraries=["m"] # Unix-like specific
                  include_dirs=[numpy.get_include()]
        )
    ]

else :

    ext_modules=[
        Extension("nms",
                sources=["nms.pyx"],
                libraries=["m"] # Unix-like specific
        )
    ]

    ext_modules_yolo2=[
        Extension("cy_yolo2_findboxes",
                  sources=["cy_yolo2_findboxes.pyx"],
                  libraries=["m"] # Unix-like specific
        )
    ]

    ext_modules_yolo=[
        Extension("cy_yolo_findboxes",
                  sources=["cy_yolo_findboxes.pyx"],
                  libraries=["m"] # Unix-like specific
        )
    ]




setup(

    #name= 'cy_findboxes',
    ext_modules = cythonize(ext_modules),
)

setup(

    #name= 'cy_findboxes',
    ext_modules = cythonize(ext_modules_yolo2),
)



setup(

    #name= 'cy_findboxes',
    ext_modules = cythonize(ext_modules_yolo),

0 个答案:

没有答案