{ "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 }