summaryrefslogtreecommitdiff
path: root/notebooks/03-look-at-iam-lines.ipynb
blob: 3db4accb419a3a66047dc24ee7faba00e272dc8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from pathlib import Path\n",
    "import torch\n",
    "from omegaconf import OmegaConf\n",
    "from hydra import compose, initialize\n",
    "from omegaconf import OmegaConf\n",
    "from hydra.utils import instantiate\n",
    "from importlib.util import find_spec\n",
    "if find_spec(\"text_recognizer\") is None:\n",
    "    import sys\n",
    "    sys.path.append('..')\n",
    "\n",
    "from text_recognizer.data.iam_lines import IAMLines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zsh:ulimit:1: value exceeds hard limit\n"
     ]
    }
   ],
   "source": [
    "!ulimit -n 65000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_config(path: Path):\n",
    "    with initialize(config_path=path.parent):\n",
    "        cfg = compose(config_name=path.name)\n",
    "    return cfg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "datamodule_path = Path(\"../training/conf/datamodule/iam_lines.yaml\")\n",
    "mapping_path = Path(\"../training/conf/mapping/word_piece.yaml\")\n",
    "mapping_path = Path(\"../training/conf/mapping/characters.yaml\")\n",
    "data_cfg = load_config(datamodule_path)\n",
    "mapping_cfg = load_config(mapping_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_cfg.batch_size = 8\n",
    "data_cfg.num_workers = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-10-11 22:00:17.870 | DEBUG    | text_recognizer.data.transforms.load_transform:_load_config:17 - Loading transforms from config: transform/iam_lines.yaml\n",
      "2021-10-11 22:00:20.796 | DEBUG    | text_recognizer.data.transforms.load_transform:_load_config:17 - Loading transforms from config: test_transform/iam_lines.yaml\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IAM Lines dataset\n",
      "Num classes: 58\n",
      "Input dims: (1, 56, 1024)\n",
      "Output dims: (89, 1)\n",
      "Train/val/test sizes: 9116, 2279, 1958\n",
      "Train Batch x stats: (torch.Size([8, 1, 56, 1024]), torch.float32, tensor(0.), tensor(0.0269), tensor(0.0916), tensor(1.))\n",
      "Train Batch y stats: (torch.Size([8, 89]), torch.int64, tensor(1), tensor(54))\n",
      "Test Batch x stats: (torch.Size([8, 1, 56, 1024]), torch.float32, tensor(0.), tensor(0.0327), tensor(0.0940), tensor(0.8588))\n",
      "Test Batch y stats: (torch.Size([8, 89]), torch.int64, tensor(1), tensor(52))\n",
      "\n"
     ]
    }
   ],
   "source": [
    "datamodule = instantiate(data_cfg, mapping=mapping_cfg)\n",
    "datamodule.prepare_data()\n",
    "datamodule.setup()\n",
    "print(datamodule)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convert_y_label_to_string(y, datamodule=datamodule):\n",
    "    return ''.join([datamodule.mapping[i] for i in y if i != 3])\n",
    "\n",
    "convert_y_label_to_string(dataset[0][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X, Y = next(iter(datamodule.train_dataloader()))\n",
    "for i in range(8):\n",
    "    plt.figure(figsize=(40, 20))\n",
    "    x, y = X[i], Y[i]\n",
    "    sentence = convert_y_label_to_string(y)\n",
    "    plt.imshow(x.squeeze(), cmap='gray', vmin=0, vmax=1)\n",
    "    plt.title(sentence)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}