赞
踩
一份深度学习环境配置的备忘。
推荐使用 Linux 系统,可以少给自己找些麻烦,既然要吃程序员这口饭,使用 Linux 这关是逃不开的。
Linux 有许多发行版,具体用哪个版本倒不必过分细究,不过还是推荐使用稳定的、使用人数多的发行版,比如 Ubuntu16.04 LST。
首先需要做一个 U 盘启动盘,接着分区。最好上 SSD 用来装系统,再挂一个 4T 的机械硬盘,分区可以如下:
/
下系统安装上之后还有些动作是需要做的:
其实还有的折腾,装机先到这里吧。
有几个库经常出一些问题,比如说 numpy、cuda/cudnn 和 protobuf。
Numpy是不得不用的python科学计算基础库,被几乎所有的深度学习框架依赖,目前版本已经到1.6。
Tips:Numpy出问题,基本上就是版本问题,框架未必支持最新版,最好不要装最新版本的,要自己制定版本安装。
搞深度学习得有块 GPU 吧,我看有很多人用 cuda8,不过还是比较推荐安装 cuda10,最近刚出的 TensorFlow2.0 就必须使用 cuda10。先安装 nvidia-driver ,再安装 cuda。
注:NVIDIM 的驱动要在 cuda 之前安装,见NVIDIA 环境配置这一小节。
官网下载链接 https://developer.nvidia.com/cuda-downloads
- # 显卡驱动安装
-
- 1.下载官网内Linux版本的.run安装包
- 2. cd xxx/xxx #以目录usr/ser为例,通过cd usr/ser进入安装包存放目录
- 3. sudo service lightdm stop #关闭图形界面
- 4. ctrl+alt+F1 #若此时长时间黑屏,通过此命令进入命令行
- 5. cd xxx/xxx #在命令行进入安装包存放目录
- 6. sudo bash NVIDIA-Linux-x86_64-xx.xx.run --no-opengl-files
-
- #只安装驱动文件,默认enter选项,进入命令行
- #–no-opengl-files 只安装驱动文件,不安装OpenGL文件。这个参数最重要
- 7. sudo reboot #重启电脑。
- 8. nvidia-smi或nvidia-settings #显示驱动详细信息,即安装成功
当初安装 caffe 的时候,因为 protobuf 的版本问题被折腾得够呛,可以用 protobuf --version
看看当前的版本,不出意外因该是 2.6.1
,有点低。
建议自己找个目录安装 3.4
以上的版本,和系统隔离,让一些包比如caffe编译的时候依赖上自己的这个库。
https://github.com/protocolbuffers/protobuf/releases
接着可以安装 opencv、caffe、tensorflow、pytorch、anaconda 了。
OpenCV 安装写过一篇:Ubuntu16.04配置OpenCV环境。
还有视频教学的: http://space.bilibili.com/365916694?
配置 nvidia-driver 环境,官网下载链接: https://www.nvidia.com/Download/index.aspx?lang=cn。
- # 命令行下载
- sudo ubuntu-drivers devices
-
- # 以上命令可能会遇到问题
- The program 'ubuntu-drivers' is currently not installed. You can install it by typing:
- sudo apt install ubuntu-drivers-common
-
- # 进入命令行界面 Ctrl + ALt + F1
- # 比如当我们安装 NVIDIA 的驱动程序时,就需要先关闭 X server
- sudo service lightdm stop
-
- # 以上命令可能遇到问题
- Failed to stop lightdm.service: Unit lightdm.service not loaded.
- how-to-install-nvidia-run
-
- # 安装命令
- # 只安装驱动文件,不安装 opengl 文件
- sudo ./NVIDIA*.run -no-opengl-files
- # 重启 X service
- sudo service lightdm start
- # 查看驱动安装结果
- nvdia-smi
nvdia 驱动安装常见问题
笔记本双显卡系统,登录界面无限循环,无法进入桌面
The Nouveau Kernel driver is currently in use by your system
https://gist.github.com/wangruohui/df039f0dc434d6486f5d4d098aa52d07
Caffe 挺不好装的,可以 fork 一下这个项目 https://github.com/longpeng2008/Caffe_Long 然后参照 这个 Makefile。这里最好指定 opencv 和 protobuf 的位置。
Caffe 安装可以参见 Caffe Installation,高于 17.04 的 ubuntu 可以一键安装 caffe,不过我们将在 ubuntu16.04 上安装 caffe。
安装 caffe 需要依赖很多东西,安装顺序也是有讲究的:
- sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev
- protobuf-compiler
- sudo apt-get install --no-install-recommends libboost-all-dev
- sudo apt-get install libatlas-base-dev
- sudo install OpenBLAS
- sudo apt-get install libopenblas-dev
- ==============================================
- # 使用三哥定制版本的 caffe
- git clone https://github.com/YujieShui/Caffe_Long
- # 接着修改 makefile
-
- ==============================================
- # 这个是使用官网版本的 caffe,我这里使用三哥定制版本的 caffe
- git clone https://github.com/BVLC/caffe.git
- cd caffe
- # 此时编译就会遇到很多的问题,看接下里给出的解决方案,再编译
- cp Makefile.config.example Makefile.config
- make -j4
- ## 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).
- # 是否使用 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
- # 是否使用功能 opencv
- # USE_OPENCV := 0
- # 是否使用 LEVELDB 输入格式
- # USE_LEVELDB := 0
- # 是否使用 USE_LMDB 输入格式
- # 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
- # 是否使用 opencv3,这里开启
- 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 的目录
- CUDA_DIR := /usr/local/cuda-8.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 lines for compatibility.
- # 与 cuda 架构有关 http://www.caffecn.cn/?/question/1077
- # cuda10 要注释掉前两个
- 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)
- # mkl for MKL
- # open for OpenBlas
- 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 := /opt/OpenBLAS/include
- BLAS_LIB := /opt/OpenBLAS/lib
- # 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/R2014b
- # 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 路径和 python 接口,PYTHON_INCLUDE PYTHON_LIB, WITH_PYTHON_LAYER
- 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)/anaconda
- # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
- # $(ANACONDA_HOME)/include/python2.7 \
- # $(ANACONDA_HOME)/lib/python2.7/site-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.
- # 其它依赖库,这里依赖了自己编译的 protobuf 和 opencv
- INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/include /usr/include/hdf5/serial /home/longpeng/opts/opencv3.2/include /home/longpeng/opts/protobuf3.1/include /usr/local/lib/python2.7/dist-packages/numpy/core/include/
- LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/cuda-8.0/lib64 /home/longpeng/opts/opencv3.2/lib /home/longpeng/opts/protobuf3.1/lib
- # 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
- # 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
- BUILD_DIR := build
- DISTRIBUTE_DIR := distribute
- #build with support for Python layers
- WITH_PYTHON_LAYER:=1
- # 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 ?= @
- # without using a build folder, for me there will be linking problem says libcaffe.o cant not find “xxx"
- https://github.com/BVLC/caffe/issues/2348#issuecomment-97093859
-
- # fatal error: hdf5.h: No such file or directory
- http://homeway.me/2018/01/25/setup-caffe-for-deep-learning/
-
- # Unsupported gpu architecture ‘compute_20'
- https://blog.csdn.net/kemgine/article/details/78781377
- http://www.caffecn.cn/?/question/1077
-
- # 使用 opencv3
- OPENCV_VERSION := 3
- https://github.com/BVLC/caffe/issues/3517
-
- # 是否使用 python 定义网络,要打开
- # Uncomment to support layers written in Python (will link against Python libs)
- WITH_PYTHON_LAYER := 1
Anaconda 在 OpenCV 和 Caffe 装好之后装,不然会导致 OpenCV 和 Caffe 的 Python 库依赖到 Anaconda 上去,我们希望它们使用系统的 Python。
具体安装我有写过一篇: 如何使用Anaconda
现在 TensorFlow2.0 已经出了,不过没有发行正式版,无法通过 anaconda 安装,需要通过 pip 进行安装。
前面的环境配置好之后,TensorFlow 的安装并不难。
其它可能还会安装一下 Docker,日常用的软件等。原则就是用到啥就下载啥,用啥自己心里要有数,别把环境搞乱就行。
不过也别害怕,不是生产环境顶多重新配,就是会浪费点时间,还是谨慎点好。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。