{
  "nbformat": 4,
  "nbformat_minor": 2,
  "metadata": {
    "colab": {
      "name": "mmocr.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3",
      "language": "python"
    },
    "language_info": {
      "name": "python",
      "version": "3.8.5"
    },
    "accelerator": "GPU",
    "widgets": {
      "application/vnd.jupyter.widget-state+json": {
        "2c92390d57494a4281fe95cc5e061092": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HBoxModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "HBoxView",
            "_dom_classes": [],
            "_model_name": "HBoxModel",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "box_style": "",
            "layout": "IPY_MODEL_a73f09aca9e24725b2e35347a902de89",
            "_model_module": "@jupyter-widgets/controls",
            "children": [
              "IPY_MODEL_0ca81ff36c61401e9943825dccd671da",
              "IPY_MODEL_728a93a11fe44e9e977ca8d75d67c7af",
              "IPY_MODEL_330f4551fe984d1ea40e4bea51831533"
            ]
          }
        },
        "a73f09aca9e24725b2e35347a902de89": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "0ca81ff36c61401e9943825dccd671da": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "HTMLView",
            "style": "IPY_MODEL_62a06be59f204e9ab16e4160db18e808",
            "_dom_classes": [],
            "description": "",
            "_model_name": "HTMLModel",
            "placeholder": "​",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": "100%",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_aa7261dfcabb4b85be7611ea1b6f7046"
          }
        },
        "728a93a11fe44e9e977ca8d75d67c7af": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "FloatProgressModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "ProgressView",
            "style": "IPY_MODEL_cd501d23a4d04be3897db97e3261f9c0",
            "_dom_classes": [],
            "description": "",
            "_model_name": "FloatProgressModel",
            "bar_style": "success",
            "max": 145703066,
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": 145703066,
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "orientation": "horizontal",
            "min": 0,
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_c678a46976e8469b8e77ba23b266174f"
          }
        },
        "330f4551fe984d1ea40e4bea51831533": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "HTMLView",
            "style": "IPY_MODEL_9f7b0826508147c4be923443e8e6243b",
            "_dom_classes": [],
            "description": "",
            "_model_name": "HTMLModel",
            "placeholder": "​",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": " 139M/139M [00:12&lt;00:00, 12.3MB/s]",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_3d80c260ae4f4ea0ab07b2ed8367600f"
          }
        },
        "62a06be59f204e9ab16e4160db18e808": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "DescriptionStyleModel",
            "description_width": "",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "aa7261dfcabb4b85be7611ea1b6f7046": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "cd501d23a4d04be3897db97e3261f9c0": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "ProgressStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "ProgressStyleModel",
            "description_width": "",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "bar_color": null,
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "c678a46976e8469b8e77ba23b266174f": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "9f7b0826508147c4be923443e8e6243b": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "DescriptionStyleModel",
            "description_width": "",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "3d80c260ae4f4ea0ab07b2ed8367600f": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        }
      }
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# MMOCR Tutorial\n",
        "\n",
        "Welcome to MMOCR! This is the official colab tutorial for using MMOCR. In this tutorial, you will learn how to\n",
        "\n",
        "- Perform inference with a pretrained text recognizer\n",
        "- Perform inference with a pretrained text detector\n",
        "- Perform end-to-end OCR with pretrained recognizer and detector\n",
        "- Combine OCR with downstream tasks\n",
        "- Perform inference with a pretrained Key Information Extraction (KIE) model\n",
        "- Train a text recognizer with a toy dataset\n",
        "\n",
        "Let's start!"
      ],
      "metadata": {
        "id": "jU9T31gbQmvs"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Install MMOCR"
      ],
      "metadata": {
        "id": "Sfvz1sywQ9_4"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "When installing dependencies for mmocr, please ensure that all the dependency versions are compatible with each other. For instance, if CUDA 10.1 is installed, then the Pytorch version must be compatible with cu10.1. Please see [getting_started.md](docs/getting_started.md) for more details. "
      ],
      "metadata": {
        "id": "q3fZP1LspEUp"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "source": [
        "%cd .."
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/\n"
          ]
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rB3qciTXpEUq",
        "outputId": "4a32aea6-3b92-4da0-b096-c6127ae71957"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Check NVCC and GCC compiler version"
      ],
      "metadata": {
        "id": "mSkZOdrMpEUr"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "source": [
        "!nvcc -V\n",
        "!gcc --version"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "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"
          ]
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2DBpcKj2RDfu",
        "outputId": "cbb83e76-b4df-418b-ea78-b8ceacdc07c2"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Install Dependencies "
      ],
      "metadata": {
        "id": "Tw7u_baQpEUs"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "source": [
        "# Install torch dependencies: (use cu110 since colab has CUDA 11)\n",
        "!pip install -U torch==1.7.0+cu110 torchvision==0.8.0 -f https://download.pytorch.org/whl/torch_stable.html\n",
        "\n",
        "# Install mmcv-full thus we could use CUDA operators\n",
        "!pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.6.0/index.html\n",
        "\n",
        "# Install mmdetection\n",
        "!pip install mmdet\n",
        "\n",
        "# Install mmocr\n",
        "!git clone https://github.com/open-mmlab/mmocr.git\n",
        "%cd mmocr\n",
        "!pip install -r requirements.txt\n",
        "!pip install -v -e ."
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Looking in links: https://download.pytorch.org/whl/torch_stable.html\n",
            "Collecting torch==1.7.0+cu110\n",
            "tcmalloc: large alloc 1137090560 bytes == 0x55fec2658000 @  0x7ff4c85391e7 0x55febfab6a18 0x55febfa81987 0x55febfc00335 0x55febfb9aa48 0x55febfa85252 0x55febfb6396e 0x55febfa84ea9 0x55febfb76c0d 0x55febfaf90d8 0x55febfa8665a 0x55febfaf4d67 0x55febfa8665a 0x55febfaf4d67 0x55febfaf3dcc 0x55febfa86fec 0x55febfa871f1 0x55febfaf6318 0x55febfaf3c35 0x55febfa86fec 0x55febfa871f1 0x55febfaf6318 0x55febfaf3c35 0x55febfa86fec 0x55febfa871f1 0x55febfaf6318 0x55febfaf3dcc 0x55febfa86fec 0x55febfa871f1 0x55febfaf6318 0x55febfaf3c35\n",
            "tcmalloc: large alloc 1421369344 bytes == 0x55ff062c2000 @  0x7ff4c853a615 0x55febfa8202c 0x55febfb6217a 0x55febfa84e4d 0x55febfb76c0d 0x55febfaf90d8 0x55febfaf3c35 0x55febfa8673a 0x55febfaf4d67 0x55febfaf3c35 0x55febfa8673a 0x55febfaf4d67 0x55febfaf3c35 0x55febfa8673a 0x55febfaf4d67 0x55febfaf3c35 0x55febfa8673a 0x55febfaf4d67 0x55febfaf3c35 0x55febfa8673a 0x55febfaf4d67 0x55febfa8665a 0x55febfaf4d67 0x55febfaf3c35 0x55febfa8673a 0x55febfaf593b 0x55febfaf3c35 0x55febfa8673a 0x55febfaf4d67 0x55febfaf4235 0x55febfa8673a\n",
            "  Using cached https://download.pytorch.org/whl/cu110/torch-1.7.0%2Bcu110-cp37-cp37m-linux_x86_64.whl (1137.1 MB)\n",
            "Collecting torchvision==0.8.0\n",
            "  Downloading torchvision-0.8.0-cp37-cp37m-manylinux1_x86_64.whl (11.8 MB)\n",
            "\u001b[K     |████████████████████████████████| 11.8 MB 258 kB/s \n",
            "\u001b[?25hRequirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch==1.7.0+cu110) (3.7.4.3)\n",
            "Collecting dataclasses\n",
            "  Downloading dataclasses-0.6-py3-none-any.whl (14 kB)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from torch==1.7.0+cu110) (1.19.5)\n",
            "Requirement already satisfied: future in /usr/local/lib/python3.7/dist-packages (from torch==1.7.0+cu110) (0.16.0)\n",
            "Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.7/dist-packages (from torchvision==0.8.0) (7.1.2)\n",
            "Installing collected packages: dataclasses, torch, torchvision\n",
            "  Attempting uninstall: torch\n",
            "    Found existing installation: torch 1.9.0+cu102\n",
            "    Uninstalling torch-1.9.0+cu102:\n",
            "      Successfully uninstalled torch-1.9.0+cu102\n",
            "  Attempting uninstall: torchvision\n",
            "    Found existing installation: torchvision 0.10.0+cu102\n",
            "    Uninstalling torchvision-0.10.0+cu102:\n",
            "      Successfully uninstalled torchvision-0.10.0+cu102\n",
            "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
            "torchtext 0.10.0 requires torch==1.9.0, but you have torch 1.7.0+cu110 which is incompatible.\u001b[0m\n",
            "Successfully installed dataclasses-0.6 torch-1.7.0+cu110 torchvision-0.8.0\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.colab-display-data+json": {
              "pip_warning": {
                "packages": [
                  "dataclasses",
                  "torch",
                  "torchvision"
                ]
              }
            }
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Looking in links: https://download.openmmlab.com/mmcv/dist/cu110/torch1.6.0/index.html\n",
            "Requirement already satisfied: mmcv-full in /usr/local/lib/python3.7/dist-packages (1.3.11)\n",
            "Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (21.0)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (1.19.5)\n",
            "Requirement already satisfied: yapf in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (0.31.0)\n",
            "Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (7.1.2)\n",
            "Requirement already satisfied: addict in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (2.4.0)\n",
            "Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (3.13)\n",
            "Requirement already satisfied: pyparsing>=2.0.2 in /usr/local/lib/python3.7/dist-packages (from packaging->mmcv-full) (2.4.7)\n",
            "Requirement already satisfied: mmdet in /usr/local/lib/python3.7/dist-packages (2.15.1)\n",
            "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from mmdet) (3.2.2)\n",
            "Requirement already satisfied: pycocotools in /usr/local/lib/python3.7/dist-packages (from mmdet) (2.0.2)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from mmdet) (1.19.5)\n",
            "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from mmdet) (1.15.0)\n",
            "Requirement already satisfied: terminaltables in /usr/local/lib/python3.7/dist-packages (from mmdet) (3.1.0)\n",
            "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmdet) (0.10.0)\n",
            "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmdet) (1.3.1)\n",
            "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmdet) (2.8.2)\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->mmdet) (2.4.7)\n",
            "Requirement already satisfied: setuptools>=18.0 in /usr/local/lib/python3.7/dist-packages (from pycocotools->mmdet) (57.4.0)\n",
            "Requirement already satisfied: cython>=0.27.3 in /usr/local/lib/python3.7/dist-packages (from pycocotools->mmdet) (0.29.24)\n",
            "fatal: destination path 'mmocr' already exists and is not an empty directory.\n",
            "/mmocr\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from -r requirements/build.txt (line 2)) (1.19.5)\n",
            "Requirement already satisfied: Polygon3 in /usr/local/lib/python3.7/dist-packages (from -r requirements/build.txt (line 3)) (3.0.9.1)\n",
            "Requirement already satisfied: pyclipper in /usr/local/lib/python3.7/dist-packages (from -r requirements/build.txt (line 4)) (1.3.0)\n",
            "Requirement already satisfied: torch>=1.1 in /usr/local/lib/python3.7/dist-packages (from -r requirements/build.txt (line 5)) (1.7.0+cu110)\n",
            "Requirement already satisfied: imgaug in /usr/local/lib/python3.7/dist-packages (from -r requirements/runtime.txt (line 1)) (0.2.9)\n",
            "Requirement already satisfied: lanms-proper in /usr/local/lib/python3.7/dist-packages (from -r requirements/runtime.txt (line 2)) (1.0.1)\n",
            "Requirement already satisfied: lmdb in /usr/local/lib/python3.7/dist-packages (from -r requirements/runtime.txt (line 3)) (0.99)\n",
            "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from -r requirements/runtime.txt (line 4)) (3.2.2)\n",
            "Requirement already satisfied: numba>=0.45.1 in /usr/local/lib/python3.7/dist-packages (from -r requirements/runtime.txt (line 5)) (0.51.2)\n",
            "Requirement already satisfied: rapidfuzz in /usr/local/lib/python3.7/dist-packages (from -r requirements/runtime.txt (line 9)) (1.5.0)\n",
            "Requirement already satisfied: scikit-image in /usr/local/lib/python3.7/dist-packages (from -r requirements/runtime.txt (line 10)) (0.16.2)\n",
            "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from -r requirements/runtime.txt (line 11)) (1.15.0)\n",
            "Requirement already satisfied: terminaltables in /usr/local/lib/python3.7/dist-packages (from -r requirements/runtime.txt (line 12)) (3.1.0)\n",
            "Requirement already satisfied: asynctest in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 1)) (0.13.0)\n",
            "Requirement already satisfied: codecov in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 2)) (2.1.12)\n",
            "Requirement already satisfied: flake8 in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 3)) (3.9.2)\n",
            "Requirement already satisfied: isort in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 4)) (5.9.3)\n",
            "Requirement already satisfied: kwarray in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 6)) (0.5.19)\n",
            "Requirement already satisfied: pytest in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 8)) (3.6.4)\n",
            "Requirement already satisfied: pytest-cov in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 9)) (2.9.0)\n",
            "Requirement already satisfied: pytest-runner in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 10)) (5.3.1)\n",
            "Requirement already satisfied: ubelt in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 11)) (0.10.0)\n",
            "Requirement already satisfied: xdoctest>=0.10.0 in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 12)) (0.15.6)\n",
            "Requirement already satisfied: yapf in /usr/local/lib/python3.7/dist-packages (from -r requirements/tests.txt (line 13)) (0.31.0)\n",
            "Requirement already satisfied: dataclasses in /usr/local/lib/python3.7/dist-packages (from torch>=1.1->-r requirements/build.txt (line 5)) (0.6)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch>=1.1->-r requirements/build.txt (line 5)) (3.7.4.3)\n",
            "Requirement already satisfied: future in /usr/local/lib/python3.7/dist-packages (from torch>=1.1->-r requirements/build.txt (line 5)) (0.16.0)\n",
            "Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from numba>=0.45.1->-r requirements/runtime.txt (line 5)) (57.4.0)\n",
            "Requirement already satisfied: llvmlite<0.35,>=0.34.0.dev0 in /usr/local/lib/python3.7/dist-packages (from numba>=0.45.1->-r requirements/runtime.txt (line 5)) (0.34.0)\n",
            "Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from imgaug->-r requirements/runtime.txt (line 1)) (1.4.1)\n",
            "Requirement already satisfied: imageio in /usr/local/lib/python3.7/dist-packages (from imgaug->-r requirements/runtime.txt (line 1)) (2.4.1)\n",
            "Requirement already satisfied: Shapely in /usr/local/lib/python3.7/dist-packages (from imgaug->-r requirements/runtime.txt (line 1)) (1.7.1)\n",
            "Requirement already satisfied: opencv-python in /usr/local/lib/python3.7/dist-packages (from imgaug->-r requirements/runtime.txt (line 1)) (4.1.2.30)\n",
            "Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from imgaug->-r requirements/runtime.txt (line 1)) (7.1.2)\n",
            "Requirement already satisfied: networkx>=2.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->-r requirements/runtime.txt (line 10)) (2.6.2)\n",
            "Requirement already satisfied: PyWavelets>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->-r requirements/runtime.txt (line 10)) (1.1.1)\n",
            "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->-r requirements/runtime.txt (line 4)) (0.10.0)\n",
            "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->-r requirements/runtime.txt (line 4)) (2.8.2)\n",
            "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->-r requirements/runtime.txt (line 4)) (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->-r requirements/runtime.txt (line 4)) (2.4.7)\n",
            "Requirement already satisfied: coverage in /usr/local/lib/python3.7/dist-packages (from codecov->-r requirements/tests.txt (line 2)) (5.5)\n",
            "Requirement already satisfied: requests>=2.7.9 in /usr/local/lib/python3.7/dist-packages (from codecov->-r requirements/tests.txt (line 2)) (2.23.0)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.9->codecov->-r requirements/tests.txt (line 2)) (2021.5.30)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.9->codecov->-r requirements/tests.txt (line 2)) (2.10)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.9->codecov->-r requirements/tests.txt (line 2)) (1.24.3)\n",
            "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.7.9->codecov->-r requirements/tests.txt (line 2)) (3.0.4)\n",
            "Requirement already satisfied: pycodestyle<2.8.0,>=2.7.0 in /usr/local/lib/python3.7/dist-packages (from flake8->-r requirements/tests.txt (line 3)) (2.7.0)\n",
            "Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from flake8->-r requirements/tests.txt (line 3)) (4.6.4)\n",
            "Requirement already satisfied: pyflakes<2.4.0,>=2.3.0 in /usr/local/lib/python3.7/dist-packages (from flake8->-r requirements/tests.txt (line 3)) (2.3.1)\n",
            "Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from flake8->-r requirements/tests.txt (line 3)) (0.6.1)\n",
            "Requirement already satisfied: more-itertools>=4.0.0 in /usr/local/lib/python3.7/dist-packages (from pytest->-r requirements/tests.txt (line 8)) (8.8.0)\n",
            "Requirement already satisfied: py>=1.5.0 in /usr/local/lib/python3.7/dist-packages (from pytest->-r requirements/tests.txt (line 8)) (1.10.0)\n",
            "Requirement already satisfied: pluggy<0.8,>=0.5 in /usr/local/lib/python3.7/dist-packages (from pytest->-r requirements/tests.txt (line 8)) (0.7.1)\n",
            "Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.7/dist-packages (from pytest->-r requirements/tests.txt (line 8)) (21.2.0)\n",
            "Requirement already satisfied: atomicwrites>=1.0 in /usr/local/lib/python3.7/dist-packages (from pytest->-r requirements/tests.txt (line 8)) (1.4.0)\n",
            "Requirement already satisfied: ordered-set in /usr/local/lib/python3.7/dist-packages (from ubelt->-r requirements/tests.txt (line 11)) (4.0.2)\n",
            "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata->flake8->-r requirements/tests.txt (line 3)) (3.5.0)\n",
            "Using pip 21.1.3 from /usr/local/lib/python3.7/dist-packages/pip (python 3.7)\n",
            "Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local/lib/python3.7/dist-packages\n",
            "sysconfig: /usr/lib/python3.7/site-packages\n",
            "Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local/lib/python3.7/dist-packages\n",
            "sysconfig: /usr/lib/python3.7/site-packages\n",
            "Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local/include/python3.7/UNKNOWN\n",
            "sysconfig: /usr/include/python3.7m/UNKNOWN\n",
            "Value for scheme.scripts does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local/bin\n",
            "sysconfig: /usr/bin\n",
            "Value for scheme.data does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local\n",
            "sysconfig: /usr\n",
            "Additional context:\n",
            "user = False\n",
            "home = None\n",
            "root = None\n",
            "prefix = None\n",
            "Non-user install because site-packages writeable\n",
            "Created temporary directory: /tmp/pip-ephem-wheel-cache-fvycazpz\n",
            "Created temporary directory: /tmp/pip-req-tracker-we_a93c3\n",
            "Initialized build tracking at /tmp/pip-req-tracker-we_a93c3\n",
            "Created build tracker: /tmp/pip-req-tracker-we_a93c3\n",
            "Entered build tracker: /tmp/pip-req-tracker-we_a93c3\n",
            "Created temporary directory: /tmp/pip-install-ok1naoq0\n",
            "Obtaining file:///mmocr\n",
            "  Added file:///mmocr to build tracker '/tmp/pip-req-tracker-we_a93c3'\n",
            "    Running setup.py (path:/mmocr/setup.py) egg_info for package from file:///mmocr\n",
            "    Created temporary directory: /tmp/pip-pip-egg-info-rei2_m70\n",
            "    Running command python setup.py egg_info\n",
            "    running egg_info\n",
            "    creating /tmp/pip-pip-egg-info-rei2_m70/mmocr.egg-info\n",
            "    writing /tmp/pip-pip-egg-info-rei2_m70/mmocr.egg-info/PKG-INFO\n",
            "    writing dependency_links to /tmp/pip-pip-egg-info-rei2_m70/mmocr.egg-info/dependency_links.txt\n",
            "    writing requirements to /tmp/pip-pip-egg-info-rei2_m70/mmocr.egg-info/requires.txt\n",
            "    writing top-level names to /tmp/pip-pip-egg-info-rei2_m70/mmocr.egg-info/top_level.txt\n",
            "    writing manifest file '/tmp/pip-pip-egg-info-rei2_m70/mmocr.egg-info/SOURCES.txt'\n",
            "    reading manifest template 'MANIFEST.in'\n",
            "    adding license file 'LICENSE'\n",
            "    writing manifest file '/tmp/pip-pip-egg-info-rei2_m70/mmocr.egg-info/SOURCES.txt'\n",
            "  Source in /mmocr has version 0.2.1, which satisfies requirement mmocr==0.2.1 from file:///mmocr\n",
            "  Removed mmocr==0.2.1 from file:///mmocr from build tracker '/tmp/pip-req-tracker-we_a93c3'\n",
            "Requirement already satisfied: imgaug in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (0.2.9)\n",
            "Requirement already satisfied: lanms-proper in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (1.0.1)\n",
            "Requirement already satisfied: lmdb in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (0.99)\n",
            "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (3.2.2)\n",
            "Requirement already satisfied: numba>=0.45.1 in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (0.51.2)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (1.19.5)\n",
            "Requirement already satisfied: Polygon3 in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (3.0.9.1)\n",
            "Requirement already satisfied: pyclipper in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (1.3.0)\n",
            "Requirement already satisfied: rapidfuzz in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (1.5.0)\n",
            "Requirement already satisfied: scikit-image in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (0.16.2)\n",
            "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (1.15.0)\n",
            "Requirement already satisfied: terminaltables in /usr/local/lib/python3.7/dist-packages (from mmocr==0.2.1) (3.1.0)\n",
            "Requirement already satisfied: llvmlite<0.35,>=0.34.0.dev0 in /usr/local/lib/python3.7/dist-packages (from numba>=0.45.1->mmocr==0.2.1) (0.34.0)\n",
            "Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from numba>=0.45.1->mmocr==0.2.1) (57.4.0)\n",
            "Requirement already satisfied: imageio in /usr/local/lib/python3.7/dist-packages (from imgaug->mmocr==0.2.1) (2.4.1)\n",
            "Requirement already satisfied: Shapely in /usr/local/lib/python3.7/dist-packages (from imgaug->mmocr==0.2.1) (1.7.1)\n",
            "Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from imgaug->mmocr==0.2.1) (7.1.2)\n",
            "Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from imgaug->mmocr==0.2.1) (1.4.1)\n",
            "Requirement already satisfied: opencv-python in /usr/local/lib/python3.7/dist-packages (from imgaug->mmocr==0.2.1) (4.1.2.30)\n",
            "Requirement already satisfied: networkx>=2.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->mmocr==0.2.1) (2.6.2)\n",
            "Requirement already satisfied: PyWavelets>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->mmocr==0.2.1) (1.1.1)\n",
            "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmocr==0.2.1) (0.10.0)\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->mmocr==0.2.1) (2.4.7)\n",
            "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmocr==0.2.1) (1.3.1)\n",
            "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->mmocr==0.2.1) (2.8.2)\n",
            "Created temporary directory: /tmp/pip-unpack-khia4f7t\n",
            "Installing collected packages: mmocr\n",
            "  Attempting uninstall: mmocr\n",
            "    Found existing installation: mmocr 0.2.1\n",
            "    Not sure how to uninstall: mmocr 0.2.1 - Check: /mmocr\n",
            "    Can't uninstall 'mmocr'. No files were found to uninstall.\n",
            "  Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "  distutils: /usr/local/lib/python3.7/dist-packages\n",
            "  sysconfig: /usr/lib/python3.7/site-packages\n",
            "  Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "  distutils: /usr/local/lib/python3.7/dist-packages\n",
            "  sysconfig: /usr/lib/python3.7/site-packages\n",
            "  Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "  distutils: /usr/local/include/python3.7/mmocr\n",
            "  sysconfig: /usr/include/python3.7m/mmocr\n",
            "  Value for scheme.scripts does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "  distutils: /usr/local/bin\n",
            "  sysconfig: /usr/bin\n",
            "  Value for scheme.data does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "  distutils: /usr/local\n",
            "  sysconfig: /usr\n",
            "  Additional context:\n",
            "  user = False\n",
            "  home = None\n",
            "  root = None\n",
            "  prefix = None\n",
            "  Running setup.py develop for mmocr\n",
            "    Running command /usr/bin/python3 -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '\"'\"'/mmocr/setup.py'\"'\"'; __file__='\"'\"'/mmocr/setup.py'\"'\"';f = getattr(tokenize, '\"'\"'open'\"'\"', open)(__file__) if os.path.exists(__file__) else io.StringIO('\"'\"'from setuptools import setup; setup()'\"'\"');code = f.read().replace('\"'\"'\\r\\n'\"'\"', '\"'\"'\\n'\"'\"');f.close();exec(compile(code, __file__, '\"'\"'exec'\"'\"'))' develop --no-deps\n",
            "    running develop\n",
            "    running egg_info\n",
            "    writing mmocr.egg-info/PKG-INFO\n",
            "    writing dependency_links to mmocr.egg-info/dependency_links.txt\n",
            "    writing requirements to mmocr.egg-info/requires.txt\n",
            "    writing top-level names to mmocr.egg-info/top_level.txt\n",
            "    reading manifest template 'MANIFEST.in'\n",
            "    adding license file 'LICENSE'\n",
            "    writing manifest file 'mmocr.egg-info/SOURCES.txt'\n",
            "    running build_ext\n",
            "    Creating /usr/local/lib/python3.7/dist-packages/mmocr.egg-link (link to .)\n",
            "    mmocr 0.2.1 is already the active version in easy-install.pth\n",
            "\n",
            "    Installed /mmocr\n",
            "Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local/lib/python3.7/dist-packages\n",
            "sysconfig: /usr/lib/python3.7/site-packages\n",
            "Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local/lib/python3.7/dist-packages\n",
            "sysconfig: /usr/lib/python3.7/site-packages\n",
            "Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local/include/python3.7/UNKNOWN\n",
            "sysconfig: /usr/include/python3.7m/UNKNOWN\n",
            "Value for scheme.scripts does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local/bin\n",
            "sysconfig: /usr/bin\n",
            "Value for scheme.data does not match. Please report this to <https://github.com/pypa/pip/issues/9617>\n",
            "distutils: /usr/local\n",
            "sysconfig: /usr\n",
            "Additional context:\n",
            "user = False\n",
            "home = None\n",
            "root = None\n",
            "prefix = None\n",
            "Successfully installed mmocr-0.2.1\n",
            "Removed build tracker: '/tmp/pip-req-tracker-we_a93c3'\n"
          ]
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "DwDY3puNNmhe",
        "tags": [
          "outputPrepend"
        ],
        "outputId": "f9a5ff35-f44f-459f-ca79-7f943938a99a"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "source": [
        "!pip uninstall mmcv-full\n",
        "!pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.6.0/index.html --no-cache-dir"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Found existing installation: mmcv-full 1.3.11\n",
            "Uninstalling mmcv-full-1.3.11:\n",
            "  Would remove:\n",
            "    /usr/local/lib/python3.7/dist-packages/mmcv/*\n",
            "    /usr/local/lib/python3.7/dist-packages/mmcv_full-1.3.11.dist-info/*\n",
            "Proceed (y/n)? y\n",
            "  Successfully uninstalled mmcv-full-1.3.11\n",
            "Looking in links: https://download.openmmlab.com/mmcv/dist/cu110/torch1.6.0/index.html\n",
            "Collecting mmcv-full\n",
            "  Downloading mmcv-full-1.3.11.tar.gz (307 kB)\n",
            "\u001b[K     |████████████████████████████████| 307 kB 8.2 MB/s \n",
            "\u001b[?25hRequirement already satisfied: addict in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (2.4.0)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (1.19.5)\n",
            "Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (21.0)\n",
            "Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (7.1.2)\n",
            "Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (3.13)\n",
            "Requirement already satisfied: yapf in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (0.31.0)\n",
            "Requirement already satisfied: pyparsing>=2.0.2 in /usr/local/lib/python3.7/dist-packages (from packaging->mmcv-full) (2.4.7)\n",
            "Building wheels for collected packages: mmcv-full\n",
            "  Building wheel for mmcv-full (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for mmcv-full: filename=mmcv_full-1.3.11-cp37-cp37m-linux_x86_64.whl size=25895154 sha256=8cedf064bd88018d0cfba49032423d19888db695f5dc98dce606653bc40d8321\n",
            "  Stored in directory: /tmp/pip-ephem-wheel-cache-8t9ngi9u/wheels/4c/8f/1d/903456a291e5bf33d99cb03cb1bbc822e2c5d32c123b873ebe\n",
            "Successfully built mmcv-full\n",
            "Installing collected packages: mmcv-full\n",
            "Successfully installed mmcv-full-1.3.11\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.colab-display-data+json": {
              "pip_warning": {
                "packages": [
                  "mmcv"
                ]
              }
            }
          },
          "metadata": {}
        }
      ],
      "metadata": {
        "id": "_o0PrIixutjd",
        "outputId": "e3a3fd44-9f4e-41e8-c6dc-58993edb4666",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 547
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Check Installed Dependencies Versions"
      ],
      "metadata": {
        "id": "DY64JCc0pEUu"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "source": [
        "# Check Pytorch installation\n",
        "import torch, torchvision\n",
        "print(torch.__version__, torch.cuda.is_available())\n",
        "\n",
        "# Check MMDetection installation\n",
        "import mmdet\n",
        "print(mmdet.__version__)\n",
        "\n",
        "# Check mmcv installation\n",
        "import mmcv\n",
        "from mmcv.ops import get_compiling_cuda_version, get_compiler_version\n",
        "print(mmcv.__version__)\n",
        "print(get_compiling_cuda_version())\n",
        "print(get_compiler_version())\n",
        "\n",
        "# Check mmocr installation\n",
        "import mmocr\n",
        "print(mmocr.__version__)\n",
        "\n",
        "%cd /mmocr/\n",
        "!ls"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1.7.0+cu110 True\n",
            "2.15.1\n",
            "1.3.11\n",
            "11.0\n",
            "GCC 7.5\n",
            "0.2.1\n",
            "/mmocr\n",
            "configs  docs_zh_CN   mmocr.egg-info   requirements\t setup.py\n",
            "demo\t LICENSE      model-index.yml  requirements.txt  tests\n",
            "docker\t MANIFEST.in  README.md        resources\t tools\n",
            "docs\t mmocr\t      README_zh-CN.md  setup.cfg\n"
          ]
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JABQfPwQN52g",
        "outputId": "188c72bd-5aa2-4521-f63b-bf6a829633c0"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Inference\n",
        "\n",
        "We provide an easy-to-use inference script, `mmocr/utils/ocr.py`, that can be either called through command line or imported as an object (the `MMOCR` class inside). In this notebook, we choose the latter option for ease of demonstration. You can check out its full usage and examples in our [official documentation](https://mmocr.readthedocs.io/en/latest/demo.html)."
      ],
      "metadata": {
        "id": "YCLL7zlu5Hm1"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Perform Inference with a Pretrained Text Recognizer \n",
        "\n",
        "We now demonstrate how to inference on a [demo text recognition image](https://github.com/open-mmlab/mmocr/raw/main/demo/demo_text_recog.jpg) with a pretrained text recognizer using command line. SAR text recognizer is used for this demo, whose checkpoint can be found in the [official documentation](https://mmocr.readthedocs.io/en/latest/textrecog_models.html#show-attend-and-read-a-simple-and-strong-baseline-for-irregular-text-recognition). But you don't need to download it manually -- Our inference script handles these cumbersome setup steps for you! \n",
        "\n",
        "Run the following command and the recognition result will be saved to `outputs/demo_text_recog_pred.jpg`. We will visualize the result in the end."
      ],
      "metadata": {
        "id": "59gHy8Y4pEUv"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "source": [
        "from mmocr.utils.ocr import MMOCR\n",
        "mmocr = MMOCR(det=None, recog='SAR')\n",
        "mmocr.readtext('demo/demo_text_recog.jpg', print_result=True, output='outputs/demo_text_recog_pred.jpg')"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Use load_from_http loader\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/mmocr/mmocr/apis/inference.py:48: UserWarning: Class names are not saved in the checkpoint's meta data, use COCO classes by default.\n",
            "  warnings.warn('Class names are not saved in the checkpoint\\'s '\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'text': 'STAR', 'score': 0.9664112031459808}\n",
            "\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[{'score': 0.9664112031459808, 'text': 'STAR'}]"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "iQQIVH9ApEUv",
        "outputId": "34347c86-7e88-4e2f-d875-ff92533c2793"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "source": [
        "# Visualize the results\n",
        "import matplotlib.pyplot as plt\n",
        "predicted_img = mmcv.imread('./outputs/demo_text_recog_pred.jpg')\n",
        "plt.imshow(mmcv.bgr2rgb(predicted_img))\n",
        "plt.show()"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 51992 characters]"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 268
        },
        "id": "0ab_YJnXpEUw",
        "outputId": "0ee7b117-f75c-49b3-f4cd-6289713ae260"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Perform Inference with a Pretrained Text Detector \n",
        "\n",
        "Next, we perform inference with a pretrained TextSnake text detector and visualize the bounding box results for the demo text detection image provided in [demo_text_det.jpg](https://raw.githubusercontent.com/open-mmlab/mmocr/main/demo/demo_text_det.jpg)."
      ],
      "metadata": {
        "id": "NgoH6qEcC9CL"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "source": [
        "from mmocr.utils.ocr import MMOCR\n",
        "mmocr = MMOCR(det='TextSnake', recog=None)\n",
        "_ = mmocr.readtext('demo/demo_text_det.jpg', output='outputs/demo_text_det_pred.jpg')"
      ],
      "outputs": [],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000,
          "referenced_widgets": [
            "2c92390d57494a4281fe95cc5e061092",
            "a73f09aca9e24725b2e35347a902de89",
            "0ca81ff36c61401e9943825dccd671da",
            "728a93a11fe44e9e977ca8d75d67c7af",
            "330f4551fe984d1ea40e4bea51831533",
            "62a06be59f204e9ab16e4160db18e808",
            "aa7261dfcabb4b85be7611ea1b6f7046",
            "cd501d23a4d04be3897db97e3261f9c0",
            "c678a46976e8469b8e77ba23b266174f",
            "9f7b0826508147c4be923443e8e6243b",
            "3d80c260ae4f4ea0ab07b2ed8367600f"
          ]
        },
        "id": "u0YyG9y0TzL4",
        "outputId": "8ba20ef3-31bc-41a6-b596-42adf9fd83f8"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "source": [
        "# Visualize the results\n",
        "import matplotlib.pyplot as plt\n",
        "predicted_img = mmcv.imread('./outputs/demo_text_det_pred.jpg')\n",
        "plt.figure(figsize=(9, 16))\n",
        "plt.imshow(mmcv.bgr2rgb(predicted_img))\n",
        "plt.show()"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 648x1152 with 1 Axes>"
            ],
            "image/png": "[encoded data, 390100 characters]"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 353
        },
        "id": "2-UHsqkZJFND",
        "outputId": "08f51ae9-1124-46fd-f858-7e659e2f2f88"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Perform end-to-end OCR with pretrained recognizer and detector\n",
        "\n",
        "With the help of `ocr.py`, we can easily combine any text detector and recognizer into a pipeline that forms a standard OCR step. Now we build our own OCR pipeline with TextSnake and SAR and apply it to [demo_text_ocr.jpg](https://raw.githubusercontent.com/open-mmlab/mmocr/main/demo/demo_text_ocr.jpg)."
      ],
      "metadata": {
        "id": "x-uRAtLa63sz"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "source": [
        "from mmocr.utils.ocr import MMOCR\n",
        "mmocr = MMOCR(det='TextSnake', recog='SAR')\n",
        "mmocr.readtext('demo/demo_text_ocr.jpg', print_result=True, output='outputs/demo_text_ocr_pred.jpg')"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Use load_from_http loader\n",
            "Use load_from_http loader\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/mmocr/mmocr/apis/inference.py:48: UserWarning: Class names are not saved in the checkpoint's meta data, use COCO classes by default.\n",
            "  warnings.warn('Class names are not saved in the checkpoint\\'s '\n",
            "/usr/local/lib/python3.7/dist-packages/mmdet/datasets/utils.py:68: UserWarning: \"ImageToTensor\" pipeline is replaced by \"DefaultFormatBundle\" for batch inference. It is recommended to manually replace it in the test data pipeline in your config file.\n",
            "  'data pipeline in your config file.', UserWarning)\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'filename': 'demo_text_ocr', 'text': ['OCBCBANK', 'soculationists', 'sanetal.enance.ounces', '70%', 'ROUND', 'SALE', 'ALLYEAR', 'is', 'SALE']}\n",
            "\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[{'filename': 'demo_text_ocr',\n",
              "  'text': ['OCBCBANK',\n",
              "   'soculationists',\n",
              "   'sanetal.enance.ounces',\n",
              "   '70%',\n",
              "   'ROUND',\n",
              "   'SALE',\n",
              "   'ALLYEAR',\n",
              "   'is',\n",
              "   'SALE']}]"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ],
      "metadata": {
        "id": "xu68YizP8qu6",
        "outputId": "8633e7eb-49c4-490b-d2fe-0d669383f156",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "source": [
        "# Visualize the results\n",
        "import matplotlib.pyplot as plt\n",
        "predicted_img = mmcv.imread('./outputs/demo_text_ocr_pred.jpg')\n",
        "plt.figure(figsize=(9, 16))\n",
        "plt.imshow(mmcv.bgr2rgb(predicted_img))\n",
        "plt.show()"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 648x1152 with 1 Axes>"
            ],
            "image/png": "[encoded data, 144820 characters]"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "metadata": {
        "id": "2AZqwCt09XqR",
        "outputId": "c7941729-ffdc-4360-fa20-84b4dc9087f2",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 262
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Combine OCR with Downstream Tasks\n",
        "\n",
        "MMOCR also supports downstream tasks of OCR, such as key information extraction (KIE). We can even add a KIE model, SDMG-R, to the pipeline applied to [demo_kie.jpeg](https://raw.githubusercontent.com/open-mmlab/mmocr/main/demo/demo_kie.jpeg) and visualize its prediction based on the OCR result.\n"
      ],
      "metadata": {
        "id": "WQ9zzYMa9p9Y"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 24,
      "source": [
        "# SDMGR relies on the dictionary provided in wildreceipt\n",
        "# First download the KIE dataset .tar file and extract it to ./data\n",
        "!mkdir data\n",
        "!wget https://download.openmmlab.com/mmocr/data/wildreceipt.tar\n",
        "!tar -xf wildreceipt.tar \n",
        "!mv wildreceipt ./data"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "--2021-08-23 03:17:39--  https://download.openmmlab.com/mmocr/data/wildreceipt.tar\n",
            "Resolving download.openmmlab.com (download.openmmlab.com)... 47.254.186.225\n",
            "Connecting to download.openmmlab.com (download.openmmlab.com)|47.254.186.225|:443... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 185323520 (177M) [application/x-tar]\n",
            "Saving to: ‘wildreceipt.tar’\n",
            "\n",
            "wildreceipt.tar     100%[===================>] 176.74M  11.2MB/s    in 16s     \n",
            "\n",
            "2021-08-23 03:17:58 (10.8 MB/s) - ‘wildreceipt.tar’ saved [185323520/185323520]\n",
            "\n"
          ]
        }
      ],
      "metadata": {
        "id": "oALHgzmrAqik",
        "outputId": "9f0ca247-37fb-44a4-f08e-9945b7885804",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "source": [
        "from mmocr.utils.ocr import MMOCR\n",
        "mmocr = MMOCR(det='TextSnake', recog='SAR', kie='SDMGR')\n",
        "mmocr.readtext('demo/demo_kie.jpeg', print_result=True, output='outputs/demo_kie_pred.jpg')"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Use load_from_http loader\n",
            "Use load_from_http loader\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/mmocr/mmocr/apis/inference.py:48: UserWarning: Class names are not saved in the checkpoint's meta data, use COCO classes by default.\n",
            "  warnings.warn('Class names are not saved in the checkpoint\\'s '\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Use load_from_http loader\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/mmdet/datasets/utils.py:68: UserWarning: \"ImageToTensor\" pipeline is replaced by \"DefaultFormatBundle\" for batch inference. It is recommended to manually replace it in the test data pipeline in your config file.\n",
            "  'data pipeline in your config file.', UserWarning)\n",
            "/mmocr/mmocr/datasets/kie_dataset.py:46: UserWarning: KIEDataset is only initialized as a downstream demo task of text detection and recognition without an annotation file.\n",
            "  'without an annotation file.', UserWarning)\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'filename': 'demo_kie', 'text': ['Appraval:052723', 'Acct:Xexexxx8425', '128.27', 'Master', '128.27', 'Total', 'Tax', '11.02', '117.25', 'subTotal', 'Cheese', '10.47', '3.Perreroni', '11.07', '3Supreme', '11.97', '26', '43.94', '0.00', '12.Crunchy.Taco', '10.00', 'SLACOMPARTY', '0.00', '12SFTTACO', '10.00', 'SFtt.Tac.Party.', '0.00', 'MONODELONS', '0.00', '10.Bean', 'Grande', '9.90', 'Beatean', '0.00', 'grande', '9.90', '0rder-113533', 'Cashier:.Eric', '7/30/2012', '8:27:32', '=Article-I.D.:']}\n",
            "\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[{'filename': 'demo_kie',\n",
              "  'text': ['Appraval:052723',\n",
              "   'Acct:Xexexxx8425',\n",
              "   '128.27',\n",
              "   'Master',\n",
              "   '128.27',\n",
              "   'Total',\n",
              "   'Tax',\n",
              "   '11.02',\n",
              "   '117.25',\n",
              "   'subTotal',\n",
              "   'Cheese',\n",
              "   '10.47',\n",
              "   '3.Perreroni',\n",
              "   '11.07',\n",
              "   '3Supreme',\n",
              "   '11.97',\n",
              "   '26',\n",
              "   '43.94',\n",
              "   '0.00',\n",
              "   '12.Crunchy.Taco',\n",
              "   '10.00',\n",
              "   'SLACOMPARTY',\n",
              "   '0.00',\n",
              "   '12SFTTACO',\n",
              "   '10.00',\n",
              "   'SFtt.Tac.Party.',\n",
              "   '0.00',\n",
              "   'MONODELONS',\n",
              "   '0.00',\n",
              "   '10.Bean',\n",
              "   'Grande',\n",
              "   '9.90',\n",
              "   'Beatean',\n",
              "   '0.00',\n",
              "   'grande',\n",
              "   '9.90',\n",
              "   '0rder-113533',\n",
              "   'Cashier:.Eric',\n",
              "   '7/30/2012',\n",
              "   '8:27:32',\n",
              "   '=Article-I.D.:']}]"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ],
      "metadata": {
        "id": "2KPRTdHVAGfF",
        "outputId": "792c3a41-c447-4b94-b23b-dfb0ddbb5bdf",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "source": [
        "# Visualize the results\n",
        "import matplotlib.pyplot as plt\n",
        "predicted_img = mmcv.imread('./outputs/demo_kie_pred.jpg')\n",
        "plt.figure(figsize=(18, 32))\n",
        "plt.imshow(mmcv.bgr2rgb(predicted_img))\n",
        "plt.show()"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 1296x2304 with 1 Axes>"
            ],
            "image/png": "[encoded data, 566048 characters]"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "metadata": {
        "id": "96hqfaovAGhl",
        "outputId": "1d5b91b6-8885-4bc2-ca9a-4925d780b4e9",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 498
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Perform Testing with a Pretrained KIE Model\n",
        "\n",
        "We perform testing on the WildReceipt dataset for KIE model by first downloading the .tar file from [Datasets Preparation](https://mmocr.readthedocs.io/en/latest/datasets.html) in MMOCR documentation and then extract the dataset. We have chosen the Visual + Textual moduality test dataset, which we evaluate with Macro F1 metrics."
      ],
      "metadata": {
        "id": "PTWMzvd3E_h8"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "source": [
        "# Can skip this step if you have downloaded wildreceipt in the last section\n",
        "# Download the KIE dataset .tar file and extract it to ./data\n",
        "!mkdir data\n",
        "!wget https://download.openmmlab.com/mmocr/data/wildreceipt.tar\n",
        "!tar -xf wildreceipt.tar \n",
        "!mv wildreceipt ./data"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "--2021-05-17 11:39:10--  https://download.openmmlab.com/mmocr/data/wildreceipt.tar\n",
            "Resolving download.openmmlab.com (download.openmmlab.com)... 47.75.20.25\n",
            "Connecting to download.openmmlab.com (download.openmmlab.com)|47.75.20.25|:443... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 185323520 (177M) [application/x-tar]\n",
            "Saving to: ‘wildreceipt.tar.3’\n",
            "\n",
            "wildreceipt.tar.3   100%[===================>] 176.74M  17.7MB/s    in 10s     \n",
            "\n",
            "2021-05-17 11:39:21 (17.1 MB/s) - ‘wildreceipt.tar.3’ saved [185323520/185323520]\n",
            "\n"
          ]
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3VEW3PQrFZ0g",
        "outputId": "885a4d2e-ca78-42ab-f4a2-dddd9a2d8321"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 29,
      "source": [
        "# Test the dataset with macro f1 metrics \n",
        "!python tools/test.py configs/kie/sdmgr/sdmgr_unet16_60e_wildreceipt.py https://download.openmmlab.com/mmocr/kie/sdmgr/sdmgr_unet16_60e_wildreceipt_20210405-16a47642.pth --eval macro_f1"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Use load_from_http loader\n",
            "Downloading: \"https://download.openmmlab.com/mmocr/kie/sdmgr/sdmgr_unet16_60e_wildreceipt_20210405-16a47642.pth\" to /root/.cache/torch/hub/checkpoints/sdmgr_unet16_60e_wildreceipt_20210405-16a47642.pth\n",
            "100% 18.4M/18.4M [00:01<00:00, 10.2MB/s]\n",
            "[>>] 472/472, 21.1 task/s, elapsed: 22s, ETA:     0s{'macro_f1': 0.87641114}\n"
          ]
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "p0MHNwybo0iI",
        "outputId": "2ac962be-9db7-4557-8853-7201c9e0696f"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Perform Training on a Toy Dataset with MMOCR Recognizer\n",
        "We now demonstrate how to perform training with an MMOCR recognizer. Since training a full academic dataset is time consuming (usually takes about several hours), we will train on the toy dataset for the SAR text recognition model and visualize the predictions. Text detection and other downstream tasks such as KIE follow similar procedures.\n",
        "\n",
        "Training a dataset usually consists of the following steps:\n",
        "1. Convert the dataset into a format supported by MMOCR (e.g. COCO for text detection). The annotation file can be in either .txt or .lmdb format, depending on the size of the dataset. This step is usually applicable to customized datasets, since the datasets and annotation files we provide are already in supported formats. \n",
        "2. Modify the config for training. \n",
        "3. Train the model. \n",
        "\n",
        "The toy dataset consisits of ten images as well as annotation files in both txt and lmdb format, which can be found in [ocr_toy_dataset](https://github.com/open-mmlab/mmocr/tree/main/tests/data/toy_dataset). "
      ],
      "metadata": {
        "id": "nYon41X7RTOT"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Visualize the Toy Dataset\n",
        "\n",
        "We first get a sense of what the toy dataset looks like by visualizing one of the images and labels. "
      ],
      "metadata": {
        "id": "FElJSp1vpEUz"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 30,
      "source": [
        "import mmcv\n",
        "import matplotlib.pyplot as plt \n",
        "\n",
        "img = mmcv.imread('./tests/data/ocr_toy_dataset/imgs/1036169.jpg')\n",
        "plt.imshow(mmcv.bgr2rgb(img))\n",
        "plt.show()"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 64696 characters]"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 121
        },
        "id": "hZfd2pnqN5-Q",
        "outputId": "70e81e99-983b-4c2d-d947-c83aaaa67e11"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 31,
      "source": [
        "# Inspect the labels of the annootation file\n",
        "!cat tests/data/ocr_toy_dataset/label.txt"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1223731.jpg GRAND\n",
            "1223733.jpg HOTEL\n",
            "1223732.jpg HOTEL\n",
            "1223729.jpg PACIFIC\n",
            "1036169.jpg 03/09/2009\n",
            "1190237.jpg ANING\n",
            "1058891.jpg Virgin\n",
            "1058892.jpg america\n",
            "1240078.jpg ATTACK\n",
            "1210236.jpg DAVIDSON\n"
          ]
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "F5M_FVVRN6Fw",
        "outputId": "7d396de9-deb8-415c-eb21-cdc0339a7bec"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Modify the Configuration File\n",
        "\n",
        "In order to perform inference for SAR on colab, we need to modify the config file to accommodate some of the settings of colab such as the number of GPU available. "
      ],
      "metadata": {
        "id": "i-GrV0xSkAc3"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 32,
      "source": [
        "from mmcv import Config\n",
        "cfg = Config.fromfile('./configs/textrecog/sar/sar_r31_parallel_decoder_toy_dataset.py')"
      ],
      "outputs": [],
      "metadata": {
        "id": "uFFH3yUgPEFj"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 33,
      "source": [
        "from mmdet.apis import set_random_seed\n",
        "\n",
        "# Set up working dir to save files and logs.\n",
        "cfg.work_dir = './demo/tutorial_exps'\n",
        "\n",
        "# The original learning rate (LR) is set for 8-GPU training.\n",
        "# We divide it by 8 since we only use one GPU.\n",
        "cfg.optimizer.lr = 0.001 / 8\n",
        "cfg.lr_config.warmup = None\n",
        "# Choose to log training results every 40 images to reduce the size of log file. \n",
        "cfg.log_config.interval = 40\n",
        "\n",
        "# Set seed thus the results are more reproducible\n",
        "cfg.seed = 0\n",
        "set_random_seed(0, deterministic=False)\n",
        "cfg.gpu_ids = range(1)\n",
        "\n",
        "# We can initialize the logger for training and have a look\n",
        "# at the final config used for training\n",
        "print(f'Config:\\n{cfg.pretty_text}')"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Config:\n",
            "checkpoint_config = dict(interval=1)\n",
            "log_config = dict(interval=40, hooks=[dict(type='TextLoggerHook')])\n",
            "dist_params = dict(backend='nccl')\n",
            "log_level = 'INFO'\n",
            "load_from = None\n",
            "resume_from = None\n",
            "workflow = [('train', 1)]\n",
            "label_convertor = dict(\n",
            "    type='AttnConvertor', dict_type='DICT90', with_unknown=True)\n",
            "model = dict(\n",
            "    type='SARNet',\n",
            "    backbone=dict(type='ResNet31OCR'),\n",
            "    encoder=dict(\n",
            "        type='SAREncoder', enc_bi_rnn=False, enc_do_rnn=0.1, enc_gru=False),\n",
            "    decoder=dict(\n",
            "        type='ParallelSARDecoder',\n",
            "        enc_bi_rnn=False,\n",
            "        dec_bi_rnn=False,\n",
            "        dec_do_rnn=0,\n",
            "        dec_gru=False,\n",
            "        pred_dropout=0.1,\n",
            "        d_k=512,\n",
            "        pred_concat=True),\n",
            "    loss=dict(type='SARLoss'),\n",
            "    label_convertor=dict(\n",
            "        type='AttnConvertor', dict_type='DICT90', with_unknown=True),\n",
            "    max_seq_len=30)\n",
            "optimizer = dict(type='Adam', lr=0.000125)\n",
            "optimizer_config = dict(grad_clip=None)\n",
            "lr_config = dict(policy='step', step=[3, 4], warmup=None)\n",
            "total_epochs = 5\n",
            "img_norm_cfg = dict(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])\n",
            "train_pipeline = [\n",
            "    dict(type='LoadImageFromFile'),\n",
            "    dict(\n",
            "        type='ResizeOCR',\n",
            "        height=48,\n",
            "        min_width=48,\n",
            "        max_width=160,\n",
            "        keep_aspect_ratio=True),\n",
            "    dict(type='ToTensorOCR'),\n",
            "    dict(type='NormalizeOCR', mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),\n",
            "    dict(\n",
            "        type='Collect',\n",
            "        keys=['img'],\n",
            "        meta_keys=[\n",
            "            'filename', 'ori_shape', 'resize_shape', 'text', 'valid_ratio'\n",
            "        ])\n",
            "]\n",
            "test_pipeline = [\n",
            "    dict(type='LoadImageFromFile'),\n",
            "    dict(\n",
            "        type='ResizeOCR',\n",
            "        height=48,\n",
            "        min_width=48,\n",
            "        max_width=160,\n",
            "        keep_aspect_ratio=True),\n",
            "    dict(type='ToTensorOCR'),\n",
            "    dict(type='NormalizeOCR', mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),\n",
            "    dict(\n",
            "        type='Collect',\n",
            "        keys=['img'],\n",
            "        meta_keys=[\n",
            "            'filename', 'ori_shape', 'resize_shape', 'valid_ratio',\n",
            "            'img_norm_cfg', 'ori_filename'\n",
            "        ])\n",
            "]\n",
            "dataset_type = 'OCRDataset'\n",
            "img_prefix = 'tests/data/ocr_toy_dataset/imgs'\n",
            "train_anno_file1 = 'tests/data/ocr_toy_dataset/label.txt'\n",
            "train1 = dict(\n",
            "    type='OCRDataset',\n",
            "    img_prefix='tests/data/ocr_toy_dataset/imgs',\n",
            "    ann_file='tests/data/ocr_toy_dataset/label.txt',\n",
            "    loader=dict(\n",
            "        type='HardDiskLoader',\n",
            "        repeat=100,\n",
            "        parser=dict(\n",
            "            type='LineStrParser',\n",
            "            keys=['filename', 'text'],\n",
            "            keys_idx=[0, 1],\n",
            "            separator=' ')),\n",
            "    pipeline=None,\n",
            "    test_mode=False)\n",
            "train_anno_file2 = 'tests/data/ocr_toy_dataset/label.lmdb'\n",
            "train2 = dict(\n",
            "    type='OCRDataset',\n",
            "    img_prefix='tests/data/ocr_toy_dataset/imgs',\n",
            "    ann_file='tests/data/ocr_toy_dataset/label.lmdb',\n",
            "    loader=dict(\n",
            "        type='LmdbLoader',\n",
            "        repeat=100,\n",
            "        parser=dict(\n",
            "            type='LineStrParser',\n",
            "            keys=['filename', 'text'],\n",
            "            keys_idx=[0, 1],\n",
            "            separator=' ')),\n",
            "    pipeline=None,\n",
            "    test_mode=False)\n",
            "test_anno_file1 = 'tests/data/ocr_toy_dataset/label.lmdb'\n",
            "test = dict(\n",
            "    type='OCRDataset',\n",
            "    img_prefix='tests/data/ocr_toy_dataset/imgs',\n",
            "    ann_file='tests/data/ocr_toy_dataset/label.lmdb',\n",
            "    loader=dict(\n",
            "        type='LmdbLoader',\n",
            "        repeat=10,\n",
            "        parser=dict(\n",
            "            type='LineStrParser',\n",
            "            keys=['filename', 'text'],\n",
            "            keys_idx=[0, 1],\n",
            "            separator=' ')),\n",
            "    pipeline=None,\n",
            "    test_mode=True)\n",
            "data = dict(\n",
            "    workers_per_gpu=2,\n",
            "    samples_per_gpu=8,\n",
            "    train=dict(\n",
            "        type='UniformConcatDataset',\n",
            "        datasets=[\n",
            "            dict(\n",
            "                type='OCRDataset',\n",
            "                img_prefix='tests/data/ocr_toy_dataset/imgs',\n",
            "                ann_file='tests/data/ocr_toy_dataset/label.txt',\n",
            "                loader=dict(\n",
            "                    type='HardDiskLoader',\n",
            "                    repeat=100,\n",
            "                    parser=dict(\n",
            "                        type='LineStrParser',\n",
            "                        keys=['filename', 'text'],\n",
            "                        keys_idx=[0, 1],\n",
            "                        separator=' ')),\n",
            "                pipeline=None,\n",
            "                test_mode=False),\n",
            "            dict(\n",
            "                type='OCRDataset',\n",
            "                img_prefix='tests/data/ocr_toy_dataset/imgs',\n",
            "                ann_file='tests/data/ocr_toy_dataset/label.lmdb',\n",
            "                loader=dict(\n",
            "                    type='LmdbLoader',\n",
            "                    repeat=100,\n",
            "                    parser=dict(\n",
            "                        type='LineStrParser',\n",
            "                        keys=['filename', 'text'],\n",
            "                        keys_idx=[0, 1],\n",
            "                        separator=' ')),\n",
            "                pipeline=None,\n",
            "                test_mode=False)\n",
            "        ],\n",
            "        pipeline=[\n",
            "            dict(type='LoadImageFromFile'),\n",
            "            dict(\n",
            "                type='ResizeOCR',\n",
            "                height=48,\n",
            "                min_width=48,\n",
            "                max_width=160,\n",
            "                keep_aspect_ratio=True),\n",
            "            dict(type='ToTensorOCR'),\n",
            "            dict(\n",
            "                type='NormalizeOCR', mean=[0.5, 0.5, 0.5], std=[0.5, 0.5,\n",
            "                                                                0.5]),\n",
            "            dict(\n",
            "                type='Collect',\n",
            "                keys=['img'],\n",
            "                meta_keys=[\n",
            "                    'filename', 'ori_shape', 'resize_shape', 'text',\n",
            "                    'valid_ratio'\n",
            "                ])\n",
            "        ]),\n",
            "    val=dict(\n",
            "        type='UniformConcatDataset',\n",
            "        datasets=[\n",
            "            dict(\n",
            "                type='OCRDataset',\n",
            "                img_prefix='tests/data/ocr_toy_dataset/imgs',\n",
            "                ann_file='tests/data/ocr_toy_dataset/label.lmdb',\n",
            "                loader=dict(\n",
            "                    type='LmdbLoader',\n",
            "                    repeat=10,\n",
            "                    parser=dict(\n",
            "                        type='LineStrParser',\n",
            "                        keys=['filename', 'text'],\n",
            "                        keys_idx=[0, 1],\n",
            "                        separator=' ')),\n",
            "                pipeline=None,\n",
            "                test_mode=True)\n",
            "        ],\n",
            "        pipeline=[\n",
            "            dict(type='LoadImageFromFile'),\n",
            "            dict(\n",
            "                type='ResizeOCR',\n",
            "                height=48,\n",
            "                min_width=48,\n",
            "                max_width=160,\n",
            "                keep_aspect_ratio=True),\n",
            "            dict(type='ToTensorOCR'),\n",
            "            dict(\n",
            "                type='NormalizeOCR', mean=[0.5, 0.5, 0.5], std=[0.5, 0.5,\n",
            "                                                                0.5]),\n",
            "            dict(\n",
            "                type='Collect',\n",
            "                keys=['img'],\n",
            "                meta_keys=[\n",
            "                    'filename', 'ori_shape', 'resize_shape', 'valid_ratio',\n",
            "                    'img_norm_cfg', 'ori_filename'\n",
            "                ])\n",
            "        ]),\n",
            "    test=dict(\n",
            "        type='UniformConcatDataset',\n",
            "        datasets=[\n",
            "            dict(\n",
            "                type='OCRDataset',\n",
            "                img_prefix='tests/data/ocr_toy_dataset/imgs',\n",
            "                ann_file='tests/data/ocr_toy_dataset/label.lmdb',\n",
            "                loader=dict(\n",
            "                    type='LmdbLoader',\n",
            "                    repeat=10,\n",
            "                    parser=dict(\n",
            "                        type='LineStrParser',\n",
            "                        keys=['filename', 'text'],\n",
            "                        keys_idx=[0, 1],\n",
            "                        separator=' ')),\n",
            "                pipeline=None,\n",
            "                test_mode=True)\n",
            "        ],\n",
            "        pipeline=[\n",
            "            dict(type='LoadImageFromFile'),\n",
            "            dict(\n",
            "                type='ResizeOCR',\n",
            "                height=48,\n",
            "                min_width=48,\n",
            "                max_width=160,\n",
            "                keep_aspect_ratio=True),\n",
            "            dict(type='ToTensorOCR'),\n",
            "            dict(\n",
            "                type='NormalizeOCR', mean=[0.5, 0.5, 0.5], std=[0.5, 0.5,\n",
            "                                                                0.5]),\n",
            "            dict(\n",
            "                type='Collect',\n",
            "                keys=['img'],\n",
            "                meta_keys=[\n",
            "                    'filename', 'ori_shape', 'resize_shape', 'valid_ratio',\n",
            "                    'img_norm_cfg', 'ori_filename'\n",
            "                ])\n",
            "        ]))\n",
            "evaluation = dict(interval=1, metric='acc')\n",
            "work_dir = './demo/tutorial_exps'\n",
            "seed = 0\n",
            "gpu_ids = range(0, 1)\n",
            "\n"
          ]
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "67OJ6oAvN6NA",
        "outputId": "a20033ed-a5d3-45d6-bdb2-29feae88004e"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Train the SAR Text Recognizer \n",
        "Finally, we train the SAR text recognizer on the toy dataset for five epochs. "
      ],
      "metadata": {
        "id": "TZj5vyqEmulE"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 34,
      "source": [
        "from mmocr.datasets import build_dataset\n",
        "from mmocr.models import build_detector\n",
        "from mmocr.apis import train_detector\n",
        "import os.path as osp\n",
        "\n",
        "# Build dataset\n",
        "datasets = [build_dataset(cfg.data.train)]\n",
        "\n",
        "# Build the detector\n",
        "model = build_detector(\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_detector(model, datasets, cfg, distributed=False, validate=True)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/mmocr/mmocr/apis/train.py:80: UserWarning: config is now expected to have a `runner` section, please set `runner` in your config.\n",
            "  'please set `runner` in your config.', UserWarning)\n",
            "2021-08-23 03:27:59,310 - mmocr - INFO - Start running, host: root@0c6e7899740e, work_dir: /mmocr/demo/tutorial_exps\n",
            "2021-08-23 03:27:59,311 - mmocr - INFO - Hooks will be executed in the following order:\n",
            "before_run:\n",
            "(VERY_HIGH   ) StepLrUpdaterHook                  \n",
            "(NORMAL      ) CheckpointHook                     \n",
            "(NORMAL      ) EvalHook                           \n",
            "(VERY_LOW    ) TextLoggerHook                     \n",
            " -------------------- \n",
            "before_train_epoch:\n",
            "(VERY_HIGH   ) StepLrUpdaterHook                  \n",
            "(NORMAL      ) EvalHook                           \n",
            "(LOW         ) IterTimerHook                      \n",
            "(VERY_LOW    ) TextLoggerHook                     \n",
            " -------------------- \n",
            "before_train_iter:\n",
            "(VERY_HIGH   ) StepLrUpdaterHook                  \n",
            "(NORMAL      ) EvalHook                           \n",
            "(LOW         ) IterTimerHook                      \n",
            " -------------------- \n",
            "after_train_iter:\n",
            "(ABOVE_NORMAL) OptimizerHook                      \n",
            "(NORMAL      ) CheckpointHook                     \n",
            "(NORMAL      ) EvalHook                           \n",
            "(LOW         ) IterTimerHook                      \n",
            "(VERY_LOW    ) TextLoggerHook                     \n",
            " -------------------- \n",
            "after_train_epoch:\n",
            "(NORMAL      ) CheckpointHook                     \n",
            "(NORMAL      ) EvalHook                           \n",
            "(VERY_LOW    ) TextLoggerHook                     \n",
            " -------------------- \n",
            "before_val_epoch:\n",
            "(LOW         ) IterTimerHook                      \n",
            "(VERY_LOW    ) TextLoggerHook                     \n",
            " -------------------- \n",
            "before_val_iter:\n",
            "(LOW         ) IterTimerHook                      \n",
            " -------------------- \n",
            "after_val_iter:\n",
            "(LOW         ) IterTimerHook                      \n",
            " -------------------- \n",
            "after_val_epoch:\n",
            "(VERY_LOW    ) TextLoggerHook                     \n",
            " -------------------- \n",
            "2021-08-23 03:27:59,313 - mmocr - INFO - workflow: [('train', 1)], max: 5 epochs\n",
            "2021-08-23 03:28:11,809 - mmocr - INFO - Epoch [1][40/250]\tlr: 1.250e-04, eta: 0:06:16, time: 0.312, data_time: 0.054, memory: 2149, loss_ce: 3.1350, loss: 3.1350\n",
            "2021-08-23 03:28:22,325 - mmocr - INFO - Epoch [1][80/250]\tlr: 1.250e-04, eta: 0:05:36, time: 0.263, data_time: 0.002, memory: 2149, loss_ce: 2.0554, loss: 2.0554\n",
            "2021-08-23 03:28:32,623 - mmocr - INFO - Epoch [1][120/250]\tlr: 1.250e-04, eta: 0:05:13, time: 0.257, data_time: 0.002, memory: 2149, loss_ce: 1.3114, loss: 1.3114\n",
            "2021-08-23 03:28:42,724 - mmocr - INFO - Epoch [1][160/250]\tlr: 1.250e-04, eta: 0:04:55, time: 0.253, data_time: 0.002, memory: 2149, loss_ce: 0.9297, loss: 0.9297\n",
            "2021-08-23 03:28:52,679 - mmocr - INFO - Epoch [1][200/250]\tlr: 1.250e-04, eta: 0:04:39, time: 0.249, data_time: 0.002, memory: 2149, loss_ce: 0.7357, loss: 0.7357\n",
            "2021-08-23 03:29:02,564 - mmocr - INFO - Epoch [1][240/250]\tlr: 1.250e-04, eta: 0:04:26, time: 0.247, data_time: 0.002, memory: 2149, loss_ce: 0.5924, loss: 0.5924\n",
            "2021-08-23 03:29:05,069 - mmocr - INFO - Saving checkpoint at 1 epochs\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 100/100, 15.6 task/s, elapsed: 6s, ETA:     0s"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "2021-08-23 03:29:13,961 - mmocr - INFO - \n",
            "Evaluateing tests/data/ocr_toy_dataset/label.lmdb with 100 images now\n",
            "2021-08-23 03:29:13,975 - mmocr - INFO - Epoch(val) [1][13]\t0_word_acc: 0.9000, 0_word_acc_ignore_case: 0.9000, 0_word_acc_ignore_case_symbol: 0.9000, 0_char_recall: 0.9355, 0_char_precision: 0.9062, 0_1-N.E.D: 0.9000\n",
            "2021-08-23 03:29:26,001 - mmocr - INFO - Epoch [2][40/250]\tlr: 1.250e-04, eta: 0:04:08, time: 0.300, data_time: 0.054, memory: 2149, loss_ce: 0.4840, loss: 0.4840\n",
            "2021-08-23 03:29:36,000 - mmocr - INFO - Epoch [2][80/250]\tlr: 1.250e-04, eta: 0:03:57, time: 0.250, data_time: 0.002, memory: 2149, loss_ce: 0.3639, loss: 0.3639\n",
            "2021-08-23 03:29:46,164 - mmocr - INFO - Epoch [2][120/250]\tlr: 1.250e-04, eta: 0:03:46, time: 0.254, data_time: 0.002, memory: 2149, loss_ce: 0.3488, loss: 0.3488\n",
            "2021-08-23 03:29:56,310 - mmocr - INFO - Epoch [2][160/250]\tlr: 1.250e-04, eta: 0:03:36, time: 0.254, data_time: 0.002, memory: 2149, loss_ce: 0.3102, loss: 0.3102\n",
            "2021-08-23 03:30:06,387 - mmocr - INFO - Epoch [2][200/250]\tlr: 1.250e-04, eta: 0:03:25, time: 0.252, data_time: 0.002, memory: 2149, loss_ce: 0.3109, loss: 0.3109\n",
            "2021-08-23 03:30:16,397 - mmocr - INFO - Epoch [2][240/250]\tlr: 1.250e-04, eta: 0:03:14, time: 0.250, data_time: 0.002, memory: 2149, loss_ce: 0.3027, loss: 0.3027\n",
            "2021-08-23 03:30:18,939 - mmocr - INFO - Saving checkpoint at 2 epochs\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 100/100, 15.4 task/s, elapsed: 6s, ETA:     0s"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "2021-08-23 03:30:27,896 - mmocr - INFO - \n",
            "Evaluateing tests/data/ocr_toy_dataset/label.lmdb with 100 images now\n",
            "2021-08-23 03:30:27,903 - mmocr - INFO - Epoch(val) [2][13]\t0_word_acc: 0.9000, 0_word_acc_ignore_case: 0.9000, 0_word_acc_ignore_case_symbol: 0.9000, 0_char_recall: 0.9355, 0_char_precision: 0.9062, 0_1-N.E.D: 0.9000\n",
            "2021-08-23 03:30:39,993 - mmocr - INFO - Epoch [3][40/250]\tlr: 1.250e-04, eta: 0:03:00, time: 0.301, data_time: 0.054, memory: 2149, loss_ce: 0.2920, loss: 0.2920\n",
            "2021-08-23 03:30:49,971 - mmocr - INFO - Epoch [3][80/250]\tlr: 1.250e-04, eta: 0:02:50, time: 0.249, data_time: 0.002, memory: 2149, loss_ce: 0.2860, loss: 0.2860\n",
            "2021-08-23 03:30:59,999 - mmocr - INFO - Epoch [3][120/250]\tlr: 1.250e-04, eta: 0:02:40, time: 0.251, data_time: 0.002, memory: 2149, loss_ce: 0.2801, loss: 0.2801\n",
            "2021-08-23 03:31:10,073 - mmocr - INFO - Epoch [3][160/250]\tlr: 1.250e-04, eta: 0:02:29, time: 0.252, data_time: 0.002, memory: 2149, loss_ce: 0.2863, loss: 0.2863\n",
            "2021-08-23 03:31:20,139 - mmocr - INFO - Epoch [3][200/250]\tlr: 1.250e-04, eta: 0:02:19, time: 0.252, data_time: 0.002, memory: 2149, loss_ce: 0.2881, loss: 0.2881\n",
            "2021-08-23 03:31:30,175 - mmocr - INFO - Epoch [3][240/250]\tlr: 1.250e-04, eta: 0:02:09, time: 0.251, data_time: 0.002, memory: 2149, loss_ce: 0.2644, loss: 0.2644\n",
            "2021-08-23 03:31:32,719 - mmocr - INFO - Saving checkpoint at 3 epochs\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 100/100, 15.3 task/s, elapsed: 7s, ETA:     0s"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "2021-08-23 03:31:41,616 - mmocr - INFO - \n",
            "Evaluateing tests/data/ocr_toy_dataset/label.lmdb with 100 images now\n",
            "2021-08-23 03:31:41,622 - mmocr - INFO - Epoch(val) [3][13]\t0_word_acc: 1.0000, 0_word_acc_ignore_case: 1.0000, 0_word_acc_ignore_case_symbol: 1.0000, 0_char_recall: 1.0000, 0_char_precision: 1.0000, 0_1-N.E.D: 1.0000\n",
            "2021-08-23 03:31:53,746 - mmocr - INFO - Epoch [4][40/250]\tlr: 1.250e-05, eta: 0:01:56, time: 0.302, data_time: 0.054, memory: 2149, loss_ce: 0.2739, loss: 0.2739\n",
            "2021-08-23 03:32:03,736 - mmocr - INFO - Epoch [4][80/250]\tlr: 1.250e-05, eta: 0:01:46, time: 0.250, data_time: 0.002, memory: 2149, loss_ce: 0.2507, loss: 0.2507\n",
            "2021-08-23 03:32:13,784 - mmocr - INFO - Epoch [4][120/250]\tlr: 1.250e-05, eta: 0:01:36, time: 0.251, data_time: 0.002, memory: 2149, loss_ce: 0.2563, loss: 0.2563\n",
            "2021-08-23 03:32:23,840 - mmocr - INFO - Epoch [4][160/250]\tlr: 1.250e-05, eta: 0:01:25, time: 0.251, data_time: 0.002, memory: 2149, loss_ce: 0.2738, loss: 0.2738\n",
            "2021-08-23 03:32:33,902 - mmocr - INFO - Epoch [4][200/250]\tlr: 1.250e-05, eta: 0:01:15, time: 0.252, data_time: 0.002, memory: 2149, loss_ce: 0.2401, loss: 0.2401\n",
            "2021-08-23 03:32:43,940 - mmocr - INFO - Epoch [4][240/250]\tlr: 1.250e-05, eta: 0:01:05, time: 0.251, data_time: 0.002, memory: 2149, loss_ce: 0.2558, loss: 0.2558\n",
            "2021-08-23 03:32:46,493 - mmocr - INFO - Saving checkpoint at 4 epochs\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 100/100, 15.4 task/s, elapsed: 6s, ETA:     0s"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "2021-08-23 03:32:55,484 - mmocr - INFO - \n",
            "Evaluateing tests/data/ocr_toy_dataset/label.lmdb with 100 images now\n",
            "2021-08-23 03:32:55,492 - mmocr - INFO - Epoch(val) [4][13]\t0_word_acc: 1.0000, 0_word_acc_ignore_case: 1.0000, 0_word_acc_ignore_case_symbol: 1.0000, 0_char_recall: 1.0000, 0_char_precision: 1.0000, 0_1-N.E.D: 1.0000\n",
            "2021-08-23 03:33:07,613 - mmocr - INFO - Epoch [5][40/250]\tlr: 1.250e-06, eta: 0:00:52, time: 0.302, data_time: 0.054, memory: 2149, loss_ce: 0.2637, loss: 0.2637\n",
            "2021-08-23 03:33:17,612 - mmocr - INFO - Epoch [5][80/250]\tlr: 1.250e-06, eta: 0:00:42, time: 0.250, data_time: 0.002, memory: 2149, loss_ce: 0.2344, loss: 0.2344\n",
            "2021-08-23 03:33:27,652 - mmocr - INFO - Epoch [5][120/250]\tlr: 1.250e-06, eta: 0:00:32, time: 0.251, data_time: 0.002, memory: 2149, loss_ce: 0.2523, loss: 0.2523\n",
            "2021-08-23 03:33:37,712 - mmocr - INFO - Epoch [5][160/250]\tlr: 1.250e-06, eta: 0:00:22, time: 0.251, data_time: 0.002, memory: 2149, loss_ce: 0.2391, loss: 0.2391\n",
            "2021-08-23 03:33:47,752 - mmocr - INFO - Epoch [5][200/250]\tlr: 1.250e-06, eta: 0:00:12, time: 0.251, data_time: 0.002, memory: 2149, loss_ce: 0.2556, loss: 0.2556\n",
            "2021-08-23 03:33:57,763 - mmocr - INFO - Epoch [5][240/250]\tlr: 1.250e-06, eta: 0:00:02, time: 0.250, data_time: 0.002, memory: 2149, loss_ce: 0.2495, loss: 0.2495\n",
            "2021-08-23 03:34:00,311 - mmocr - INFO - Saving checkpoint at 5 epochs\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 100/100, 15.4 task/s, elapsed: 6s, ETA:     0s"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "2021-08-23 03:34:09,084 - mmocr - INFO - \n",
            "Evaluateing tests/data/ocr_toy_dataset/label.lmdb with 100 images now\n",
            "2021-08-23 03:34:09,090 - mmocr - INFO - Epoch(val) [5][13]\t0_word_acc: 1.0000, 0_word_acc_ignore_case: 1.0000, 0_word_acc_ignore_case_symbol: 1.0000, 0_char_recall: 1.0000, 0_char_precision: 1.0000, 0_1-N.E.D: 1.0000\n"
          ]
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mDVkK6yjpEU1",
        "outputId": "9d0494c8-06c5-4c75-c898-71b679198b83"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Test and Visualize the Predictions\n",
        "\n",
        "For completeness, we also perform testing on the latest checkpoint and evaluate the results with hmean-iou metrics. The predictions are saved in the ./outputs file. "
      ],
      "metadata": {
        "id": "sklydRNXnfJk"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 35,
      "source": [
        "from mmocr.apis import init_detector, model_inference\n",
        "\n",
        "img = './tests/data/ocr_toy_dataset/imgs/1036169.jpg'\n",
        "checkpoint = \"./demo/tutorial_exps/epoch_5.pth\"\n",
        "out_file = 'outputs/1036169.jpg'\n",
        "\n",
        "model = init_detector(cfg, checkpoint, device=\"cuda:0\")\n",
        "if model.cfg.data.test['type'] == 'ConcatDataset':\n",
        "    model.cfg.data.test.pipeline = model.cfg.data.test['datasets'][0].pipeline\n",
        "\n",
        "\n",
        "result = model_inference(model, img)\n",
        "print(f'result: {result}')\n",
        "\n",
        "img = model.show_result(\n",
        "        img, result, out_file=out_file, show=False)\n",
        "\n",
        "mmcv.imwrite(img, out_file)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Use load_from_local loader\n",
            "result: {'text': '03/09/2009', 'score': [0.9998674392700195, 0.9986717700958252, 0.9974325299263, 0.9999891519546509, 0.9976925849914551, 0.9968488812446594, 0.997633695602417, 0.9999977350234985, 0.999995231628418, 0.9993376135826111]}\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 35
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-HbXY7uUpEU1",
        "outputId": "535374c9-aae8-4a80-caa7-45a8402daa22"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 36,
      "source": [
        "# Visualize the results\n",
        "predicted_img = mmcv.imread('./outputs/1036169.jpg')\n",
        "plt.figure(figsize=(4, 4))\n",
        "plt.imshow(mmcv.bgr2rgb(predicted_img))\n",
        "plt.show()"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 288x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 48188 characters]"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 146
        },
        "id": "k3s27QIGQCnT",
        "outputId": "c945a860-4358-4939-9ac6-c5d712b2c7d6"
      }
    }
  ]
}
