目标检测工具箱MMDetection安装及使用示例

      之前在https://blog.csdn.net/fengbingchun/article/details/86693037 中介绍过MMDetection,它是OpenMMLab项目的一部分,是基于PyTorch的目标检测开源工具箱,最新发布的版本为v2.25.1,License为Apache-2.0。

      最新版的MMDetection既有CUDA模式也有CPU模式。在CPU模式下,可以进行模型训练、测试或者推理,但是有些功能在CPU模式下不支持,如ROI pooling、Deformable Convolution等,因此推荐使用CUDA模式。

      最新版本与之前介绍的在安装及接口方面变化都较大,在之前版本可正常执行的程序,在新版本已无法运行,因此这里重新做了整理:

      1.安装:使用conda安装

      (1).创建openmmlab虚拟环境:

conda create -n openmmlab python=3.8
conda activate openmmlab

      (2).安装PyTorch:这里PyTorch使用1.11.0版本,CUDA使用10.2版本,此CUDA版本对PyTorch各版本都支持

conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=10.2 -c pytorch

      (3).安装MMCV:MMCV有两个版本,这里安装带CUDA的mmcv-full

      1).mmcv-full: 完整版,包含所有的特性以及丰富的开箱即用的CUDA算子,安装此版本需要较长时间。

      2).mmcv:精简版,不包含CUDA算子但包含其余所有特性和功能,类似MMCV 1.0之前的版本。

      不要在同一个环境中安装两个版本,否则可能会遇到类似ModuleNotFound的错误。在安装一个版本之前,需要先卸载另一个:

pip uninstall mmcv-full
pip uninstall mmcv

      注意:mmcv-full版本与mmdetection版本存在兼容对应关系,mmcv不能使用最新版,MMDetection 2.25.1要求MMCV版本(mmcv-full)为[1.3.17, 1.6.0),这里使用1.5.3版本。CUDA版本和PyTorch版本与安装PyTorch时保持一致

pip install mmcv-full==1.5.3 -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.11.0/index.html

      (4).安装MMDetection:花费时间较长

pip install mmdet==2.25.1

      2.测试:通过Faster R-CNN进行目标检测,训练数据集是COCO

      (1).下载模型(checkpoint):

model_path = "../../data/model/"
model_name = "faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"

if os.path.isfile(model_path + model_name) == False:
    print("model file does not exist, now download ...")
    url = "http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"
    subprocess.run(["wget", "-P", model_path, url])	

      (2).根据配置文件和checkpoint文件构建模型:

config_file = "../../src/mmdetection/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py"
model = init_detector(config_file, model_path+model_name, device='cuda:0')

      (3).准备测试图像:

image_path = "../../data/image/"
images_name = ["1.jpg", "2.jpg", "3.jpg"]
images_name[:] = [image_path+x for x in images_name]

      (4).进行推理检测:

inference_detector(model, images_name)

      (5).显示执行结果及保存图像:显示框的多少有阈值score_thr来控制

out_dir = "../../data/result/"
if not os.path.exists(out_dir):
    os.mkdir(out_dir)

def show_and_save_result(img, result, out_dir, dataset="coco", score_thr=0.6):
	print("test image:", img)
	class_names = get_classes(dataset)
	labels = [np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(result)]
	labels = np.concatenate(labels)
	bboxes = np.vstack(result)
	
	index = img.rfind("/")
	mmcv.imshow_det_bboxes(img, bboxes, labels, class_names, score_thr, show=True, out_file=out_dir+img[index+1:])

      执行结果如下图所示:以下原始图像均来自网络

 

       GitHubhttps://github.com/fengbingchun/PyTorch_Test

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值