ubuntu anaconda 安装caffe及pycharm 使用pycharm注意事项

1.安装主要步骤参考下面链接:

https://www.linuxidc.com/Linux/2017-11/148629.htm
https://blog.csdn.net/sinat_23853639/article/details/80990967
https://blog.csdn.net/CAU_Ayao/article/details/80578600

注意事项:

------------------------

使用caffe

添加环境变量

sudo vi /etc/profile

export PYTHONPATH=/home/devops/caffe-1.0/python:$PYTHONPATH

python

import caffe

----------------------

需要修改配置的文件

a.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 := 1
USE_LEVELDB := 1
USE_LMDB := 1
# This code is taken from https://github.com/sh1r0/caffe-android-lib
# USE_HDF5 := 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-9.0
# 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
BLAS := open
# 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 :=/home/cv/anaconda3/envs/python36/include/python3.6m \
#	/home/cv/anaconda3/envs/python36/lib/python3.6/site-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := /home/cv/anaconda3/envs/python36
#ANACONDA_HOME := /home/cv/anaconda3
PYTHON_INCLUDE :=/home/cv/anaconda3/envs/python36/include \
	/home/cv/anaconda3/envs/python36/include/python3.6m \
	/home/cv/anaconda3/envs/python36/lib/python3.6/site-packages/numpy/core/include
# 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下面行可以屏蔽
PYTHON_LIBRARIES := boost_python3 python3.6m
# 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 := /home/cv/anaconda3/envs/python36/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 /usr/include/hdf5/serial \
	/usr/local/cuda-9.0/include /usr/include/opencv /usr/include/opencv2
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial \
         /usr/lib/x86_64-linux-gnu

# 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 ?= @

b.Makefile


# Complete build flags.
COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
#NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

c. ~/.bashrc

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

#....省略....

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/cv/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/cv/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/cv/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/cv/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
#export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
#export LD_LIBRARY_PATH=/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH

#export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
#export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

#export PYTHONPATH=/home/cv/anaconda3/envs/python36
#export PYTHONPATH=/home/cv/anaconda3/envs/python36/caffe-master/python/:/home/cv/anaconda3/envs/python36/caffe-master/python/caffe/
#export PYTHONPATH=/home/cv/anaconda3/envs/python36/caffe-master/python/caffe

############
export CUDA_HOME=/usr/local/cuda-9.0
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:/usr/local/cuda-9.0/extras/CUPTI/lib64:$LD_LIBRARY_PATHs
export PATH=/usr/local/cuda-9.0/bin:$PATH
export LD_LIBRARY_PATH="/usr/local/cuda-9.0/lib64:/usr/local/cuda-9.0/extras/CUPTI/lib64:/usr/local/cuda-9.0/lib64:/usr/local/cuda-9.0/lib64"

export PYTHONPATH=/home/cv/anaconda3/envs/python36/caffe-master/python::$PYTHONPATH
conda deactivate

d. /etc/profile

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

exportPATH=/home/cv/anaconda3/bin:$PATH
export PYTHONPATH=/home/cv/anaconda3/envs/python36/caffe-master/python:$PYTHONPATH

2.测试caffe demo参考下面链接:
测试pycaffe-demo:https://blog.csdn.net/c406495762/article/details/70306550

3.遇到的问题

------------------------------

nvcc fatal : Unknown option ‘fPIC’

nvcc本身不支持-fPIC编译参数,但是却提供了-Xcompiler
用途上,使用nvcc -h可以发现,这是提供了向低级编译工具(gcc)传递编译参数的功能,因此在编译.cu文件时,在nvcc后加上 -shared -Xcomplier -fPIC 即可链接。
我遇到的错误是参考的博客中,在添加

NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)  

    1

-Xcompiler之前少了一个空格

------------------------------

ubuntu运行caffe遇到Cannot create Cublas handle. Cublas won’t be available.这个错误。

通过sudo权限执行sudo ./examples/mnist/train_lenet.sh

修改文件权限[rwx]-[rwx]-[rwx]  u=用户权限,g=组权限,o=不同组其他用户权限

r=读(4)

w=写(2)

x=执行(1)

------------------------------

可能会遇到./create_mnist.sh: 17: ./create_mnist.sh: build/examples/mnist/convert_mnist_data.bin: not found这样的错误,这是因为新版caffe都需要从根目录上执行,这里我们是从mnist目录下来执行的,所以我们需要先执行:

cd ~/caffe/

进入根目录,然后执行命令:

./examples/mnist/train_lenet.sh

------------------------------

ubuntu anaconda 安装caffe及pycharm 使用pycharm注意事项_第1张图片

网上查找后,有的说是驱动问题,在没有更改驱动的情况下,将命令

./examples/mnist/train_lenet.sh换成

sudo ./examples/mnist/train_lenet.sh

如果仍然出现问题,就只有更换驱动版本了

------------------------------

error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

解决:修改Makefile文件

CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS) -std=c++11
LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11

4.pycharm使用caffe

settings--project--project interpreter--show all--show path for the selected

ubuntu anaconda 安装caffe及pycharm 使用pycharm注意事项_第2张图片

 

ubuntu anaconda 安装caffe及pycharm 使用pycharm注意事项_第3张图片

 

------------------------------

你可能感兴趣的:(机器学习,Linux,Python)