Spaces:
Runtime error
Runtime error
File size: 16,836 Bytes
3b96cb1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/open-mmlab/mmsegmentation/blob/main/demo/MMSegmentation_Tutorial.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FVmnaxFJvsb8"
},
"source": [
"# MMSegmentation Tutorial\n",
"Welcome to MMSegmentation! \n",
"\n",
"In this tutorial, we demo\n",
"* How to do inference with MMSeg trained weight\n",
"* How to train on your own dataset and visualize the results. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QS8YHrEhbpas"
},
"source": [
"## Install MMSegmentation\n",
"This step may take several minutes. \n",
"\n",
"We use PyTorch 1.12 and CUDA 11.3 for this tutorial. You may install other versions by change the version number in pip install command. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UWyLrLYaNEaL",
"outputId": "32a47fe3-f10d-47a1-f6b9-b7c235abdab1"
},
"outputs": [],
"source": [
"# Check nvcc version\n",
"!nvcc -V\n",
"# Check GCC version\n",
"!gcc --version"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Ki3WUBjKbutg",
"outputId": "14bd14b0-4d8c-4fa9-e3f9-da35c0efc0d5"
},
"outputs": [],
"source": [
"# Install PyTorch\n",
"!conda install pytorch==1.12.0 torchvision==0.13.0 torchaudio==0.12.0 cudatoolkit=11.3 -c pytorch\n",
"# Install mim\n",
"!pip install -U openmim\n",
"# Install mmengine\n",
"!mim install mmengine\n",
"# Install MMCV\n",
"!mim install 'mmcv >= 2.0.0rc1'\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nR-hHRvbNJJZ",
"outputId": "10c3b131-d4db-458c-fc10-b94b1c6ed546"
},
"outputs": [],
"source": [
"!rm -rf mmsegmentation\n",
"!git clone -b main https://github.com/open-mmlab/mmsegmentation.git \n",
"%cd mmsegmentation\n",
"!pip install -e ."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mAE_h7XhPT7d",
"outputId": "83bf0f8e-fc69-40b1-f9fe-0025724a217c"
},
"outputs": [],
"source": [
"# Check Pytorch installation\n",
"import torch, torchvision\n",
"print(torch.__version__, torch.cuda.is_available())\n",
"\n",
"# Check MMSegmentation installation\n",
"import mmseg\n",
"print(mmseg.__version__)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ta51clKX4cwM"
},
"source": [
"## Finetune a semantic segmentation model on a new dataset\n",
"\n",
"To finetune on a customized dataset, the following steps are necessary. \n",
"1. Add a new dataset class. \n",
"2. Create a config file accordingly. \n",
"3. Perform training and evaluation. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AcZg6x_K5Zs3"
},
"source": [
"### Add a new dataset\n",
"\n",
"Datasets in MMSegmentation require image and semantic segmentation maps to be placed in folders with the same prefix. To support a new dataset, we may need to modify the original file structure. \n",
"\n",
"In this tutorial, we give an example of converting the dataset. You may refer to [docs](https://github.com/open-mmlab/mmsegmentation/blob/master/docs/en/tutorials/customize_datasets.md#customize-datasets-by-reorganizing-data) for details about dataset reorganization. \n",
"\n",
"We use [Stanford Background Dataset](http://dags.stanford.edu/projects/scenedataset.html) as an example. The dataset contains 715 images chosen from existing public datasets [LabelMe](http://labelme.csail.mit.edu), [MSRC](http://research.microsoft.com/en-us/projects/objectclassrecognition), [PASCAL VOC](http://pascallin.ecs.soton.ac.uk/challenges/VOC) and [Geometric Context](http://www.cs.illinois.edu/homes/dhoiem/). Images from these datasets are mainly outdoor scenes, each containing approximately 320-by-240 pixels. \n",
"In this tutorial, we use the region annotations as labels. There are 8 classes in total, i.e. sky, tree, road, grass, water, building, mountain, and foreground object. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TFIt7MHq5Wls",
"outputId": "74a126e4-c8a4-4d2f-a910-b58b71843a23"
},
"outputs": [],
"source": [
"# download and unzip\n",
"!wget http://dags.stanford.edu/data/iccv09Data.tar.gz -O stanford_background.tar.gz\n",
"!tar xf stanford_background.tar.gz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 377
},
"id": "78LIci7F9WWI",
"outputId": "c432ddac-5a50-47b1-daac-5a26b07afea2"
},
"outputs": [],
"source": [
"# Let's take a look at the dataset\n",
"import mmcv\n",
"import mmengine\n",
"import matplotlib.pyplot as plt\n",
"\n",
"\n",
"img = mmcv.imread('iccv09Data/images/6000124.jpg')\n",
"plt.figure(figsize=(8, 6))\n",
"plt.imshow(mmcv.bgr2rgb(img))\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "L5mNQuc2GsVE"
},
"source": [
"We need to convert the annotation into semantic map format as an image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WnGZfribFHCx"
},
"outputs": [],
"source": [
"# define dataset root and directory for images and annotations\n",
"data_root = 'iccv09Data'\n",
"img_dir = 'images'\n",
"ann_dir = 'labels'\n",
"# define class and palette for better visualization\n",
"classes = ('sky', 'tree', 'road', 'grass', 'water', 'bldg', 'mntn', 'fg obj')\n",
"palette = [[128, 128, 128], [129, 127, 38], [120, 69, 125], [53, 125, 34], \n",
" [0, 11, 123], [118, 20, 12], [122, 81, 25], [241, 134, 51]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WnGZfribFHCx"
},
"outputs": [],
"source": [
"import os.path as osp\n",
"import numpy as np\n",
"from PIL import Image\n",
"\n",
"# convert dataset annotation to semantic segmentation map\n",
"for file in mmengine.scandir(osp.join(data_root, ann_dir), suffix='.regions.txt'):\n",
" seg_map = np.loadtxt(osp.join(data_root, ann_dir, file)).astype(np.uint8)\n",
" seg_img = Image.fromarray(seg_map).convert('P')\n",
" seg_img.putpalette(np.array(palette, dtype=np.uint8))\n",
" seg_img.save(osp.join(data_root, ann_dir, file.replace('.regions.txt', \n",
" '.png')))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 377
},
"id": "5MCSS9ABfSks",
"outputId": "92b9bafc-589e-48fc-c9e9-476f125d6522"
},
"outputs": [],
"source": [
"# Let's take a look at the segmentation map we got\n",
"import matplotlib.patches as mpatches\n",
"img = Image.open('iccv09Data/labels/6000124.png')\n",
"plt.figure(figsize=(8, 6))\n",
"im = plt.imshow(np.array(img.convert('RGB')))\n",
"\n",
"# create a patch (proxy artist) for every color \n",
"patches = [mpatches.Patch(color=np.array(palette[i])/255., \n",
" label=classes[i]) for i in range(8)]\n",
"# put those patched as legend-handles into the legend\n",
"plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., \n",
" fontsize='large')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WbeLYCp2k5hl"
},
"outputs": [],
"source": [
"# split train/val set randomly\n",
"split_dir = 'splits'\n",
"mmengine.mkdir_or_exist(osp.join(data_root, split_dir))\n",
"filename_list = [osp.splitext(filename)[0] for filename in mmengine.scandir(\n",
" osp.join(data_root, ann_dir), suffix='.png')]\n",
"with open(osp.join(data_root, split_dir, 'train.txt'), 'w') as f:\n",
" # select first 4/5 as train set\n",
" train_length = int(len(filename_list)*4/5)\n",
" f.writelines(line + '\\n' for line in filename_list[:train_length])\n",
"with open(osp.join(data_root, split_dir, 'val.txt'), 'w') as f:\n",
" # select last 1/5 as train set\n",
" f.writelines(line + '\\n' for line in filename_list[train_length:])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HchvmGYB_rrO"
},
"source": [
"After downloading the data, we need to implement `load_annotations` function in the new dataset class `StanfordBackgroundDataset`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "LbsWOw62_o-X"
},
"outputs": [],
"source": [
"from mmseg.registry import DATASETS\n",
"from mmseg.datasets import BaseSegDataset\n",
"\n",
"\n",
"@DATASETS.register_module()\n",
"class StanfordBackgroundDataset(BaseSegDataset):\n",
" METAINFO = dict(classes = classes, palette = palette)\n",
" def __init__(self, **kwargs):\n",
" super().__init__(img_suffix='.jpg', seg_map_suffix='.png', **kwargs)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yUVtmn3Iq3WA"
},
"source": [
"### Create a config file\n",
"In the next step, we need to modify the config for the training. To accelerate the process, we finetune the model from trained weights."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Download config and checkpoint files\n",
"!mim download mmsegmentation --config pspnet_r50-d8_4xb2-40k_cityscapes-512x1024 --dest ."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Wwnj9tRzqX_A"
},
"outputs": [],
"source": [
"from mmengine import Config\n",
"cfg = Config.fromfile('configs/pspnet/pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py')\n",
"print(f'Config:\\n{cfg.pretty_text}')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1y2oV5w97jQo"
},
"source": [
"Since the given config is used to train PSPNet on the cityscapes dataset, we need to modify it accordingly for our new dataset. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "eyKnYC1Z7iCV",
"outputId": "6195217b-187f-4675-994b-ba90d8bb3078"
},
"outputs": [],
"source": [
"# Since we use only one GPU, BN is used instead of SyncBN\n",
"cfg.norm_cfg = dict(type='BN', requires_grad=True)\n",
"cfg.crop_size = (256, 256)\n",
"cfg.model.data_preprocessor.size = cfg.crop_size\n",
"cfg.model.backbone.norm_cfg = cfg.norm_cfg\n",
"cfg.model.decode_head.norm_cfg = cfg.norm_cfg\n",
"cfg.model.auxiliary_head.norm_cfg = cfg.norm_cfg\n",
"# modify num classes of the model in decode/auxiliary head\n",
"cfg.model.decode_head.num_classes = 8\n",
"cfg.model.auxiliary_head.num_classes = 8\n",
"\n",
"# Modify dataset type and path\n",
"cfg.dataset_type = 'StanfordBackgroundDataset'\n",
"cfg.data_root = data_root\n",
"\n",
"cfg.train_dataloader.batch_size = 8\n",
"\n",
"cfg.train_pipeline = [\n",
" dict(type='LoadImageFromFile'),\n",
" dict(type='LoadAnnotations'),\n",
" dict(type='RandomResize', scale=(320, 240), ratio_range=(0.5, 2.0), keep_ratio=True),\n",
" dict(type='RandomCrop', crop_size=cfg.crop_size, cat_max_ratio=0.75),\n",
" dict(type='RandomFlip', prob=0.5),\n",
" dict(type='PackSegInputs')\n",
"]\n",
"\n",
"cfg.test_pipeline = [\n",
" dict(type='LoadImageFromFile'),\n",
" dict(type='Resize', scale=(320, 240), keep_ratio=True),\n",
" # add loading annotation after ``Resize`` because ground truth\n",
" # does not need to do resize data transform\n",
" dict(type='LoadAnnotations'),\n",
" dict(type='PackSegInputs')\n",
"]\n",
"\n",
"\n",
"cfg.train_dataloader.dataset.type = cfg.dataset_type\n",
"cfg.train_dataloader.dataset.data_root = cfg.data_root\n",
"cfg.train_dataloader.dataset.data_prefix = dict(img_path=img_dir, seg_map_path=ann_dir)\n",
"cfg.train_dataloader.dataset.pipeline = cfg.train_pipeline\n",
"cfg.train_dataloader.dataset.ann_file = 'splits/train.txt'\n",
"\n",
"cfg.val_dataloader.dataset.type = cfg.dataset_type\n",
"cfg.val_dataloader.dataset.data_root = cfg.data_root\n",
"cfg.val_dataloader.dataset.data_prefix = dict(img_path=img_dir, seg_map_path=ann_dir)\n",
"cfg.val_dataloader.dataset.pipeline = cfg.test_pipeline\n",
"cfg.val_dataloader.dataset.ann_file = 'splits/val.txt'\n",
"\n",
"cfg.test_dataloader = cfg.val_dataloader\n",
"\n",
"\n",
"# Load the pretrained weights\n",
"cfg.load_from = 'pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth'\n",
"\n",
"# Set up working dir to save files and logs.\n",
"cfg.work_dir = './work_dirs/tutorial'\n",
"\n",
"cfg.train_cfg.max_iters = 200\n",
"cfg.train_cfg.val_interval = 200\n",
"cfg.default_hooks.logger.interval = 10\n",
"cfg.default_hooks.checkpoint.interval = 200\n",
"\n",
"# Set seed to facilitate reproducing the result\n",
"cfg['randomness'] = dict(seed=0)\n",
"\n",
"# Let's have a look at the final config used for training\n",
"print(f'Config:\\n{cfg.pretty_text}')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QWuH14LYF2gQ"
},
"source": [
"### Train and Evaluation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jYKoSfdMF12B",
"outputId": "422219ca-d7a5-4890-f09f-88c959942e64"
},
"outputs": [],
"source": [
"from mmengine.runner import Runner\n",
"\n",
"runner = Runner.from_cfg(cfg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# start training\n",
"runner.train()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DEkWOP-NMbc_"
},
"source": [
"Inference with trained model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 645
},
"id": "ekG__UfaH_OU",
"outputId": "1437419c-869a-4902-df86-d4f6f8b2597a"
},
"outputs": [],
"source": [
"from mmseg.apis import init_model, inference_model, show_result_pyplot\n",
"\n",
"# Init the model from the config and the checkpoint\n",
"checkpoint_path = './work_dirs/tutorial/iter_200.pth'\n",
"model = init_model(cfg, checkpoint_path, 'cuda:0')\n",
"\n",
"img = mmcv.imread('iccv09Data/images/6000124.jpg')\n",
"result = inference_model(model, img)\n",
"plt.figure(figsize=(8, 6))\n",
"vis_result = show_result_pyplot(model, img, result)\n",
"plt.imshow(mmcv.bgr2rgb(vis_result))\n"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"include_colab_link": true,
"name": "MMSegmentation Tutorial.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3.10.6 ('pt1.12')",
"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.6"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
},
"vscode": {
"interpreter": {
"hash": "0442e67aee3d9cbb788fa6e86d60c4ffa94ad7f1943c65abfecb99a6f4696c58"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|