Caffe+GPU+Opencv3.1+Python3.5+Anaconda:fatal error: Python.h: No such file or directory

Caffe+GPU+Opencv3.1+Python3.5+Anaconda:fatal error: Python.h: No such file or directory

简单地说,我最近想在我的项目中使用 Caffe。 我的OS是Ubuntu14.04,有Opencv3.1+Python3.5+Anaconda+GPU 我已经全部通过了:

make all
make pycaffe
make test
make runtest

但是什么时候可以尝试make pycaffe,却无法通过:

Python.h: No such file or directory

这是我的 'makefile.config',我确定 'Python.h' 已经在路径中,这让我很困惑。

USE_CUDNN := 1
OPENCV_VERSION := 3
ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
         $(ANACONDA_HOME)/include/python3.5m \
         $(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include \
PYTHON_LIB := $(ANACONDA_HOME)/lib
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
USE_PKG_CONFIG := 1
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
                 /usr/lib/python3.5/dist-packages/numpy/core/include

因为我用的是Python3.5,所以我取消了下面的注释:

PYTHON_INCLUDE := /usr/include/python2.7 \
        /usr/lib/python2.7/dist-packages/numpy/core/include
PYTHON_LIB := /usr/lib

非常感谢有人能提供帮助,

您对 PYTHON_INCLUDE 有两个定义:您需要决定是选择 "python3" 风味还是 "anaconda" 风味...

你的 python.h 文件在哪里?试试 shell

find / -name "Python.h" -type f

看看它到底在哪里。然后在 makefile.config

中为 PYTHON_INCLUDE 选择正确的设置

在Ubuntu14.04上配置caffe差不多花了(腰)一周,太费时间的原因是我用的是最新版的OpencvPython和anaconda。在这里我想分享一下我的经验。

  1. Makefile.config

    # cuDNN acceleration switch (uncomment to build with cuDNN).
    USE_CUDNN := 1
    # Uncomment if you’re using OpenCV 3
    OPENCV_VERSION := 3
    # CUDA directory contains bin/ and lib/ directories that we need.
    CUDA_DIR := /usr/local/cuda
    # CUDA architecture setting: going with all of them.
    # For CUDA < 6.0, comment the *_50 lines for compatibility.
    CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
    -gencode arch=compute_20,code=sm_21 \
    -gencode arch=compute_30,code=sm_30 \
    -gencode arch=compute_35,code=sm_35 \
    -gencode arch=compute_50,code=sm_50 \
    -gencode arch=compute_50,code=compute_50
    # BLAS choice: atlas for ATLAS (default)
    BLAS := atlas
    # We need to be able to find Python.h and numpy/arrayobject.h.
    ANACONDA_HOME := $(HOME)/anaconda3
    PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
    $(ANACONDA_HOME)/include/python3.5m \
    $(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include \
    # Uncomment to use Python 3 (default is Python 2)
    PYTHON_LIBRARIES := boost_python3 python3.5m
    # We need to be able to find libpythonX.X.so or .dylib.
    PYTHON_LIB := $(ANACONDA_HOME)/lib
    # Whatever else you find you need goes here.
    INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
    LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
    # N.B. both build and distribute dirs are cleared on `make clean`
    BUILD_DIR := build
    DISTRIBUTE_DIR := distribute
    # The ID of the GPU that ‘make runtest’ will use to run unit tests.
    TEST_GPUID := 0
    # enable pretty build (comment to see full commands)
    Q ?= @
    
  2. /.bashrc

    #Caffemake
    export PYTHONPATH=~/caffe/python/:$PYTHONPATH
    #Opencv
    export LD_LIBRARY_PATH=/home/kaku/anaconda3/lib:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH=”/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH”
    
  3. 备注:

    1. library must be installed:
    libboost-all-dev, although in some tutorial mentioned must install libboost1.55-all-dev.
    protobuf-cpp-3.0.0-beta-2.zip or upper one
    protobuf-python-3.0.0-beta-2.zip or upper one
    http://blog.csdn.net/lien0906/article/details/51784191
    https://github.com/google/protobuf/issues/1276
    
  4. 其他调试:详见my own blog.

在遇到同样的问题并使用 Gentoo 系统后,我尝试了其他方法。我通过 Gentoo 插槽同时安装了 2 python 个实例:

ares ~ # eselect python list
Available Python interpreters, in order of preference:
  [1]   python3.4
  [2]   python2.7

我的默认是2.7,所以我尝试切换到3.4。问题是它需要对 2 个文件进行一些更改。

我注意到 2.7 的类似更改根本不起作用,路径是正确的,但底层有问题...

Makefile.config 文件我更改为使用 Python 3 (3.4) :

PYTHON_LIBRARIES := boost_python3 python3.4m
PYTHON_INCLUDE := /usr/include/python3.4m \
                 /usr/lib64/python3.4/site-packages/numpy/core/include

不过,当您只是更改它时,只要 CMake 仍然指向 2.7,它就不会起作用。我检查了一下:

mkdir build; cd build;cmake ..;

输出为:

-- Python:
--   Interpreter       :   /usr/bin/python2.7 (ver. 2.7.12)
--   Libraries         :   /usr/lib64/libpython2.7.so (ver 2.7.12)
--   NumPy             :   /usr/lib64/python2.7/site-packages/numpy/core/include (ver 1.12.1)

所以我在 CMakeLists.txt 文件中更改了这一行:

set(python_version "2" CACHE STRING "Specify which Python version to use")

至(将值 2 更改为 3):

set(python_version "3" CACHE STRING "Specify which Python version to use")

并再次执行 cmake(清理后)并最终得到:

-- Python:
--   Interpreter       :   /usr/bin/python3 (ver. 3.4.5)
--   Libraries         :   /usr/lib64/libpython3.4m.so (ver 3.4.5)
--   NumPy             :   /usr/lib64/python3.4/site-packages/numpy/core/include (ver 1.12.1)

现在 make -j8 命令顺利完成。我注意到我在编译 (-j8) 时使用了多线程选项,因为我在一些论坛上发现建议只使用 -j1(单线程),所以我不是这种情况。

我的 Make.config 中有以下内容:

PYTHON_LIB := /usr/lib

PYTHON_INCLUDE :=  /usr/include/python2.7 \
                /usr/lib/python2.7/dist-packages/numpy/core/include

以及我的 ~/.bashrc 中的以下内容:

export PYTHONPATH=$HOME/caffe/python
export CAFFE_ROOT=$HOME/caffe

您必须 运行 在 cd $CAFFE_ROOT 中执行以下操作: 让所有 制作pycaffe 进行测试 进行运行测试

我的设置是在 CentOS 和 Python 2.7 中,但它应该是类似的想法。

[jalal@ivcgpu1 caffe]$ lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.4.1708 (Core) 
Release:    7.4.1708
Codename:   Core
[jalal@ivcgpu1 caffe]$ uname -a
Linux ivcgpu1.bu.edu 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux