{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "name": "MMSegmentation Tutorial.ipynb",
   "provenance": [],
   "collapsed_sections": [],
   "include_colab_link": true
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "accelerator": "GPU",
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "view-in-github",
    "colab_type": "text"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/open-mmlab/mmsegmentation/blob/master/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.5.0 and CUDA 10.1 for this tutorial. You may install other versions by change the version number in pip install command. "
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "UWyLrLYaNEaL",
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "outputId": "32a47fe3-f10d-47a1-f6b9-b7c235abdab1"
   },
   "source": [
    "# Check nvcc version\n",
    "!nvcc -V\n",
    "# Check GCC version\n",
    "!gcc --version"
   ],
   "execution_count": 1,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "nvcc: NVIDIA (R) Cuda compiler driver\n",
      "Copyright (c) 2005-2020 NVIDIA Corporation\n",
      "Built on Wed_Jul_22_19:09:09_PDT_2020\n",
      "Cuda compilation tools, release 11.0, V11.0.221\n",
      "Build cuda_11.0_bu.TC445_37.28845127_0\n",
      "gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0\n",
      "Copyright (C) 2017 Free Software Foundation, Inc.\n",
      "This is free software; see the source for copying conditions.  There is NO\n",
      "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
      "\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "Ki3WUBjKbutg",
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "outputId": "14bd14b0-4d8c-4fa9-e3f9-da35c0efc0d5"
   },
   "source": [
    "# Install PyTorch\n",
    "!pip install -U torch==1.5.0+cu101 torchvision==0.6.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html\n",
    "# Install MMCV\n",
    "!pip install mmcv-full==latest+torch1.5.0+cu101 -f https://download.openmmlab.com/mmcv/dist/index.html"
   ],
   "execution_count": 2,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "Looking in links: https://download.pytorch.org/whl/torch_stable.html\n",
      "Requirement already up-to-date: torch==1.5.0+cu101 in /usr/local/lib/python3.7/dist-packages (1.5.0+cu101)\n",
      "Requirement already up-to-date: torchvision==0.6.0+cu101 in /usr/local/lib/python3.7/dist-packages (0.6.0+cu101)\n",
      "Requirement already satisfied, skipping upgrade: numpy in /usr/local/lib/python3.7/dist-packages (from torch==1.5.0+cu101) (1.19.5)\n",
      "Requirement already satisfied, skipping upgrade: future in /usr/local/lib/python3.7/dist-packages (from torch==1.5.0+cu101) (0.16.0)\n",
      "Requirement already satisfied, skipping upgrade: pillow>=4.1.1 in /usr/local/lib/python3.7/dist-packages (from torchvision==0.6.0+cu101) (7.1.2)\n",
      "Looking in links: https://download.openmmlab.com/mmcv/dist/index.html\n",
      "Collecting mmcv-full==latest+torch1.5.0+cu101\n",
      "  Using cached https://download.openmmlab.com/mmcv/dist/1.3.0/torch1.5.0/cu101/mmcv_full-latest%2Btorch1.5.0%2Bcu101-cp37-cp37m-manylinux1_x86_64.whl\n",
      "Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from mmcv-full==latest+torch1.5.0+cu101) (7.1.2)\n",
      "Requirement already satisfied: addict in /usr/local/lib/python3.7/dist-packages (from mmcv-full==latest+torch1.5.0+cu101) (2.4.0)\n",
      "Requirement already satisfied: yapf in /usr/local/lib/python3.7/dist-packages (from mmcv-full==latest+torch1.5.0+cu101) (0.31.0)\n",
      "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from mmcv-full==latest+torch1.5.0+cu101) (1.19.5)\n",
      "Requirement already satisfied: opencv-python>=3 in /usr/local/lib/python3.7/dist-packages (from mmcv-full==latest+torch1.5.0+cu101) (4.1.2.30)\n",
      "Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from mmcv-full==latest+torch1.5.0+cu101) (3.13)\n",
      "Installing collected packages: mmcv-full\n",
      "  Found existing installation: mmcv-full 1.3.0\n",
      "    Uninstalling mmcv-full-1.3.0:\n",
      "      Successfully uninstalled mmcv-full-1.3.0\n",
      "Successfully installed mmcv-full-1.3.0\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "nR-hHRvbNJJZ",
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "outputId": "10c3b131-d4db-458c-fc10-b94b1c6ed546"
   },
   "source": [
    "!rm -rf mmsegmentation\n",
    "!git clone https://github.com/open-mmlab/mmsegmentation.git \n",
    "%cd mmsegmentation\n",
    "!pip install -e ."
   ],
   "execution_count": 3,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "Cloning into 'mmsegmentation'...\n",
      "remote: Enumerating objects: 64, done.\u001B[K\n",
      "remote: Counting objects: 100% (64/64), done.\u001B[K\n",
      "remote: Compressing objects: 100% (60/60), done.\u001B[K\n",
      "remote: Total 2194 (delta 17), reused 12 (delta 4), pack-reused 2130\u001B[K\n",
      "Receiving objects: 100% (2194/2194), 3.35 MiB | 26.82 MiB/s, done.\n",
      "Resolving deltas: 100% (1536/1536), done.\n",
      "/content/mmsegmentation\n",
      "Obtaining file:///content/mmsegmentation\n",
      "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from mmsegmentation==0.12.0) (3.2.2)\n",
      "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from mmsegmentation==0.12.0) (1.19.5)\n",
      "Requirement already satisfied: terminaltables in /usr/local/lib/python3.7/dist-packages (from mmsegmentation==0.12.0) (3.1.0)\n",
      "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmsegmentation==0.12.0) (2.8.1)\n",
      "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmsegmentation==0.12.0) (0.10.0)\n",
      "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmsegmentation==0.12.0) (1.3.1)\n",
      "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmsegmentation==0.12.0) (2.4.7)\n",
      "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.1->matplotlib->mmsegmentation==0.12.0) (1.15.0)\n",
      "Installing collected packages: mmsegmentation\n",
      "  Found existing installation: mmsegmentation 0.12.0\n",
      "    Can't uninstall 'mmsegmentation'. No files were found to uninstall.\n",
      "  Running setup.py develop for mmsegmentation\n",
      "Successfully installed mmsegmentation\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "mAE_h7XhPT7d",
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "outputId": "83bf0f8e-fc69-40b1-f9fe-0025724a217c"
   },
   "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__)"
   ],
   "execution_count": 4,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "1.5.0+cu101 True\n",
      "0.12.0\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "eUcuC3dUv32I"
   },
   "source": [
    "## Run Inference with MMSeg trained weight"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "2hd41IGaiNet",
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "outputId": "b7b2aafc-edf2-43e4-ea43-0b5dd0aa4b4a"
   },
   "source": [
    "!mkdir checkpoints\n",
    "!wget https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmsegmentation/models/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth -P checkpoints"
   ],
   "execution_count": 5,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "--2021-04-07 22:14:41--  https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmsegmentation/models/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth\n",
      "Resolving open-mmlab.s3.ap-northeast-2.amazonaws.com (open-mmlab.s3.ap-northeast-2.amazonaws.com)... 52.219.58.127\n",
      "Connecting to open-mmlab.s3.ap-northeast-2.amazonaws.com (open-mmlab.s3.ap-northeast-2.amazonaws.com)|52.219.58.127|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 196205945 (187M) [application/x-www-form-urlencoded]\n",
      "Saving to: ‘checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth’\n",
      "\n",
      "pspnet_r50-d8_512x1 100%[===================>] 187.12M  15.8MB/s    in 13s     \n",
      "\n",
      "2021-04-07 22:14:54 (14.2 MB/s) - ‘checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth’ saved [196205945/196205945]\n",
      "\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "H8Fxg8i-wHJE"
   },
   "source": [
    "from mmseg.apis import inference_segmentor, init_segmentor, show_result_pyplot\n",
    "from mmseg.core.evaluation import get_palette"
   ],
   "execution_count": 6,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "umk8sJ0Xuace"
   },
   "source": [
    "config_file = 'configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py'\n",
    "checkpoint_file = 'checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth'"
   ],
   "execution_count": 7,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "nWlQFuTgudxu",
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "outputId": "5e45f4f6-5bcf-4d04-bb9c-0428ee84a576"
   },
   "source": [
    "# build the model from a config file and a checkpoint file\n",
    "model = init_segmentor(config_file, checkpoint_file, device='cuda:0')"
   ],
   "execution_count": 8,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "Use load_from_local loader\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "izFv6pSRujk9"
   },
   "source": [
    "# test a single image\n",
    "img = 'demo/demo.png'\n",
    "result = inference_segmentor(model, img)"
   ],
   "execution_count": 9,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "bDcs9udgunQK",
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 504
    },
    "outputId": "7c55f713-4085-47fd-fa06-720a321d0795"
   },
   "source": [
    "# show the results\n",
    "show_result_pyplot(model, img, result, get_palette('cityscapes'))"
   ],
   "execution_count": 10,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "/content/mmsegmentation/mmseg/models/segmentors/base.py:271: UserWarning: show==False and out_file is not specified, only result image will be returned\n",
      "  warnings.warn('show==False and out_file is not specified, only '\n"
     ],
     "name": "stderr"
    },
    {
     "output_type": "display_data",
     "data": {
      "image/png": "[encoded data, 548292 characters]\n",
      "text/plain": [
       "<Figure size 1080x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "tags": [],
      "needs_background": "light"
     }
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Ta51clKX4cwM"
   },
   "source": [
    "## Train a semantic segmentation model on a new dataset\n",
    "\n",
    "To train on a customized dataset, the following steps are neccessary. \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 perfix. 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/docs/tutorials/new_dataset.md) for details about dataset reorganization. \n",
    "\n",
    "We use [Standord 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",
   "metadata": {
    "id": "TFIt7MHq5Wls",
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "outputId": "74a126e4-c8a4-4d2f-a910-b58b71843a23"
   },
   "source": [
    "# download and unzip\n",
    "!wget http://dags.stanford.edu/data/iccv09Data.tar.gz -O standford_background.tar.gz\n",
    "!tar xf standford_background.tar.gz"
   ],
   "execution_count": 11,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "--2021-04-07 22:15:00--  http://dags.stanford.edu/data/iccv09Data.tar.gz\n",
      "Resolving dags.stanford.edu (dags.stanford.edu)... 171.64.68.10\n",
      "Connecting to dags.stanford.edu (dags.stanford.edu)|171.64.68.10|:80... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 14727974 (14M) [application/x-gzip]\n",
      "Saving to: ‘standford_background.tar.gz’\n",
      "\n",
      "standford_backgroun 100%[===================>]  14.04M  23.4MB/s    in 0.6s    \n",
      "\n",
      "2021-04-07 22:15:00 (23.4 MB/s) - ‘standford_background.tar.gz’ saved [14727974/14727974]\n",
      "\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "78LIci7F9WWI",
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 377
    },
    "outputId": "c432ddac-5a50-47b1-daac-5a26b07afea2"
   },
   "source": [
    "# Let's take a look at the dataset\n",
    "import mmcv\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "img = mmcv.imread('iccv09Data/images/6000124.jpg')\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.imshow(mmcv.bgr2rgb(img))\n",
    "plt.show()"
   ],
   "execution_count": 12,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "image/png": "[encoded data, 272220 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "tags": [],
      "needs_background": "light"
     }
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "L5mNQuc2GsVE"
   },
   "source": [
    "We need to convert the annotation into semantic map format as an image."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "WnGZfribFHCx"
   },
   "source": [
    "import os.path as osp\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "# convert dataset annotation to semantic segmentation map\n",
    "data_root = 'iccv09Data'\n",
    "img_dir = 'images'\n",
    "ann_dir = 'labels'\n",
    "# define class and plaette 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]]\n",
    "for file in mmcv.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')))"
   ],
   "execution_count": 13,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "5MCSS9ABfSks",
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 377
    },
    "outputId": "92b9bafc-589e-48fc-c9e9-476f125d6522"
   },
   "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()"
   ],
   "execution_count": 14,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "image/png": "[encoded data, 28672 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "tags": [],
      "needs_background": "light"
     }
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "WbeLYCp2k5hl"
   },
   "source": [
    "# split train/val set randomly\n",
    "split_dir = 'splits'\n",
    "mmcv.mkdir_or_exist(osp.join(data_root, split_dir))\n",
    "filename_list = [osp.splitext(filename)[0] for filename in mmcv.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:])"
   ],
   "execution_count": 15,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "HchvmGYB_rrO"
   },
   "source": [
    "After downloading the data, we need to implement `load_annotations` function in the new dataset class `StandfordBackgroundDataset`."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "LbsWOw62_o-X"
   },
   "source": [
    "from mmseg.datasets.builder import DATASETS\n",
    "from mmseg.datasets.custom import CustomDataset\n",
    "\n",
    "@DATASETS.register_module()\n",
    "class StandfordBackgroundDataset(CustomDataset):\n",
    "  CLASSES = classes\n",
    "  PALETTE = palette\n",
    "  def __init__(self, split, **kwargs):\n",
    "    super().__init__(img_suffix='.jpg', seg_map_suffix='.png', \n",
    "                     split=split, **kwargs)\n",
    "    assert osp.exists(self.img_dir) and self.split is not None\n",
    "\n",
    "    "
   ],
   "execution_count": 16,
   "outputs": []
  },
  {
   "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",
   "metadata": {
    "id": "Wwnj9tRzqX_A"
   },
   "source": [
    "from mmcv import Config\n",
    "cfg = Config.fromfile('configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py')"
   ],
   "execution_count": 17,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "1y2oV5w97jQo"
   },
   "source": [
    "Since the given config is used to train PSPNet on cityscapes dataset, we need to modify it accordingly for our new dataset.  "
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "eyKnYC1Z7iCV",
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "outputId": "6195217b-187f-4675-994b-ba90d8bb3078"
   },
   "source": [
    "from mmseg.apis import set_random_seed\n",
    "\n",
    "# Since we use ony one GPU, BN is used instead of SyncBN\n",
    "cfg.norm_cfg = dict(type='BN', requires_grad=True)\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 = 'StandfordBackgroundDataset'\n",
    "cfg.data_root = data_root\n",
    "\n",
    "cfg.data.samples_per_gpu = 8\n",
    "cfg.data.workers_per_gpu=8\n",
    "\n",
    "cfg.img_norm_cfg = dict(\n",
    "    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)\n",
    "cfg.crop_size = (256, 256)\n",
    "cfg.train_pipeline = [\n",
    "    dict(type='LoadImageFromFile'),\n",
    "    dict(type='LoadAnnotations'),\n",
    "    dict(type='Resize', img_scale=(320, 240), ratio_range=(0.5, 2.0)),\n",
    "    dict(type='RandomCrop', crop_size=cfg.crop_size, cat_max_ratio=0.75),\n",
    "    dict(type='RandomFlip', flip_ratio=0.5),\n",
    "    dict(type='PhotoMetricDistortion'),\n",
    "    dict(type='Normalize', **cfg.img_norm_cfg),\n",
    "    dict(type='Pad', size=cfg.crop_size, pad_val=0, seg_pad_val=255),\n",
    "    dict(type='DefaultFormatBundle'),\n",
    "    dict(type='Collect', keys=['img', 'gt_semantic_seg']),\n",
    "]\n",
    "\n",
    "cfg.test_pipeline = [\n",
    "    dict(type='LoadImageFromFile'),\n",
    "    dict(\n",
    "        type='MultiScaleFlipAug',\n",
    "        img_scale=(320, 240),\n",
    "        # img_ratios=[0.5, 0.75, 1.0, 1.25, 1.5, 1.75],\n",
    "        flip=False,\n",
    "        transforms=[\n",
    "            dict(type='Resize', keep_ratio=True),\n",
    "            dict(type='RandomFlip'),\n",
    "            dict(type='Normalize', **cfg.img_norm_cfg),\n",
    "            dict(type='ImageToTensor', keys=['img']),\n",
    "            dict(type='Collect', keys=['img']),\n",
    "        ])\n",
    "]\n",
    "\n",
    "\n",
    "cfg.data.train.type = cfg.dataset_type\n",
    "cfg.data.train.data_root = cfg.data_root\n",
    "cfg.data.train.img_dir = img_dir\n",
    "cfg.data.train.ann_dir = ann_dir\n",
    "cfg.data.train.pipeline = cfg.train_pipeline\n",
    "cfg.data.train.split = 'splits/train.txt'\n",
    "\n",
    "cfg.data.val.type = cfg.dataset_type\n",
    "cfg.data.val.data_root = cfg.data_root\n",
    "cfg.data.val.img_dir = img_dir\n",
    "cfg.data.val.ann_dir = ann_dir\n",
    "cfg.data.val.pipeline = cfg.test_pipeline\n",
    "cfg.data.val.split = 'splits/val.txt'\n",
    "\n",
    "cfg.data.test.type = cfg.dataset_type\n",
    "cfg.data.test.data_root = cfg.data_root\n",
    "cfg.data.test.img_dir = img_dir\n",
    "cfg.data.test.ann_dir = ann_dir\n",
    "cfg.data.test.pipeline = cfg.test_pipeline\n",
    "cfg.data.test.split = 'splits/val.txt'\n",
    "\n",
    "# We can still use the pre-trained Mask RCNN model though we do not need to\n",
    "# use the mask branch\n",
    "cfg.load_from = 'checkpoints/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.runner.max_iters = 200\n",
    "cfg.log_config.interval = 10\n",
    "cfg.evaluation.interval = 200\n",
    "cfg.checkpoint_config.interval = 200\n",
    "\n",
    "# Set seed to facitate reproducing the result\n",
    "cfg.seed = 0\n",
    "set_random_seed(0, deterministic=False)\n",
    "cfg.gpu_ids = range(1)\n",
    "\n",
    "# Let's have a look at the final config used for training\n",
    "print(f'Config:\\n{cfg.pretty_text}')"
   ],
   "execution_count": 18,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "Config:\n",
      "norm_cfg = dict(type='BN', requires_grad=True)\n",
      "model = dict(\n",
      "    type='EncoderDecoder',\n",
      "    pretrained='open-mmlab://resnet50_v1c',\n",
      "    backbone=dict(\n",
      "        type='ResNetV1c',\n",
      "        depth=50,\n",
      "        num_stages=4,\n",
      "        out_indices=(0, 1, 2, 3),\n",
      "        dilations=(1, 1, 2, 4),\n",
      "        strides=(1, 2, 1, 1),\n",
      "        norm_cfg=dict(type='BN', requires_grad=True),\n",
      "        norm_eval=False,\n",
      "        style='pytorch',\n",
      "        contract_dilation=True),\n",
      "    decode_head=dict(\n",
      "        type='PSPHead',\n",
      "        in_channels=2048,\n",
      "        in_index=3,\n",
      "        channels=512,\n",
      "        pool_scales=(1, 2, 3, 6),\n",
      "        dropout_ratio=0.1,\n",
      "        num_classes=8,\n",
      "        norm_cfg=dict(type='BN', requires_grad=True),\n",
      "        align_corners=False,\n",
      "        loss_decode=dict(\n",
      "            type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)),\n",
      "    auxiliary_head=dict(\n",
      "        type='FCNHead',\n",
      "        in_channels=1024,\n",
      "        in_index=2,\n",
      "        channels=256,\n",
      "        num_convs=1,\n",
      "        concat_input=False,\n",
      "        dropout_ratio=0.1,\n",
      "        num_classes=8,\n",
      "        norm_cfg=dict(type='BN', requires_grad=True),\n",
      "        align_corners=False,\n",
      "        loss_decode=dict(\n",
      "            type='CrossEntropyLoss', use_sigmoid=False, loss_weight=0.4)),\n",
      "    train_cfg=dict(),\n",
      "    test_cfg=dict(mode='whole'))\n",
      "dataset_type = 'StandfordBackgroundDataset'\n",
      "data_root = 'iccv09Data'\n",
      "img_norm_cfg = dict(\n",
      "    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)\n",
      "crop_size = (256, 256)\n",
      "train_pipeline = [\n",
      "    dict(type='LoadImageFromFile'),\n",
      "    dict(type='LoadAnnotations'),\n",
      "    dict(type='Resize', img_scale=(320, 240), ratio_range=(0.5, 2.0)),\n",
      "    dict(type='RandomCrop', crop_size=(256, 256), cat_max_ratio=0.75),\n",
      "    dict(type='RandomFlip', flip_ratio=0.5),\n",
      "    dict(type='PhotoMetricDistortion'),\n",
      "    dict(\n",
      "        type='Normalize',\n",
      "        mean=[123.675, 116.28, 103.53],\n",
      "        std=[58.395, 57.12, 57.375],\n",
      "        to_rgb=True),\n",
      "    dict(type='Pad', size=(256, 256), pad_val=0, seg_pad_val=255),\n",
      "    dict(type='DefaultFormatBundle'),\n",
      "    dict(type='Collect', keys=['img', 'gt_semantic_seg'])\n",
      "]\n",
      "test_pipeline = [\n",
      "    dict(type='LoadImageFromFile'),\n",
      "    dict(\n",
      "        type='MultiScaleFlipAug',\n",
      "        img_scale=(320, 240),\n",
      "        flip=False,\n",
      "        transforms=[\n",
      "            dict(type='Resize', keep_ratio=True),\n",
      "            dict(type='RandomFlip'),\n",
      "            dict(\n",
      "                type='Normalize',\n",
      "                mean=[123.675, 116.28, 103.53],\n",
      "                std=[58.395, 57.12, 57.375],\n",
      "                to_rgb=True),\n",
      "            dict(type='ImageToTensor', keys=['img']),\n",
      "            dict(type='Collect', keys=['img'])\n",
      "        ])\n",
      "]\n",
      "data = dict(\n",
      "    samples_per_gpu=8,\n",
      "    workers_per_gpu=8,\n",
      "    train=dict(\n",
      "        type='StandfordBackgroundDataset',\n",
      "        data_root='iccv09Data',\n",
      "        img_dir='images',\n",
      "        ann_dir='labels',\n",
      "        pipeline=[\n",
      "            dict(type='LoadImageFromFile'),\n",
      "            dict(type='LoadAnnotations'),\n",
      "            dict(type='Resize', img_scale=(320, 240), ratio_range=(0.5, 2.0)),\n",
      "            dict(type='RandomCrop', crop_size=(256, 256), cat_max_ratio=0.75),\n",
      "            dict(type='RandomFlip', flip_ratio=0.5),\n",
      "            dict(type='PhotoMetricDistortion'),\n",
      "            dict(\n",
      "                type='Normalize',\n",
      "                mean=[123.675, 116.28, 103.53],\n",
      "                std=[58.395, 57.12, 57.375],\n",
      "                to_rgb=True),\n",
      "            dict(type='Pad', size=(256, 256), pad_val=0, seg_pad_val=255),\n",
      "            dict(type='DefaultFormatBundle'),\n",
      "            dict(type='Collect', keys=['img', 'gt_semantic_seg'])\n",
      "        ],\n",
      "        split='splits/train.txt'),\n",
      "    val=dict(\n",
      "        type='StandfordBackgroundDataset',\n",
      "        data_root='iccv09Data',\n",
      "        img_dir='images',\n",
      "        ann_dir='labels',\n",
      "        pipeline=[\n",
      "            dict(type='LoadImageFromFile'),\n",
      "            dict(\n",
      "                type='MultiScaleFlipAug',\n",
      "                img_scale=(320, 240),\n",
      "                flip=False,\n",
      "                transforms=[\n",
      "                    dict(type='Resize', keep_ratio=True),\n",
      "                    dict(type='RandomFlip'),\n",
      "                    dict(\n",
      "                        type='Normalize',\n",
      "                        mean=[123.675, 116.28, 103.53],\n",
      "                        std=[58.395, 57.12, 57.375],\n",
      "                        to_rgb=True),\n",
      "                    dict(type='ImageToTensor', keys=['img']),\n",
      "                    dict(type='Collect', keys=['img'])\n",
      "                ])\n",
      "        ],\n",
      "        split='splits/val.txt'),\n",
      "    test=dict(\n",
      "        type='StandfordBackgroundDataset',\n",
      "        data_root='iccv09Data',\n",
      "        img_dir='images',\n",
      "        ann_dir='labels',\n",
      "        pipeline=[\n",
      "            dict(type='LoadImageFromFile'),\n",
      "            dict(\n",
      "                type='MultiScaleFlipAug',\n",
      "                img_scale=(320, 240),\n",
      "                flip=False,\n",
      "                transforms=[\n",
      "                    dict(type='Resize', keep_ratio=True),\n",
      "                    dict(type='RandomFlip'),\n",
      "                    dict(\n",
      "                        type='Normalize',\n",
      "                        mean=[123.675, 116.28, 103.53],\n",
      "                        std=[58.395, 57.12, 57.375],\n",
      "                        to_rgb=True),\n",
      "                    dict(type='ImageToTensor', keys=['img']),\n",
      "                    dict(type='Collect', keys=['img'])\n",
      "                ])\n",
      "        ],\n",
      "        split='splits/val.txt'))\n",
      "log_config = dict(\n",
      "    interval=10, hooks=[dict(type='TextLoggerHook', by_epoch=False)])\n",
      "dist_params = dict(backend='nccl')\n",
      "log_level = 'INFO'\n",
      "load_from = 'checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth'\n",
      "resume_from = None\n",
      "workflow = [('train', 1)]\n",
      "cudnn_benchmark = True\n",
      "optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0005)\n",
      "optimizer_config = dict()\n",
      "lr_config = dict(policy='poly', power=0.9, min_lr=0.0001, by_epoch=False)\n",
      "runner = dict(type='IterBasedRunner', max_iters=200)\n",
      "checkpoint_config = dict(by_epoch=False, interval=200)\n",
      "evaluation = dict(interval=200, metric='mIoU')\n",
      "work_dir = './work_dirs/tutorial'\n",
      "seed = 0\n",
      "gpu_ids = range(0, 1)\n",
      "\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "QWuH14LYF2gQ"
   },
   "source": [
    "### Train and Evaluation"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "jYKoSfdMF12B",
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "outputId": "422219ca-d7a5-4890-f09f-88c959942e64"
   },
   "source": [
    "from mmseg.datasets import build_dataset\n",
    "from mmseg.models import build_segmentor\n",
    "from mmseg.apis import train_segmentor\n",
    "\n",
    "\n",
    "# Build the dataset\n",
    "datasets = [build_dataset(cfg.data.train)]\n",
    "\n",
    "# Build the detector\n",
    "model = build_segmentor(\n",
    "    cfg.model, train_cfg=cfg.get('train_cfg'), test_cfg=cfg.get('test_cfg'))\n",
    "# Add an attribute for visualization convenience\n",
    "model.CLASSES = datasets[0].CLASSES\n",
    "\n",
    "# Create work_dir\n",
    "mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))\n",
    "train_segmentor(model, datasets, cfg, distributed=False, validate=True, \n",
    "                meta=dict())"
   ],
   "execution_count": 19,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.7/dist-packages/mmcv/utils/misc.py:304: UserWarning: \"flip_ratio\" is deprecated in `RandomFlip.__init__`, please use \"prob\" instead\n",
      "  f'\"{src_arg_name}\" is deprecated in '\n",
      "2021-04-07 22:15:26,312 - mmseg - INFO - Loaded 572 images\n",
      "2021-04-07 22:15:26,915 - mmseg - INFO - Use load_from_openmmlab loader\n",
      "2021-04-07 22:15:26,999 - mmseg - WARNING - The model and loaded state dict do not match exactly\n",
      "\n",
      "unexpected key in source state_dict: fc.weight, fc.bias\n",
      "\n",
      "2021-04-07 22:15:27,070 - mmseg - INFO - Loaded 143 images\n",
      "2021-04-07 22:15:27,072 - mmseg - INFO - load checkpoint from checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth\n",
      "2021-04-07 22:15:27,073 - mmseg - INFO - Use load_from_local loader\n",
      "2021-04-07 22:15:27,228 - mmseg - WARNING - The model and loaded state dict do not match exactly\n",
      "\n",
      "size mismatch for decode_head.conv_seg.weight: copying a param with shape torch.Size([19, 512, 1, 1]) from checkpoint, the shape in current model is torch.Size([8, 512, 1, 1]).\n",
      "size mismatch for decode_head.conv_seg.bias: copying a param with shape torch.Size([19]) from checkpoint, the shape in current model is torch.Size([8]).\n",
      "size mismatch for auxiliary_head.conv_seg.weight: copying a param with shape torch.Size([19, 256, 1, 1]) from checkpoint, the shape in current model is torch.Size([8, 256, 1, 1]).\n",
      "size mismatch for auxiliary_head.conv_seg.bias: copying a param with shape torch.Size([19]) from checkpoint, the shape in current model is torch.Size([8]).\n",
      "2021-04-07 22:15:27,232 - mmseg - INFO - Start running, host: root@c8cc0e0b80dc, work_dir: /content/mmsegmentation/work_dirs/tutorial\n",
      "2021-04-07 22:15:27,237 - mmseg - INFO - workflow: [('train', 1)], max: 200 iters\n",
      "2021-04-07 22:15:33,883 - mmseg - INFO - Iter [10/200]\tlr: 9.598e-03, eta: 0:01:58, time: 0.626, data_time: 0.039, memory: 3772, decode.loss_seg: 1.5570, decode.acc_seg: 44.2138, aux.loss_seg: 0.6808, aux.acc_seg: 40.7060, loss: 2.2378\n",
      "2021-04-07 22:15:39,777 - mmseg - INFO - Iter [20/200]\tlr: 9.149e-03, eta: 0:01:49, time: 0.589, data_time: 0.008, memory: 3772, decode.loss_seg: 0.8328, decode.acc_seg: 67.4587, aux.loss_seg: 0.5270, aux.acc_seg: 65.5612, loss: 1.3598\n",
      "2021-04-07 22:15:45,723 - mmseg - INFO - Iter [30/200]\tlr: 8.698e-03, eta: 0:01:42, time: 0.595, data_time: 0.008, memory: 3772, decode.loss_seg: 0.6151, decode.acc_seg: 65.5550, aux.loss_seg: 0.3798, aux.acc_seg: 64.0860, loss: 0.9949\n",
      "2021-04-07 22:15:51,759 - mmseg - INFO - Iter [40/200]\tlr: 8.244e-03, eta: 0:01:36, time: 0.603, data_time: 0.008, memory: 3772, decode.loss_seg: 0.5840, decode.acc_seg: 68.8598, aux.loss_seg: 0.3035, aux.acc_seg: 66.3350, loss: 0.8875\n",
      "2021-04-07 22:15:57,851 - mmseg - INFO - Iter [50/200]\tlr: 7.788e-03, eta: 0:01:30, time: 0.609, data_time: 0.008, memory: 3772, decode.loss_seg: 0.5198, decode.acc_seg: 69.1188, aux.loss_seg: 0.2708, aux.acc_seg: 66.1400, loss: 0.7906\n",
      "2021-04-07 22:16:04,047 - mmseg - INFO - Iter [60/200]\tlr: 7.328e-03, eta: 0:01:24, time: 0.620, data_time: 0.008, memory: 3772, decode.loss_seg: 0.7124, decode.acc_seg: 66.1938, aux.loss_seg: 0.3291, aux.acc_seg: 63.7193, loss: 1.0415\n",
      "2021-04-07 22:16:10,183 - mmseg - INFO - Iter [70/200]\tlr: 6.865e-03, eta: 0:01:19, time: 0.614, data_time: 0.008, memory: 3772, decode.loss_seg: 0.6217, decode.acc_seg: 67.6348, aux.loss_seg: 0.2921, aux.acc_seg: 65.4327, loss: 0.9138\n",
      "2021-04-07 22:16:16,975 - mmseg - INFO - Iter [80/200]\tlr: 6.398e-03, eta: 0:01:14, time: 0.679, data_time: 0.083, memory: 3772, decode.loss_seg: 0.5825, decode.acc_seg: 67.3635, aux.loss_seg: 0.2740, aux.acc_seg: 66.0855, loss: 0.8565\n",
      "2021-04-07 22:16:22,951 - mmseg - INFO - Iter [90/200]\tlr: 5.928e-03, eta: 0:01:07, time: 0.598, data_time: 0.008, memory: 3772, decode.loss_seg: 0.5509, decode.acc_seg: 71.3504, aux.loss_seg: 0.2507, aux.acc_seg: 70.5064, loss: 0.8016\n",
      "2021-04-07 22:16:28,880 - mmseg - INFO - Iter [100/200]\tlr: 5.453e-03, eta: 0:01:01, time: 0.593, data_time: 0.008, memory: 3772, decode.loss_seg: 0.6903, decode.acc_seg: 62.3287, aux.loss_seg: 0.3010, aux.acc_seg: 62.1792, loss: 0.9913\n",
      "2021-04-07 22:16:34,786 - mmseg - INFO - Iter [110/200]\tlr: 4.974e-03, eta: 0:00:54, time: 0.591, data_time: 0.008, memory: 3772, decode.loss_seg: 0.5627, decode.acc_seg: 68.7782, aux.loss_seg: 0.2505, aux.acc_seg: 68.3666, loss: 0.8132\n",
      "2021-04-07 22:16:40,679 - mmseg - INFO - Iter [120/200]\tlr: 4.489e-03, eta: 0:00:48, time: 0.589, data_time: 0.008, memory: 3772, decode.loss_seg: 0.5006, decode.acc_seg: 70.7204, aux.loss_seg: 0.2400, aux.acc_seg: 69.5582, loss: 0.7406\n",
      "2021-04-07 22:16:46,554 - mmseg - INFO - Iter [130/200]\tlr: 3.998e-03, eta: 0:00:42, time: 0.588, data_time: 0.008, memory: 3772, decode.loss_seg: 0.4775, decode.acc_seg: 70.6324, aux.loss_seg: 0.2211, aux.acc_seg: 69.0519, loss: 0.6986\n",
      "2021-04-07 22:16:52,442 - mmseg - INFO - Iter [140/200]\tlr: 3.500e-03, eta: 0:00:36, time: 0.589, data_time: 0.008, memory: 3772, decode.loss_seg: 0.4812, decode.acc_seg: 71.5263, aux.loss_seg: 0.2262, aux.acc_seg: 68.9376, loss: 0.7074\n",
      "2021-04-07 22:16:59,045 - mmseg - INFO - Iter [150/200]\tlr: 2.994e-03, eta: 0:00:30, time: 0.660, data_time: 0.075, memory: 3772, decode.loss_seg: 0.4366, decode.acc_seg: 73.8778, aux.loss_seg: 0.2085, aux.acc_seg: 71.9269, loss: 0.6452\n",
      "2021-04-07 22:17:04,994 - mmseg - INFO - Iter [160/200]\tlr: 2.478e-03, eta: 0:00:24, time: 0.595, data_time: 0.008, memory: 3772, decode.loss_seg: 0.4244, decode.acc_seg: 73.4474, aux.loss_seg: 0.1975, aux.acc_seg: 72.5327, loss: 0.6219\n",
      "2021-04-07 22:17:10,945 - mmseg - INFO - Iter [170/200]\tlr: 1.949e-03, eta: 0:00:18, time: 0.595, data_time: 0.008, memory: 3772, decode.loss_seg: 0.4268, decode.acc_seg: 71.7624, aux.loss_seg: 0.2042, aux.acc_seg: 70.3237, loss: 0.6311\n",
      "2021-04-07 22:17:16,919 - mmseg - INFO - Iter [180/200]\tlr: 1.402e-03, eta: 0:00:12, time: 0.597, data_time: 0.008, memory: 3772, decode.loss_seg: 0.4488, decode.acc_seg: 72.1597, aux.loss_seg: 0.2177, aux.acc_seg: 70.9026, loss: 0.6665\n",
      "2021-04-07 22:17:22,916 - mmseg - INFO - Iter [190/200]\tlr: 8.277e-04, eta: 0:00:06, time: 0.600, data_time: 0.008, memory: 3772, decode.loss_seg: 0.4651, decode.acc_seg: 75.1950, aux.loss_seg: 0.2244, aux.acc_seg: 73.2528, loss: 0.6894\n",
      "2021-04-07 22:17:28,838 - mmseg - INFO - Saving checkpoint at 200 iterations\n"
     ],
     "name": "stderr"
    },
    {
     "output_type": "stream",
     "text": [
      "[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 143/143, 27.5 task/s, elapsed: 5s, ETA:     0s"
     ],
     "name": "stdout"
    },
    {
     "output_type": "stream",
     "text": [
      "2021-04-07 22:17:35,967 - mmseg - INFO - per class results:\n",
      "2021-04-07 22:17:35,969 - mmseg - INFO - \n",
      "+--------+-------+-------+\n",
      "| Class  | IoU   | Acc   |\n",
      "+--------+-------+-------+\n",
      "| sky    | 87.18 | 91.91 |\n",
      "| tree   | 69.54 | 90.08 |\n",
      "| road   | 84.38 | 92.03 |\n",
      "| grass  | 72.91 | 90.34 |\n",
      "| water  | 57.42 | 62.66 |\n",
      "| bldg   | 78.36 | 87.32 |\n",
      "| mntn   | 0.0   | 0.0   |\n",
      "| fg obj | 67.42 | 82.39 |\n",
      "+--------+-------+-------+\n",
      "2021-04-07 22:17:35,974 - mmseg - INFO - Summary:\n",
      "2021-04-07 22:17:35,976 - mmseg - INFO - \n",
      "+--------+-------+-------+-------+\n",
      "| Scope  | mIoU  | mAcc  | aAcc  |\n",
      "+--------+-------+-------+-------+\n",
      "| global | 64.65 | 74.59 | 85.92 |\n",
      "+--------+-------+-------+-------+\n",
      "2021-04-07 22:17:35,986 - mmseg - INFO - Iter(val) [200]\tmIoU: 0.6465, mAcc: 0.7459, aAcc: 0.8592, IoU.sky: 0.8718, IoU.tree: 0.6954, IoU.road: 0.8438, IoU.grass: 0.7291, IoU.water: 0.5742, IoU.bldg: 0.7836, IoU.mntn: 0.0000, IoU.fg obj: 0.6742, Acc.sky: 0.9191, Acc.tree: 0.9008, Acc.road: 0.9203, Acc.grass: 0.9034, Acc.water: 0.6266, Acc.bldg: 0.8732, Acc.mntn: 0.0000, Acc.fg obj: 0.8239\n"
     ],
     "name": "stderr"
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "DEkWOP-NMbc_"
   },
   "source": [
    "Inference with trained model"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "ekG__UfaH_OU",
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 645
    },
    "outputId": "1437419c-869a-4902-df86-d4f6f8b2597a"
   },
   "source": [
    "img = mmcv.imread('iccv09Data/images/6000124.jpg')\n",
    "\n",
    "model.cfg = cfg\n",
    "result = inference_segmentor(model, img)\n",
    "plt.figure(figsize=(8, 6))\n",
    "show_result_pyplot(model, img, result, palette)"
   ],
   "execution_count": 20,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "/content/mmsegmentation/mmseg/models/segmentors/base.py:271: UserWarning: show==False and out_file is not specified, only result image will be returned\n",
      "  warnings.warn('show==False and out_file is not specified, only '\n"
     ],
     "name": "stderr"
    },
    {
     "output_type": "display_data",
     "data": {
      "text/plain": [
       "<Figure size 576x432 with 0 Axes>"
      ]
     },
     "metadata": {
      "tags": []
     }
    },
    {
     "output_type": "display_data",
     "data": {
      "image/png": "[encoded data, 467324 characters]\n",
      "text/plain": [
       "<Figure size 1080x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "tags": [],
      "needs_background": "light"
     }
    }
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}
