title: Import compiled cyton file from child directory
body: <p>I am doing some object detection and I am trying to use the <a href="https://github.com/thtrieu/darkflow" rel="nofollow noreferrer">darkflow</a> 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 <code>darkflow.&lt;sub folder&gt;</code> but i get an error when i run the program with the cyton files. Each file in the <code>cython_utils</code> folder has been updated to accommodate the extra parent directory and a recompile after the update as outlined in the Readme. The error is </p>
  
  <pre><code>File "/media/GIT/objdetect/darkflow/net/yolo/test.py", line 7, in &lt;module&gt;        
  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'
  </code></pre>
  
  <p>I have tried using relitive paths to find the files, as well as <a href="https://stackoverflow.com/questions/16780014/import-file-from-parent-directory">finding the parent directory automatically</a> but neither has worked.</p>
  
  <p>When i compile the <code>cython_utils</code> folder i get some warnings like, (full output posted further below)</p>
  
  <pre><code>Warning: Extension name 'nms' does not match fully qualified name 'cython_utils.nms' of 'nms.pyx'
  Compiling nms.pyx because it changed.
  </code></pre>
  
  <p>How can one add parent folders with cython compiled folders?</p>
  
  <p>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.</p>
  
  <p>NOTE: If i comment out the import line and where it is used the code, the programme completes without an error. </p>
  
  <p>For example what i would like.</p>
  
  <pre><code>project/
     -&gt; darkflow/
           -&gt; cfg/
               -&gt; other subfiles
           -&gt; cython_utils/
               -&gt; build/
                   -&gt; temp.linux-x86_64-3.5/
                       -&gt; cy_yolo2_findboxes.o
                       -&gt; cy_yolo_findboxes.o
                       -&gt; nms.o
               -&gt; cy_yolo_findboxes.c
               -&gt; cy_yolo_findboxes.pyx
               -&gt; other files
           -&gt; dark/
               -&gt; other subfiles
               .
               .
               .
     -&gt; main.py
  </code></pre>
  
  <p>What works.</p>
  
  <pre><code>project/
       -&gt; cfg/
           -&gt; other subfiles
       -&gt; cython_utils/
           -&gt; build/
               -&gt; temp.linux-x86_64-3.5/
                   -&gt; cy_yolo2_findboxes.o
                   -&gt; cy_yolo_findboxes.o
                   -&gt; nms.o
               -&gt; cy_yolo_findboxes.c
               -&gt; other files
           -&gt; dark/
               .
               .
               .
       -&gt; main.py
  </code></pre>
  
  <h2>compiling cython</h2>
  
  <p><code>python3 setup.py build_ext --inplace</code></p>
  
  <pre><code>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></pre>
  
  <h2>Code</h2>
  
  <p>The original <code>cython_utils</code> folder contained
  <code>cy_yolo2_findboxes.pyx</code>, <code>cy_yolo2_findboxes.pyx</code>, <code>nms.pxd</code>, <code>nms.pyx</code>, <code>setup.py</code></p>
  
  <p>darkflow/net/yolo/<strong>test.py</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p>darkflow/cython_utils/<strong>cy_yolo_findboxes.pyx</strong></p>
  
  <pre><code>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] &gt; 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))
  </code></pre>
  
  <p>darkflow/cython_utils/<strong>nms.pxd</strong></p>
  
  <pre><code>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] )
  </code></pre>
  
  <p>darkflow/cython_utils/<strong>nms.pyx</strong></p>
  
  <pre><code>import numpy as np
  cimport numpy as np
  cimport cython
  from libc.math cimport exp
  from darkflow.utils.box import BoundBox
  </code></pre>
  
  <p>darkflow/cython_utils/<strong>setup.py</strong></p>
  
  <pre><code>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),
  </code></pre>