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 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) $ nvidia-smi 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 | +-------------------------------+----------------------+----------------------+
另外,对于 CUDA 和 cuDNN 版本的查询可通过以下方式进行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ nvcc --version 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 --
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 $ 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 $ 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 Solving environment: \ 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)? Downloading and Extracting Packages ca-certificates-2020 | 147 KB | certifi-2020.4.5.2 | 152 KB | ld_impl_linux-64-2.3 | 616 KB | Preparing transaction: done Verifying transaction: done Executing transaction: done
安装完成后,我们可以通过 $ 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
conda 常用命令 1 2 3 4 5 6 7 8 9 $ conda --version $ conda creat -n your_env_name python==3.x $ conda remove --name your_env_name --all $ conda create --name new_env --clone old_env $ source activate your_env_name $ source deactivate $ conda info -e $ conda install --name your_env_name package_name $ conda update conda