{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MMDect 快速上手\n", "\n", "参考 [安装教程](https://mmdetection.readthedocs.io/zh_CN/latest/get_started.html) 安装 `mmdet`:\n", "\n", "1. 安装 Torch 相关包:\n", "\n", " ```bash\n", " conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch\n", " ```\n", "\n", "2. 安装 `mmdet`\n", "\n", " ```bash\n", " pip install mmdet\n", " ```\n", "\n", "下面验证安装。\n", "\n", "首先,下载模型:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2022-06-10 12:16:34-- http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth\n", "Resolving download.openmmlab.com (download.openmmlab.com)... 47.97.233.89\n", "Connecting to download.openmmlab.com (download.openmmlab.com)|47.97.233.89|:80... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 167287506 (160M) [application/octet-stream]\n", "Saving to: ‘faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth’\n", "\n", "faster_rcnn_r50_fpn 100%[===================>] 159.54M 3.34MB/s in 57s \n", "\n", "2022-06-10 12:17:30 (2.82 MB/s) - ‘faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth’ saved [167287506/167287506]\n", "\n" ] } ], "source": [ "!wget http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth\n", "!mkdir checkpoints\n", "!mv faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "验证:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "load checkpoint from local path: checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth\n" ] } ], "source": [ "from mmdet.apis import init_detector, inference_detector\n", "\n", "root = \"/media/pc/data/4tb/lxw/books/mmdetection\"\n", "config_file = f\"{root}/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py\"\n", "# 从 model zoo 下载 checkpoint 并放在 `checkpoints/` 文件下\n", "# 网址为: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth\n", "checkpoint_file = \"checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth\"\n", "device = \"cuda:0\"\n", "# 初始化检测器\n", "model = init_detector(config_file, checkpoint_file, device=device)\n", "# 推理演示图像\n", "result = inference_detector(model, f\"{root}/demo/demo.jpg\")" ] } ], "metadata": { "interpreter": { "hash": "452aa7e8bc10f56bfe9c2c54d01151ac4b597132b413a3ebc69bccf4dbbf142e" }, "kernelspec": { "display_name": "Python 3.10.4 ('openmmlab')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }