(详细教程)mmdetection训练自己的模型,测试,评估

本文详细介绍了如何在mmdetection框架下,基于PyTorch训练和测试目标检测模型。内容包括环境配置、数据集转换、配置文件修改、模型训练与测试的步骤,以及相关资源推荐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 简介

商汤科技(2018 COCO 目标检测挑战赛冠军)和香港中文大学最近开源了一个基于Pytorch实现的深度学习目标检测工具箱mmdetection,支持Faster-RCNN,Mask-RCNN,Fast-RCNN,Cascade-RCNN等主流目标检测框架。可以快速部署自己的模型。

2.环境要求

  1. Linux (不支持windows)
  2. Python 3.5+
  3. >=PyTorch 1.1
  4. >=CUDA 9.0
  5. NCCL 2
  6. >=GCC 4.9
  7. mmcv

3.安装

按照官方文档建议先安装Anaconda,创建python虚拟环境,使用conda进行安装

  1. conda创建一个虚拟环境
conda create -n open-mmlab python=3.7 -y #创建名为open-mmlab,python版本为3.7的虚拟环境
conda activate open-mmlab  #进入虚拟环境
  1. 安装pytorch及torchvision
conda install pytorch torchvision -c pytorch

将会安装以下包
在这里插入图片描述
如果不指定任何版本会安装最新的cuda10.1版的pytorch=1.4。可以指定pytorch和对应的cuda版本,如安装cuda9.0版的pytorch1.1

conda install pytorch=1.1 torchvision cudatoolkit=9.0 -c pytorch

网速问题
conda 使用默认源可能下载的很慢,可以改成国内的清华源,在用户目录下创建.condarc文件

gedit ~/.condarc

将下面这部分清华源代码复制到用户目录~/.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 config --show-sources

显示清华源就好了,如果不行conda clean -i更新一下。
还因为网速问题下载不了的话,下载不了的时候会在错误中提示包的下载地址,手动下载下来放到~/anaconda3/pkgs/目录下,使用以下命令安装本地包

conda install --use-local  mypackage
  1. 下载mmdetection
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
  1. 安装mmdetection
pip install mmcv cython   #安装mmcv和cython
pip install albumentations>=0.3.2 imagecorruptions pycocotools six terminaltables #安装依赖包
python setup.py develop  #编译mmtection库,需要等一会

pip安装时如果速度很慢,临时使用在后面加 -i https://pypi.tuna.tsinghua.edu.cn/simple,使用国内源,例如使用清华源安装mmcv:

pip install mmcv -i https://pypi.tuna.tsinghua.edu.cn/simple

永久更换为国内源则建文件~/.pip/pip.conf

mkdir ~/.pip
cd ~/.pip
gedit pip.conf

复制以下内容即可

[global]
 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

必须先安装mmcv,再运行setup.py编译,不然会报错。

  1. 验证是否安装成功

下载一个faster_rcnn_r50_fpn_1x的预训练模型,保存到mmdetection/checkpoints目录下,运行下面的代码,如果能显示图片,说明安装成功了。

from mmdet.apis import init_detector, inference_detector, show_result
import mmcv

config_file = 'configs/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth'

# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')

# test a single image and show the results
img = 'test.jpg'  # or
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值