ubuntu16.04+python2.7+cuda9.0的caffe环境安装配置

安装caffe最重要的是版本问题!版本问题!版本问题!对于不同版本修改的地方是不一样的。选择好正确的版本就相当于成功了90%,剩下的10%靠运气。

(人呐有时候就不可知,一个人的命运啊当然要靠个人奋斗,但更要考虑历史的进程--spoken by haha)

前段时间我们冰箱旁边的服务器被修理了一顿,cudnn被删了,cuda升级成了9.0,所以之前的caffe有的就不能用了,安装方式也发生了改变。因此趁最近重新整理了一下安装流程。

一、首先确认此时的python环境

     如果系统环境是2.7那就好办了。如果系统环境是3.5或3.6就需要创建虚拟环境

       #conda create -n python27 python=2.7

(下面的函数包必须安装否则caffe无法正常运行)

source activate python27
conda install numpy
conda install scipy
pip install scikit-image
pip install protobuf

二、安装caffe 

  首先下载caffe,然后进入caffe根目录

git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config

接下来设置makefile.config(注意cuda8.0和9.0的设置方式不同)

   1.将 #WITH_PYTHON_LAYER := 1 修改为 WITH_PYTHON_LAYER := 1

2.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 
修改为: 
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial       

 

3.根据Makefile.config的注释:
将

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 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_52,code=sm_52 \
                -gencode arch=compute_60,code=sm_60 \
                -gencode arch=compute_61,code=sm_61 \
                -gencode arch=compute_61,code=compute_61
改为:
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
                -gencode arch=compute_35,code=sm_35 \
                -gencode arch=compute_50,code=sm_50 \
                -gencode arch=compute_52,code=sm_52 \
                -gencode arch=compute_60,code=sm_60 \
                -gencode arch=compute_61,code=sm_61 \
                -gencode arch=compute_61,code=compute_61

 

4.修改 caffe 目录下的 Makefile 文件
将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
将:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
改为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

 三、编译(见证奇迹的时刻)

         注意编译的时候要在python2.7的环境下执行。如果系统是3.5/3.6需要先激活2.7  source activate python27

make all -j8
make pycaffe
make test
然后导入python接口验证pycaffe是否安装成功
#python
#import sys (因为此时是在python=2.7的虚拟环境下运行,导入caffe包之前,需要将caffe包导入到python的搜索路径下)
#cafferoot='/home/jingdonglin/caffe/'
#sys.path.insert(0,cafferoot+'python')
#import caffe
(如果没有报错,说明安装成功)


 

   

四、思考

    当环境里既有python3.6的系统环境,也有python2.7的虚拟环境。当我们import caffe 的时候,caffe包从哪里载入到python的搜索路径呢?

      实际上弄清这个问题我们就明白了环境变量的含义。对于python=2.7的虚拟环境,我们可以直接将python的搜索路径打印出来print(sys.path)。如下图所示每当我们输入import XX时,它就是会从下面的路径中搜索并导入。由于是虚拟环境,故从conda的envs里面的lib库导入。我们在虚拟环境下载的numpy等等各种包都在该目录(lib)下。从下面截图的第一个红圈可以看出这个时候我们并没有导入caffe(/home/jingdnglin/caffe/python)。所以会报错 no module named caffe。当我们把路径加入后即sys.path.insert(),可以看出python的搜索路径中多了/home/jingdnglin/caffe/python(第二个红圈处)。此时便能import caffe成功。

     以上,酱紫。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值