anaconda把与python有关的库(numpy,scipy等等)都打成一个包,建议用anaconda来进行安装。下载对应系统和对应版本的anaconda来安装,减少安装过程的问题。
pycaffe 的安装以及 notebook 环境配置是为了更方便的去使用 深度学习框架caffe。notebook 使用浏览器作为界面使用,可以编写和执行 python 代码。
一、查看Python版本
linux系统都自带python,用version查看版本
python --version
二、利用anaconda来配置python环境
1.在continuum官网 下载anaconda
现在的版本有python3.6版本和python2.7版本
2.下载好对应版本、对应系统的anaconda
sudo Anaconda2-2.5.0-Linux-x86_64.sh
2.将anaconda安装路径加入到环境变量(.bashrc)中
3.安装成功后,在当前用户根目录下生成一个anaconda2的文件夹
三、编译python接口
1、将caffe根目录下的python文件夹加入到环境变量,打开配置文件bashrc
sudo gedit ~/.bashrc
2.在最后面加入
export PYTHONPATH=/home/caffe/python:$PYTHONPATH
3.保存退出,更新配置文件
sudo ldconfig
4.修改编译配置文件Makefile.config
4.1修改ANACONDAHOME :=(HOME)/anaconda2
对PYTHON_INCLUDE取消注释
4.2注释掉原PYTHON_LIB路径,使用$(SNACONDA_HOME)/lib
4.3取消对WITH_PYTHON_layer :=1
的注释
Makefile.config文件
## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
# You should not set this flag if you will be reading LMDBs with any
# possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr
# 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
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas
# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib
# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := $(HOME)/anaconda2
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3.5/dist-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib
# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib
# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1
# Whatever else you find you need goes here.
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
# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib
# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1
# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1
# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1
# 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 ?= @
5.修改完编译配置文件后,最后进行编译:
sudo make pycaffe
sudo make test
sudo make runtest
6.查看python接口是否编译成功,进入python环境
python
7.导入caffe
import caffe
四、启动jupyter notebook
1.anaconda已集成jupyter notebook
2.把caffe中的和python相关的内容的路径添加到python的编译路径中
import sys
sys.path.append("/home/cow/caffe/python")
sys.path.append("/home/cow/caffe/python/caffe")
3.之前安装过protobuf在编译hadoop源码的时候,不过并没有添加对python的支持,这里要对python进行支持才可以安装pycaffe
4.protobuf建议不要用conda install来安装,使用pip命令安装
~/anaconda2/bin/pip install protobuf
4.1不然import caffe会出现ImportError: No module named google.protobuf.internal
5.运行jupyter notebook
jupyter notebook
6.新建ipynb文件
自动在浏览器中打开notebook,点击New-python2按钮,新建一个扩展名为ipynb的文件。在这个网页上,可以像在命令行下面一样运行python代码
7.查看更多快捷键
输入代码后,按shift+enter运行。
更多的快捷键,可点击上方的help-Keyboard shortcuts查看,或者先按esc退出编辑状态,再按h键查看