{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1e40a88b", "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 PIL import Image\n", "import torch\n", "from torch import nn\n", "from importlib.util import find_spec\n", "if find_spec(\"text_recognizer\") is None:\n", " import sys\n", " sys.path.append('..')\n", " " ] }, { "cell_type": "code", "execution_count": 2, "id": "3e812a1e", "metadata": {}, "outputs": [], "source": [ "import attr" ] }, { "cell_type": "code", "execution_count": 3, "id": "d3a6146b-94b1-4618-a4e4-00f8e23ffdb0", "metadata": {}, "outputs": [], "source": [ "from hydra import compose, initialize\n", "from omegaconf import OmegaConf\n", "from hydra.utils import instantiate" ] }, { "cell_type": "code", "execution_count": 4, "id": "8741a844-3b97-47c4-a2a1-5a268d40923c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "_target_: text_recognizer.data.mappings.WordPieceMapping\n", "num_features: 1000\n", "tokens: iamdb_1kwp_tokens_1000.txt\n", "lexicon: iamdb_1kwp_lex_1000.txt\n", "data_dir: null\n", "use_words: false\n", "prepend_wordsep: false\n", "special_tokens:\n", "- \n", "- \n", "-

\n", "extra_symbols:\n", "- '\n", "\n", " '\n", "\n", "{'_target_': 'text_recognizer.data.mappings.WordPieceMapping', 'num_features': 1000, 'tokens': 'iamdb_1kwp_tokens_1000.txt', 'lexicon': 'iamdb_1kwp_lex_1000.txt', 'data_dir': None, 'use_words': False, 'prepend_wordsep': False, 'special_tokens': ['', '', '

'], 'extra_symbols': ['\\n']}\n" ] } ], "source": [ "# context initialization\n", "with initialize(config_path=\"../training/conf/model/mapping\", job_name=\"test_app\"):\n", " cfg = compose(config_name=\"word_piece\")\n", " print(OmegaConf.to_yaml(cfg))\n", " print(cfg)" ] }, { "cell_type": "code", "execution_count": 5, "id": "c9271d46-37b1-4d06-a603-46b5ed82f821", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2021-07-30 23:08:27.495 | DEBUG | text_recognizer.data.mappings:__attrs_post_init__:89 - Using data dir: /home/aktersnurra/projects/text-recognizer/data/downloaded/iam/iamdb\n" ] } ], "source": [ "tt =instantiate(cfg)" ] }, { "cell_type": "code", "execution_count": 6, "id": "bf1b07ac-9de7-4d24-a36b-09847bc6bc6f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "WordPieceMapping(extra_symbols={'\\n'}, mapping=['', '', '', '

', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ', '!', '\"', '#', '&', \"'\", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '?', '\\n'], inverse_mapping={'': 0, '': 1, '': 2, '

': 3, '0': 4, '1': 5, '2': 6, '3': 7, '4': 8, '5': 9, '6': 10, '7': 11, '8': 12, '9': 13, 'A': 14, 'B': 15, 'C': 16, 'D': 17, 'E': 18, 'F': 19, 'G': 20, 'H': 21, 'I': 22, 'J': 23, 'K': 24, 'L': 25, 'M': 26, 'N': 27, 'O': 28, 'P': 29, 'Q': 30, 'R': 31, 'S': 32, 'T': 33, 'U': 34, 'V': 35, 'W': 36, 'X': 37, 'Y': 38, 'Z': 39, 'a': 40, 'b': 41, 'c': 42, 'd': 43, 'e': 44, 'f': 45, 'g': 46, 'h': 47, 'i': 48, 'j': 49, 'k': 50, 'l': 51, 'm': 52, 'n': 53, 'o': 54, 'p': 55, 'q': 56, 'r': 57, 's': 58, 't': 59, 'u': 60, 'v': 61, 'w': 62, 'x': 63, 'y': 64, 'z': 65, ' ': 66, '!': 67, '\"': 68, '#': 69, '&': 70, \"'\": 71, '(': 72, ')': 73, '*': 74, '+': 75, ',': 76, '-': 77, '.': 78, '/': 79, ':': 80, ';': 81, '?': 82, '\\n': 83}, input_size=[28, 28], data_dir=PosixPath('/home/aktersnurra/projects/text-recognizer/data/downloaded/iam/iamdb'), num_features=1000, tokens='iamdb_1kwp_tokens_1000.txt', lexicon='iamdb_1kwp_lex_1000.txt', use_words=False, prepend_wordsep=False, special_tokens={'

', '', ''}, wordpiece_processor=)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tt" ] }, { "cell_type": "code", "execution_count": null, "id": "2452e8f4-cc5f-4763-9a25-4fa27b7f143e", "metadata": {}, "outputs": [], "source": [ "tt.mapping" ] }, { "cell_type": "code", "execution_count": null, "id": "6b722ca0-9c65-4f90-be4e-b7334ea81237", "metadata": {}, "outputs": [], "source": [ "# context initialization\n", "with initialize(config_path=\"../training/conf/model/\", job_name=\"test_app\"):\n", " cfg = compose(config_name=\"lit_transformer\")\n", " print(OmegaConf.to_yaml(cfg))\n", " print(cfg)" ] }, { "cell_type": "code", "execution_count": null, "id": "9c797159-845e-42c6-bd65-1c976ad627cd", "metadata": {}, "outputs": [], "source": [ "# context initialization\n", "with initialize(config_path=\"../training/conf/network/\", job_name=\"test_app\"):\n", " cfg = compose(config_name=\"conv_transformer\")\n", " print(OmegaConf.to_yaml(cfg))\n", " print(cfg)" ] }, { "cell_type": "code", "execution_count": null, "id": "dcfbe2ab-6775-4aa4-acf4-57203a3f5511", "metadata": {}, "outputs": [], "source": [ "from importlib import import_module" ] }, { "cell_type": "code", "execution_count": null, "id": "e3d4c70e-509d-457a-ac81-2bac27cb95d2", "metadata": {}, "outputs": [], "source": [ "x = import_module(\"text_recognizer.networks.transformer.attention\")" ] }, { "cell_type": "code", "execution_count": null, "id": "039d4a7f-f70d-43a1-8b5f-7e766ac01010", "metadata": {}, "outputs": [], "source": [ "y = partial(getattr(x, \"Attention\"), dim=16, num_heads=2, **cfg.decoder.attn_kwargs)" ] }, { "cell_type": "code", "execution_count": null, "id": "9be1d661-bfac-4826-ab8d-453557713f68", "metadata": {}, "outputs": [], "source": [ "y().causal" ] }, { "cell_type": "code", "execution_count": null, "id": "54b35e6f-35db-4769-8bc5-ed1764768cf2", "metadata": {}, "outputs": [], "source": [ "y(causal=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "af2c8cfa-0b45-4681-b671-0f97ace62516", "metadata": {}, "outputs": [], "source": [ "net = instantiate(cfg)" ] }, { "cell_type": "code", "execution_count": null, "id": "8f0742ad-5e2f-42d5-83e7-6e46398b4f0f", "metadata": {}, "outputs": [], "source": [ "net" ] }, { "cell_type": "code", "execution_count": null, "id": "709be6cc-6708-4561-ad45-28f433612a0d", "metadata": {}, "outputs": [], "source": [] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 5 }