├── README.md ├── mmdet-0827 ├── 【A】安装配置MMDetection.ipynb ├── 【B】预训练目标检测模型-预测单张图像.ipynb ├── 【C】预训练目标检测模型-预测视频.ipynb ├── 【D1】在本地安装MMDetection.ipynb └── 【D2】预训练目标检测模型-预测摄像头实时画面.ipynb └── mmdet-0829 ├── 【A】安装配置MMDetection.ipynb ├── 【B1】预训练目标检测模型-预测单张图像-命令行.ipynb ├── 【C】预训练目标检测模型-预测视频.ipynb ├── 【D1】在本地安装MMDetection.ipynb ├── 【D2】预训练目标检测模型-预测摄像头实时画面.ipynb ├── 【E1】训练自己的目标检测模型.ipynb └── 【E2】气球数据集 (2).ipynb /README.md: -------------------------------------------------------------------------------- 1 | # MMDetection教程 2 | 3 | OpenMMLab主页:https://openmmlab.com/ 4 | 5 | MMDetection主页:https://github.com/open-mmlab/mmdetection 6 | 7 | 视频讲解:同济子豪兄 https://space.bilibili.com/1900783 8 | 9 | 代码测试[云GPU环境](https://featurize.cn?s=d7ce99f842414bfcaea5662a97581bd1):GPU RTX 3060、CUDA v11.2 10 | -------------------------------------------------------------------------------- /mmdet-0827/【C】预训练目标检测模型-预测视频.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b9bb0119-0008-4e9e-bd37-931f368b4ab2", 6 | "metadata": {}, 7 | "source": [ 8 | "# MMDetection模型库预训练目标检测模型-预测视频\n", 9 | "\n", 10 | "作者:同济子豪兄 2022-08-27\n", 11 | "\n", 12 | "模型库:https://github.com/open-mmlab/mmdetection/blob/master/docs/en/model_zoo.md" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "id": "bd628bf6-be55-41c0-897f-9ab4acb9763d", 18 | "metadata": {}, 19 | "source": [ 20 | "## 进入 mmdetection 主目录" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "id": "c878eeb3-9da4-41cd-ad07-8e620e420e86", 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "import os\n", 31 | "os.chdir('mmdetection')" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "44625547-494a-4980-97fe-503f3d7fff1d", 37 | "metadata": {}, 38 | "source": [ 39 | "## 视频预测-命令行" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 24, 45 | "id": "f88e7ece-6cd0-4be1-9c4e-f4300ea1c621", 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "load checkpoint from http path: https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_x_8x8_300e_coco/yolox_x_8x8_300e_coco_20211126_140254-1ef88d67.pth\n", 53 | "[ ] 0/159, elapsed: 0s, ETA:/environment/miniconda3/lib/python3.7/site-packages/torch/functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:2157.)\n", 54 | " return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]\n", 55 | "[>>>>>>>>>>>>>>>>>>>>>>>>>> ] 138/159, 2.4 task/s, elapsed: 57s, ETA: 9s\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "!python demo/video_demo.py \\\n", 61 | " data/street_20220330_174028.mp4 \\\n", 62 | " configs/yolox/yolox_x_8x8_300e_coco.py \\\n", 63 | " https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_x_8x8_300e_coco/yolox_x_8x8_300e_coco_20211126_140254-1ef88d67.pth \\\n", 64 | " --device cuda:0 \\\n", 65 | " --score-thr 0.3 \\\n", 66 | " --wait-time 0 \\\n", 67 | " --out output/video_output.mp4\n", 68 | " " 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "11a9cd86-0bd7-4f5c-abf0-6ff6d33c485c", 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 3 (ipykernel)", 83 | "language": "python", 84 | "name": "python3" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 3 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython3", 96 | "version": "3.7.10" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 5 101 | } 102 | -------------------------------------------------------------------------------- /mmdet-0827/【D1】在本地安装MMDetection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c3f0537a", 6 | "metadata": {}, 7 | "source": [ 8 | "# 在本地安装 MMClassification 环境\n", 9 | "\n", 10 | "本教程以 Macbook Pro 为例\n", 11 | "\n", 12 | "同济子豪兄 2022-8-16" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "id": "033be292", 18 | "metadata": {}, 19 | "source": [ 20 | "## 安装 mmcv" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "a5beb309", 26 | "metadata": {}, 27 | "source": [ 28 | "在 mac 上安装 mmcv -full:https://openmmlab.feishu.cn/docs/doccnJd89XfYFB9Q3x8oJFGYG2b" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 16, 34 | "id": "5c3f527b", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "Looking in indexes: https://mirrors.aliyun.com/pypi/simple/\n", 42 | "Collecting mmcv-full==1.5.0\n", 43 | " Downloading https://mirrors.aliyun.com/pypi/packages/29/26/46588ebfc79b653aa67851c16b2f29a365116db3ead59d09c6aea5416e9d/mmcv-full-1.5.0.tar.gz (530 kB)\n", 44 | " |████████████████████████████████| 530 kB 2.5 MB/s \n", 45 | "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25ldone\n", 46 | "\u001b[?25hRequirement already satisfied: addict in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (2.4.0)\n", 47 | "Requirement already satisfied: numpy in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (1.21.6)\n", 48 | "Requirement already satisfied: packaging in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (21.3)\n", 49 | "Requirement already satisfied: Pillow in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (8.4.0)\n", 50 | "Requirement already satisfied: pyyaml in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (6.0)\n", 51 | "Requirement already satisfied: yapf in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (0.31.0)\n", 52 | "Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from packaging->mmcv-full==1.5.0) (3.0.6)\n", 53 | "Building wheels for collected packages: mmcv-full\n", 54 | " Building wheel for mmcv-full (setup.py) ... \u001b[?25ldone\n", 55 | "\u001b[?25h Created wheel for mmcv-full: filename=mmcv_full-1.5.0-cp37-cp37m-macosx_10_9_x86_64.whl size=1289983 sha256=441cfc419dc118ff99f7f766f6926af8bf3ce946e16c7a03907dd3184677b6b1\n", 56 | " Stored in directory: /Users/tommy/Library/Caches/pip/wheels/6b/aa/59/25309b93c3a5f6de2269fa6daecaa2b3e9eff108b2f1fd6e74\n", 57 | "Successfully built mmcv-full\n", 58 | "Installing collected packages: mmcv-full\n", 59 | "Successfully installed mmcv-full-1.5.0\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "!pip install mmcv-full==1.5.0" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 17, 70 | "id": "8e4106dd", 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "'1.5.0'" 77 | ] 78 | }, 79 | "execution_count": 17, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "import mmcv\n", 86 | "mmcv.__version__" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "id": "0cbc6dfa", 92 | "metadata": {}, 93 | "source": [ 94 | "## 进入openmmlab虚拟环境(如有)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 1, 100 | "id": "33eee3a4", 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "\r\n", 108 | "CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.\r\n", 109 | "To initialize your shell, run\r\n", 110 | "\r\n", 111 | " $ conda init \r\n", 112 | "\r\n", 113 | "Currently supported shells are:\r\n", 114 | " - bash\r\n", 115 | " - fish\r\n", 116 | " - tcsh\r\n", 117 | " - xonsh\r\n", 118 | " - zsh\r\n", 119 | " - powershell\r\n", 120 | "\r\n", 121 | "See 'conda init --help' for more information and options.\r\n", 122 | "\r\n", 123 | "IMPORTANT: You may need to close and restart your shell after running 'conda init'.\r\n", 124 | "\r\n", 125 | "\r\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "!conda activate open-mmlab" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 4, 136 | "id": "d1a9082e", 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "import os\n", 141 | "import mmcv" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "id": "44a265e5", 147 | "metadata": {}, 148 | "source": [ 149 | "## 从Github下载最新版 mmdetection" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 5, 155 | "id": "6ffd34fc", 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "# 删除已有的 mmdetection 文件夹(若有)\n", 160 | "!rm -rf mmdetection" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 7, 166 | "id": "8961b8e2", 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [ 170 | "# 从 Github下载 MMDetection 算法库\n", 171 | "!git clone https://github.com/open-mmlab/mmdetection.git" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 9, 177 | "id": "9ea54fd7", 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "# 进入 mmpose 目录\n", 182 | "os.chdir('mmdetection')" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 10, 188 | "id": "58ae41f4", 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "Looking in indexes: https://mirrors.aliyun.com/pypi/simple/\n", 196 | "Obtaining file:///Users/tommy/Desktop/pytorch/mmdetection/mmdetection\n", 197 | " Preparing metadata (setup.py) ... \u001b[?25ldone\n", 198 | "\u001b[?25hRequirement already satisfied: matplotlib in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (3.5.0)\n", 199 | "Requirement already satisfied: numpy in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (1.21.6)\n", 200 | "Requirement already satisfied: pycocotools in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (2.0.3)\n", 201 | "Requirement already satisfied: six in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (1.16.0)\n", 202 | "Requirement already satisfied: terminaltables in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (3.1.10)\n", 203 | "Requirement already satisfied: cycler>=0.10 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (0.11.0)\n", 204 | "Requirement already satisfied: packaging>=20.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (21.3)\n", 205 | "Requirement already satisfied: kiwisolver>=1.0.1 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (1.3.2)\n", 206 | "Requirement already satisfied: pyparsing>=2.2.1 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (3.0.6)\n", 207 | "Requirement already satisfied: pillow>=6.2.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (8.4.0)\n", 208 | "Requirement already satisfied: fonttools>=4.22.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (4.28.3)\n", 209 | "Requirement already satisfied: setuptools-scm>=4 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (6.3.2)\n", 210 | "Requirement already satisfied: python-dateutil>=2.7 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (2.8.2)\n", 211 | "Requirement already satisfied: setuptools>=18.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from pycocotools->mmdet==2.25.1) (59.4.0)\n", 212 | "Requirement already satisfied: cython>=0.27.3 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from pycocotools->mmdet==2.25.1) (0.29.25)\n", 213 | "Requirement already satisfied: tomli>=1.0.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from setuptools-scm>=4->matplotlib->mmdet==2.25.1) (1.2.2)\n", 214 | "Installing collected packages: mmdet\n", 215 | " Attempting uninstall: mmdet\n", 216 | " Found existing installation: mmdet 2.19.0\n", 217 | " Uninstalling mmdet-2.19.0:\n", 218 | " Successfully uninstalled mmdet-2.19.0\n", 219 | " Running setup.py develop for mmdet\n", 220 | "Successfully installed mmdet-2.25.1\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | "# Mac电脑\n", 226 | "!CC=clang CXX=clang++ CFLAGS='-stdlib=libc++' pip install -e ." 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "id": "632af888", 232 | "metadata": {}, 233 | "source": [ 234 | "## 验证安装成功" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 14, 240 | "id": "af972b17", 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "'1.5.0'" 247 | ] 248 | }, 249 | "execution_count": 14, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "import mmcv\n", 256 | "mmcv.__version__" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 15, 262 | "id": "01f4397c", 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "name": "stdout", 267 | "output_type": "stream", 268 | "text": [ 269 | "2.25.1\n" 270 | ] 271 | } 272 | ], 273 | "source": [ 274 | "import mmdet\n", 275 | "print(mmdet.__version__)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "id": "64325715", 282 | "metadata": {}, 283 | "outputs": [], 284 | "source": [] 285 | } 286 | ], 287 | "metadata": { 288 | "kernelspec": { 289 | "display_name": "Python 3 (ipykernel)", 290 | "language": "python", 291 | "name": "python3" 292 | }, 293 | "language_info": { 294 | "codemirror_mode": { 295 | "name": "ipython", 296 | "version": 3 297 | }, 298 | "file_extension": ".py", 299 | "mimetype": "text/x-python", 300 | "name": "python", 301 | "nbconvert_exporter": "python", 302 | "pygments_lexer": "ipython3", 303 | "version": "3.7.12" 304 | } 305 | }, 306 | "nbformat": 4, 307 | "nbformat_minor": 5 308 | } 309 | -------------------------------------------------------------------------------- /mmdet-0827/【D2】预训练目标检测模型-预测摄像头实时画面.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "78ebef97", 6 | "metadata": {}, 7 | "source": [ 8 | "# 预训练目标检测模型-预测摄像头实时画面\n", 9 | "\n", 10 | "调用MMDetection模型库中的预训练图像分类模型,调用本地摄像头,进行逐帧实时目标检测,显示带预测结果的实时画面。\n", 11 | "\n", 12 | "本代码需在`连接摄像头的本地`运行,不能在云GPU平台运行。\n", 13 | "\n", 14 | "同济子豪兄:https://space.bilibili.com/1900783\n", 15 | "\n", 16 | "测试运行环境:Macbook Pro\n", 17 | "\n", 18 | "2022-8-27" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "id": "e3f17858", 24 | "metadata": {}, 25 | "source": [ 26 | "## 打开终端,运行以下命令\n", 27 | "\n", 28 | "```\n", 29 | "cd mmdetection\n", 30 | "\n", 31 | "```\n", 32 | "\n", 33 | "```\n", 34 | "python demo/webcam_demo.py \\\n", 35 | " configs/yolox/yolox_s_8x8_300e_coco.py \\\n", 36 | " https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_s_8x8_300e_coco/yolox_s_8x8_300e_coco_20211121_095711-4592a793.pth \\\n", 37 | " --device cpu \\\n", 38 | " --camera-id 0 \\\n", 39 | " --score-thr 0.3\n", 40 | " \n", 41 | " \n", 42 | "```" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "id": "5d7eb9f9", 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [] 52 | } 53 | ], 54 | "metadata": { 55 | "kernelspec": { 56 | "display_name": "Python 3 (ipykernel)", 57 | "language": "python", 58 | "name": "python3" 59 | }, 60 | "language_info": { 61 | "codemirror_mode": { 62 | "name": "ipython", 63 | "version": 3 64 | }, 65 | "file_extension": ".py", 66 | "mimetype": "text/x-python", 67 | "name": "python", 68 | "nbconvert_exporter": "python", 69 | "pygments_lexer": "ipython3", 70 | "version": "3.7.12" 71 | } 72 | }, 73 | "nbformat": 4, 74 | "nbformat_minor": 5 75 | } 76 | -------------------------------------------------------------------------------- /mmdet-0829/【C】预训练目标检测模型-预测视频.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b9bb0119-0008-4e9e-bd37-931f368b4ab2", 6 | "metadata": {}, 7 | "source": [ 8 | "# MMDetection模型库预训练目标检测模型-预测视频\n", 9 | "\n", 10 | "作者:同济子豪兄 2022-08-27\n", 11 | "\n", 12 | "模型库:https://github.com/open-mmlab/mmdetection/blob/master/docs/en/model_zoo.md" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "id": "bd628bf6-be55-41c0-897f-9ab4acb9763d", 18 | "metadata": {}, 19 | "source": [ 20 | "## 进入 mmdetection 主目录" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "id": "c878eeb3-9da4-41cd-ad07-8e620e420e86", 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "import os\n", 31 | "os.chdir('mmdetection')" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "44625547-494a-4980-97fe-503f3d7fff1d", 37 | "metadata": {}, 38 | "source": [ 39 | "## 视频预测-命令行" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "id": "f88e7ece-6cd0-4be1-9c4e-f4300ea1c621", 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "usage: video_demo.py [-h] [--device DEVICE] [--score-thr SCORE_THR]\n", 53 | " [--out OUT] [--show] [--wait-time WAIT_TIME]\n", 54 | " video config checkpoint\n", 55 | "video_demo.py: error: unrecognized arguments: --palette coco\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "!python demo/video_demo.py \\\n", 61 | " data/street_20220330_174028.mp4 \\\n", 62 | " configs/yolox/yolox_x_8x8_300e_coco.py \\\n", 63 | " https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_x_8x8_300e_coco/yolox_x_8x8_300e_coco_20211126_140254-1ef88d67.pth \\\n", 64 | " --device cuda:0 \\\n", 65 | " --score-thr 0.3 \\\n", 66 | " --wait-time 0 \\\n", 67 | " --out output/C_video_output.mp4\n", 68 | " " 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "11a9cd86-0bd7-4f5c-abf0-6ff6d33c485c", 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 3 (ipykernel)", 83 | "language": "python", 84 | "name": "python3" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 3 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython3", 96 | "version": "3.7.10" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 5 101 | } 102 | -------------------------------------------------------------------------------- /mmdet-0829/【D1】在本地安装MMDetection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c3f0537a", 6 | "metadata": {}, 7 | "source": [ 8 | "# 在本地安装 MMClassification 环境\n", 9 | "\n", 10 | "本教程以 Macbook Pro 为例\n", 11 | "\n", 12 | "同济子豪兄 2022-8-16" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "id": "033be292", 18 | "metadata": {}, 19 | "source": [ 20 | "## 安装 mmcv" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "a5beb309", 26 | "metadata": {}, 27 | "source": [ 28 | "在 mac 上安装 mmcv -full:https://openmmlab.feishu.cn/docs/doccnJd89XfYFB9Q3x8oJFGYG2b" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 16, 34 | "id": "5c3f527b", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "Looking in indexes: https://mirrors.aliyun.com/pypi/simple/\n", 42 | "Collecting mmcv-full==1.5.0\n", 43 | " Downloading https://mirrors.aliyun.com/pypi/packages/29/26/46588ebfc79b653aa67851c16b2f29a365116db3ead59d09c6aea5416e9d/mmcv-full-1.5.0.tar.gz (530 kB)\n", 44 | " |████████████████████████████████| 530 kB 2.5 MB/s \n", 45 | "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25ldone\n", 46 | "\u001b[?25hRequirement already satisfied: addict in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (2.4.0)\n", 47 | "Requirement already satisfied: numpy in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (1.21.6)\n", 48 | "Requirement already satisfied: packaging in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (21.3)\n", 49 | "Requirement already satisfied: Pillow in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (8.4.0)\n", 50 | "Requirement already satisfied: pyyaml in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (6.0)\n", 51 | "Requirement already satisfied: yapf in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmcv-full==1.5.0) (0.31.0)\n", 52 | "Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from packaging->mmcv-full==1.5.0) (3.0.6)\n", 53 | "Building wheels for collected packages: mmcv-full\n", 54 | " Building wheel for mmcv-full (setup.py) ... \u001b[?25ldone\n", 55 | "\u001b[?25h Created wheel for mmcv-full: filename=mmcv_full-1.5.0-cp37-cp37m-macosx_10_9_x86_64.whl size=1289983 sha256=441cfc419dc118ff99f7f766f6926af8bf3ce946e16c7a03907dd3184677b6b1\n", 56 | " Stored in directory: /Users/tommy/Library/Caches/pip/wheels/6b/aa/59/25309b93c3a5f6de2269fa6daecaa2b3e9eff108b2f1fd6e74\n", 57 | "Successfully built mmcv-full\n", 58 | "Installing collected packages: mmcv-full\n", 59 | "Successfully installed mmcv-full-1.5.0\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "!pip install mmcv-full==1.5.0" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 17, 70 | "id": "8e4106dd", 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "'1.5.0'" 77 | ] 78 | }, 79 | "execution_count": 17, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "import mmcv\n", 86 | "mmcv.__version__" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "id": "0cbc6dfa", 92 | "metadata": {}, 93 | "source": [ 94 | "## 进入openmmlab虚拟环境(如有)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 1, 100 | "id": "33eee3a4", 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "\r\n", 108 | "CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.\r\n", 109 | "To initialize your shell, run\r\n", 110 | "\r\n", 111 | " $ conda init \r\n", 112 | "\r\n", 113 | "Currently supported shells are:\r\n", 114 | " - bash\r\n", 115 | " - fish\r\n", 116 | " - tcsh\r\n", 117 | " - xonsh\r\n", 118 | " - zsh\r\n", 119 | " - powershell\r\n", 120 | "\r\n", 121 | "See 'conda init --help' for more information and options.\r\n", 122 | "\r\n", 123 | "IMPORTANT: You may need to close and restart your shell after running 'conda init'.\r\n", 124 | "\r\n", 125 | "\r\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "!conda activate open-mmlab" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 4, 136 | "id": "d1a9082e", 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "import os\n", 141 | "import mmcv" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "id": "44a265e5", 147 | "metadata": {}, 148 | "source": [ 149 | "## 从Github下载最新版 mmdetection" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 5, 155 | "id": "6ffd34fc", 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "# 删除已有的 mmdetection 文件夹(若有)\n", 160 | "!rm -rf mmdetection" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 7, 166 | "id": "8961b8e2", 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [ 170 | "# 从 Github下载 MMDetection 算法库\n", 171 | "!git clone https://github.com/open-mmlab/mmdetection.git" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 9, 177 | "id": "9ea54fd7", 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "# 进入 mmpose 目录\n", 182 | "os.chdir('mmdetection')" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 10, 188 | "id": "58ae41f4", 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "Looking in indexes: https://mirrors.aliyun.com/pypi/simple/\n", 196 | "Obtaining file:///Users/tommy/Desktop/pytorch/mmdetection/mmdetection\n", 197 | " Preparing metadata (setup.py) ... \u001b[?25ldone\n", 198 | "\u001b[?25hRequirement already satisfied: matplotlib in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (3.5.0)\n", 199 | "Requirement already satisfied: numpy in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (1.21.6)\n", 200 | "Requirement already satisfied: pycocotools in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (2.0.3)\n", 201 | "Requirement already satisfied: six in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (1.16.0)\n", 202 | "Requirement already satisfied: terminaltables in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from mmdet==2.25.1) (3.1.10)\n", 203 | "Requirement already satisfied: cycler>=0.10 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (0.11.0)\n", 204 | "Requirement already satisfied: packaging>=20.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (21.3)\n", 205 | "Requirement already satisfied: kiwisolver>=1.0.1 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (1.3.2)\n", 206 | "Requirement already satisfied: pyparsing>=2.2.1 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (3.0.6)\n", 207 | "Requirement already satisfied: pillow>=6.2.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (8.4.0)\n", 208 | "Requirement already satisfied: fonttools>=4.22.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (4.28.3)\n", 209 | "Requirement already satisfied: setuptools-scm>=4 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (6.3.2)\n", 210 | "Requirement already satisfied: python-dateutil>=2.7 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from matplotlib->mmdet==2.25.1) (2.8.2)\n", 211 | "Requirement already satisfied: setuptools>=18.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from pycocotools->mmdet==2.25.1) (59.4.0)\n", 212 | "Requirement already satisfied: cython>=0.27.3 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from pycocotools->mmdet==2.25.1) (0.29.25)\n", 213 | "Requirement already satisfied: tomli>=1.0.0 in /Users/tommy/opt/anaconda3/envs/open-mmlab/lib/python3.7/site-packages (from setuptools-scm>=4->matplotlib->mmdet==2.25.1) (1.2.2)\n", 214 | "Installing collected packages: mmdet\n", 215 | " Attempting uninstall: mmdet\n", 216 | " Found existing installation: mmdet 2.19.0\n", 217 | " Uninstalling mmdet-2.19.0:\n", 218 | " Successfully uninstalled mmdet-2.19.0\n", 219 | " Running setup.py develop for mmdet\n", 220 | "Successfully installed mmdet-2.25.1\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | "# Mac电脑\n", 226 | "!CC=clang CXX=clang++ CFLAGS='-stdlib=libc++' pip install -e ." 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "id": "632af888", 232 | "metadata": {}, 233 | "source": [ 234 | "## 验证安装成功" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 14, 240 | "id": "af972b17", 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "'1.5.0'" 247 | ] 248 | }, 249 | "execution_count": 14, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "import mmcv\n", 256 | "mmcv.__version__" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 15, 262 | "id": "01f4397c", 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "name": "stdout", 267 | "output_type": "stream", 268 | "text": [ 269 | "2.25.1\n" 270 | ] 271 | } 272 | ], 273 | "source": [ 274 | "import mmdet\n", 275 | "print(mmdet.__version__)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "id": "64325715", 282 | "metadata": {}, 283 | "outputs": [], 284 | "source": [] 285 | } 286 | ], 287 | "metadata": { 288 | "kernelspec": { 289 | "display_name": "Python 3 (ipykernel)", 290 | "language": "python", 291 | "name": "python3" 292 | }, 293 | "language_info": { 294 | "codemirror_mode": { 295 | "name": "ipython", 296 | "version": 3 297 | }, 298 | "file_extension": ".py", 299 | "mimetype": "text/x-python", 300 | "name": "python", 301 | "nbconvert_exporter": "python", 302 | "pygments_lexer": "ipython3", 303 | "version": "3.7.12" 304 | } 305 | }, 306 | "nbformat": 4, 307 | "nbformat_minor": 5 308 | } 309 | -------------------------------------------------------------------------------- /mmdet-0829/【D2】预训练目标检测模型-预测摄像头实时画面.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "78ebef97", 6 | "metadata": {}, 7 | "source": [ 8 | "# 预训练目标检测模型-预测摄像头实时画面\n", 9 | "\n", 10 | "调用MMDetection模型库中的预训练图像分类模型,调用本地摄像头,进行逐帧实时目标检测,显示带预测结果的实时画面。\n", 11 | "\n", 12 | "本代码需在`连接摄像头的本地`运行,不能在云GPU平台运行。\n", 13 | "\n", 14 | "同济子豪兄:https://space.bilibili.com/1900783\n", 15 | "\n", 16 | "测试运行环境:Macbook Pro\n", 17 | "\n", 18 | "2022-8-27" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "id": "e3f17858", 24 | "metadata": {}, 25 | "source": [ 26 | "## 打开终端,运行以下命令\n", 27 | "\n", 28 | "`cd mmdetection`\n", 29 | "\n", 30 | "```shell\n", 31 | "python demo/webcam_demo.py \\\n", 32 | " configs/yolox/yolox_s_8x8_300e_coco.py \\\n", 33 | " https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_s_8x8_300e_coco/yolox_s_8x8_300e_coco_20211121_095711-4592a793.pth \\\n", 34 | " --device cpu \\\n", 35 | " --camera-id 0 \\\n", 36 | " --score-thr 0.3\n", 37 | "```" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "id": "4a78a24f-bb38-4a66-8ec2-fa92820ffc7e", 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "id": "5d7eb9f9", 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3 (ipykernel)", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.7.10" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 5 78 | } 79 | -------------------------------------------------------------------------------- /mmdet-0829/【E2】气球数据集 (2).ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "76694ad8-549d-4f99-aad3-35bde499c8eb", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "ee8a8865-a4b9-4bd7-8594-c0f785d6f61a", 14 | "metadata": { 15 | "id": "7GrWIJywLV-V" 16 | }, 17 | "source": [ 18 | "## 在自定义数据集上训练检测模型\n", 19 | "\n", 20 | "训练新模型通常有三个步骤:\n", 21 | "1. 支持新数据集\n", 22 | "2. 修改配置文件\n", 23 | "3. 训练模型\n", 24 | "\n", 25 | "而MMDetection中有三种方法来支持新数据集: \n", 26 | "1. 将数据集整理为COCO格式\n", 27 | "2. 将数据集整理为中间格式\n", 28 | "3. 直接实现新数据集的支持\n", 29 | "\n", 30 | "\n", 31 | "我们将基于数据集[balloon dataset](http://www.cvlibs.net/datasets/kitti/eval_object.php?obj_benchmark=3d),通过三种方法来完成本节教程。这是一个带有mask的数据集,其中训练集包含61张图片,验证集包含13张图片。其他方法以及进阶用法可以参考[doc](https://mmdetection.readthedocs.io/en/latest/tutorials/new_dataset.html#)。\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "cf5bbd7b-1c46-4660-9596-305e717c2da8", 37 | "metadata": {}, 38 | "source": [ 39 | "## 进入 mmdetection 主目录" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "id": "07b95bd6-bc01-4475-adab-02a3ec4a454c", 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "import os\n", 50 | "os.chdir('mmdetection')" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "id": "3945da1f-d3a8-4f99-afb2-79a9d0f8fac7", 56 | "metadata": { 57 | "id": "I9lIVXsmAH4a" 58 | }, 59 | "source": [ 60 | "## 下载并观察气球数据集" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "id": "1a2b3623-83c5-4638-8f30-c2a482f57da4", 67 | "metadata": { 68 | "colab": { 69 | "base_uri": "https://localhost:8080/" 70 | }, 71 | "executionInfo": { 72 | "elapsed": 28954, 73 | "status": "ok", 74 | "timestamp": 1619707017622, 75 | "user": { 76 | "displayName": "Leju Yu", 77 | "photoUrl": "", 78 | "userId": "02803640020004660259" 79 | }, 80 | "user_tz": -480 81 | }, 82 | "id": "F0iZe5Ovyd_0", 83 | "outputId": "e287634e-d89a-43e2-827b-e2f676acc455" 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "#下载并解压数据集\n", 88 | "!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.1/balloon_dataset.zip\n", 89 | "!unzip balloon_dataset.zip > /dev/null" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "id": "9f2ead70-3e5a-4bf9-91d5-df7f543f1e37", 96 | "metadata": { 97 | "colab": { 98 | "base_uri": "https://localhost:8080/" 99 | }, 100 | "executionInfo": { 101 | "elapsed": 11648, 102 | "status": "ok", 103 | "timestamp": 1619503163421, 104 | "user": { 105 | "displayName": "Yingfan Hu", 106 | "photoUrl": "", 107 | "userId": "05511228327870757093" 108 | }, 109 | "user_tz": -480 110 | }, 111 | "id": "Wuwxw1oZRtVZ", 112 | "outputId": "68567e11-71e0-4076-aa97-7bc14f0a1780" 113 | }, 114 | "outputs": [], 115 | "source": [ 116 | "!tree balloon" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "id": "377db8ab-c984-4711-b370-b3f3d7eab0f0", 123 | "metadata": { 124 | "colab": { 125 | "base_uri": "https://localhost:8080/", 126 | "height": 1000, 127 | "output_embedded_package_id": "1SkNR1JyVJzSVzKeXx0R1a17Ewtr4SOCi" 128 | }, 129 | "executionInfo": { 130 | "elapsed": 27740, 131 | "status": "ok", 132 | "timestamp": 1619503183519, 133 | "user": { 134 | "displayName": "Yingfan Hu", 135 | "photoUrl": "", 136 | "userId": "05511228327870757093" 137 | }, 138 | "user_tz": -480 139 | }, 140 | "id": "YnQQqzOWzE91", 141 | "outputId": "9d93fd16-a3b5-4aa2-bb5b-f3af3712eda5" 142 | }, 143 | "outputs": [], 144 | "source": [ 145 | "# 简单查看数据集的图片\n", 146 | "from PIL import Image\n", 147 | "Image.open('balloon/val/14898532020_ba6199dd22_k.jpg')" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "id": "d2e6e793-7f50-41a2-9267-4d2f5a7d9e73", 153 | "metadata": { 154 | "id": "2dGmEUdIDW-T" 155 | }, 156 | "source": [ 157 | "这里我们以单张图像为例查看balloon数据集的标签json格式,可以看到标签包含基本的图像名,大小信息以及mask所在的区域。" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "id": "6f82789f-b014-445c-a2e1-9df4e4d8f6c3", 164 | "metadata": { 165 | "colab": { 166 | "base_uri": "https://localhost:8080/" 167 | }, 168 | "executionInfo": { 169 | "elapsed": 25694, 170 | "status": "ok", 171 | "timestamp": 1619503183520, 172 | "user": { 173 | "displayName": "Yingfan Hu", 174 | "photoUrl": "", 175 | "userId": "05511228327870757093" 176 | }, 177 | "user_tz": -480 178 | }, 179 | "id": "n7rwalnPd6e1", 180 | "outputId": "eb03da5b-55f2-4edf-976c-6158c001c72a" 181 | }, 182 | "outputs": [], 183 | "source": [ 184 | "# 查看单张图片的标签格式\n", 185 | "import json\n", 186 | "\n", 187 | "with open('balloon/train/via_region_data.json') as f:\n", 188 | " data = json.load(f)\n", 189 | "anno1 = list(data.keys())[0]\n", 190 | "print(anno1)\n", 191 | "print(json.dumps(data[anno1], indent=2))" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "id": "63e3c110-c8b6-4756-a8ad-3d69c3463298", 197 | "metadata": { 198 | "id": "1aetc5PnWab_" 199 | }, 200 | "source": [ 201 | "## 下载训练好的mask rcnn模型" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": null, 207 | "id": "f195a0d9-fe73-4ed2-80ec-76bf141415dc", 208 | "metadata": { 209 | "colab": { 210 | "base_uri": "https://localhost:8080/" 211 | }, 212 | "executionInfo": { 213 | "elapsed": 40231, 214 | "status": "ok", 215 | "timestamp": 1619707035882, 216 | "user": { 217 | "displayName": "Leju Yu", 218 | "photoUrl": "", 219 | "userId": "02803640020004660259" 220 | }, 221 | "user_tz": -480 222 | }, 223 | "id": "0Gqf1-g2WmJj", 224 | "outputId": "b821211d-3fa5-48cf-ff1c-21b010fb8497" 225 | }, 226 | "outputs": [], 227 | "source": [ 228 | "# 下载训练好的 Mask R CNN 模型到checkpoints文件夹\n", 229 | "!wget http://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth -P checkpoints\n" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "id": "b1a0e7ec-d574-477d-95a7-64859bcaeace", 235 | "metadata": { 236 | "id": "E73y5Lru-wBx" 237 | }, 238 | "source": [ 239 | "## 方法一:将数据集整理为COCO格式\n", 240 | "\n", 241 | "这里我们需要了解COCO数据集的格式,并将balloon的标注文件转换为coco格式。" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "id": "8df45bf0-7a39-4214-a480-fab9b0ea70ff", 247 | "metadata": { 248 | "id": "UAUnZ_WODcPs" 249 | }, 250 | "source": [ 251 | "### 查看COCO数据集格式\n", 252 | "\n", 253 | "对于实例分割来说,比较重要的COCO格式如下所示。\n", 254 | "\n", 255 | "更详细完整的格式说明可以参考[这里](https://cocodataset.org/#format-data)\n", 256 | "\n", 257 | "```python\n", 258 | "{\n", 259 | " \"images\": [image],\n", 260 | " \"annotations\": [annotation],\n", 261 | " \"categories\": [category]\n", 262 | "}\n", 263 | "\n", 264 | "# 图像信息\n", 265 | "image = {\n", 266 | " \"id\": int, #图像id\n", 267 | " \"width\": int, #宽\n", 268 | " \"height\": int, #高\n", 269 | " \"file_name\": str, #图像文件名\n", 270 | "}\n", 271 | "\n", 272 | "#标注信息\n", 273 | "annotation = {\n", 274 | " \"id\": int, #标注id \n", 275 | " \"image_id\": int, #对应的图像id\n", 276 | " \"category_id\": int, #类别id\n", 277 | " \"segmentation\": RLE or [polygon], #分割mask信息\n", 278 | " \"area\": float, #mask面积\n", 279 | " \"bbox\": [x,y,width,height], #边界框信息\n", 280 | " \"iscrowd\": 0 or 1, #是否为密集图片\n", 281 | "}\n", 282 | "\n", 283 | "#类别信息\n", 284 | "categories = [{\n", 285 | " \"id\": int, #类别id\n", 286 | " \"name\": str, #类别名\n", 287 | " \"supercategory\": str, #母类名称\n", 288 | "}]\n", 289 | "```" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "id": "7571ef97-746b-4b27-be54-72a574f0fbd2", 295 | "metadata": { 296 | "id": "yK4u-oW2GAG8" 297 | }, 298 | "source": [ 299 | "### 转换标注文件格式\n", 300 | "\n", 301 | "编写功能函数`convert_balloon_to_coco`完成格式转换。" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "id": "b342da2c-f5b6-441b-8c64-bd5202973a59", 308 | "metadata": { 309 | "colab": { 310 | "base_uri": "https://localhost:8080/" 311 | }, 312 | "executionInfo": { 313 | "elapsed": 37469, 314 | "status": "ok", 315 | "timestamp": 1619707040747, 316 | "user": { 317 | "displayName": "Leju Yu", 318 | "photoUrl": "", 319 | "userId": "02803640020004660259" 320 | }, 321 | "user_tz": -480 322 | }, 323 | "id": "2slFGs3gGFLk", 324 | "outputId": "da9e3e90-04d1-426f-a671-57e806a8ac06" 325 | }, 326 | "outputs": [], 327 | "source": [ 328 | "import os.path as osp\n", 329 | "import mmcv\n", 330 | "\n", 331 | "def convert_balloon_to_coco(ann_file, out_file, image_prefix):\n", 332 | " data_infos = mmcv.load(ann_file)\n", 333 | "\n", 334 | " annotations = []\n", 335 | " images = []\n", 336 | " obj_count = 0\n", 337 | " for idx, v in enumerate(mmcv.track_iter_progress(data_infos.values())):\n", 338 | " filename = v['filename']\n", 339 | " img_path = osp.join(image_prefix, filename)\n", 340 | " height, width = mmcv.imread(img_path).shape[:2]\n", 341 | "\n", 342 | " images.append(dict(\n", 343 | " id=idx,\n", 344 | " file_name=filename,\n", 345 | " height=height,\n", 346 | " width=width))\n", 347 | "\n", 348 | " bboxes = []\n", 349 | " labels = []\n", 350 | " masks = []\n", 351 | " for _, obj in v['regions'].items():\n", 352 | " assert not obj['region_attributes']\n", 353 | " obj = obj['shape_attributes']\n", 354 | " px = obj['all_points_x']\n", 355 | " py = obj['all_points_y']\n", 356 | " poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]\n", 357 | " poly = [p for x in poly for p in x]\n", 358 | "\n", 359 | " x_min, y_min, x_max, y_max = (\n", 360 | " min(px), min(py), max(px), max(py))\n", 361 | "\n", 362 | " data_anno = dict(\n", 363 | " image_id=idx,\n", 364 | " id=obj_count,\n", 365 | " category_id=0,\n", 366 | " bbox=[x_min, y_min, x_max - x_min, y_max - y_min],\n", 367 | " area=(x_max - x_min) * (y_max - y_min),\n", 368 | " segmentation=[poly],\n", 369 | " iscrowd=0)\n", 370 | " annotations.append(data_anno)\n", 371 | " obj_count += 1\n", 372 | "\n", 373 | " coco_format_json = dict(\n", 374 | " images=images,\n", 375 | " annotations=annotations,\n", 376 | " categories=[{'id':0, 'name': 'balloon'}])\n", 377 | " mmcv.dump(coco_format_json, out_file)\n", 378 | "convert_balloon_to_coco('balloon/train/via_region_data.json', 'balloon/train/coco.json', 'balloon/train')\n", 379 | "convert_balloon_to_coco('balloon/val/via_region_data.json', 'balloon/val/coco.json', 'balloon/val')" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": null, 385 | "id": "63ccbe75-6db0-4ee9-a00e-ec5bc17642e4", 386 | "metadata": { 387 | "executionInfo": { 388 | "elapsed": 1239, 389 | "status": "ok", 390 | "timestamp": 1619708541450, 391 | "user": { 392 | "displayName": "Leju Yu", 393 | "photoUrl": "", 394 | "userId": "02803640020004660259" 395 | }, 396 | "user_tz": -480 397 | }, 398 | "id": "alMkrn0IJGon" 399 | }, 400 | "outputs": [], 401 | "source": [ 402 | "# import json\n", 403 | "\n", 404 | "# with open('balloon/val/coco.json') as f:\n", 405 | "# data = json.load(f)\n", 406 | "\n", 407 | "# with open('coco.json', 'w') as f:\n", 408 | "# json.dump(data, f, indent=4)" 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "id": "a33e704f-9dc6-41dc-b845-b80d324bf0dc", 414 | "metadata": { 415 | "id": "H2M2ZF7NGm8p" 416 | }, 417 | "source": [ 418 | "### 修改 config 配置文件并保存" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": null, 424 | "id": "ccda9b54-6a78-4f96-8bf7-258f9bfd642b", 425 | "metadata": { 426 | "colab": { 427 | "base_uri": "https://localhost:8080/" 428 | }, 429 | "executionInfo": { 430 | "elapsed": 6713, 431 | "status": "ok", 432 | "timestamp": 1619665867640, 433 | "user": { 434 | "displayName": "Leju Yu", 435 | "photoUrl": "", 436 | "userId": "02803640020004660259" 437 | }, 438 | "user_tz": -480 439 | }, 440 | "id": "4fzwFF1cGWm-", 441 | "outputId": "07979c7c-54ea-4f79-a332-33a70270844e" 442 | }, 443 | "outputs": [], 444 | "source": [ 445 | "from mmcv import Config\n", 446 | "from mmdet.apis import set_random_seed\n", 447 | "\n", 448 | "# 获取基本配置文件参数\n", 449 | "cfg = Config.fromfile('./configs/mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_coco.py')\n", 450 | "\n", 451 | "# 修改数据集类型以及文件路径\n", 452 | "cfg.dataset_type = 'COCODataset'\n", 453 | "cfg.data_root = 'balloon/'\n", 454 | "cfg.classes = ('balloon',)\n", 455 | "\n", 456 | "cfg.data.train.data_root = 'balloon/'\n", 457 | "cfg.data.train.ann_file = 'train/coco.json'\n", 458 | "cfg.data.train.img_prefix = 'train'\n", 459 | "cfg.data.train.classes = cfg.classes\n", 460 | "\n", 461 | "cfg.data.val.data_root = 'balloon/'\n", 462 | "cfg.data.val.ann_file = 'val/coco.json'\n", 463 | "cfg.data.val.img_prefix = 'val'\n", 464 | "cfg.data.val.classes = cfg.classes\n", 465 | "\n", 466 | "cfg.data.test.data_root = 'balloon/'\n", 467 | "cfg.data.test.ann_file = 'val/coco.json'\n", 468 | "cfg.data.test.img_prefix = 'val'\n", 469 | "cfg.data.test.classes = cfg.classes\n", 470 | "\n", 471 | "# 修改bbox_head和mask_head中的类别数\n", 472 | "cfg.model.roi_head.bbox_head.num_classes = 1\n", 473 | "cfg.model.roi_head.mask_head.num_classes = 1\n", 474 | "# 使用预训练好的faster rcnn模型用于finetuning\n", 475 | "cfg.load_from = 'checkpoints/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth'\n", 476 | "# 设置工作目录用于存放log和临时文件\n", 477 | "cfg.work_dir = 'work_dirs/balloon'\n", 478 | "\n", 479 | "# 原本的学习率是在8卡基础上训练设置的,现在单卡需要除以8\n", 480 | "cfg.optimizer.lr = 0.02 / 8\n", 481 | "cfg.lr_config.warmup = None\n", 482 | "cfg.log_config.interval = 10\n", 483 | "\n", 484 | "# # 设置evaluation间隔减少运行时间\n", 485 | "# cfg.evaluation.interval = 12\n", 486 | "# # 设置存档点间隔减少存储空间的消耗\n", 487 | "# cfg.checkpoint_config.interval = 12\n", 488 | "\n", 489 | "# 设置运行epoch数\n", 490 | "cfg.runner.max_epochs = 1\n", 491 | "\n", 492 | "# 固定随机种子使得结果可复现\n", 493 | "cfg.seed = 0\n", 494 | "set_random_seed(0, deterministic=False)\n", 495 | "cfg.gpu_ids = range(1)\n", 496 | "\n", 497 | "cfg.device = 'cuda'\n", 498 | "\n", 499 | "# 打印所有的配置参数\n", 500 | "# print(f'Config:\\n{cfg.pretty_text}')\n", 501 | "\n", 502 | "# 保存配置文件\n", 503 | "mmcv.mkdir_or_exist('work_dirs/balloon')\n", 504 | "cfg.dump('work_dirs/balloon/cocoformat.py')" 505 | ] 506 | }, 507 | { 508 | "cell_type": "markdown", 509 | "id": "bba48d6e-154e-4f11-af12-7ee52a2e2f2f", 510 | "metadata": { 511 | "id": "9iM2JQYZWzp0" 512 | }, 513 | "source": [ 514 | "### 训练新模型" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": null, 520 | "id": "155797b8-6818-44b1-b42a-a0fcdce4e6f8", 521 | "metadata": {}, 522 | "outputs": [], 523 | "source": [ 524 | "from mmdet.datasets import build_dataset\n", 525 | "from mmdet.models import build_detector\n", 526 | "from mmdet.apis import train_detector\n", 527 | "\n", 528 | "# 构建数据集\n", 529 | "datasets = [build_dataset(cfg.data.train)]\n", 530 | "\n", 531 | "# 构建检测模型\n", 532 | "model = build_detector(cfg.model)\n", 533 | "# 添加类别文字属性提高可视化效果\n", 534 | "model.CLASSES = datasets[0].CLASSES\n", 535 | "\n", 536 | "# 创建工作目录并训练模型\n", 537 | "mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": null, 543 | "id": "9f6b76b5-7499-4b6e-9023-b3800c1213ae", 544 | "metadata": { 545 | "colab": { 546 | "base_uri": "https://localhost:8080/" 547 | }, 548 | "executionInfo": { 549 | "elapsed": 35106, 550 | "status": "ok", 551 | "timestamp": 1619665911343, 552 | "user": { 553 | "displayName": "Leju Yu", 554 | "photoUrl": "", 555 | "userId": "02803640020004660259" 556 | }, 557 | "user_tz": -480 558 | }, 559 | "id": "g9mcZjrSW905", 560 | "outputId": "779441b9-c94d-480a-f47a-78f17732b26b" 561 | }, 562 | "outputs": [], 563 | "source": [ 564 | "# 训练模型\n", 565 | "train_detector(model, datasets, cfg, distributed=False, validate=True)" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "id": "afe18ea0-6814-4719-b772-912fc0b50b79", 571 | "metadata": { 572 | "id": "rLTt2kH2YY-P" 573 | }, 574 | "source": [ 575 | "### 在测试集评价模型\n", 576 | "\n", 577 | "训练完模型后,需要对模型进行评价分析,这里可以用mmdet提供的test.py来直接运行。更多评价方法可以查看[这里](https://mmdetection.readthedocs.io/en/latest/1_exist_data_model.html)" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": null, 583 | "id": "b39c0721-1747-4c5f-b691-e1587e19dd4c", 584 | "metadata": { 585 | "colab": { 586 | "base_uri": "https://localhost:8080/" 587 | }, 588 | "executionInfo": { 589 | "elapsed": 12392, 590 | "status": "ok", 591 | "timestamp": 1619665946957, 592 | "user": { 593 | "displayName": "Leju Yu", 594 | "photoUrl": "", 595 | "userId": "02803640020004660259" 596 | }, 597 | "user_tz": -480 598 | }, 599 | "id": "5n62JruH9rdY", 600 | "outputId": "2667cc97-ee45-43b0-f3b6-6b0b10bdf622" 601 | }, 602 | "outputs": [], 603 | "source": [ 604 | "!python tools/test.py work_dirs/balloon/cocoformat.py work_dirs/balloon/latest.pth --eval bbox segm" 605 | ] 606 | }, 607 | { 608 | "cell_type": "markdown", 609 | "id": "ba487e60-5e2a-491d-adcb-e96301227d86", 610 | "metadata": { 611 | "id": "ca6M5InoF2Yr" 612 | }, 613 | "source": [ 614 | "### 预测新图像" 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": null, 620 | "id": "d859d070-79a3-427d-a7cf-da78e6621177", 621 | "metadata": { 622 | "colab": { 623 | "base_uri": "https://localhost:8080/", 624 | "height": 759 625 | }, 626 | "executionInfo": { 627 | "elapsed": 12418, 628 | "status": "ok", 629 | "timestamp": 1619665963224, 630 | "user": { 631 | "displayName": "Leju Yu", 632 | "photoUrl": "", 633 | "userId": "02803640020004660259" 634 | }, 635 | "user_tz": -480 636 | }, 637 | "id": "x3W1-_D1BYGW", 638 | "outputId": "e2f086b9-3313-4d8f-b151-75a55cde88b3" 639 | }, 640 | "outputs": [], 641 | "source": [ 642 | "from mmdet.apis import inference_detector, show_result_pyplot\n", 643 | "\n", 644 | "img = mmcv.imread('balloon/val/14898532020_ba6199dd22_k.jpg')\n", 645 | "\n", 646 | "model.cfg = cfg\n", 647 | "result = inference_detector(model, img)\n", 648 | "show_result_pyplot(model, img, result)" 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": null, 654 | "id": "40e3a224-2522-4b31-a827-2286a83a0c81", 655 | "metadata": {}, 656 | "outputs": [], 657 | "source": [ 658 | "result[0][0].shape" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": null, 664 | "id": "d9159330-9ce4-4b09-b1b9-104e3369a84f", 665 | "metadata": {}, 666 | "outputs": [], 667 | "source": [ 668 | "len(result[1][0])" 669 | ] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "id": "91308b6b-7bd3-4458-95f9-b7d6b77457f4", 675 | "metadata": {}, 676 | "outputs": [], 677 | "source": [ 678 | "result[1][0][0].shape" 679 | ] 680 | }, 681 | { 682 | "cell_type": "markdown", 683 | "id": "15698778-d97a-4479-9b66-c7d771fed1e4", 684 | "metadata": { 685 | "id": "JuF21F2rHOle", 686 | "tags": [] 687 | }, 688 | "source": [ 689 | "## 方法二:将数据集转换为中间格式\n", 690 | "\n", 691 | "除了coco格式,mmdet还支持中间格式的定义。\n" 692 | ] 693 | }, 694 | { 695 | "cell_type": "markdown", 696 | "id": "8f2790d6-621b-4696-8235-c97eeb697a1d", 697 | "metadata": { 698 | "id": "QA1pFg-FeO3l" 699 | }, 700 | "source": [ 701 | "#### 数据集中间格式\n", 702 | "\n", 703 | "```python\n", 704 | "[\n", 705 | " {\n", 706 | " 'filename': 'a.jpg',\n", 707 | " 'width': 1280,\n", 708 | " 'height': 720,\n", 709 | " 'ann': {\n", 710 | " 'bboxes': (n, 4),\n", 711 | " 'labels': (n, ),\n", 712 | " 'bboxes_ignore': (k, 4), (optional field)\n", 713 | " 'labels_ignore': (k, 4) (optional field),\n", 714 | " 'masks': [poly]\n", 715 | " }\n", 716 | " },\n", 717 | " ...\n", 718 | "]\n", 719 | "```\n", 720 | "我们需要根据上述中间类型来编写转换函数。" 721 | ] 722 | }, 723 | { 724 | "cell_type": "markdown", 725 | "id": "dc5bbee3-3970-4642-9883-1ed3eaca2617", 726 | "metadata": { 727 | "id": "nw6z8ynZGprI" 728 | }, 729 | "source": [ 730 | "### 转换标注文件格式\n", 731 | "\n", 732 | "编写功能函数`convert_balloon_to_middle`完成格式转换。" 733 | ] 734 | }, 735 | { 736 | "cell_type": "code", 737 | "execution_count": null, 738 | "id": "03006d89-078e-4501-ac07-7eecaf0946a2", 739 | "metadata": { 740 | "colab": { 741 | "base_uri": "https://localhost:8080/" 742 | }, 743 | "executionInfo": { 744 | "elapsed": 4135, 745 | "status": "ok", 746 | "timestamp": 1619708521475, 747 | "user": { 748 | "displayName": "Leju Yu", 749 | "photoUrl": "", 750 | "userId": "02803640020004660259" 751 | }, 752 | "user_tz": -480 753 | }, 754 | "id": "U-SkIvoDHJpR", 755 | "outputId": "78e49e2d-aaf3-4088-ba33-189c5d2f3764" 756 | }, 757 | "outputs": [], 758 | "source": [ 759 | "import os.path as osp\n", 760 | "import numpy as np\n", 761 | "\n", 762 | "def convert_balloon_to_middle(ann_file, out_file, image_prefix):\n", 763 | " data_infos = mmcv.load(ann_file)\n", 764 | "\n", 765 | " middle_format = []\n", 766 | " obj_count = 0\n", 767 | " for idx, v in enumerate(mmcv.track_iter_progress(data_infos.values())):\n", 768 | " filename = v['filename']\n", 769 | " img_path = osp.join(image_prefix, filename)\n", 770 | " height, width = mmcv.imread(img_path).shape[:2]\n", 771 | "\n", 772 | " data_info = dict(filename=filename, width=width, height=height)\n", 773 | "\n", 774 | " gt_bboxes = []\n", 775 | " gt_labels = []\n", 776 | " gt_masks_ann = []\n", 777 | "\n", 778 | " for _, obj in v['regions'].items():\n", 779 | " assert not obj['region_attributes']\n", 780 | " obj = obj['shape_attributes']\n", 781 | " px = obj['all_points_x']\n", 782 | " py = obj['all_points_y']\n", 783 | " poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]\n", 784 | " poly = [p for x in poly for p in x]\n", 785 | "\n", 786 | " x_min, y_min, x_max, y_max = (\n", 787 | " min(px), min(py), max(px), max(py))\n", 788 | "\n", 789 | " bbox=[x_min, y_min, x_max, y_max]\n", 790 | " label = 0\n", 791 | " segmentation=[poly]\n", 792 | " gt_bboxes.append(bbox)\n", 793 | " gt_labels.append(label)\n", 794 | " gt_masks_ann.append(segmentation)\n", 795 | "\n", 796 | " data_anno = dict(\n", 797 | " bboxes=np.array(gt_bboxes, dtype=np.float32).reshape(-1, 4),\n", 798 | " labels=np.array(gt_labels, dtype=np.long),\n", 799 | " # bboxes_ignore = np.zeros((0, 4), dtype=np.float32),\n", 800 | " # masks=gt_masks_ann\n", 801 | " ) \n", 802 | " \n", 803 | " data_info.update(ann=data_anno)\n", 804 | " middle_format.append(data_info)\n", 805 | "\n", 806 | " mmcv.dump(middle_format, out_file)\n", 807 | "convert_balloon_to_middle('balloon/train/via_region_data.json', 'balloon/train/middle.pkl', 'balloon/train')\n", 808 | "convert_balloon_to_middle('balloon/val/via_region_data.json', 'balloon/val/middle.pkl', 'balloon/val')" 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": null, 814 | "id": "76988f4d-785d-4954-8c80-c179a687d676", 815 | "metadata": { 816 | "colab": { 817 | "base_uri": "https://localhost:8080/" 818 | }, 819 | "executionInfo": { 820 | "elapsed": 1072, 821 | "status": "ok", 822 | "timestamp": 1619516478281, 823 | "user": { 824 | "displayName": "Yingfan Hu", 825 | "photoUrl": "", 826 | "userId": "05511228327870757093" 827 | }, 828 | "user_tz": -480 829 | }, 830 | "id": "kqoyZQeGssxf", 831 | "outputId": "c74e1f0b-909a-4b72-a6a0-7fde68718df1" 832 | }, 833 | "outputs": [], 834 | "source": [ 835 | "# 查看单张图片的标签格式\n", 836 | "import json\n", 837 | "\n", 838 | "data = mmcv.load('balloon/train/middle.pkl')\n", 839 | "print(data[3])" 840 | ] 841 | }, 842 | { 843 | "cell_type": "markdown", 844 | "id": "4eef7852-9c2c-48b2-ac56-92e986c92b09", 845 | "metadata": { 846 | "id": "0p8D4xqU5a7c" 847 | }, 848 | "source": [ 849 | "### 修改 config 配置并保存\n", 850 | "\n", 851 | "注意这里记得修改各个部分的数据集类型为`CustomDataset`、对应标注文件为`middle.pkl`、工作目录和配置文件中的部分为`middleformat`。" 852 | ] 853 | }, 854 | { 855 | "cell_type": "code", 856 | "execution_count": null, 857 | "id": "22ac52f2-4396-4e7d-a031-4310e9960964", 858 | "metadata": { 859 | "colab": { 860 | "base_uri": "https://localhost:8080/" 861 | }, 862 | "executionInfo": { 863 | "elapsed": 8024, 864 | "status": "ok", 865 | "timestamp": 1619707074226, 866 | "user": { 867 | "displayName": "Leju Yu", 868 | "photoUrl": "", 869 | "userId": "02803640020004660259" 870 | }, 871 | "user_tz": -480 872 | }, 873 | "id": "SEwH-ssoGzHk", 874 | "outputId": "6f272f91-4478-4945-eed7-cfa2a9fef512" 875 | }, 876 | "outputs": [], 877 | "source": [ 878 | "from mmcv import Config\n", 879 | "from mmdet.apis import set_random_seed\n", 880 | "\n", 881 | "# 获取基本配置文件参数\n", 882 | "# cfg = Config.fromfile('./configs/mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_coco.py')\n", 883 | "cfg = Config.fromfile('./configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_1x_coco.py')\n", 884 | "\n", 885 | "# 修改数据集类型以及文件路径\n", 886 | "cfg.dataset_type = 'CustomDataset'\n", 887 | "cfg.data_root = 'balloon/'\n", 888 | "cfg.classes = ('balloon',)\n", 889 | "\n", 890 | "cfg.data.train.type = 'CustomDataset'\n", 891 | "cfg.data.train.data_root = 'balloon/'\n", 892 | "cfg.data.train.ann_file = 'train/middle.pkl'\n", 893 | "cfg.data.train.img_prefix = 'train'\n", 894 | "cfg.data.train.classes = cfg.classes\n", 895 | "\n", 896 | "cfg.data.val.type = 'CustomDataset'\n", 897 | "cfg.data.val.data_root = 'balloon/'\n", 898 | "cfg.data.val.ann_file = 'val/middle.pkl'\n", 899 | "cfg.data.val.img_prefix = 'val'\n", 900 | "cfg.data.val.classes = cfg.classes\n", 901 | "\n", 902 | "cfg.data.test.type = 'CustomDataset'\n", 903 | "cfg.data.test.data_root = 'balloon/'\n", 904 | "cfg.data.test.ann_file = 'val/middle.pkl'\n", 905 | "cfg.data.test.img_prefix = 'val'\n", 906 | "cfg.data.test.classes = cfg.classes\n", 907 | "\n", 908 | "# 修改bbox_head和mask_head中的类别数\n", 909 | "cfg.model.roi_head.bbox_head.num_classes = 1\n", 910 | "# cfg.model.roi_head.mask_head.num_classes = 1\n", 911 | "# 使用预训练好的faster rcnn模型用于finetuning\n", 912 | "cfg.load_from = 'checkpoints/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth'\n", 913 | "# 设置工作目录用于存放log和临时文件\n", 914 | "cfg.work_dir = 'work_dirs/balloon/middleformat'\n", 915 | "\n", 916 | "# 原本的学习率是在8卡基础上训练设置的,现在单卡需要除以8\n", 917 | "cfg.optimizer.lr = 0.02 / 8\n", 918 | "cfg.lr_config.warmup = None\n", 919 | "cfg.log_config.interval = 10\n", 920 | "\n", 921 | "# 由于是自定义数据集,需要修改评价方法\n", 922 | "cfg.evaluation.metric = 'mAP'\n", 923 | "# # 设置evaluation间隔减少运行时间\n", 924 | "# cfg.evaluation.interval = 12\n", 925 | "# # 设置存档点间隔减少存储空间的消耗\n", 926 | "# cfg.checkpoint_config.interval = 12\n", 927 | "\n", 928 | "# 设置运行epoch数\n", 929 | "cfg.runner.max_epochs = 1\n", 930 | "\n", 931 | "# 固定随机种子使得结果可复现\n", 932 | "cfg.seed = 0\n", 933 | "set_random_seed(0, deterministic=False)\n", 934 | "cfg.gpu_ids = range(1)\n", 935 | "\n", 936 | "cfg.device = 'cuda'\n", 937 | "\n", 938 | "# 打印所有的配置参数\n", 939 | "# print(f'Config:\\n{cfg.pretty_text}')\n", 940 | "\n", 941 | "# 保存配置文件\n", 942 | "mmcv.mkdir_or_exist('work_dirs/balloon/middleformat')\n", 943 | "cfg.dump('work_dirs/balloon/middleformat/middleformat.py')" 944 | ] 945 | }, 946 | { 947 | "cell_type": "markdown", 948 | "id": "e2274f43-02b0-4cd8-91bd-07ee5c69d38d", 949 | "metadata": { 950 | "id": "c5uXqxsETHIV" 951 | }, 952 | "source": [ 953 | "### 训练新模型\n", 954 | "\n", 955 | "这里需要根据配置文件构建数据集,检测模型并完成训练。" 956 | ] 957 | }, 958 | { 959 | "cell_type": "code", 960 | "execution_count": null, 961 | "id": "e7d66285-2e7d-4138-b506-70e186848f92", 962 | "metadata": {}, 963 | "outputs": [], 964 | "source": [ 965 | "from mmdet.datasets import build_dataset\n", 966 | "from mmdet.models import build_detector\n", 967 | "from mmdet.apis import train_detector\n", 968 | "\n", 969 | "# 构建数据集\n", 970 | "datasets = [build_dataset(cfg.data.train)]\n", 971 | "\n", 972 | "# 构建检测模型\n", 973 | "model = build_detector(cfg.model)\n", 974 | "# 添加类别文字属性提高可视化效果\n", 975 | "model.CLASSES = datasets[0].CLASSES\n", 976 | "\n", 977 | "# 创建工作目录并训练模型\n", 978 | "mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))" 979 | ] 980 | }, 981 | { 982 | "cell_type": "code", 983 | "execution_count": null, 984 | "id": "8d6a318d-3acf-45e6-9091-fca9f5133c33", 985 | "metadata": { 986 | "colab": { 987 | "base_uri": "https://localhost:8080/" 988 | }, 989 | "executionInfo": { 990 | "elapsed": 22086, 991 | "status": "ok", 992 | "timestamp": 1619666886924, 993 | "user": { 994 | "displayName": "Leju Yu", 995 | "photoUrl": "", 996 | "userId": "02803640020004660259" 997 | }, 998 | "user_tz": -480 999 | }, 1000 | "id": "7WBWHu010PN3", 1001 | "outputId": "353ed931-b2b3-41c7-bcaf-ce8a632db99b" 1002 | }, 1003 | "outputs": [], 1004 | "source": [ 1005 | "# 训练模型\n", 1006 | "train_detector(model, datasets, cfg, distributed=False, validate=True)" 1007 | ] 1008 | }, 1009 | { 1010 | "cell_type": "markdown", 1011 | "id": "23a99476-2d5a-42a4-a2e8-a75ef520ee06", 1012 | "metadata": { 1013 | "id": "juA1FBeuTKP5" 1014 | }, 1015 | "source": [ 1016 | "### 测试集上评价模型" 1017 | ] 1018 | }, 1019 | { 1020 | "cell_type": "code", 1021 | "execution_count": null, 1022 | "id": "114e09d1-3569-4341-ac1b-8a1eb380125a", 1023 | "metadata": { 1024 | "colab": { 1025 | "base_uri": "https://localhost:8080/" 1026 | }, 1027 | "executionInfo": { 1028 | "elapsed": 30324, 1029 | "status": "ok", 1030 | "timestamp": 1619666897650, 1031 | "user": { 1032 | "displayName": "Leju Yu", 1033 | "photoUrl": "", 1034 | "userId": "02803640020004660259" 1035 | }, 1036 | "user_tz": -480 1037 | }, 1038 | "id": "Qtzdd1cLMq_S", 1039 | "outputId": "ed5490c4-f189-4aff-e52d-ecc4f09a201f" 1040 | }, 1041 | "outputs": [], 1042 | "source": [ 1043 | "!python tools/test.py work_dirs/balloon/middleformat/middleformat.py work_dirs/balloon/middleformat/latest.pth --eval mAP" 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "markdown", 1048 | "id": "5f693ac9-d70c-45c0-b033-eaf7228def81", 1049 | "metadata": {}, 1050 | "source": [ 1051 | "### 预测新图像" 1052 | ] 1053 | }, 1054 | { 1055 | "cell_type": "code", 1056 | "execution_count": null, 1057 | "id": "41b925c2-2647-4c30-b053-bf988261042e", 1058 | "metadata": { 1059 | "colab": { 1060 | "base_uri": "https://localhost:8080/", 1061 | "height": 759 1062 | }, 1063 | "executionInfo": { 1064 | "elapsed": 40276, 1065 | "status": "ok", 1066 | "timestamp": 1619666908576, 1067 | "user": { 1068 | "displayName": "Leju Yu", 1069 | "photoUrl": "", 1070 | "userId": "02803640020004660259" 1071 | }, 1072 | "user_tz": -480 1073 | }, 1074 | "id": "4a0-nLpEMwfP", 1075 | "outputId": "9ff80a35-7547-44db-c1b3-e0da60382c35" 1076 | }, 1077 | "outputs": [], 1078 | "source": [ 1079 | "from mmdet.apis import inference_detector, show_result_pyplot\n", 1080 | "\n", 1081 | "img = mmcv.imread('balloon/val/14898532020_ba6199dd22_k.jpg')\n", 1082 | "\n", 1083 | "model.cfg = cfg\n", 1084 | "result = inference_detector(model, img)\n", 1085 | "\n", 1086 | "show_result_pyplot(model, img, result)" 1087 | ] 1088 | }, 1089 | { 1090 | "cell_type": "markdown", 1091 | "id": "a821d4f9-be76-4796-b346-df4422d11712", 1092 | "metadata": { 1093 | "id": "0JGGbWWoIGaB" 1094 | }, 1095 | "source": [ 1096 | "## 方法三:直接实现新数据集的支持" 1097 | ] 1098 | }, 1099 | { 1100 | "cell_type": "markdown", 1101 | "id": "34c4511f-27e3-4ebf-8b0f-1e42e8d918f4", 1102 | "metadata": {}, 1103 | "source": [ 1104 | "在`mmdet/datasets`新建`BalloonDataset.py`文件,填充内容如下:\n", 1105 | "\n", 1106 | "并在`mmdet/datasets/__init__.py`中,增加`from .BalloonDataset import BalloonDataset`" 1107 | ] 1108 | }, 1109 | { 1110 | "cell_type": "code", 1111 | "execution_count": null, 1112 | "id": "4c5df927-d969-4582-97df-498ef16e2bc1", 1113 | "metadata": { 1114 | "id": "GdSaB2ad0EdX" 1115 | }, 1116 | "outputs": [], 1117 | "source": [ 1118 | "# mmdet/datasets/BalloonDataset.py\n", 1119 | "\n", 1120 | "import copy\n", 1121 | "import os.path as osp\n", 1122 | "\n", 1123 | "import mmcv\n", 1124 | "import numpy as np\n", 1125 | "\n", 1126 | "from mmdet.datasets.builder import DATASETS\n", 1127 | "from mmdet.datasets.custom import CustomDataset\n", 1128 | "\n", 1129 | "@DATASETS.register_module()\n", 1130 | "class BalloonDataset(CustomDataset):\n", 1131 | "\n", 1132 | " CLASSES = ('balloon',)\n", 1133 | "\n", 1134 | " def load_annotations(self, ann_file):\n", 1135 | " data_infos = mmcv.load(self.ann_file)\n", 1136 | "\n", 1137 | " middle_format = []\n", 1138 | " obj_count = 0\n", 1139 | " for idx, v in enumerate(mmcv.track_iter_progress(data_infos.values())):\n", 1140 | " filename = v['filename']\n", 1141 | " img_path = osp.join(self.img_prefix, filename)\n", 1142 | " height, width = mmcv.imread(img_path).shape[:2]\n", 1143 | "\n", 1144 | " data_info = dict(filename=filename, width=width, height=height)\n", 1145 | "\n", 1146 | " gt_bboxes = []\n", 1147 | " gt_labels = []\n", 1148 | " gt_masks_ann = []\n", 1149 | "\n", 1150 | " for _, obj in v['regions'].items():\n", 1151 | " assert not obj['region_attributes']\n", 1152 | " obj = obj['shape_attributes']\n", 1153 | " px = obj['all_points_x']\n", 1154 | " py = obj['all_points_y']\n", 1155 | " poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]\n", 1156 | " poly = [p for x in poly for p in x]\n", 1157 | "\n", 1158 | " x_min, y_min, x_max, y_max = (\n", 1159 | " min(px), min(py), max(px), max(py))\n", 1160 | "\n", 1161 | " bbox=[x_min, y_min, x_max, y_max]\n", 1162 | " label = 0\n", 1163 | " segmentation=[poly]\n", 1164 | " gt_bboxes.append(bbox)\n", 1165 | " gt_labels.append(label)\n", 1166 | " gt_masks_ann.append(segmentation)\n", 1167 | "\n", 1168 | " data_anno = dict(\n", 1169 | " bboxes=np.array(gt_bboxes, dtype=np.float32).reshape(-1, 4),\n", 1170 | " labels=np.array(gt_labels, dtype=np.long),\n", 1171 | " # bboxes_ignore = np.zeros((0, 4), dtype=np.float32),\n", 1172 | " # masks=gt_masks_ann\n", 1173 | " ) \n", 1174 | " \n", 1175 | " data_info.update(ann=data_anno)\n", 1176 | " middle_format.append(data_info)\n", 1177 | "\n", 1178 | " return middle_format" 1179 | ] 1180 | }, 1181 | { 1182 | "cell_type": "markdown", 1183 | "id": "9e176d3d-577f-4466-9e41-e0f429aa6ab6", 1184 | "metadata": { 1185 | "id": "mZcasm6mQ4nM" 1186 | }, 1187 | "source": [ 1188 | "### 修改config配置文件并保存\n", 1189 | "\n", 1190 | "这里同样注意修改数据集类型,标注文件名以及工作目录和配置文件存储路径等" 1191 | ] 1192 | }, 1193 | { 1194 | "cell_type": "code", 1195 | "execution_count": null, 1196 | "id": "85fbcff8-90aa-42cf-9ea5-85273469dd38", 1197 | "metadata": { 1198 | "colab": { 1199 | "base_uri": "https://localhost:8080/" 1200 | }, 1201 | "executionInfo": { 1202 | "elapsed": 8300, 1203 | "status": "ok", 1204 | "timestamp": 1619666909133, 1205 | "user": { 1206 | "displayName": "Leju Yu", 1207 | "photoUrl": "", 1208 | "userId": "02803640020004660259" 1209 | }, 1210 | "user_tz": -480 1211 | }, 1212 | "id": "Gax3-zHEyj1Y", 1213 | "outputId": "210d738e-fd3d-439e-c98a-7e03c89651f3" 1214 | }, 1215 | "outputs": [], 1216 | "source": [ 1217 | "from mmcv import Config\n", 1218 | "from mmdet.apis import set_random_seed\n", 1219 | "\n", 1220 | "# 获取基本配置文件参数\n", 1221 | "# cfg = Config.fromfile('./configs/mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_coco.py')\n", 1222 | "cfg = Config.fromfile('./configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_1x_coco.py')\n", 1223 | "\n", 1224 | "# 修改数据集类型以及文件路径\n", 1225 | "cfg.dataset_type = 'BalloonDataset'\n", 1226 | "cfg.data_root = 'balloon/'\n", 1227 | "\n", 1228 | "cfg.data.train.type = 'BalloonDataset'\n", 1229 | "cfg.data.train.data_root = 'balloon/'\n", 1230 | "cfg.data.train.ann_file = 'train/via_region_data.json'\n", 1231 | "cfg.data.train.img_prefix = 'train'\n", 1232 | "\n", 1233 | "cfg.data.val.type = 'BalloonDataset'\n", 1234 | "cfg.data.val.data_root = 'balloon/'\n", 1235 | "cfg.data.val.ann_file = 'val/via_region_data.json'\n", 1236 | "cfg.data.val.img_prefix = 'val'\n", 1237 | "\n", 1238 | "cfg.data.test.type = 'BalloonDataset'\n", 1239 | "cfg.data.test.data_root = 'balloon/'\n", 1240 | "cfg.data.test.ann_file = 'val/via_region_data.json'\n", 1241 | "cfg.data.test.img_prefix = 'val'\n", 1242 | "\n", 1243 | "# 修改bbox_head和mask_head中的类别数\n", 1244 | "cfg.model.roi_head.bbox_head.num_classes = 1\n", 1245 | "# cfg.model.roi_head.mask_head.num_classes = 1\n", 1246 | "# 使用预训练好的faster rcnn模型用于finetuning\n", 1247 | "cfg.load_from = 'checkpoints/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth'\n", 1248 | "# 设置工作目录用于存放log和临时文件\n", 1249 | "cfg.work_dir = 'work_dirs/balloon/newdataset'\n", 1250 | "\n", 1251 | "# 原本的学习率是在8卡基础上训练设置的,现在单卡需要除以8\n", 1252 | "cfg.optimizer.lr = 0.02 / 8\n", 1253 | "cfg.lr_config.warmup = None\n", 1254 | "cfg.log_config.interval = 10\n", 1255 | "\n", 1256 | "# 由于是自定义数据集,需要修改评价方法\n", 1257 | "cfg.evaluation.metric = 'mAP'\n", 1258 | "# # 设置evaluation间隔减少运行时间\n", 1259 | "# cfg.evaluation.interval = 12\n", 1260 | "# # 设置存档点间隔减少存储空间的消耗\n", 1261 | "# cfg.checkpoint_config.interval = 12\n", 1262 | "\n", 1263 | "# 设置运行epoch数\n", 1264 | "cfg.runner.max_epochs = 1\n", 1265 | "\n", 1266 | "# 固定随机种子使得结果可复现\n", 1267 | "cfg.seed = 0\n", 1268 | "set_random_seed(0, deterministic=False)\n", 1269 | "cfg.gpu_ids = range(1)\n", 1270 | "\n", 1271 | "cfg.device = 'cuda'\n", 1272 | "\n", 1273 | "# 打印所有的配置参数\n", 1274 | "# print(f'Config:\\n{cfg.pretty_text}')\n", 1275 | "\n", 1276 | "# 保存配置文件\n", 1277 | "mmcv.mkdir_or_exist('work_dirs/balloon/newdataset')\n", 1278 | "cfg.dump('work_dirs/balloon/newdataset/newdataset.py')" 1279 | ] 1280 | }, 1281 | { 1282 | "cell_type": "markdown", 1283 | "id": "1b2a2650-3bb2-4972-b0c2-cf08980fd1f3", 1284 | "metadata": { 1285 | "id": "111W_oZV_3wa" 1286 | }, 1287 | "source": [ 1288 | "### 训练新模型" 1289 | ] 1290 | }, 1291 | { 1292 | "cell_type": "code", 1293 | "execution_count": null, 1294 | "id": "258d48a8-ffcb-44f2-acaf-4762ffce5b4d", 1295 | "metadata": {}, 1296 | "outputs": [], 1297 | "source": [ 1298 | "from mmdet.datasets import build_dataset\n", 1299 | "from mmdet.models import build_detector\n", 1300 | "from mmdet.apis import train_detector\n", 1301 | "\n", 1302 | "# 构建数据集\n", 1303 | "datasets = [build_dataset(cfg.data.train)]\n", 1304 | "\n", 1305 | "# 构建检测模型\n", 1306 | "model = build_detector(cfg.model)\n", 1307 | "# 添加类别文字属性提高可视化效果\n", 1308 | "model.CLASSES = datasets[0].CLASSES\n", 1309 | "\n", 1310 | "# 创建工作目录并训练模型\n", 1311 | "mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))" 1312 | ] 1313 | }, 1314 | { 1315 | "cell_type": "code", 1316 | "execution_count": null, 1317 | "id": "67e880e2-3484-474f-886a-25898e1feccc", 1318 | "metadata": { 1319 | "colab": { 1320 | "base_uri": "https://localhost:8080/" 1321 | }, 1322 | "executionInfo": { 1323 | "elapsed": 27585, 1324 | "status": "ok", 1325 | "timestamp": 1619666928428, 1326 | "user": { 1327 | "displayName": "Leju Yu", 1328 | "photoUrl": "", 1329 | "userId": "02803640020004660259" 1330 | }, 1331 | "user_tz": -480 1332 | }, 1333 | "id": "stuhtUucJ-wD", 1334 | "outputId": "e142776f-c14f-4ba8-cebf-852d36eeecd0" 1335 | }, 1336 | "outputs": [], 1337 | "source": [ 1338 | "# 训练模型\n", 1339 | "train_detector(model, datasets, cfg, distributed=False, validate=True)" 1340 | ] 1341 | }, 1342 | { 1343 | "cell_type": "markdown", 1344 | "id": "ffa52f0e-b8b0-42ed-a91e-3b397870a6d0", 1345 | "metadata": { 1346 | "id": "MfQ-yspZLuuI" 1347 | }, 1348 | "source": [ 1349 | "### 测试集上评价模型" 1350 | ] 1351 | }, 1352 | { 1353 | "cell_type": "code", 1354 | "execution_count": null, 1355 | "id": "07da0dec-f7a1-4e53-a555-17123ef1fdc2", 1356 | "metadata": { 1357 | "colab": { 1358 | "base_uri": "https://localhost:8080/" 1359 | }, 1360 | "executionInfo": { 1361 | "elapsed": 35993, 1362 | "status": "ok", 1363 | "timestamp": 1619666939084, 1364 | "user": { 1365 | "displayName": "Leju Yu", 1366 | "photoUrl": "", 1367 | "userId": "02803640020004660259" 1368 | }, 1369 | "user_tz": -480 1370 | }, 1371 | "id": "TLz4Hd8a2aeP", 1372 | "outputId": "22b1ee7c-0db1-4443-fcbb-4de0130bc93c" 1373 | }, 1374 | "outputs": [], 1375 | "source": [ 1376 | "!python tools/test.py work_dirs/ballon/newdataset/newdataset.py work_dirs/ballon/newdataset/latest.pth --eval mAP" 1377 | ] 1378 | }, 1379 | { 1380 | "cell_type": "markdown", 1381 | "id": "b365ba7d-33b8-4ce8-b83c-11d53badae35", 1382 | "metadata": {}, 1383 | "source": [ 1384 | "### 预测新图像" 1385 | ] 1386 | }, 1387 | { 1388 | "cell_type": "code", 1389 | "execution_count": null, 1390 | "id": "cc77e019-fb60-4f88-be8a-363711687c3d", 1391 | "metadata": { 1392 | "colab": { 1393 | "base_uri": "https://localhost:8080/", 1394 | "height": 759 1395 | }, 1396 | "executionInfo": { 1397 | "elapsed": 33277, 1398 | "status": "ok", 1399 | "timestamp": 1619666950000, 1400 | "user": { 1401 | "displayName": "Leju Yu", 1402 | "photoUrl": "", 1403 | "userId": "02803640020004660259" 1404 | }, 1405 | "user_tz": -480 1406 | }, 1407 | "id": "K5x_EzHN2e2z", 1408 | "outputId": "ef91c076-2509-4962-b817-ebaecf058861" 1409 | }, 1410 | "outputs": [], 1411 | "source": [ 1412 | "from mmdet.apis import inference_detector, show_result_pyplot\n", 1413 | "\n", 1414 | "img = mmcv.imread('balloon/val/14898532020_ba6199dd22_k.jpg')\n", 1415 | "\n", 1416 | "model.cfg = cfg\n", 1417 | "result = inference_detector(model, img)\n", 1418 | "\n", 1419 | "show_result_pyplot(model, img, result)" 1420 | ] 1421 | }, 1422 | { 1423 | "cell_type": "code", 1424 | "execution_count": null, 1425 | "id": "46c027b9-b97b-407b-b568-87b56128b30d", 1426 | "metadata": {}, 1427 | "outputs": [], 1428 | "source": [] 1429 | } 1430 | ], 1431 | "metadata": { 1432 | "kernelspec": { 1433 | "display_name": "", 1434 | "name": "" 1435 | }, 1436 | "language_info": { 1437 | "name": "" 1438 | } 1439 | }, 1440 | "nbformat": 4, 1441 | "nbformat_minor": 5 1442 | } 1443 | --------------------------------------------------------------------------------