J'ai un problème avec python 2.7 et Cython sur Ubuntu 16.04. J'essaie d'exécuter du code à partir du cours cs231n (Convolutional Neural Networks). Mais la seule fonction col2im_6d_cython ne fonctionne pas. L'erreur est:Python 2.7 et Cython: nom global col2im_6d_cython n'est pas défini
NameError: global name 'col2im_6d_cython' is not defined
La fonction col2im_6d_cython est défini dans 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
Le fichier où col2im_6d_cython est appelée à partir est 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 et im2col_cython fonctionne correctement, mais seulement col2im_6d_cython ne fonctionne pas.
Il me semble qu'il y a un problème avec l'installation de Cython. Je l'ai installé en exécutant: python setup.py build_ext --inplace
setup.py est:
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),
)
et moi avons eu un avertissement lors de l'installation Cython:
Warning: Extension name 'im2col_cython' does not match fully qualified name 'cs231n.im2col_cython' of 'im2col_cython.pyx'
running build_ext
Pourquoi seulement col2im_6d_cython ne fonctionne pas? Y a-t-il des moyens de le réparer?
Merci d'avance!
Cela ne vous aide pas, mais il y a une longue liste de personnes ayant des problèmes similaires avec le cours cs231 à ce sujet ici, et n'ayant pas de vraies solutions. – DavidW