ubuntu16 conda虚拟环境 配置mmdetection2.3 完整过程

安装版本

torch:1.5.1
python 3.6
mmdetection:2.3.0
mmcv-full 1.0.5

步骤

1、创建conda虚拟环境

conda create -n mmlab2.3 python=3.6 //创建一个名为mmlab2.3的虚拟环境
conda activate mmlab2.3 //进入虚拟环境

2、安装torch

根据自己系统cuda版本,安装torch 1.5.1,pytorch官网,我用的pip安装,如果网慢也可以考虑wheel安装
torch版本
安装结束后,验证torch是否可用:
进入python环境,使用命令import torch torch.cuda.is_available()为true即成功!
验证torch是否安装成功

3、安装mmcv-full

mmdetection要求mmcv-full版本是1.05,自己去mmcv github下载,再安装

把github上的mmcv-full 1.0.5代码下载下来。

安装:

cd mmcv-1.0.5  # 进入下载的文件夹
pip install pytest-runner
pip install opencv-python==4.2.0.34 # 必须是这个版本
MMCV_WITH_OPS=1 pip install -e .

4、安装pycocoapi

!!!!注意:mmdetection 在 cocoapi 外面有封装,所以要使用 mmdetection 封装后的 cocoapi
有两种方法,一是git clone 会报错,二是源码安装,现在用官方之前推荐的gut clone会报错,也不知道为什么,于是直接源码下载安装

第一步:github下载cocoapi-master

第二步:

cd cocoapi-master/pycocotools  #进入文件夹
python setup.py install   #安装

5、安装mmdetection

第一步:
在github下载mmdetection 2.3.0版本

第二步:

cd mmdetection
pip install -r requirements/build.txt
pip install -v -e .

可能会遇到报错 git clone cocoapi 错误,这是因为代码里还是默认git clone cocoapi;
解决:
删除requirments中所有git clone cocoapi语句,requirments有以下几个文件,一个个看,我记得是有两个文件里有git clone cocoapi语句,直接删除。
在这里插入图片描述

至此,安装已经完成,使用conda list即可查看,如果mmcv-full mmdet都存在,就是成功安装了。

5.1 测试代码可能会遇到的错误

报错"Tcl": have 8.6.11 , need exactly 8.6.8

在这里插入图片描述
解决:
参考这个文档,把两个文件中的8.6.8改为8.6.11(和文档是反的)
https://www.codeleading.com/article/77285561880/

报错cv2.imwrite … empty
是因为封装的show_result_pyplot函数返回的是空值,可以自己简单封装一下。
https://blog.csdn.net/m0_60360632/article/details/121554049

5.2我自己的测试代码

from mmdet.apis import init_detector, inference_detector # , show_result_pyplot
import cv2

def show_result_pyplot(model, img, result, score_thr=0.3, fig_size=(15, 10)):
    """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

config_file = './configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
# 从 model zoo 下载 checkpoint 并放在 `checkpoints/` 文件下
# 网址为: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
checkpoint_file = '../checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = 'cuda:0'

# 初始化检测器
model = init_detector(config_file, checkpoint_file, device=device)

# 推理演示图像
name = "./demo/demo.jpg"
result = inference_detector(model, name)

img = show_result_pyplot(model, name, result, score_thr=0.8)
cv2.imwrite("demo1.jpg", img)

安了好多个版本都报错,开始安装最新版本mmdet2.24.0, mmcv-full一直安上后mmdet检测不到,报错No Module named mmcv,没找到解决方法,就安了一个旧版本用着。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值