GeophyAI

Python与地球物理数据处理

0%

CentOS下Anaconda及Tensorflow安装

CentOS7下深度学习环境配置(Anaconda+Tensorflow+Keras)

硬件信息查询

笔者的CPU和GPU型号分别如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ cat /proc/cpuinfo |grep name|cut -f2 -d:|uniq -c #查看CPU型号
12 Intel(R) Xeon(R) E-2286G CPU @ 4.00GHz
$ lspci | grep -i nvidia 查看连接的GPU设备
01:00.0 VGA compatible controller: NVIDIA Corporation TU102 [GeForce RTX 2080 Ti Rev. A] (rev a1)
01:00.1 Audio device: NVIDIA Corporation TU102 High Definition Audio Controller (rev a1)
01:00.2 USB controller: NVIDIA Corporation TU102 USB 3.1 Host Controller (rev a1)
01:00.3 Serial bus controller [0c80]: NVIDIA Corporation TU102 USB Type-C UCSI Controller (rev a1)
#如果CUDA和驱动已经安装完成并且~/.bashrc也配置完成则可以通过 nvidia-smi 查询驱动及GPU信息。
$ nvidia-smi #输出GPU运行状态及驱动版本
Tue Jun 9 11:30:14 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.82 Driver Version: 440.82 CUDA Version: 10.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce RTX 208... Off | 00000000:01:00.0 On | N/A |
| 66% 84C P2 233W / 250W | 10969MiB / 11016MiB | 91% Default |
+-------------------------------+----------------------+----------------------+

另外,对于 CUDAcuDNN 版本的查询可通过以下方式进行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ nvcc --version #查询cuda版本
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130
#或者
$ cat /usr/local/cuda/version.txt
CUDA Version 10.0.130
$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2 #查询cudnn版本
#define CUDNN_MAJOR 7
#define CUDNN_MINOR 6
#define CUDNN_PATCHLEVEL 5
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)
#include "driver_types.h"

Anaconda 安装

64位Linux的 Anacoda 安装包可以到清华源上下载,笔者下载安装的是5.3.1版本(下载地址),文件格式为 .sh,安装包下载完成后,我们进入到下载目录中,打开 terminal 并输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ bash Anaconda3-5.3.1-Linux-x86_64.sh -u # -u可自定义安装目录
#进入安装后敲回车,直到出现界面并输入yes继续安装或者no退出安装:
$ Please answer 'yes' or 'no':'
>>> yes
Anaconda3 will now be installed into this location:
/当前将要安装的目录
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/home/wangsw/anaconda3] >>> /此处输入你需要安装的目录
#部分目录需要其它用户或者root权限,确认之后之后将会完成安装,最后会提示是否需要将Anaconda路径写入环境变量(推荐自动写入)以及是否需要使用VSCode(根据个人需要):
$ Do you wish the installer to initialize Anaconda3
in your /root/.bashrc ? [yes|no]
[no] >>> no
$ Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>> no
#此时安装已经完成。

conda 环境创建

然后我们开始配置 conda 环境,不同 conda 环境可以安装不同 python 版本以及 tensorflow 等版本,其相当于一个容器,可用于不同需求的生产环境。为了加速 conda 环境配置过程,与安装完系统后进行相同的操作:更新下载源(可使用清华源、中科大源等)

1
2
3
4
5
6
7
$ conda config --show #输入后找到 channels 属性可查看已有下载源,若没有镜像源则执行以下命令
$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
$ conda config --set show_channel_urls yes #该命令可允许下载时显示源地址
$ conda clean -i #清除索引缓存,保证用的是镜像站提供的索引。
$ conda config --show #查看是否源地址是否更新完成

更新完源之后我们建立深度学习环境,首先创建一个特定 python 版本的 conda 环境:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
$ conda create -n env_name python=3.x #env_name处输入自己想要建立的环境名称,x处填写需要的python版本,笔者为了保证原有代码的可移植性,使用python3.6版本
Solving environment: \ #环境配置中,耐心等待即可
## Package Plan ##
environment location: /xxxxxx/Anaconda/envs/env_name
added / updated specs:
- python=3.6

The following packages will be downloaded:
package | build
---------------------------|-----------------
certifi-2020.4.5.2 | py36h9f0ad1d_0 152 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
ld_impl_linux-64-2.34 | h53a641e_5 616 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
ca-certificates-2020.4.5.2 | hecda079_0 147 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
------------------------------------------------------------
Total: 914 KB

The following NEW packages will be INSTALLED:

_libgcc_mutex: 0.1-conda_forge https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
_openmp_mutex: 4.5-0_gnu https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
ca-certificates: 2020.4.5.2-hecda079_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
certifi: 2020.4.5.2-py36h9f0ad1d_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
ld_impl_linux-64: 2.34-h53a641e_5 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libffi: 3.2.1-he1b5a44_1007 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libgcc-ng: 9.2.0-h24d8f2e_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libgomp: 9.2.0-h24d8f2e_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libstdcxx-ng: 9.2.0-hdf63c60_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
ncurses: 6.1-hf484d3e_1002 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
openssl: 1.1.1g-h516909a_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pip: 20.1.1-py_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python: 3.6.10-h8356626_1011_cpython https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python_abi: 3.6-1_cp36m https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
readline: 8.0-hf8c457e_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
setuptools: 47.1.1-py36h9f0ad1d_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
sqlite: 3.30.1-hcee41ef_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
tk: 8.6.10-hed695b0_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
wheel: 0.34.2-py_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
xz: 5.2.5-h516909a_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
zlib: 1.2.11-h516909a_1006 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge

Proceed ([y]/n)? #输入y即可,可以看到安装包均从上文中配置的地址中下载

Downloading and Extracting Packages
ca-certificates-2020 | 147 KB | ##################################### | 100%
certifi-2020.4.5.2 | 152 KB | ##################################### | 100%
ld_impl_linux-64-2.3 | 616 KB | ##################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use:
# > source activate env_name
#
# To deactivate an active environment, use:
# > source deactivate
#

安装完成后,我们可以通过 $ source activate env_name来打开创建好的环境,pip list可查看当前环境下已经安装的轮子,pip show [package_name]可以查看轮子的版本等信息(输入时去掉中括号)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(env_name)[root]$ pip list #查看已安装轮子
Package Version
---------- -------------------
certifi 2020.4.5.2
pip 20.1.1
setuptools 47.1.1.post20200529
wheel 0.34.2
(env_name)[root]$ pip show certifi #查看轮子信息
Name: certifi
Version: 2020.4.5.2
Summary: Python package for providing Mozilla's CA Bundle.
Home-page: https://certifiio.readthedocs.io/en/latest/
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: MPL-2.0
Location: /..../Anaconda/envs/test/lib/python3.6/site-packages
Requires:
Required-by:

同样的,为了加速 conda 环境内 pip 下载的速度我们首先更换下载源:

1
2
(env_name)[root]$ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
Writing to /..../.config/pip/pip.conf

之后,可通过 $pip install package 的命令下载和安装轮子了(如果没有配置下载源的话,可以通过$ pip install package -i https://pypi.tuna.tsinghua.edu.cn/simple的方式临时使用 url 下载)。需要注意的是,该环境下安装的包只能在环境内使用,这也允许我们能够使用多个版本的生产环境,只需要$ source activate env_name即可。

1
2
3
4
5
#笔者使用的生产环境之一为:
(env_name)[root]$ pip install tensorflow-gpu==1.14.0
(env_name)[root]$ pip install keras==2.2.5
(env_name)[root]$ pip install numpy==1.16.0
(env_name)[root]$ source deactivate #退出已打开的环境env_name

conda 常用命令

1
2
3
4
5
6
7
8
9
$ conda --version #查看conda版本
$ conda creat -n your_env_name python==3.x #创建python版本为3.x的conda环境
$ conda remove --name your_env_name --all #删除已创建环境
$ conda create --name new_env --clone old_env #创建新conda环境new_env并将old_env环境复制到前者中
$ source activate your_env_name #激活已创建环境
$ source deactivate #退出当前窗口中已激活环境
$ conda info -e #查询已安装环境
$ conda install --name your_env_name package_name #在指定环境your_env_name中安装package_name包(例如conda install my_env tensorflow-gpu==2.0.0)
$ conda update conda #(不推荐)将conda更新至最新版本,但也会更新其它相关包