2017-04-15 6 views
0

Ich habe ein Problem mit Python 2.7 und Cython auf Ubuntu 16.04. Ich versuche, Code aus dem cs231n-Kurs (Convolutional Neural Networks) auszuführen. Aber die einzige Funktion col2im_6d_cython funktioniert nicht. Der Fehler ist:Python 2.7 und Cython: globaler Name col2im_6d_cython ist nicht definiert

NameError: global name 'col2im_6d_cython' is not defined 

Die Funktion col2im_6d_cython in im2col_cython.pyx definiert ist:

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 

Die Datei, in col2im_6d_cython aus aufgerufen wird, ist 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 

c ol2im_cython und im2col_cython funktionieren ordnungsgemäß, aber nur col2im_6d_cython funktioniert nicht.

Es scheint mir, dass ein Problem mit der Cython-Installation vorliegt. Ich installiert habe es durch Ausführen von: Python setup.py build_ext --inplace

setup.py ist:

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

Und ich hatte eine Warnung, wenn Cython Installation:

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

Warum funktioniert nur col2im_6d_cython nicht funktioniert? Gibt es Möglichkeiten, es zu beheben?

Vielen Dank im Voraus!

+0

Dies hilft Ihnen nicht, aber es gibt eine lange Liste von Menschen mit ähnlichen Problemen mit diesem Kurs cs231 hier darüber zu veröffentlichen, und keine wirklichen Lösungen zu bekommen. – DavidW

Antwort

0

Das Problem wurde durch vollständige Deinstallation von Anaconda3 und Installation von Anaconda2 gelöst. Dann habe ich eine neue Umgebung erstellt und alle benötigten Pakete neu installiert. Der Fehler erscheint jetzt nicht

Verwandte Themen