1

I am doing some object detection and I am trying to use the darkflow code of the yolo object detection which works correctly as outlined in the Readme. However, to make the programme folder cleaner, I am trying to group all the files and folders of the darkflow program into one parent folder. When i do this i update all the import fuctions to darkflow.<sub folder> but i get an error when i run the program with the cyton files. Each file in the cython_utils folder has been updated to accommodate the extra parent directory and a recompile after the update as outlined in the Readme. The error is

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'

I have tried using relitive paths to find the files, as well as finding the parent directory automatically but neither has worked.

When i compile the cython_utils folder i get some warnings like, (full output posted further below)

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

How can one add parent folders with cython compiled folders?

This is the first time i have dealt with the cython module and i would have thought it would be just change each modules path location and then recompile.

NOTE: If i comment out the import line and where it is used the code, the programme completes without an error.

For example what i would like.

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

What works.

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

compiling 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

Code

The original cython_utils folder contained cy_yolo2_findboxes.pyx, cy_yolo2_findboxes.pyx, nms.pxd, nms.pyx, setup.py

darkflow/net/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),
Community
  • 1
  • 1
heych
  • 33
  • 9
  • So did you solve problem with `'ModuleNotFoundError: No module named 'cython_utils.cy_yolo_findboxes'`? – user25 Apr 20 '17 at 12:22
  • No. At the moment i am just using all the folders and files outside of the 'darkflow' folder. This works but just adds more folders and files in the parent directory. – heych Apr 21 '17 at 01:03
  • I reinstalled numpy and it helped to build cython_utils normally – user25 Apr 21 '17 at 16:30

0 Answers0