2020-07-10 16:55:47 +08:00
{
2021-04-09 10:29:00 +08:00
"cells": [
{
"cell_type": "markdown",
"metadata": {
2021-10-03 23:55:25 +08:00
"colab_type": "text",
"id": "view-in-github"
2021-04-09 10:29:00 +08:00
},
"source": [
2023-03-31 16:26:30 +08:00
"<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>"
2021-04-09 10:29:00 +08:00
]
2020-07-10 16:55:47 +08:00
},
2021-04-09 10:29:00 +08:00
{
"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",
2022-11-21 11:03:08 +08:00
"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. "
2021-04-09 10:29:00 +08:00
]
},
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "UWyLrLYaNEaL",
2021-04-09 10:29:00 +08:00
"outputId": "32a47fe3-f10d-47a1-f6b9-b7c235abdab1"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-02-10 15:13:57 +08:00
"# Check nvcc version\n",
"!nvcc -V\n",
"# Check GCC version\n",
2022-11-21 11:03:08 +08:00
"!gcc --version"
2021-04-09 10:29:00 +08:00
]
},
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "Ki3WUBjKbutg",
2021-04-09 10:29:00 +08:00
"outputId": "14bd14b0-4d8c-4fa9-e3f9-da35c0efc0d5"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-11-21 11:03:08 +08:00
"# Install PyTorch\n",
2022-11-21 11:09:08 +08:00
"!conda install pytorch==1.12.0 torchvision==0.13.0 torchaudio==0.12.0 cudatoolkit=11.3 -c pytorch\n",
2022-09-30 14:15:39 +08:00
"# Install mim\n",
"!pip install -U openmim\n",
"# Install mmengine\n",
"!mim install mmengine\n",
2022-02-10 15:13:57 +08:00
"# Install MMCV\n",
2022-11-21 11:03:08 +08:00
"!mim install 'mmcv >= 2.0.0rc1'\n"
2021-04-09 10:29:00 +08:00
]
},
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "nR-hHRvbNJJZ",
2021-04-09 10:29:00 +08:00
"outputId": "10c3b131-d4db-458c-fc10-b94b1c6ed546"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-02-10 15:13:57 +08:00
"!rm -rf mmsegmentation\n",
2023-03-31 16:26:30 +08:00
"!git clone -b main https://github.com/open-mmlab/mmsegmentation.git \n",
2022-02-10 15:13:57 +08:00
"%cd mmsegmentation\n",
2021-04-09 10:29:00 +08:00
"!pip install -e ."
]
},
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "mAE_h7XhPT7d",
2021-04-09 10:29:00 +08:00
"outputId": "83bf0f8e-fc69-40b1-f9fe-0025724a217c"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-11-21 11:03:08 +08:00
"# Check Pytorch installation\n",
"import torch, torchvision\n",
"print(torch.__version__, torch.cuda.is_available())\n",
"\n",
2022-02-10 15:13:57 +08:00
"# Check MMSegmentation installation\n",
"import mmseg\n",
2021-04-09 10:29:00 +08:00
"print(mmseg.__version__)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ta51clKX4cwM"
},
"source": [
2022-09-30 14:15:39 +08:00
"## Finetune a semantic segmentation model on a new dataset\n",
2021-04-09 10:29:00 +08:00
"\n",
2022-09-30 14:15:39 +08:00
"To finetune on a customized dataset, the following steps are necessary. \n",
2021-04-09 10:29:00 +08:00
"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",
2022-02-11 13:07:46 +08:00
"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",
2021-04-09 10:29:00 +08:00
"\n",
2022-03-22 14:38:26 +08:00
"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",
2021-04-09 10:29:00 +08:00
"\n",
2022-02-11 13:07:46 +08:00
"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",
2021-04-09 10:29:00 +08:00
"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",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "TFIt7MHq5Wls",
2021-04-09 10:29:00 +08:00
"outputId": "74a126e4-c8a4-4d2f-a910-b58b71843a23"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-02-10 15:13:57 +08:00
"# download and unzip\n",
2022-02-11 13:07:46 +08:00
"!wget http://dags.stanford.edu/data/iccv09Data.tar.gz -O stanford_background.tar.gz\n",
"!tar xf stanford_background.tar.gz"
2021-04-09 10:29:00 +08:00
]
},
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 377
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "78LIci7F9WWI",
2021-04-09 10:29:00 +08:00
"outputId": "c432ddac-5a50-47b1-daac-5a26b07afea2"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-02-10 15:13:57 +08:00
"# Let's take a look at the dataset\n",
"import mmcv\n",
2022-09-30 14:15:39 +08:00
"import mmengine\n",
2022-02-10 15:13:57 +08:00
"import matplotlib.pyplot as plt\n",
"\n",
2022-09-30 14:15:39 +08:00
"\n",
2022-02-10 15:13:57 +08:00
"img = mmcv.imread('iccv09Data/images/6000124.jpg')\n",
"plt.figure(figsize=(8, 6))\n",
"plt.imshow(mmcv.bgr2rgb(img))\n",
2021-04-09 10:29:00 +08:00
"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",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"id": "WnGZfribFHCx"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-09-30 14:15:39 +08:00
"# define dataset root and directory for images and annotations\n",
2021-04-09 10:29:00 +08:00
"data_root = 'iccv09Data'\n",
"img_dir = 'images'\n",
"ann_dir = 'labels'\n",
2022-09-30 14:15:39 +08:00
"# define class and palette for better visualization\n",
2021-04-09 10:29:00 +08:00
"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",
2022-09-30 14:15:39 +08:00
" [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",
2021-04-09 10:29:00 +08:00
" 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')))"
2021-10-03 23:55:25 +08:00
]
2021-04-09 10:29:00 +08:00
},
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 377
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "5MCSS9ABfSks",
2021-04-09 10:29:00 +08:00
"outputId": "92b9bafc-589e-48fc-c9e9-476f125d6522"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"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",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"id": "WbeLYCp2k5hl"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
"# split train/val set randomly\n",
"split_dir = 'splits'\n",
2022-09-30 14:15:39 +08:00
"mmengine.mkdir_or_exist(osp.join(data_root, split_dir))\n",
"filename_list = [osp.splitext(filename)[0] for filename in mmengine.scandir(\n",
2021-04-09 10:29:00 +08:00
" 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:])"
2021-10-03 23:55:25 +08:00
]
2021-04-09 10:29:00 +08:00
},
{
"cell_type": "markdown",
"metadata": {
"id": "HchvmGYB_rrO"
},
"source": [
2022-03-10 20:07:12 +08:00
"After downloading the data, we need to implement `load_annotations` function in the new dataset class `StanfordBackgroundDataset`."
2021-04-09 10:29:00 +08:00
]
},
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"id": "LbsWOw62_o-X"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-09-30 14:15:39 +08:00
"from mmseg.registry import DATASETS\n",
"from mmseg.datasets import BaseSegDataset\n",
"\n",
2021-04-09 10:29:00 +08:00
"\n",
"@DATASETS.register_module()\n",
2022-07-26 12:01:40 +08:00
"class StanfordBackgroundDataset(BaseSegDataset):\n",
2022-09-30 14:15:39 +08:00
" METAINFO = dict(classes = classes, palette = palette)\n",
" def __init__(self, **kwargs):\n",
" super().__init__(img_suffix='.jpg', seg_map_suffix='.png', **kwargs)\n",
2021-04-09 10:29:00 +08:00
" "
2021-10-03 23:55:25 +08:00
]
2021-04-09 10:29:00 +08:00
},
{
"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."
]
},
2022-09-30 14:15:39 +08:00
{
"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 ."
]
},
2021-04-09 10:29:00 +08:00
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"id": "Wwnj9tRzqX_A"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-09-30 14:15:39 +08:00
"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}')"
2021-10-03 23:55:25 +08:00
]
2021-04-09 10:29:00 +08:00
},
{
"cell_type": "markdown",
"metadata": {
"id": "1y2oV5w97jQo"
},
"source": [
2022-02-11 13:07:46 +08:00
"Since the given config is used to train PSPNet on the cityscapes dataset, we need to modify it accordingly for our new dataset. "
2021-04-09 10:29:00 +08:00
]
},
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "eyKnYC1Z7iCV",
2021-04-09 10:29:00 +08:00
"outputId": "6195217b-187f-4675-994b-ba90d8bb3078"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-03-10 20:07:12 +08:00
"# Since we use only one GPU, BN is used instead of SyncBN\n",
2021-04-09 10:29:00 +08:00
"cfg.norm_cfg = dict(type='BN', requires_grad=True)\n",
2022-09-30 14:15:39 +08:00
"cfg.crop_size = (256, 256)\n",
"cfg.model.data_preprocessor.size = cfg.crop_size\n",
2021-04-09 10:29:00 +08:00
"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",
2022-03-10 20:07:12 +08:00
"cfg.dataset_type = 'StanfordBackgroundDataset'\n",
2021-04-09 10:29:00 +08:00
"cfg.data_root = data_root\n",
"\n",
2022-09-30 14:15:39 +08:00
"cfg.train_dataloader.batch_size = 8\n",
2021-04-09 10:29:00 +08:00
"\n",
"cfg.train_pipeline = [\n",
" dict(type='LoadImageFromFile'),\n",
" dict(type='LoadAnnotations'),\n",
2022-09-30 14:15:39 +08:00
" dict(type='RandomResize', scale=(320, 240), ratio_range=(0.5, 2.0), keep_ratio=True),\n",
2021-04-09 10:29:00 +08:00
" dict(type='RandomCrop', crop_size=cfg.crop_size, cat_max_ratio=0.75),\n",
2022-09-30 14:15:39 +08:00
" dict(type='RandomFlip', prob=0.5),\n",
" dict(type='PackSegInputs')\n",
2021-04-09 10:29:00 +08:00
"]\n",
"\n",
"cfg.test_pipeline = [\n",
" dict(type='LoadImageFromFile'),\n",
2022-09-30 14:15:39 +08:00
" 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",
2021-04-09 10:29:00 +08:00
"]\n",
"\n",
"\n",
2022-09-30 14:15:39 +08:00
"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",
2021-04-09 10:29:00 +08:00
"\n",
2022-09-30 14:15:39 +08:00
"cfg.test_dataloader = cfg.val_dataloader\n",
2021-04-09 10:29:00 +08:00
"\n",
"\n",
2022-09-30 14:15:39 +08:00
"# Load the pretrained weights\n",
"cfg.load_from = 'pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth'\n",
2021-04-09 10:29:00 +08:00
"\n",
"# Set up working dir to save files and logs.\n",
"cfg.work_dir = './work_dirs/tutorial'\n",
"\n",
2022-09-30 14:15:39 +08:00
"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",
2021-04-09 10:29:00 +08:00
"\n",
2022-09-30 14:15:39 +08:00
"# Set seed to facilitate reproducing the result\n",
"cfg['randomness'] = dict(seed=0)\n",
2021-04-09 10:29:00 +08:00
"\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",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "jYKoSfdMF12B",
2021-04-09 10:29:00 +08:00
"outputId": "422219ca-d7a5-4890-f09f-88c959942e64"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-09-30 14:15:39 +08:00
"from mmengine.runner import Runner\n",
2021-04-09 10:29:00 +08:00
"\n",
2023-02-23 20:33:17 +08:00
"runner = Runner.from_cfg(cfg)"
2022-09-30 14:15:39 +08:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# start training\n",
"runner.train()"
2021-04-09 10:29:00 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DEkWOP-NMbc_"
},
"source": [
"Inference with trained model"
]
},
{
"cell_type": "code",
2021-10-03 23:55:25 +08:00
"execution_count": null,
2021-04-09 10:29:00 +08:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 645
2020-07-10 16:55:47 +08:00
},
2021-10-03 23:55:25 +08:00
"id": "ekG__UfaH_OU",
2021-04-09 10:29:00 +08:00
"outputId": "1437419c-869a-4902-df86-d4f6f8b2597a"
},
2021-10-03 23:55:25 +08:00
"outputs": [],
2021-04-09 10:29:00 +08:00
"source": [
2022-11-19 19:47:59 +08:00
"from mmseg.apis import init_model, inference_model, show_result_pyplot\n",
2021-04-09 10:29:00 +08:00
"\n",
2022-11-19 19:47:59 +08:00
"# 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",
2022-09-30 14:15:39 +08:00
"\n",
"img = mmcv.imread('iccv09Data/images/6000124.jpg')\n",
2022-07-12 16:19:59 +08:00
"result = inference_model(model, img)\n",
2021-04-09 10:29:00 +08:00
"plt.figure(figsize=(8, 6))\n",
2022-11-19 19:47:59 +08:00
"vis_result = show_result_pyplot(model, img, result)\n",
"plt.imshow(mmcv.bgr2rgb(vis_result))\n"
2021-04-09 10:29:00 +08:00
]
}
2021-10-03 23:55:25 +08:00
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"include_colab_link": true,
"name": "MMSegmentation Tutorial.ipynb",
"provenance": []
},
"kernelspec": {
2023-02-23 20:33:17 +08:00
"display_name": "Python 3.10.6 ('pt1.12')",
2022-02-10 15:13:57 +08:00
"language": "python",
2021-10-03 23:55:25 +08:00
"name": "python3"
},
2022-02-10 15:13:57 +08:00
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-02-23 20:33:17 +08:00
"version": "3.10.6"
2022-02-10 15:13:57 +08:00
},
2021-10-03 23:55:25 +08:00
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
2022-09-30 14:15:39 +08:00
},
"vscode": {
"interpreter": {
2023-02-23 20:33:17 +08:00
"hash": "0442e67aee3d9cbb788fa6e86d60c4ffa94ad7f1943c65abfecb99a6f4696c58"
2022-09-30 14:15:39 +08:00
}
2021-10-03 23:55:25 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 2
2021-04-09 10:29:00 +08:00
}