简体   繁体   中英

Python 2.7 and Cython: global name col2im_6d_cython is not defined

I have a problem with python 2.7 and Cython on Ubuntu 16.04. I am trying to run code from the cs231n course (Convolutional Neural Networks). But the only function col2im_6d_cython does not work. The error is:

NameError: global name 'col2im_6d_cython' is not defined

The function col2im_6d_cython is defined in im2col_cython.pyx :

def col2im_6d_cython(np.ndarray[DTYPE_t, ndim=6] cols, int N, int C, int H, int W,
        int HH, int WW, int pad, int stride):
    cdef np.ndarray x = np.empty((N, C, H, W), dtype=cols.dtype)
    cdef int out_h = (H + 2 * pad - HH) / stride + 1
    cdef int out_w = (W + 2 * pad - WW) / stride + 1
    cdef np.ndarray[DTYPE_t, ndim=4] x_padded = np.zeros((N, C, H + 2 * pad, W + 2 * pad),
                                                         dtype=cols.dtype)

    col2im_6d_cython_inner(cols, x_padded, N, C, H, W, HH, WW, out_h, out_w, pad, stride)

    if pad > 0:
        return x_padded[:, :, pad:-pad, pad:-pad]
    return x_padded 

The file where col2im_6d_cython is called from is fast_layers.py :

from cs231n.im2col_cython import col2im_cython, im2col_cython
from cs231n.im2col_cython import col2im_6d_cython

def conv_backward_strides(dout, cache):
        x, w, b, conv_param, x_cols = cache
    stride, pad = conv_param['stride'], conv_param['pad']

    N, C, H, W = x.shape
    F, _, HH, WW = w.shape
    _, _, out_h, out_w = dout.shape

    db = np.sum(dout, axis=(0, 2, 3))

    dout_reshaped = dout.transpose(1, 0, 2, 3).reshape(F, -1)
    dw = dout_reshaped.dot(x_cols.T).reshape(w.shape)

    dx_cols = w.reshape(F, -1).T.dot(dout_reshaped)
    dx_cols.shape = (C, HH, WW, N, out_h, out_w)
    dx = col2im_6d_cython(dx_cols, N, C, H, W, HH, WW, pad, stride)

    return dx, dw, db

col2im_cython and im2col_cython work properly, but only col2im_6d_cython does not work.

It seems to me that there is a problem with Cython installation. I've installed it by running: python setup.py build_ext --inplace

setup.py is:

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

extensions = [
Extension('im2col_cython', ['im2col_cython.pyx'],
        include_dirs = [numpy.get_include()]
),
]

setup(
    ext_modules = cythonize(extensions),
)

And I had a warning when installing Cython:

Warning: Extension name 'im2col_cython' does not match fully qualified name 'cs231n.im2col_cython' of 'im2col_cython.pyx'
running build_ext

Why does only col2im_6d_cython not work? Are there any ways to fix it?

Thank you all in advance!

The problem solved by uninstalling Anaconda3 completely and installing Anaconda2. Then I've created new environment and reinstalled all needed packages. The error does not appear now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM