From 75801019981492eedf9280cb352eea3d8e99b65f Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Mon, 2 Aug 2021 21:13:48 +0200 Subject: Fix log import, fix mapping in datamodules, fix nn modules can be hashed --- notebooks/05c-test-model-end-to-end.ipynb | 152 +++++++++++------------------- 1 file changed, 56 insertions(+), 96 deletions(-) (limited to 'notebooks/05c-test-model-end-to-end.ipynb') diff --git a/notebooks/05c-test-model-end-to-end.ipynb b/notebooks/05c-test-model-end-to-end.ipynb index a96e484..b652bdd 100644 --- a/notebooks/05c-test-model-end-to-end.ipynb +++ b/notebooks/05c-test-model-end-to-end.ipynb @@ -26,16 +26,6 @@ { "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": [], @@ -47,193 +37,163 @@ }, { "cell_type": "code", - "execution_count": 4, - "id": "8741a844-3b97-47c4-a2a1-5a268d40923c", + "execution_count": 3, + "id": "6b722ca0-9c65-4f90-be4e-b7334ea81237", "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", + "mapping:\n", + " _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", + "_target_: text_recognizer.models.transformer.TransformerLitModel\n", + "interval: step\n", + "monitor: val/loss\n", + "ignore_tokens:\n", "- \n", "- \n", "-

\n", - "extra_symbols:\n", - "- '\n", + "start_token: \n", + "end_token: \n", + "pad_token:

\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" + "{'mapping': {'_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']}, '_target_': 'text_recognizer.models.transformer.TransformerLitModel', 'interval': 'step', 'monitor': 'val/loss', 'ignore_tokens': ['', '', '

'], 'start_token': '', 'end_token': '', 'pad_token': '

'}\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", + "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": 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", + "id": "9c797159-845e-42c6-bd65-1c976ad627cd", "metadata": {}, "outputs": [], "source": [ - "tt.mapping" + "# 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": "6b722ca0-9c65-4f90-be4e-b7334ea81237", + "id": "af2c8cfa-0b45-4681-b671-0f97ace62516", "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)" + "net = instantiate(cfg)" ] }, { "cell_type": "code", "execution_count": null, - "id": "9c797159-845e-42c6-bd65-1c976ad627cd", - "metadata": {}, + "id": "8f0742ad-5e2f-42d5-83e7-6e46398b4f0f", + "metadata": { + "tags": [] + }, "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)" + "net" ] }, { "cell_type": "code", "execution_count": null, - "id": "dcfbe2ab-6775-4aa4-acf4-57203a3f5511", + "id": "40be59bc-db79-4af1-9df4-e280f7a56481", "metadata": {}, "outputs": [], "source": [ - "from importlib import import_module" + "img = torch.rand(4, 1, 576, 640)" ] }, { "cell_type": "code", "execution_count": null, - "id": "e3d4c70e-509d-457a-ac81-2bac27cb95d2", + "id": "d5a8f10b-edf5-4a18-9747-f016db72c384", "metadata": {}, "outputs": [], "source": [ - "x = import_module(\"text_recognizer.networks.transformer.attention\")" + "y = torch.randint(0, 1006, (4, 451))" ] }, { "cell_type": "code", "execution_count": null, - "id": "039d4a7f-f70d-43a1-8b5f-7e766ac01010", + "id": "19423ef1-3d98-4af3-8748-fdd3bb817300", "metadata": {}, "outputs": [], "source": [ - "y = partial(getattr(x, \"Attention\"), dim=16, num_heads=2, **cfg.decoder.attn_kwargs)" + "y.shape" ] }, { "cell_type": "code", "execution_count": null, - "id": "9be1d661-bfac-4826-ab8d-453557713f68", + "id": "0712ee7e-4f66-4fb1-bc91-d8a127eb7ac7", "metadata": {}, "outputs": [], "source": [ - "y().causal" + "net = net.cuda()\n", + "img = img.cuda()\n", + "y = y.cuda()" ] }, { "cell_type": "code", "execution_count": null, - "id": "54b35e6f-35db-4769-8bc5-ed1764768cf2", + "id": "719154b4-47db-4c91-bae4-8c572c4a4536", "metadata": {}, "outputs": [], "source": [ - "y(causal=True)" + "net(img, y).shape" ] }, { "cell_type": "code", "execution_count": null, - "id": "af2c8cfa-0b45-4681-b671-0f97ace62516", + "id": "bcb7db0f-0afe-44eb-9bb7-b988fbead95a", "metadata": {}, "outputs": [], "source": [ - "net = instantiate(cfg)" + "from torchsummary import summary" ] }, { "cell_type": "code", "execution_count": null, - "id": "8f0742ad-5e2f-42d5-83e7-6e46398b4f0f", + "id": "31af8ee1-28d3-46b8-a847-6506d29bc45c", "metadata": {}, "outputs": [], "source": [ - "net" + "summary(net, [(1, 576, 640), (451,)], device=\"cpu\", depth=2)" ] }, { "cell_type": "code", "execution_count": null, - "id": "709be6cc-6708-4561-ad45-28f433612a0d", + "id": "4d6d836f-d169-48b4-92e6-ca17179e6f85", "metadata": {}, "outputs": [], "source": [] -- cgit v1.2.3-70-g09d2