【Linux & Deep learning】从0开始安装MMDetection

前言

废话不多说,跟着操作就行了,别搞错了。
建立环境:Ubuntu18.04服务器 + 能跑深度学习的N卡。
注:由于外链太多,无法推荐,大家自行百度试试…

第一步:安装conda

可选的

根据需要,自行选择 Anaconda3 / Miniconda3 。

conda安装流程(以miniconda为例)

Linux系统(以Ubuntu为例)

wget -c https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod 777 Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh

Windows系统/SSH操作

  1. 安装 Xftp7 (或其他远程传输软件)
  2. 连接到远程Linux服务器
  3. 下载最新版本miniconda3
  4. 将文件拖到服务器上相应的文件夹中(路径请自行选择)
  5. 安装MobaXterm或其他SSH远程连接软件
  6. MobaXterm连接到远程Linux服务器(自行百度)
  7. cd到4中自行选择的路径,以 /conda 为例
chmod 777 Miniconda3-latest-Linux-x86_64.sh
cd /conda
sh Miniconda3-latest-Linux-x86_64.sh

如果没有疑问,从现在开始,以终端语言执行操作。

检查conda安装情况

conda -V
若出现conda 4.x.0
表明安装成功

第二步:安装nvidia驱动

可用nvidia-smi查看。

一般来说有,如果没有,请百度或咨询老师、同学、同事…

第三步:安装CUDA和cudnn

强烈建议:选择CUDA10.2+pytorch1.9.0
也可以选择其他版本,但以上版本完美通过测试

如果之前存在另外的CUDA版本

找到cuda目录,卸载:
cuda目录以/usr/local/cuda-10.0为例:

# 默认为root操作,无root请加sudo
/usr/local/cuda-10.0/bin/uninstall_cuda_10.0.pl
rm -rf /usr/local/cuda-10.0/

安装CUDA←

选择对应选项

# 根据上述选项,可以得到以下操作命令
# 该命令截止2022.03.22为最新
# 请先cd到合适的下载路径
wget https://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run
sh cuda_10.2.89_440.33.01_linux.run

如果此时没有错误,应该进入安装界面。安装界面为命令行人机交互页面,如下图:
【Linux & Deep learning】从0开始安装MMDetection
accept之后,到如下界面,用Enter键筛选如下(其他有必要再安装)。
【Linux & Deep learning】从0开始安装MMDetection
注1:Done为返回上一级。Install为开始安装。
注2:Options->Toolkits Options->Change Toolkit Install Path中选择合适的安装路径,此处仅截图为例。
【Linux & Deep learning】从0开始安装MMDetection
安装完成后会显示类似successful的提示(此处就不再重复安装了,故不出示图例)。之后开始配置环境:

vim ~/.bashrc
# 如使用的是zsh,则为
# vim ~/.zshrc
# 其他同理

通过vim操作加入如下三句。(别说vim也不会!!)

export CUDA_HOME=/usr/local/cuda-10.0
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64
export PATH=${CUDA_HOME}/bin:${PATH}

最后激活重载环境配置。

source ~/.bashrc

安装cudnn←

【Linux & Deep learning】从0开始安装MMDetection
cudnn通过windows下载+ftp文件传输是最简单、快捷、方便的。(所以此处不用纯命令行安装方式)
安装cudnn需要登录自己的Nvidia账号,没有就注册一个吧。登录过程比较麻烦,需要多点耐心。出现下图的话可以按暂不跳过。【Linux & Deep learning】从0开始安装MMDetection
点击下载,如红色箭头所示。
【Linux & Deep learning】从0开始安装MMDetection
同样按照conda安装中SSH操作的步骤,将下载好的tar.xz文件放到合适的解压路径。
接下来运行解压缩命令:

# 按照流程,下载的压缩包名称为
# cudnn-linux-x86_64-8.3.2.44_cuda10.2-archive.tar.xz
tar -xvf cudnn-linux-x86_64-8.3.2.44_cuda10.2-archive.tar.xz
# 不用 -zxvf 的原因是可能会出现如下报错:
# gzip: stdin: not in gzip format
# tar: Child returned status 1
# tar: Error is not recoverable: exiting now

解压完成后,输入ls,应该会发现同名文件夹:
【Linux & Deep learning】从0开始安装MMDetection

# 每个人的安装路径都不同,所以根据自己的下载路径进行对应更改
cp /*cudnn安装路径*/lib/* /*cuda安装路径*/lib64/
cp /*cudnn安装路径*/include/* /*cuda安装路径*/include/

别着急,我们待会再检查CUDA、cudnn是否安装成功。

第四步:安装MMDetection

首先激活环境,并安装pytorch:

conda create -n open-mmlab python=3.7 -y
conda activate open-mmlab
conda install pytorch==1.9.0 torchvision==0.10.0 torchaudio==0.9.0 cudatoolkit=10.2 -c pytorch

这个时候,我们来检查一下torch、CUDA、cudnn的安装情况:

python # 打开终端的python
import torch
print(torch.__version__)
print(torch.version.cuda)
print(torch.backends.cudnn.version())

【Linux & Deep learning】从0开始安装MMDetection
如果结果如上图,那么没有问题,继续。

pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.9.0/index.html
pip install mmdet

这里当然可以按照MMDetection的步骤走git clone,但是对于服务器无法翻墙的同学们来说,最好用pip自动安装方式。

终于结束了!继续冲! ! !

第五步:运行你的第一个mmdet程序

准备mmdetection文件夹

github搜索mmdetection,download zip(win)/git clone(linux)然后解压放到特定位置什么的不用我多说了好吗?假设这是路径A。

折腾了这么久,终于可以测试了

创建一个py文件并将其命名为mmdet_test.py。然后,直接上代码:
请先下载faster rcnn pth模型放置到文件夹中,记住路径,假设这是路径B。

from mmdet.apis import init_detector, inference_detector
import cv2

def show_result_pyplot(model, img, result, score_thr=0.5):
    """Visualize the detection results on the image.
    Args:
        model (nn.Module): The loaded detector.
        img (str or np.ndarray): Image filename or loaded image.
        result (tuple[list] or list): The detection result, can be either
            (bbox, segm) or just bbox.
        score_thr (float): The threshold to visualize the bboxes and masks.
        fig_size (tuple): Figure size of the pyplot figure.
    """
    if hasattr(model, 'module'):
        model = model.module
    img = model.show_result(img, result, score_thr=score_thr, show=False)
    return img
    
# 记得路径A吗?加上/mmdetection/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py这一段就可以了
config_file = 
# 示例:config_file = '/workspace/cwh/mmdetection/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'

# 刚叫你下载的pth文件!记得路径B吗?
checkpoint_file = 
# 示例:checkpoint_file = './dataset/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'

# 使用CUDA
device = 'cuda:0'
# 初始化检测器
model = init_detector(config_file, checkpoint_file, device=device)
# 推理演示图像
# 还是你的路径A!看看能不能找到/mmdetection/demo/demo.jpg
img_path =
# 示例:img_path = '/workspace/cwh/mmdetection/demo/demo.jpg'
result = inference_detector(model, img_path)
# 只显示>0.8评分的框
det_img = show_result_pyplot(model, img_path, result, score_thr=0.8)
# 选择一个输出图像的路径
out_file =
# 示例:out_file = '/workspace/cwh/result.jpg'
cv2.imwrite(out_file, det_img)

这里我给一张图:
【Linux & Deep learning】从0开始安装MMDetection

如果你能得到这个结果:
请添加图片描述
结束辣!恭喜你的成绩:“我也是一个对目标检测有点了解的人。”

结语

这种环境已经把我弄得乱七八糟了两三天,我的心态都快崩溃了。我当时没有及时记录。可能有一些错误的地方我没有写下来。看看有没有热心的观众提问。当然,最好没人问。证明教程完美通过了哈哈~
写于2022.03.22

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2022年3月23日 下午2:35
下一篇 2022年3月23日 下午10:40

相关推荐