平台为window 7,使用Microsoft visual studio 2015。当使用import pyximport;Pyximport.install(),弹出错误消息。我不知道如何解决它,即使我已经在谷歌上搜索了很多这类问题。谢谢。原来的代码是有人,我正在学习他的代码,这样我就能理解。下面是代码:
from __future__ import division
import numpy as np
import cython
cimport numpy as np
DTYPE = np.float
ctypedef np.float_t DTYPE_t
ctypedef Py_ssize_t uint
cdef inline DTYPE_t dtype_t_max(DTYPE_t a, DTYPE_t b): return a if a >= b else b
cdef inline int int_max(int a, int b): return a if a >= b else b
cdef inline int int_min(int a, int b): return a if a <= b else b
@cython.boundscheck(False)
@cython.wraparound(False)
def pool_bc01(np.ndarray[DTYPE_t, ndim=4] imgs,
np.ndarray[DTYPE_t, ndim=4] poolout,
np.ndarray[np.int_t, ndim=5] switches,
uint pool_h, uint pool_w, uint stride_y, uint stride_x):
""" Multi-image, multi-channel pooling
imgs has shape (n_imgs, n_channels, img_h, img_w)
poolout has shape (n_imgs, n_channels, img_h//stride_y, img_w//stride_x)
switches has shape (n_imgs, n_channels, img_h//stride_y, img_w//stride_x, 2)
"""
# TODO: mean pool
cdef uint n_imgs = imgs.shape[0]
cdef uint n_channels = imgs.shape[1]
cdef uint img_h = imgs.shape[2]
cdef uint img_w = imgs.shape[3]
cdef uint out_h = img_h // stride_y
cdef uint out_w = img_w // stride_x
cdef int pool_h_top = pool_h // 2 - 1 + pool_h % 2
cdef int pool_h_bottom = pool_h // 2 + 1
cdef int pool_w_left = pool_w // 2 - 1 + pool_w % 2
cdef int pool_w_right = pool_w // 2 + 1
if not n_imgs == poolout.shape[0] == switches.shape[0]:
raise ValueError('Mismatch in number of images.')
if not n_channels == poolout.shape[1] == switches.shape[1]:
raise ValueError('Mismatch in number of channels.')
if not (out_h == poolout.shape[2] == switches.shape[2] and out_w == poolout.shape[3] == switches.shape[3]):
raise ValueError('Mismatch in image shape.')
if not switches.shape[4] == 2:
raise ValueError('switches should only have length 2 in the 5. dimension.')
cdef uint i, c, y, x, y_out, x_out
cdef int y_min, y_max, x_min, x_max
cdef uint img_y, img_x
cdef uint img_y_max = 0
cdef uint img_x_max = 0
cdef DTYPE_t value, new_value
for i in range(n_imgs):
for c in range(n_channels):
for y_out in range(out_h):
y = y_out*stride_y
y_min = int_max(y-pool_h_top, 0)
y_max = int_min(y+pool_h_bottom, img_h)
for x_out in range(out_w):
x = x_out*stride_x
x_min = int_max(x-pool_w_left, 0)
x_max = int_min(x+pool_w_right, img_w)
value = -9e99
for img_y in range(y_min, y_max):
for img_x in range(x_min, x_max):
new_value = imgs[i, c, img_y, img_x]
if new_value > value:
value = new_value
img_y_max = img_y
img_x_max = img_x
poolout[i, c, y_out, x_out] = value
switches[i, c, y_out, x_out, 0] = img_y_max
switches[i, c, y_out, x_out, 1] = img_x_max
@cython.boundscheck(False)
@cython.wraparound(False)
def bprop_pool_bc01(np.ndarray[DTYPE_t, ndim=4] poolout_grad,
np.ndarray[np.int_t, ndim=5] switches,
np.ndarray[DTYPE_t, ndim=4] imgs_grad):
cdef uint n_imgs = poolout_grad.shape[0]
cdef uint n_channels = poolout_grad.shape[1]
cdef uint poolout_h = poolout_grad.shape[2]
cdef uint poolout_w = poolout_grad.shape[3]
cdef uint i, c, y, x, img_y, img_x
imgs_grad[...] = 0
for i in range(n_imgs):
for c in range(n_channels):
for y in range(poolout_h):
for x in range(poolout_w):
img_y = switches[i, c, y, x, 0]
img_x = switches[i, c, y, x, 1]
imgs_grad[i, c, img_y, img_x] = poolout_grad[i, c, y, x]
然后我在python 3.5中导入
import numpy as np
import pyximport
pyximport.install()
from pool import pool_bc01
回溯(最近一次调用):
文件",第6行,在From pool import pool_bc01
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libsite-packagespyximportpyximport.py",第445行,在load_module .py中language_level = self.language_level)
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libsite-packagespyximportpyximport.py",第232行,在load_module .py中提高exc.with_traceback (tb)
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libsite-packagespyximportpyximport.py",第216行,在load_module .py中原地= build_inplace language_level = language_level)
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libsite-packagespyximportpyximport.py",第192行,在build_module .py中reload_support = pyxargs.reload_support)
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libsite-packagespyximportpyxbuild.py",第102行,在pyx_to_dll中dist.run_commands ()
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libdistutilsdist.py",第955行,在run_commands中self.run_command (cmd)
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libdistutilsdist.py",第974行,在run_command中cmd_obj.run ()
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libsite-packagesCythonDistutilsbuild_ext.py",第164行,在运行中_build_ext.build_ext.run(自我)
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libdistutilscommandbuild_ext.py",第338行,在运行中self.build_extensions ()
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libsite-packagesCythonDistutilsbuild_ext.py",第172行,在build_extensions .py中self.build_extension (ext)
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libdistutilscommandbuild_ext.py",第532行,在build_extension .py中取决于= ext.depends)
文件"C:UserstcaoAppDataLocalContinuumAnaconda3libdistutils_msvccompiler.py",行386,在编译提高CompileError (msg)
importterror: Building module pool failed: ["distutils.errors。编译错误:命令'C:\Program Files (x86)\VC\BIN\x86_amd64\cl.exe' failed with exit status 2n"]
我得到的错误是
fatal error: numpy/arrayobject.h: No such file or directory #include "numpy/arrayobject.h"
后面跟着与您所显示的类似的回溯。如果我确保pyximport知道numpy包含目录
import pyximport
import numpy as np
pyximport.install(setup_args={'include_dirs': np.get_include()})
似乎编译得很好。