Ubuntu20.04 PyRetri基于PyTorch的基于开源深度学习的无监督图像检索工具箱🔥 环境搭建

一、驱动安装

1、根据推荐选择NVIDIA驱动版本安装,安装后重启电脑


驱动安装

2、重启后检查NVIDIA驱动安装是否成功

  • 查看关于“图形”,是否显示了NVIDIA


    查看图形出,看驱动是否成功
  • 查看你的显卡驱动所使用的内核版本,命令:cat /proc/driver/nvidia/version
cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module  455.45.01  Thu Nov  5 23:03:56 UTC 2020
GCC version:  gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) 
  • 查看本地驱动安装情况,命令:cat /var/log/dpkg.log | grep nvidia
cat /var/log/dpkg.log | grep nvidia
2020-11-21 17:36:53 install libnvidia-cfg1-455:amd64 <无> 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:53 status half-installed libnvidia-cfg1-455:amd64 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:53 status unpacked libnvidia-cfg1-455:amd64 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:53 install libnvidia-common-455:all <无> 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:53 status half-installed libnvidia-common-455:all 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:53 status unpacked libnvidia-common-455:all 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:53 install libnvidia-compute-455:amd64 <无> 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:53 status half-installed libnvidia-compute-455:amd64 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:55 status unpacked libnvidia-compute-455:amd64 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:55 install libnvidia-compute-455:i386 <无> 455.38-0ubuntu0.20.04.1
2020-11-21 17:36:55 status half-installed libnvidia-compute-455:i386 455.38-0ubuntu0.20.04.1
***

或,命令:sudo dpkg --list | grep nvidia-*

sudo dpkg --list | grep nvidia-*
ii  libnvidia-cfg1-455:amd64                   455.45.01-0ubuntu1                    amd64        NVIDIA binary OpenGL/GLX configuration library
ii  libnvidia-common-455                       455.45.01-0ubuntu1                    all          Shared files used by the NVIDIA libraries
ii  libnvidia-compute-455:amd64                455.45.01-0ubuntu1                    amd64        NVIDIA libcompute package
ii  libnvidia-decode-455:amd64                 455.45.01-0ubuntu1                    amd64        NVIDIA Video Decoding runtime libraries
ii  libnvidia-encode-455:amd64                 455.45.01-0ubuntu1                    amd64        NVENC Video Encoding runtime library
ii  libnvidia-extra-455:amd64                  455.45.01-0ubuntu1                    amd64        Extra libraries for the NVIDIA driver
ii  libnvidia-fbc1-455:amd64                   455.45.01-0ubuntu1                    amd64        NVIDIA OpenGL-based Framebuffer Capture runtime library
ii  libnvidia-gl-455:amd64                     455.45.01-0ubuntu1                    amd64        NVIDIA OpenGL/GLX/EGL/GLES GLVND libraries and Vulkan ICD
ii  libnvidia-ifr1-455:amd64                   455.45.01-0ubuntu1                    amd64        NVIDIA OpenGL-based Inband Frame Readback runtime library
ii  nvidia-compute-utils-455                   455.45.01-0ubuntu1                    amd64        NVIDIA compute utilities
ii  nvidia-dkms-455                            455.45.01-0ubuntu1                    amd64        NVIDIA DKMS package
ii  nvidia-driver-455                          455.45.01-0ubuntu1                    amd64        NVIDIA driver metapackage
ii  nvidia-kernel-common-455                   455.45.01-0ubuntu1                    amd64        Shared files used with the kernel module
ii  nvidia-kernel-source-455                   455.45.01-0ubuntu1                    amd64        NVIDIA kernel source package
ii  nvidia-modprobe                            455.45.01-0ubuntu1                    amd64        Load the NVIDIA kernel driver and create device files
ii  nvidia-prime                               0.8.14                                all          Tools to enable NVIDIA's Prime
ii  nvidia-settings                            455.45.01-0ubuntu1                    amd64        Tool for configuring the NVIDIA graphics driver
ii  nvidia-utils-455                           455.45.01-0ubuntu1                    amd64        NVIDIA driver support binaries
ii  screen-resolution-extra                    0.18build1                            all          Extension for the nvidia-settings control panel
ii  xserver-xorg-video-nvidia-455              455.45.01-0ubuntu1                    amd64        NVIDIA binary Xorg driver

二、CUDA安装,使用阿里镜像加速方式

wget https://mirrors.aliyun.com/nvidia-cuda/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://mirrors.aliyun.com/nvidia-cuda/ubuntu2004/x86_64/7fa2af80.pub
sudo add-apt-repository "deb https://mirrors.aliyun.com/nvidia-cuda/ubuntu2004/x86_64/ /"
sudo apt-get update
sudo apt-get -y install cuda

注意:使用sudo apt-get -y install cuda-11-1方式安装时可能会导致错误:依赖: cuda-runtime-11-1 (>= 11.1.1) 但是它将不会被安装依赖: cuda-toolkit-11-1 (>= 11.1.1) 但是它将不会被安装依赖: cuda-demo-suite-11-1 (>= 11.1.74) 但是它将不会被安装,使用aptitude安装:

  • 先安装aptitude
sudo apt-get install aptitude
  • 再安装cuda
sudo aptitude install cuda

安装完成后,重启一下电脑

  • 查看安装结果
ls -l /usr/local/
lrwxrwxrwx  1 root root   22 11月 22 11:09 cuda -> /etc/alternatives/cuda
lrwxrwxrwx  1 root root   25 11月 22 11:09 cuda-11 -> /etc/alternatives/cuda-11
  • 配置环境变量,命令:gedit ~/.bashrc
$ gedit ~/.bashrc
  • 验证安装结果,命令:nvcc --version
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Oct_12_20:09:46_PDT_2020
Cuda compilation tools, release 11.1, V11.1.105
Build cuda_11.1.TC455_06.29190527_0

三、Anaconda安装,默认Python3

按回车键,一直按住回车键,直到出现下图所示:

image

输入yes:

image

然后按回车键,出现下图的画面输入yes后自动添加环境变量

image
  • 配置环境变量
gedit ~/.bashrc

底部添加,路径根据自己的实际情况进行修改

export PATH=/home/oem/anaconda3/bin{PATH:+:{PATH}}`
刷新重新加载配置

source ~/.bashrc

查看版本

$ conda --version
conda 4.9.2
  • 更换国内源,安装提速
    可先执行 conda config --set show_channel_urls yes生成.condarc文件之后再修改
conda config --set show_channel_urls yes

修改配置

gedit .condarc

替换内容:

channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

运行 conda clean -i 清除索引缓存,保证用的是镜像站提供的索引。
到这里已经完成安装。

# 创建 python38虚拟环境,python版本为3.8
conda create -n python38 python=3.8
# 激活环境
source activate
# 退出环境
source deactivate
# 切换环境
conda activate your-env-name

三、安装pytorch

conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch
  • 安装完成后,测试安装结果
$ python3
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch 
>>> x = torch.empty(5, 3) 
>>> print(x) 
tensor([[1.0966e+14, 4.5883e-41, 1.0966e+14],
        [4.5883e-41, 1.0966e+14, 4.5883e-41],
        [1.0968e+14, 4.5883e-41, 1.0966e+14],
        [4.5883e-41, 1.0968e+14, 4.5883e-41],
        [1.0968e+14, 4.5883e-41, 1.0968e+14]])

四、安装numpy、sklearn、yacs、tqdm

conda install numpy
conda install scikit-learn
conda install yacs
conda install tqdm

如果安装失败,另外一种源码安装

$ git clone https://github.com/rbgirshick/yacs.git
$ cd yacs
$ python setup.py install

五、安装NCCL

为了下载NCCL,请确保您已注册NVIDIA开发人员计划

  1. 转到:NVIDIA NCCL主页
  2. 点击下载。
  3. 完成简短调查,然后单击Submit。
  4. 接受条款和条件。显示NCCL的可用下载版本列表。
  5. 选择要安装的NCCL版本。显示可用资源列表。请参考以下各节,以根据所使用的Linux发行版选择正确的软件包。

Ubuntu

在Ubuntu上安装NCCL要求您首先将包含NCCL软件包的存储库添加到APT系统,然后通过APT安装NCCL软件包。有两个可用的存储库;本地存储库和网络存储库。建议选择后者以在发布新版本时轻松检索升级。

  1. 安装存储库。
  • 对于本地NCCL 存储库:
sudo dpkg -i nccl-repo-<version>.deb
  • 对于网络存储库:
sudo dpkg -i nvidia-machine-learning-repo-<version>.deb
  1. 更新APT数据库:
sudo apt update
  1. 安装 libnccl2与APT打包。此外,如果您需要使用NCCL编译应用程序,则可以安装 libnccl开发 包装:

注意:如果使用网络存储库,则以下命令将CUDA升级到最新版本。

sudo apt install libnccl2 libnccl-dev

如果您希望保留较旧版本的CUDA,请指定特定版本,例如:

sudo apt install libnccl2=2.4.8-1+cuda10.0 libnccl-dev=2.4.8-1+cuda10.0

请参阅下载页面以获取确切的软件包版本。

已知错误排查

(fastreid) oem@zxb:~/work/pwork/fast-reid$ python tools/train_net.py --config-file ./configs/Market1501/bagtricks_R50.yml --num-gpus 4
./fastreid/evaluation/rank.py:15: UserWarning: Cython rank evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython rank evaluation (very fast so highly recommended) is '
./fastreid/evaluation/roc.py:19: UserWarning: Cython roc evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython roc evaluation (very fast so highly recommended) is '
Command Line Args: Namespace(config_file='./configs/Market1501/bagtricks_R50.yml', dist_url='tcp://127.0.0.1:62767', eval_only=False, machine_rank=0, num_gpus=4, num_machines=1, opts=[], resume=False)
./fastreid/evaluation/rank.py:15: UserWarning: Cython rank evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython rank evaluation (very fast so highly recommended) is '
./fastreid/evaluation/rank.py:15: UserWarning: Cython rank evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython rank evaluation (very fast so highly recommended) is '
./fastreid/evaluation/rank.py:15: UserWarning: Cython rank evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython rank evaluation (very fast so highly recommended) is '
./fastreid/evaluation/rank.py:15: UserWarning: Cython rank evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython rank evaluation (very fast so highly recommended) is '
./fastreid/evaluation/roc.py:19: UserWarning: Cython roc evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython roc evaluation (very fast so highly recommended) is '
./fastreid/evaluation/roc.py:19: UserWarning: Cython roc evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython roc evaluation (very fast so highly recommended) is '
./fastreid/evaluation/roc.py:19: UserWarning: Cython roc evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython roc evaluation (very fast so highly recommended) is '
./fastreid/evaluation/roc.py:19: UserWarning: Cython roc evaluation (very fast so highly recommended) is unavailable, now use python evaluation.
'Cython roc evaluation (very fast so highly recommended) is '
Process group URL: tcp://127.0.0.1:62767
Process group URL: tcp://127.0.0.1:62767
Process group URL: tcp://127.0.0.1:62767
Process group URL: tcp://127.0.0.1:62767
Traceback (most recent call last):
File "tools/train_net.py", line 57, in <module>
args=(args,),
File "./fastreid/engine/launch.py", line 68, in launch
daemon=False,
File "/home/oem/anaconda3/envs/fastreid/lib/python3.7/site-packages/torch/multiprocessing/spawn.py", line 199, in spawn
return start_processes(fn, args, nprocs, join, daemon, start_method='spawn')
File "/home/oem/anaconda3/envs/fastreid/lib/python3.7/site-packages/torch/multiprocessing/spawn.py", line 157, in start_processes
while not context.join():
File "/home/oem/anaconda3/envs/fastreid/lib/python3.7/site-packages/torch/multiprocessing/spawn.py", line 118, in join
raise Exception(msg)
Exception:
-- Process 2 terminated with the following error:
Traceback (most recent call last):
File "/home/oem/anaconda3/envs/fastreid/lib/python3.7/site-packages/torch/multiprocessing/spawn.py", line 19, in _wrap
fn(i, *args)
File "./fastreid/engine/launch.py", line 86, in _distributed_worker
raise e
File "./fastreid/engine/launch.py", line 81, in _distributed_worker
backend="NCCL", init_method=dist_url, world_size=world_size, rank=global_rank
File "/home/oem/anaconda3/envs/fastreid/lib/python3.7/site-packages/torch/distributed/distributed_c10d.py", line 442, in init_process_group
barrier()
File "/home/oem/anaconda3/envs/fastreid/lib/python3.7/site-packages/torch/distributed/distributed_c10d.py", line 1947, in barrier
work = _default_pg.barrier()
RuntimeError: NCCL error in: /opt/conda/conda-bld/pytorch_1603729047590/work/torch/lib/c10d/ProcessGroupNCCL.cpp:784, invalid usage, NCCL version 2.7.8

  • 原因:NCCL 未安装或GPU数量设置错误

conda本地包安装

在想要安装 pytorch-1.2 时,安装pytorch官方指导,以管理员身份打开 Anaconda Prompt,输入以下命令进行安装

conda install pytorch torchvision cudatoolkit=10.0 -c pytorch

但是网速特别慢,过了一会儿后,显示以下信息


在这里插入图片描述

原因是网速太慢或被墙,导致安装失败,如何解决呢?
从该提示信息中,发现了安装包下载的网址为

https://conda.anaconda.org/pytorch/win-64/pytorch-1.2.0-py3.5_cuda100_cudnn7_1.tar.bz2

将该网址复制到浏览器的地址栏,将自动下载该地址包(前提是网络能穿墙)。
下载完后将该包放到 Anaconda3/pkgs 目录,或者移动到包所在的目录


在这里插入图片描述

执行以下命令可进行本地安装

conda install --use-local pytorch-1.2.0-py3.5_cuda100_cudnn7_1.tar.bz2

总结

以后遇到类似情况,可以先通过浏览器手动下载相应的包,然后利用conda install --use-local 进行本地安装

查看torch 和 CUDA版本

import torch
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.version.cuda)

包安装

  • pip批量导出包含环境中所有组件的requirements.txt文件
pip freeze > requirements.txt
  • pip批量安装requirements.txt文件中包含的组件依赖
pip install -r requirements.txt
  • conda批量导出包含环境中所有组件的requirements.txt文件
conda list -e > requirements.txt
  • conda批量安装requirements.txt文件中包含的组件依赖
conda install --yes --file requirements.txt
  • 安装opencv
pip install -i https://pypi.mirrors.ustc.edu.cn/simple/ opencv-python

查看 GPU 运行情况

  • Linux
# -l 实时监控
 nvidia-smi -l
  • Windows
cd C:\Program Files\NVIDIA Corporation\NVSMI
# -l 实时监控
nvidia-smi.exe -l
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345

推荐阅读更多精彩内容