{"nbformat":4,"nbformat_minor":0,"metadata":{"kernelspec":{"display_name":"Python [conda env:ml]","language":"python","name":"conda-env-ml-py"},"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.8.3"},"colab":{"name":"Kopie von run.ipynb","provenance":[{"file_id":"https://github.com/derInformatiker/AIcrowd-AIBlitz8-Solution/blob/main/challenge3/run.ipynb","timestamp":1621771826141}],"collapsed_sections":[],"toc_visible":true},"accelerator":"GPU","widgets":{"application/vnd.jupyter.widget-state+json":{"e697bfdb6d1c481180984038332bd5e3":{"model_module":"@jupyter-widgets/controls","model_name":"HBoxModel","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_2777eabbb42c41e996f1f64efb0a2f9d","_model_module":"@jupyter-widgets/controls","children":["IPY_MODEL_3b7c172fd82045a980b6fba38dad58ef","IPY_MODEL_80f82e0611704cb0bafd907f015ff46c","IPY_MODEL_5dfc15a57856467c92feffb7f7ccd6f6"]}},"2777eabbb42c41e996f1f64efb0a2f9d":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","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":"row wrap","width":"100%","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":"inline-flex","left":null}},"3b7c172fd82045a980b6fba38dad58ef":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","state":{"_view_name":"HTMLView","style":"IPY_MODEL_562b61a5effa446ca5b94f77c43d66ed","_dom_classes":[],"description":"","_model_name":"HTMLModel","placeholder":"​","_view_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","value":"Predicting: 100%","_view_count":null,"_view_module_version":"1.5.0","description_tooltip":null,"_model_module":"@jupyter-widgets/controls","layout":"IPY_MODEL_a99f91736e6b47f8b349b6baadc17742"}},"80f82e0611704cb0bafd907f015ff46c":{"model_module":"@jupyter-widgets/controls","model_name":"FloatProgressModel","state":{"_view_name":"ProgressView","style":"IPY_MODEL_8a5fa7d1298b40009b6eaca328e65aa6","_dom_classes":[],"description":"","_model_name":"FloatProgressModel","bar_style":"success","max":1,"_view_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","value":1,"_view_count":null,"_view_module_version":"1.5.0","orientation":"horizontal","min":0,"description_tooltip":null,"_model_module":"@jupyter-widgets/controls","layout":"IPY_MODEL_f6d32c8d19b6489f88b2933cb663ffba"}},"5dfc15a57856467c92feffb7f7ccd6f6":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","state":{"_view_name":"HTMLView","style":"IPY_MODEL_25ca847206dd482a9ad35c66bbf05080","_dom_classes":[],"description":"","_model_name":"HTMLModel","placeholder":"​","_view_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","value":" 313/313 [00:12&lt;00:00, 25.49it/s]","_view_count":null,"_view_module_version":"1.5.0","description_tooltip":null,"_model_module":"@jupyter-widgets/controls","layout":"IPY_MODEL_99bbfd47a9df4dcaa8494a53bbaf523a"}},"562b61a5effa446ca5b94f77c43d66ed":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","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"}},"a99f91736e6b47f8b349b6baadc17742":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","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}},"8a5fa7d1298b40009b6eaca328e65aa6":{"model_module":"@jupyter-widgets/controls","model_name":"ProgressStyleModel","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"}},"f6d32c8d19b6489f88b2933cb663ffba":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","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":"2","_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}},"25ca847206dd482a9ad35c66bbf05080":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","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"}},"99bbfd47a9df4dcaa8494a53bbaf523a":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","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","metadata":{"id":"neQJsdUGFJJV"},"source":["# Speed Recognition Solution"]},{"cell_type":"markdown","metadata":{"id":"Fijz1vAYNuZh"},"source":["In this challenge I have used Star-Net without TPS Transform at the tail of the model. This model has a ResNet as a feature extractor, a BiLSTM and a Linear layer at the top. \n","To make a submission you don't have to train it because I have uploaded my best model. If you wan't to train it you can just uncommon the two lines which are marked with a comment."]},{"cell_type":"code","metadata":{"id":"4Qz241WMEw5C","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1621770896104,"user_tz":-120,"elapsed":5376,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"18826a18-a42e-4ae9-fed8-65c2ab050248"},"source":["!pip install -U aicrowd-cli"],"execution_count":null,"outputs":[{"output_type":"stream","text":["Collecting aicrowd-cli\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/a5/8a/fca67e8c1cb1501a9653cd653232bf6fdebbb2393e3de861aad3636a1136/aicrowd_cli-0.1.6-py3-none-any.whl (51kB)\n","\r\u001b[K     |██████▍                         | 10kB 20.1MB/s eta 0:00:01\r\u001b[K     |████████████▊                   | 20kB 21.0MB/s eta 0:00:01\r\u001b[K     |███████████████████             | 30kB 16.2MB/s eta 0:00:01\r\u001b[K     |█████████████████████████▍      | 40kB 14.9MB/s eta 0:00:01\r\u001b[K     |███████████████████████████████▊| 51kB 7.8MB/s eta 0:00:01\r\u001b[K     |████████████████████████████████| 61kB 4.9MB/s \n","\u001b[?25hCollecting click<8,>=7.1.2\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82kB)\n","\u001b[K     |████████████████████████████████| 92kB 6.5MB/s \n","\u001b[?25hCollecting requests-toolbelt<1,>=0.9.1\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/60/ef/7681134338fc097acef8d9b2f8abe0458e4d87559c689a8c306d0957ece5/requests_toolbelt-0.9.1-py2.py3-none-any.whl (54kB)\n","\u001b[K     |████████████████████████████████| 61kB 7.5MB/s \n","\u001b[?25hRequirement already satisfied, skipping upgrade: toml<1,>=0.10.2 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (0.10.2)\n","Collecting tqdm<5,>=4.56.0\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/72/8a/34efae5cf9924328a8f34eeb2fdaae14c011462d9f0e3fcded48e1266d1c/tqdm-4.60.0-py2.py3-none-any.whl (75kB)\n","\u001b[K     |████████████████████████████████| 81kB 6.7MB/s \n","\u001b[?25hCollecting rich<11,>=10.0.0\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/6b/39/fbe8d15f0b017d63701f2a42e4ccb9a73cd4175e5c56214c1b5685e3dd79/rich-10.2.2-py3-none-any.whl (203kB)\n","\u001b[K     |████████████████████████████████| 204kB 10.6MB/s \n","\u001b[?25hCollecting requests<3,>=2.25.1\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/29/c1/24814557f1d22c56d50280771a17307e6bf87b70727d975fd6b2ce6b014a/requests-2.25.1-py2.py3-none-any.whl (61kB)\n","\u001b[K     |████████████████████████████████| 61kB 7.7MB/s \n","\u001b[?25hCollecting gitpython<4,>=3.1.12\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/27/da/6f6224fdfc47dab57881fe20c0d1bc3122be290198ba0bf26a953a045d92/GitPython-3.1.17-py3-none-any.whl (166kB)\n","\u001b[K     |████████████████████████████████| 174kB 12.9MB/s \n","\u001b[?25hCollecting commonmark<0.10.0,>=0.9.0\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/b1/92/dfd892312d822f36c55366118b95d914e5f16de11044a27cf10a7d71bbbf/commonmark-0.9.1-py2.py3-none-any.whl (51kB)\n","\u001b[K     |████████████████████████████████| 51kB 7.9MB/s \n","\u001b[?25hRequirement already satisfied, skipping upgrade: pygments<3.0.0,>=2.6.0 in /usr/local/lib/python3.7/dist-packages (from rich<11,>=10.0.0->aicrowd-cli) (2.6.1)\n","Collecting colorama<0.5.0,>=0.4.0\n","  Downloading https://files.pythonhosted.org/packages/44/98/5b86278fbbf250d239ae0ecb724f8572af1c91f4a11edf4d36a206189440/colorama-0.4.4-py2.py3-none-any.whl\n","Requirement already satisfied, skipping upgrade: typing-extensions<4.0.0,>=3.7.4; python_version < \"3.8\" in /usr/local/lib/python3.7/dist-packages (from rich<11,>=10.0.0->aicrowd-cli) (3.7.4.3)\n","Requirement already satisfied, skipping upgrade: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (1.24.3)\n","Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2020.12.5)\n","Requirement already satisfied, skipping upgrade: chardet<5,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (3.0.4)\n","Requirement already satisfied, skipping upgrade: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2.10)\n","Collecting gitdb<5,>=4.0.1\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/ea/e8/f414d1a4f0bbc668ed441f74f44c116d9816833a48bf81d22b697090dba8/gitdb-4.0.7-py3-none-any.whl (63kB)\n","\u001b[K     |████████████████████████████████| 71kB 8.1MB/s \n","\u001b[?25hCollecting smmap<5,>=3.0.1\n","  Downloading https://files.pythonhosted.org/packages/68/ee/d540eb5e5996eb81c26ceffac6ee49041d473bc5125f2aa995cf51ec1cf1/smmap-4.0.0-py2.py3-none-any.whl\n","\u001b[31mERROR: google-colab 1.0.0 has requirement requests~=2.23.0, but you'll have requests 2.25.1 which is incompatible.\u001b[0m\n","\u001b[31mERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible.\u001b[0m\n","Installing collected packages: click, requests, requests-toolbelt, tqdm, commonmark, colorama, rich, smmap, gitdb, gitpython, aicrowd-cli\n","  Found existing installation: click 8.0.0\n","    Uninstalling click-8.0.0:\n","      Successfully uninstalled click-8.0.0\n","  Found existing installation: requests 2.23.0\n","    Uninstalling requests-2.23.0:\n","      Successfully uninstalled requests-2.23.0\n","  Found existing installation: tqdm 4.41.1\n","    Uninstalling tqdm-4.41.1:\n","      Successfully uninstalled tqdm-4.41.1\n","Successfully installed aicrowd-cli-0.1.6 click-7.1.2 colorama-0.4.4 commonmark-0.9.1 gitdb-4.0.7 gitpython-3.1.17 requests-2.25.1 requests-toolbelt-0.9.1 rich-10.2.2 smmap-4.0.0 tqdm-4.60.0\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"Q44k35GbGtT_","colab":{"base_uri":"https://localhost:8080/","height":1000},"executionInfo":{"status":"ok","timestamp":1621771359650,"user_tz":-120,"elapsed":8606,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"6719869a-0ece-4ac5-c2bb-acf892860e87"},"source":["!pip install pytorch-lightning"],"execution_count":null,"outputs":[{"output_type":"stream","text":["Collecting pytorch-lightning\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/32/b9/59ce5be6679884579c276f5f208587c3312e8323bd7ce27be278b7af98b3/pytorch_lightning-1.3.2-py3-none-any.whl (805kB)\n","\u001b[K     |████████████████████████████████| 808kB 7.4MB/s \n","\u001b[?25hCollecting fsspec[http]>=2021.4.0\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/bc/52/816d1a3a599176057bf29dfacb1f8fadb61d35fbd96cb1bab4aaa7df83c0/fsspec-2021.5.0-py3-none-any.whl (111kB)\n","\u001b[K     |████████████████████████████████| 112kB 22.1MB/s \n","\u001b[?25hCollecting future>=0.17.1\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/45/0b/38b06fd9b92dc2b68d58b75f900e97884c45bedd2ff83203d933cf5851c9/future-0.18.2.tar.gz (829kB)\n","\u001b[K     |████████████████████████████████| 829kB 15.6MB/s \n","\u001b[?25hCollecting torchmetrics>=0.2.0\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/3b/e8/513cd9d0b1c83dc14cd8f788d05cd6a34758d4fd7e4f9e5ecd5d7d599c95/torchmetrics-0.3.2-py3-none-any.whl (274kB)\n","\u001b[K     |████████████████████████████████| 276kB 34.0MB/s \n","\u001b[?25hRequirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from pytorch-lightning) (20.9)\n","Requirement already satisfied: tqdm>=4.41.0 in /usr/local/lib/python3.7/dist-packages (from pytorch-lightning) (4.60.0)\n","Requirement already satisfied: numpy>=1.17.2 in /usr/local/lib/python3.7/dist-packages (from pytorch-lightning) (1.19.5)\n","Collecting PyYAML<=5.4.1,>=5.1\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/7a/a5/393c087efdc78091afa2af9f1378762f9821c9c1d7a22c5753fb5ac5f97a/PyYAML-5.4.1-cp37-cp37m-manylinux1_x86_64.whl (636kB)\n","\u001b[K     |████████████████████████████████| 645kB 34.0MB/s \n","\u001b[?25hCollecting pyDeprecate==0.3.0\n","  Downloading https://files.pythonhosted.org/packages/14/52/aa227a0884df71ed1957649085adf2b8bc2a1816d037c2f18b3078854516/pyDeprecate-0.3.0-py3-none-any.whl\n","Requirement already satisfied: tensorboard!=2.5.0,>=2.2.0 in /usr/local/lib/python3.7/dist-packages (from pytorch-lightning) (2.4.1)\n","Requirement already satisfied: torch>=1.4 in /usr/local/lib/python3.7/dist-packages (from pytorch-lightning) (1.8.1+cu101)\n","Requirement already satisfied: requests; extra == \"http\" in /usr/local/lib/python3.7/dist-packages (from fsspec[http]>=2021.4.0->pytorch-lightning) (2.25.1)\n","Collecting aiohttp; extra == \"http\"\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/88/c0/5890b4c8b04a79b7360e8fe4490feb0bb3ab179743f199f0e6220cebd568/aiohttp-3.7.4.post0-cp37-cp37m-manylinux2014_x86_64.whl (1.3MB)\n","\u001b[K     |████████████████████████████████| 1.3MB 30.0MB/s \n","\u001b[?25hRequirement already satisfied: pyparsing>=2.0.2 in /usr/local/lib/python3.7/dist-packages (from packaging->pytorch-lightning) (2.4.7)\n","Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (3.12.4)\n","Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (1.30.0)\n","Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (56.1.0)\n","Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (1.15.0)\n","Requirement already satisfied: wheel>=0.26; python_version >= \"3\" in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (0.36.2)\n","Requirement already satisfied: absl-py>=0.4 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (0.12.0)\n","Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (1.8.0)\n","Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (0.4.4)\n","Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (2.0.0)\n","Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (3.3.4)\n","Requirement already satisfied: grpcio>=1.24.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (1.32.0)\n","Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch>=1.4->pytorch-lightning) (3.7.4.3)\n","Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests; extra == \"http\"->fsspec[http]>=2021.4.0->pytorch-lightning) (1.24.3)\n","Requirement already satisfied: chardet<5,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests; extra == \"http\"->fsspec[http]>=2021.4.0->pytorch-lightning) (3.0.4)\n","Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests; extra == \"http\"->fsspec[http]>=2021.4.0->pytorch-lightning) (2020.12.5)\n","Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests; extra == \"http\"->fsspec[http]>=2021.4.0->pytorch-lightning) (2.10)\n","Collecting yarl<2.0,>=1.0\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/f1/62/046834c5fc998c88ab2ef722f5d42122230a632212c8afa76418324f53ff/yarl-1.6.3-cp37-cp37m-manylinux2014_x86_64.whl (294kB)\n","\u001b[K     |████████████████████████████████| 296kB 49.4MB/s \n","\u001b[?25hCollecting multidict<7.0,>=4.5\n","\u001b[?25l  Downloading https://files.pythonhosted.org/packages/7c/a6/4123b8165acbe773d1a8dc8e3f0d1edea16d29f7de018eda769abb56bd30/multidict-5.1.0-cp37-cp37m-manylinux2014_x86_64.whl (142kB)\n","\u001b[K     |████████████████████████████████| 143kB 50.0MB/s \n","\u001b[?25hCollecting async-timeout<4.0,>=3.0\n","  Downloading https://files.pythonhosted.org/packages/e1/1e/5a4441be21b0726c4464f3f23c8b19628372f606755a9d2e46c187e65ec4/async_timeout-3.0.1-py3-none-any.whl\n","Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.7/dist-packages (from aiohttp; extra == \"http\"->fsspec[http]>=2021.4.0->pytorch-lightning) (21.2.0)\n","Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (0.2.8)\n","Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (4.2.2)\n","Requirement already satisfied: rsa<5,>=3.1.4; python_version >= \"3.6\" in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (4.7.2)\n","Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (1.3.0)\n","Requirement already satisfied: importlib-metadata; python_version < \"3.8\" in /usr/local/lib/python3.7/dist-packages (from markdown>=2.6.8->tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (4.0.1)\n","Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.7/dist-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (0.4.8)\n","Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (3.1.0)\n","Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata; python_version < \"3.8\"->markdown>=2.6.8->tensorboard!=2.5.0,>=2.2.0->pytorch-lightning) (3.4.1)\n","Building wheels for collected packages: future\n","  Building wheel for future (setup.py) ... \u001b[?25l\u001b[?25hdone\n","  Created wheel for future: filename=future-0.18.2-cp37-none-any.whl size=491058 sha256=8a953afb063fb2d71728407135f58c701bd3cbb8be7d8065c87f00ff91612870\n","  Stored in directory: /root/.cache/pip/wheels/8b/99/a0/81daf51dcd359a9377b110a8a886b3895921802d2fc1b2397e\n","Successfully built future\n","Installing collected packages: multidict, yarl, async-timeout, aiohttp, fsspec, future, torchmetrics, PyYAML, pyDeprecate, pytorch-lightning\n","  Found existing installation: future 0.16.0\n","    Uninstalling future-0.16.0:\n","      Successfully uninstalled future-0.16.0\n","  Found existing installation: PyYAML 3.13\n","    Uninstalling PyYAML-3.13:\n","      Successfully uninstalled PyYAML-3.13\n","Successfully installed PyYAML-5.4.1 aiohttp-3.7.4.post0 async-timeout-3.0.1 fsspec-2021.5.0 future-0.18.2 multidict-5.1.0 pyDeprecate-0.3.0 pytorch-lightning-1.3.2 torchmetrics-0.3.2 yarl-1.6.3\n"],"name":"stdout"},{"output_type":"display_data","data":{"application/vnd.colab-display-data+json":{"pip_warning":{"packages":["yaml"]}}},"metadata":{"tags":[]}}]},{"cell_type":"code","metadata":{"id":"Z_3HX78ZE1NP","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1621770896987,"user_tz":-120,"elapsed":886,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"45b5ba98-a73d-4618-db90-f4e81be5b45f"},"source":["API_KEY = '' #Please enter your API Key from [https://www.aicrowd.com/participants/me]\n","!aicrowd login --api-key $API_KEY"],"execution_count":null,"outputs":[{"output_type":"stream","text":["\u001b[32mAPI Key valid\u001b[0m\n","\u001b[32mSaved API Key successfully!\u001b[0m\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"dYxGozTYFBYt","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1621770932370,"user_tz":-120,"elapsed":29242,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"2c4b346b-b139-4c06-aace-70ae522c1182"},"source":["!aicrowd dataset download --challenge f1-speed-recognition"],"execution_count":null,"outputs":[{"output_type":"stream","text":["sample_submission.csv: 100% 97.8k/97.8k [00:00<00:00, 2.78MB/s]\n","test.zip: 100% 96.9M/96.9M [00:12<00:00, 7.65MB/s]\n","train.csv: 100% 407k/407k [00:00<00:00, 4.91MB/s]\n","train.zip: 100% 385M/385M [00:04<00:00, 94.0MB/s]\n","val.csv: 100% 36.7k/36.7k [00:00<00:00, 2.16MB/s]\n","val.zip: 100% 37.8M/37.8M [00:03<00:00, 11.5MB/s]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"m0HBiT0cEtRn"},"source":["!rm -rf data\n","!mkdir data\n","\n","!unzip -q train.zip  -d data/train\n","!unzip -q val.zip -d data/val\n","!unzip -q test.zip  -d data/test\n","\n","!mv train.csv data/train.csv\n","!mv val.csv data/val.csv\n","!mv sample_submission.csv data/sample_submission.csv"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"ShEbZ0j0GFZe"},"source":["import pandas as pd\n","import torch\n","from torch.utils.data import Dataset\n","import cv2\n","import numpy as np\n","\n","class ImgDataset(Dataset):\n","    def __init__(self,df,mode,transforms = None):\n","        self.imageID = df['ImageID']\n","        self.labels = df['label']\n","        self.transforms = transforms\n","        self.mode = mode\n","        \n","    def __getitem__(self,x):\n","        path = self.imageID.iloc[x]\n","        label = np.array([i for i in str(self.labels.iloc[x])]).astype(int)\n","        label = np.concatenate((label,np.zeros(23-len(label))+10))\n","        label = [np.eye(11)[int(i)] for i in label]\n","        if self.mode == 'train':\n","            i = cv2.imread(f'data/'+str(path)+'.jpg')[64+32:128+32,64+20:192-20]\n","        else:\n","            \n","            i = cv2.imread(f'data/{self.mode}/'+str(path)+'.jpg')[64+32:128+32,64+20:192-20]\n","        \n","        i = cv2.cvtColor(i, cv2.COLOR_BGR2RGB)\n","        if self.transforms:\n","            i = self.transforms(image = i)['image']\n","            \n","        i = torch.tensor(i) / 255.0\n","        i = i.permute(2,0,1)\n","        if self.mode != 'test':\n","            return i, torch.Tensor(label), torch.Tensor([float(self.labels.iloc[x])])\n","        else:\n","            return i\n","    \n","    def __len__(self):\n","        return len(self.imageID)\n","    \n","def getTrainDs(train_tr = None):\n","    train_df = pd.read_csv('data/trainval.csv')\n","    return ImgDataset(train_df,'train',train_tr)\n","\n","def getValDs(val_tr):\n","    val_df = pd.read_csv('data/val.csv')\n","    return ImgDataset(val_df,'val',val_tr)\n","\n","def getTestDs(test_tr):\n","    val_df = pd.read_csv('data/sample_submission.csv')\n","    return ImgDataset(val_df,'test',test_tr)\n","\n","def writeSub(p):\n","    test_df = pd.read_csv('data/sample_submission.csv')\n","    "],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"FZ_iYX2WHA-F"},"source":["import torch.nn as nn\n","import torch.nn.functional as F\n","\n","\n","class VGG_FeatureExtractor(nn.Module):\n","    \"\"\" FeatureExtractor of CRNN (https://arxiv.org/pdf/1507.05717.pdf) \"\"\"\n","\n","    def __init__(self, input_channel, output_channel=512):\n","        super(VGG_FeatureExtractor, self).__init__()\n","        self.output_channel = [int(output_channel / 8), int(output_channel / 4),\n","                               int(output_channel / 2), output_channel]  # [64, 128, 256, 512]\n","        self.ConvNet = nn.Sequential(\n","            nn.Conv2d(input_channel, self.output_channel[0], 3, 1, 1), nn.ReLU(True),\n","            nn.MaxPool2d(2, 2),  # 64x16x50\n","            nn.Conv2d(self.output_channel[0], self.output_channel[1], 3, 1, 1), nn.ReLU(True),\n","            nn.MaxPool2d(2, 2),  # 128x8x25\n","            nn.Conv2d(self.output_channel[1], self.output_channel[2], 3, 1, 1), nn.ReLU(True),  # 256x8x25\n","            nn.Conv2d(self.output_channel[2], self.output_channel[2], 3, 1, 1), nn.ReLU(True),\n","            nn.MaxPool2d((2, 1), (2, 1)),  # 256x4x25\n","            nn.Conv2d(self.output_channel[2], self.output_channel[3], 3, 1, 1, bias=False),\n","            nn.BatchNorm2d(self.output_channel[3]), nn.ReLU(True),  # 512x4x25\n","            nn.Conv2d(self.output_channel[3], self.output_channel[3], 3, 1, 1, bias=False),\n","            nn.BatchNorm2d(self.output_channel[3]), nn.ReLU(True),\n","            nn.MaxPool2d((2, 1), (2, 1)),  # 512x2x25\n","            nn.Conv2d(self.output_channel[3], self.output_channel[3], 2, 1, 0), nn.ReLU(True))  # 512x1x24\n","\n","    def forward(self, input):\n","        return self.ConvNet(input)\n","\n","\n","class RCNN_FeatureExtractor(nn.Module):\n","    \"\"\" FeatureExtractor of GRCNN (https://papers.nips.cc/paper/6637-gated-recurrent-convolution-neural-network-for-ocr.pdf) \"\"\"\n","\n","    def __init__(self, input_channel, output_channel=512):\n","        super(RCNN_FeatureExtractor, self).__init__()\n","        self.output_channel = [int(output_channel / 8), int(output_channel / 4),\n","                               int(output_channel / 2), output_channel]  # [64, 128, 256, 512]\n","        self.ConvNet = nn.Sequential(\n","            nn.Conv2d(input_channel, self.output_channel[0], 3, 1, 1), nn.ReLU(True),\n","            nn.MaxPool2d(2, 2),  # 64 x 16 x 50\n","            GRCL(self.output_channel[0], self.output_channel[0], num_iteration=5, kernel_size=3, pad=1),\n","            nn.MaxPool2d(2, 2),  # 64 x 8 x 25\n","            GRCL(self.output_channel[0], self.output_channel[1], num_iteration=5, kernel_size=3, pad=1),\n","            nn.MaxPool2d(2, (2, 1), (0, 1)),  # 128 x 4 x 26\n","            GRCL(self.output_channel[1], self.output_channel[2], num_iteration=5, kernel_size=3, pad=1),\n","            nn.MaxPool2d(2, (2, 1), (0, 1)),  # 256 x 2 x 27\n","            nn.Conv2d(self.output_channel[2], self.output_channel[3], 2, 1, 0, bias=False),\n","            nn.BatchNorm2d(self.output_channel[3]), nn.ReLU(True))  # 512 x 1 x 26\n","\n","    def forward(self, input):\n","        return self.ConvNet(input)\n","\n","\n","class ResNet_FeatureExtractor(nn.Module):\n","    \"\"\" FeatureExtractor of FAN (http://openaccess.thecvf.com/content_ICCV_2017/papers/Cheng_Focusing_Attention_Towards_ICCV_2017_paper.pdf) \"\"\"\n","\n","    def __init__(self, input_channel, output_channel=512):\n","        super(ResNet_FeatureExtractor, self).__init__()\n","        self.ConvNet = ResNet(input_channel, output_channel, BasicBlock, [1, 2, 5, 3])\n","\n","    def forward(self, input):\n","        return self.ConvNet(input)\n","\n","\n","# For Gated RCNN\n","class GRCL(nn.Module):\n","\n","    def __init__(self, input_channel, output_channel, num_iteration, kernel_size, pad):\n","        super(GRCL, self).__init__()\n","        self.wgf_u = nn.Conv2d(input_channel, output_channel, 1, 1, 0, bias=False)\n","        self.wgr_x = nn.Conv2d(output_channel, output_channel, 1, 1, 0, bias=False)\n","        self.wf_u = nn.Conv2d(input_channel, output_channel, kernel_size, 1, pad, bias=False)\n","        self.wr_x = nn.Conv2d(output_channel, output_channel, kernel_size, 1, pad, bias=False)\n","\n","        self.BN_x_init = nn.BatchNorm2d(output_channel)\n","\n","        self.num_iteration = num_iteration\n","        self.GRCL = [GRCL_unit(output_channel) for _ in range(num_iteration)]\n","        self.GRCL = nn.Sequential(*self.GRCL)\n","\n","    def forward(self, input):\n","        \"\"\" The input of GRCL is consistant over time t, which is denoted by u(0)\n","        thus wgf_u / wf_u is also consistant over time t.\n","        \"\"\"\n","        wgf_u = self.wgf_u(input)\n","        wf_u = self.wf_u(input)\n","        x = F.relu(self.BN_x_init(wf_u))\n","\n","        for i in range(self.num_iteration):\n","            x = self.GRCL[i](wgf_u, self.wgr_x(x), wf_u, self.wr_x(x))\n","\n","        return x\n","\n","\n","class GRCL_unit(nn.Module):\n","\n","    def __init__(self, output_channel):\n","        super(GRCL_unit, self).__init__()\n","        self.BN_gfu = nn.BatchNorm2d(output_channel)\n","        self.BN_grx = nn.BatchNorm2d(output_channel)\n","        self.BN_fu = nn.BatchNorm2d(output_channel)\n","        self.BN_rx = nn.BatchNorm2d(output_channel)\n","        self.BN_Gx = nn.BatchNorm2d(output_channel)\n","\n","    def forward(self, wgf_u, wgr_x, wf_u, wr_x):\n","        G_first_term = self.BN_gfu(wgf_u)\n","        G_second_term = self.BN_grx(wgr_x)\n","        G = F.sigmoid(G_first_term + G_second_term)\n","\n","        x_first_term = self.BN_fu(wf_u)\n","        x_second_term = self.BN_Gx(self.BN_rx(wr_x) * G)\n","        x = F.relu(x_first_term + x_second_term)\n","\n","        return x\n","\n","\n","class BasicBlock(nn.Module):\n","    expansion = 1\n","\n","    def __init__(self, inplanes, planes, stride=1, downsample=None):\n","        super(BasicBlock, self).__init__()\n","        self.conv1 = self._conv3x3(inplanes, planes)\n","        self.bn1 = nn.BatchNorm2d(planes)\n","        self.conv2 = self._conv3x3(planes, planes)\n","        self.bn2 = nn.BatchNorm2d(planes)\n","        self.relu = nn.ReLU(inplace=True)\n","        self.downsample = downsample\n","        self.stride = stride\n","\n","    def _conv3x3(self, in_planes, out_planes, stride=1):\n","        \"3x3 convolution with padding\"\n","        return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,\n","                         padding=1, bias=False)\n","\n","    def forward(self, x):\n","        residual = x\n","\n","        out = self.conv1(x)\n","        out = self.bn1(out)\n","        out = self.relu(out)\n","\n","        out = self.conv2(out)\n","        out = self.bn2(out)\n","\n","        if self.downsample is not None:\n","            residual = self.downsample(x)\n","        out += residual\n","        out = self.relu(out)\n","\n","        return out\n","\n","\n","class ResNet(nn.Module):\n","\n","    def __init__(self, input_channel, output_channel, block, layers):\n","        super(ResNet, self).__init__()\n","\n","        self.output_channel_block = [int(output_channel / 4), int(output_channel / 2), output_channel, output_channel]\n","\n","        self.inplanes = int(output_channel / 8)\n","        self.conv0_1 = nn.Conv2d(input_channel, int(output_channel / 16),\n","                                 kernel_size=3, stride=1, padding=1, bias=False)\n","        self.bn0_1 = nn.BatchNorm2d(int(output_channel / 16))\n","        self.conv0_2 = nn.Conv2d(int(output_channel / 16), self.inplanes,\n","                                 kernel_size=3, stride=1, padding=1, bias=False)\n","        self.bn0_2 = nn.BatchNorm2d(self.inplanes)\n","        self.relu = nn.ReLU(inplace=True)\n","\n","        self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)\n","        self.layer1 = self._make_layer(block, self.output_channel_block[0], layers[0])\n","        self.conv1 = nn.Conv2d(self.output_channel_block[0], self.output_channel_block[\n","                               0], kernel_size=3, stride=1, padding=1, bias=False)\n","        self.bn1 = nn.BatchNorm2d(self.output_channel_block[0])\n","\n","        self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)\n","        self.layer2 = self._make_layer(block, self.output_channel_block[1], layers[1], stride=1)\n","        self.conv2 = nn.Conv2d(self.output_channel_block[1], self.output_channel_block[\n","                               1], kernel_size=3, stride=1, padding=1, bias=False)\n","        self.bn2 = nn.BatchNorm2d(self.output_channel_block[1])\n","\n","        self.maxpool3 = nn.MaxPool2d(kernel_size=2, stride=(2, 1), padding=(0, 1))\n","        self.layer3 = self._make_layer(block, self.output_channel_block[2], layers[2], stride=1)\n","        self.conv3 = nn.Conv2d(self.output_channel_block[2], self.output_channel_block[\n","                               2], kernel_size=3, stride=1, padding=1, bias=False)\n","        self.bn3 = nn.BatchNorm2d(self.output_channel_block[2])\n","\n","        self.layer4 = self._make_layer(block, self.output_channel_block[3], layers[3], stride=1)\n","        self.conv4_1 = nn.Conv2d(self.output_channel_block[3], self.output_channel_block[\n","                                 3], kernel_size=2, stride=(2, 1), padding=(0, 1), bias=False)\n","        self.bn4_1 = nn.BatchNorm2d(self.output_channel_block[3])\n","        self.conv4_2 = nn.Conv2d(self.output_channel_block[3], self.output_channel_block[\n","                                 3], kernel_size=2, stride=1, padding=0, bias=False)\n","        self.bn4_2 = nn.BatchNorm2d(self.output_channel_block[3])\n","\n","    def _make_layer(self, block, planes, blocks, stride=1):\n","        downsample = None\n","        if stride != 1 or self.inplanes != planes * block.expansion:\n","            downsample = nn.Sequential(\n","                nn.Conv2d(self.inplanes, planes * block.expansion,\n","                          kernel_size=1, stride=stride, bias=False),\n","                nn.BatchNorm2d(planes * block.expansion),\n","            )\n","\n","        layers = []\n","        layers.append(block(self.inplanes, planes, stride, downsample))\n","        self.inplanes = planes * block.expansion\n","        for i in range(1, blocks):\n","            layers.append(block(self.inplanes, planes))\n","\n","        return nn.Sequential(*layers)\n","\n","    def forward(self, x):\n","        x = self.conv0_1(x)\n","        x = self.bn0_1(x)\n","        x = self.relu(x)\n","        x = self.conv0_2(x)\n","        x = self.bn0_2(x)\n","        x = self.relu(x)\n","\n","        x = self.maxpool1(x)\n","        x = self.layer1(x)\n","        x = self.conv1(x)\n","        x = self.bn1(x)\n","        x = self.relu(x)\n","\n","        x = self.maxpool2(x)\n","        x = self.layer2(x)\n","        x = self.conv2(x)\n","        x = self.bn2(x)\n","        x = self.relu(x)\n","\n","        x = self.maxpool3(x)\n","        x = self.layer3(x)\n","        x = self.conv3(x)\n","        x = self.bn3(x)\n","        x = self.relu(x)\n","\n","        x = self.layer4(x)\n","        x = self.conv4_1(x)\n","        x = self.bn4_1(x)\n","        x = self.relu(x)\n","        x = self.conv4_2(x)\n","        x = self.bn4_2(x)\n","        x = self.relu(x)\n","\n","        return x\n","import torch\n","import torch.nn as nn\n","import torch.nn.functional as F\n","device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n","\n","\n","class Attention(nn.Module):\n","\n","    def __init__(self, input_size, hidden_size, num_classes):\n","        super(Attention, self).__init__()\n","        self.attention_cell = AttentionCell(input_size, hidden_size, num_classes)\n","        self.hidden_size = hidden_size\n","        self.num_classes = num_classes\n","        self.generator = nn.Linear(hidden_size, num_classes)\n","\n","    def _char_to_onehot(self, input_char, onehot_dim=38):\n","        input_char = input_char.unsqueeze(1)\n","        batch_size = input_char.size(0)\n","        one_hot = torch.FloatTensor(batch_size, onehot_dim).zero_().to(device)\n","        one_hot = one_hot.scatter_(1, input_char, 1)\n","        return one_hot\n","\n","    def forward(self, batch_H, text, is_train=True, batch_max_length=25):\n","        \"\"\"\n","        input:\n","            batch_H : contextual_feature H = hidden state of encoder. [batch_size x num_steps x contextual_feature_channels]\n","            text : the text-index of each image. [batch_size x (max_length+1)]. +1 for [GO] token. text[:, 0] = [GO].\n","        output: probability distribution at each step [batch_size x num_steps x num_classes]\n","        \"\"\"\n","        batch_size = batch_H.size(0)\n","        num_steps = batch_max_length + 1  # +1 for [s] at end of sentence.\n","\n","        output_hiddens = torch.FloatTensor(batch_size, num_steps, self.hidden_size).fill_(0).to(device)\n","        hidden = (torch.FloatTensor(batch_size, self.hidden_size).fill_(0).to(device),\n","                  torch.FloatTensor(batch_size, self.hidden_size).fill_(0).to(device))\n","\n","        if is_train:\n","            for i in range(num_steps):\n","                # one-hot vectors for a i-th char. in a batch\n","                char_onehots = self._char_to_onehot(text[:, i], onehot_dim=self.num_classes)\n","                # hidden : decoder's hidden s_{t-1}, batch_H : encoder's hidden H, char_onehots : one-hot(y_{t-1})\n","                hidden, alpha = self.attention_cell(hidden, batch_H, char_onehots)\n","                output_hiddens[:, i, :] = hidden[0]  # LSTM hidden index (0: hidden, 1: Cell)\n","            probs = self.generator(output_hiddens)\n","\n","        else:\n","            targets = torch.LongTensor(batch_size).fill_(0).to(device)  # [GO] token\n","            probs = torch.FloatTensor(batch_size, num_steps, self.num_classes).fill_(0).to(device)\n","\n","            for i in range(num_steps):\n","                char_onehots = self._char_to_onehot(targets, onehot_dim=self.num_classes)\n","                hidden, alpha = self.attention_cell(hidden, batch_H, char_onehots)\n","                probs_step = self.generator(hidden[0])\n","                probs[:, i, :] = probs_step\n","                _, next_input = probs_step.max(1)\n","                targets = next_input\n","\n","        return probs  # batch_size x num_steps x num_classes\n","\n","\n","class AttentionCell(nn.Module):\n","\n","    def __init__(self, input_size, hidden_size, num_embeddings):\n","        super(AttentionCell, self).__init__()\n","        self.i2h = nn.Linear(input_size, hidden_size, bias=False)\n","        self.h2h = nn.Linear(hidden_size, hidden_size)  # either i2i or h2h should have bias\n","        self.score = nn.Linear(hidden_size, 1, bias=False)\n","        self.rnn = nn.LSTMCell(input_size + num_embeddings, hidden_size)\n","        self.hidden_size = hidden_size\n","\n","    def forward(self, prev_hidden, batch_H, char_onehots):\n","        # [batch_size x num_encoder_step x num_channel] -> [batch_size x num_encoder_step x hidden_size]\n","        batch_H_proj = self.i2h(batch_H)\n","        prev_hidden_proj = self.h2h(prev_hidden[0]).unsqueeze(1)\n","        e = self.score(torch.tanh(batch_H_proj + prev_hidden_proj))  # batch_size x num_encoder_step * 1\n","\n","        alpha = F.softmax(e, dim=1)\n","        context = torch.bmm(alpha.permute(0, 2, 1), batch_H).squeeze(1)  # batch_size x num_channel\n","        concat_context = torch.cat([context, char_onehots], 1)  # batch_size x (num_channel + num_embedding)\n","        cur_hidden = self.rnn(concat_context, prev_hidden)\n","        return cur_hidden, alpha\n","import torch.nn as nn\n","\n","\n","class BidirectionalLSTM(nn.Module):\n","\n","    def __init__(self, input_size, hidden_size, output_size):\n","        super(BidirectionalLSTM, self).__init__()\n","        self.rnn = nn.LSTM(input_size, hidden_size, bidirectional=True, batch_first=True)\n","        self.linear = nn.Linear(hidden_size * 2, output_size)\n","\n","    def forward(self, input):\n","        \"\"\"\n","        input : visual feature [batch_size x T x input_size]\n","        output : contextual feature [batch_size x T x output_size]\n","        \"\"\"\n","        self.rnn.flatten_parameters()\n","        recurrent, _ = self.rnn(input)  # batch_size x T x input_size -> batch_size x T x (2*hidden_size)\n","        output = self.linear(recurrent)  # batch_size x T x output_size\n","        return output\n","import numpy as np\n","import torch\n","import torch.nn as nn\n","import torch.nn.functional as F\n","device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n","\n","\n","class TPS_SpatialTransformerNetwork(nn.Module):\n","    \"\"\" Rectification Network of RARE, namely TPS based STN \"\"\"\n","\n","    def __init__(self, F, I_size, I_r_size, I_channel_num=1):\n","        \"\"\" Based on RARE TPS\n","        input:\n","            batch_I: Batch Input Image [batch_size x I_channel_num x I_height x I_width]\n","            I_size : (height, width) of the input image I\n","            I_r_size : (height, width) of the rectified image I_r\n","            I_channel_num : the number of channels of the input image I\n","        output:\n","            batch_I_r: rectified image [batch_size x I_channel_num x I_r_height x I_r_width]\n","        \"\"\"\n","        super(TPS_SpatialTransformerNetwork, self).__init__()\n","        self.F = F\n","        self.I_size = I_size\n","        self.I_r_size = I_r_size  # = (I_r_height, I_r_width)\n","        self.I_channel_num = I_channel_num\n","        self.LocalizationNetwork = LocalizationNetwork(self.F, self.I_channel_num)\n","        self.GridGenerator = GridGenerator(self.F, self.I_r_size)\n","\n","    def forward(self, batch_I):\n","        batch_C_prime = self.LocalizationNetwork(batch_I)  # batch_size x K x 2\n","        build_P_prime = self.GridGenerator.build_P_prime(batch_C_prime)  # batch_size x n (= I_r_width x I_r_height) x 2\n","        build_P_prime_reshape = build_P_prime.reshape([build_P_prime.size(0), self.I_r_size[0], self.I_r_size[1], 2])\n","        \n","        if torch.__version__ > \"1.2.0\":\n","            batch_I_r = F.grid_sample(batch_I, build_P_prime_reshape, padding_mode='border', align_corners=True)\n","        else:\n","            batch_I_r = F.grid_sample(batch_I, build_P_prime_reshape, padding_mode='border')\n","\n","        return batch_I_r\n","\n","\n","class LocalizationNetwork(nn.Module):\n","    \"\"\" Localization Network of RARE, which predicts C' (K x 2) from I (I_width x I_height) \"\"\"\n","\n","    def __init__(self, F, I_channel_num):\n","        super(LocalizationNetwork, self).__init__()\n","        self.F = F\n","        self.I_channel_num = I_channel_num\n","        self.conv = nn.Sequential(\n","            nn.Conv2d(in_channels=self.I_channel_num, out_channels=64, kernel_size=3, stride=1, padding=1,\n","                      bias=False), nn.BatchNorm2d(64), nn.ReLU(True),\n","            nn.MaxPool2d(2, 2),  # batch_size x 64 x I_height/2 x I_width/2\n","            nn.Conv2d(64, 128, 3, 1, 1, bias=False), nn.BatchNorm2d(128), nn.ReLU(True),\n","            nn.MaxPool2d(2, 2),  # batch_size x 128 x I_height/4 x I_width/4\n","            nn.Conv2d(128, 256, 3, 1, 1, bias=False), nn.BatchNorm2d(256), nn.ReLU(True),\n","            nn.MaxPool2d(2, 2),  # batch_size x 256 x I_height/8 x I_width/8\n","            nn.Conv2d(256, 512, 3, 1, 1, bias=False), nn.BatchNorm2d(512), nn.ReLU(True),\n","            nn.AdaptiveAvgPool2d(1)  # batch_size x 512\n","        )\n","\n","        self.localization_fc1 = nn.Sequential(nn.Linear(512, 256), nn.ReLU(True))\n","        self.localization_fc2 = nn.Linear(256, self.F * 2)\n","\n","        # Init fc2 in LocalizationNetwork\n","        self.localization_fc2.weight.data.fill_(0)\n","        \"\"\" see RARE paper Fig. 6 (a) \"\"\"\n","        ctrl_pts_x = np.linspace(-1.0, 1.0, int(F / 2))\n","        ctrl_pts_y_top = np.linspace(0.0, -1.0, num=int(F / 2))\n","        ctrl_pts_y_bottom = np.linspace(1.0, 0.0, num=int(F / 2))\n","        ctrl_pts_top = np.stack([ctrl_pts_x, ctrl_pts_y_top], axis=1)\n","        ctrl_pts_bottom = np.stack([ctrl_pts_x, ctrl_pts_y_bottom], axis=1)\n","        initial_bias = np.concatenate([ctrl_pts_top, ctrl_pts_bottom], axis=0)\n","        self.localization_fc2.bias.data = torch.from_numpy(initial_bias).float().view(-1)\n","\n","    def forward(self, batch_I):\n","        \"\"\"\n","        input:     batch_I : Batch Input Image [batch_size x I_channel_num x I_height x I_width]\n","        output:    batch_C_prime : Predicted coordinates of fiducial points for input batch [batch_size x F x 2]\n","        \"\"\"\n","        batch_size = batch_I.size(0)\n","        features = self.conv(batch_I).view(batch_size, -1)\n","        batch_C_prime = self.localization_fc2(self.localization_fc1(features)).view(batch_size, self.F, 2)\n","        return batch_C_prime\n","\n","\n","class GridGenerator(nn.Module):\n","    \"\"\" Grid Generator of RARE, which produces P_prime by multipling T with P \"\"\"\n","\n","    def __init__(self, F, I_r_size):\n","        \"\"\" Generate P_hat and inv_delta_C for later \"\"\"\n","        super(GridGenerator, self).__init__()\n","        self.eps = 1e-6\n","        self.I_r_height, self.I_r_width = I_r_size\n","        self.F = F\n","        self.C = self._build_C(self.F)  # F x 2\n","        self.P = self._build_P(self.I_r_width, self.I_r_height)\n","        ## for multi-gpu, you need register buffer\n","        self.register_buffer(\"inv_delta_C\", torch.tensor(self._build_inv_delta_C(self.F, self.C)).float())  # F+3 x F+3\n","        self.register_buffer(\"P_hat\", torch.tensor(self._build_P_hat(self.F, self.C, self.P)).float())  # n x F+3\n","        ## for fine-tuning with different image width, you may use below instead of self.register_buffer\n","        #self.inv_delta_C = torch.tensor(self._build_inv_delta_C(self.F, self.C)).float().cuda()  # F+3 x F+3\n","        #self.P_hat = torch.tensor(self._build_P_hat(self.F, self.C, self.P)).float().cuda()  # n x F+3\n","\n","    def _build_C(self, F):\n","        \"\"\" Return coordinates of fiducial points in I_r; C \"\"\"\n","        ctrl_pts_x = np.linspace(-1.0, 1.0, int(F / 2))\n","        ctrl_pts_y_top = -1 * np.ones(int(F / 2))\n","        ctrl_pts_y_bottom = np.ones(int(F / 2))\n","        ctrl_pts_top = np.stack([ctrl_pts_x, ctrl_pts_y_top], axis=1)\n","        ctrl_pts_bottom = np.stack([ctrl_pts_x, ctrl_pts_y_bottom], axis=1)\n","        C = np.concatenate([ctrl_pts_top, ctrl_pts_bottom], axis=0)\n","        return C  # F x 2\n","\n","    def _build_inv_delta_C(self, F, C):\n","        \"\"\" Return inv_delta_C which is needed to calculate T \"\"\"\n","        hat_C = np.zeros((F, F), dtype=float)  # F x F\n","        for i in range(0, F):\n","            for j in range(i, F):\n","                r = np.linalg.norm(C[i] - C[j])\n","                hat_C[i, j] = r\n","                hat_C[j, i] = r\n","        np.fill_diagonal(hat_C, 1)\n","        hat_C = (hat_C ** 2) * np.log(hat_C)\n","        # print(C.shape, hat_C.shape)\n","        delta_C = np.concatenate(  # F+3 x F+3\n","            [\n","                np.concatenate([np.ones((F, 1)), C, hat_C], axis=1),  # F x F+3\n","                np.concatenate([np.zeros((2, 3)), np.transpose(C)], axis=1),  # 2 x F+3\n","                np.concatenate([np.zeros((1, 3)), np.ones((1, F))], axis=1)  # 1 x F+3\n","            ],\n","            axis=0\n","        )\n","        inv_delta_C = np.linalg.inv(delta_C)\n","        return inv_delta_C  # F+3 x F+3\n","\n","    def _build_P(self, I_r_width, I_r_height):\n","        I_r_grid_x = (np.arange(-I_r_width, I_r_width, 2) + 1.0) / I_r_width  # self.I_r_width\n","        I_r_grid_y = (np.arange(-I_r_height, I_r_height, 2) + 1.0) / I_r_height  # self.I_r_height\n","        P = np.stack(  # self.I_r_width x self.I_r_height x 2\n","            np.meshgrid(I_r_grid_x, I_r_grid_y),\n","            axis=2\n","        )\n","        return P.reshape([-1, 2])  # n (= self.I_r_width x self.I_r_height) x 2\n","\n","    def _build_P_hat(self, F, C, P):\n","        n = P.shape[0]  # n (= self.I_r_width x self.I_r_height)\n","        P_tile = np.tile(np.expand_dims(P, axis=1), (1, F, 1))  # n x 2 -> n x 1 x 2 -> n x F x 2\n","        C_tile = np.expand_dims(C, axis=0)  # 1 x F x 2\n","        P_diff = P_tile - C_tile  # n x F x 2\n","        rbf_norm = np.linalg.norm(P_diff, ord=2, axis=2, keepdims=False)  # n x F\n","        rbf = np.multiply(np.square(rbf_norm), np.log(rbf_norm + self.eps))  # n x F\n","        P_hat = np.concatenate([np.ones((n, 1)), P, rbf], axis=1)\n","        return P_hat  # n x F+3\n","\n","    def build_P_prime(self, batch_C_prime):\n","        \"\"\" Generate Grid from batch_C_prime [batch_size x F x 2] \"\"\"\n","        batch_size = batch_C_prime.size(0)\n","        batch_inv_delta_C = self.inv_delta_C.repeat(batch_size, 1, 1)\n","        batch_P_hat = self.P_hat.repeat(batch_size, 1, 1)\n","        batch_C_prime_with_zeros = torch.cat((batch_C_prime, torch.zeros(\n","            batch_size, 3, 2).float().to(device)), dim=1)  # batch_size x F+3 x 2\n","        batch_T = torch.bmm(batch_inv_delta_C, batch_C_prime_with_zeros)  # batch_size x F+3 x 2\n","        batch_P_prime = torch.bmm(batch_P_hat, batch_T)  # batch_size x n x 2\n","        return batch_P_prime  # batch_size x n x 2\n"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"S06o98WHFTSd"},"source":["import os\n","import torch\n","from torch import nn\n","import pandas as pd\n","import torch.nn.functional as F\n","from torch.utils.data import DataLoader\n","import pytorch_lightning as pl\n","\n","# CODE FROM https://github.com/clovaai/deep-text-recognition-benchmark\n","\"\"\"\n","Copyright (c) 2019-present NAVER Corp.\n","Licensed under the Apache License, Version 2.0 (the \"License\");\n","you may not use this file except in compliance with the License.\n","You may obtain a copy of the License at\n","    http://www.apache.org/licenses/LICENSE-2.0\n","Unless required by applicable law or agreed to in writing, software\n","distributed under the License is distributed on an \"AS IS\" BASIS,\n","WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n","See the License for the specific language governing permissions and\n","limitations under the License.\n","\"\"\"\n","\n","\n","import torch.nn as nn\n","\n","class Model(nn.Module):\n","\n","    def __init__(self):\n","        super(Model, self).__init__()\n","\n","        \"\"\" Transformation \"\"\"\n","        \n","        self.Transformation = TPS_SpatialTransformerNetwork(\n","            F=20, I_size=(64, 80), I_r_size=(64, 80), I_channel_num=3)\n","        \n","\n","        \"\"\" FeatureExtraction \"\"\"\n","        self.FeatureExtraction = ResNet_FeatureExtractor(3, 512)\n","        \n","        self.FeatureExtraction_output = 512  # int(imgH/16-1) * 512\n","        self.AdaptiveAvgPool = nn.AdaptiveAvgPool2d((None, 1))  # Transform final (imgH/16-1) -> 1\n","\n","        \"\"\" Sequence modeling\"\"\"\n","        \n","        self.SequenceModeling = nn.Sequential(\n","            BidirectionalLSTM(self.FeatureExtraction_output, 256, 256),\n","            BidirectionalLSTM(256, 256, 256))\n","        self.SequenceModeling_output = 256\n","        \n","\n","        \"\"\" Prediction \"\"\"\n","        \n","        self.Prediction = nn.Linear(self.SequenceModeling_output, 11)\n","\n","    def forward(self, input, is_train=True):\n","\n","        \"\"\" Feature extraction stage \"\"\"\n","        visual_feature = self.FeatureExtraction(input)\n","        visual_feature = self.AdaptiveAvgPool(visual_feature.permute(0, 3, 1, 2))  # [b, c, h, w] -> [b, w, c, h]\n","        visual_feature = visual_feature.squeeze(3)\n","        \n","        contextual_feature = self.SequenceModeling(visual_feature)\n","\n","\n","        \"\"\" Prediction stage \"\"\"\n","        \n","        prediction = self.Prediction(contextual_feature.contiguous())\n","\n","        return prediction.permute(1,0,2)\n","\n","def toNum(t):\n","    output = []\n","    t = t.argmax(2)\n","    for label in t:\n","        try:\n","            l = []\n","            for i in label.tolist():\n","                if i == 10:\n","                    break\n","                l.append(str(i))\n","            output.append(int(''.join(l)))\n","        except:\n","            output.append(0)\n","    return torch.Tensor(output).unsqueeze(1).cuda()\n","    \n","    \n","class Classifier(pl.LightningModule):\n","\n","    def __init__(self,args):\n","        super().__init__()\n","        self.args = args\n","        self.model = Model()\n","        \n","    def forward(self, x):\n","        # in lightning, forward defines the prediction/inference actions\n","        x = self.model(x)\n","        return x.permute(1,0,2)\n","    def training_step(self, batch, batch_idx):\n","        # training_step defined the train loop.\n","        # It is independent of forward\n","        x, y, num = batch\n","        p = self(x)\n","        loss = F.binary_cross_entropy_with_logits(p, y)\n","        # Logging to TensorBoard by default\n","        self.log('train_loss', loss)\n","        return loss\n","    \n","    def validation_step(self, batch, batch_idx):\n","        # training_step defined the train loop.\n","        # It is independent of forward\n","        x, y, num = batch\n","        p = self(x)\n","        loss = F.binary_cross_entropy_with_logits(p, y)\n","        mse = F.mse_loss(toNum(p),num)\n","        # Logging to TensorBoard by default\n","        self.log('val_loss', loss)\n","        self.log('val_mse',mse)\n","        return loss\n","    \n","    def test_step(self, batch, batch_idx):\n","        # training_step defined the train loop.\n","        # It is independent of forward\n","        x, y, num = batch\n","        p = self(x)\n","        \n","        loss = F.binary_cross_entropy_with_logits(p, y)\n","        mse = F.mse_loss(toNum(p),num)\n","        # Logging to TensorBoard by default\n","        self.log('test_loss', loss)\n","        self.log('test_mse',mse)\n","        return loss\n","    \n","    def configure_optimizers(self):\n","        optimizer = torch.optim.Adam(self.parameters(), lr=3e-4)\n","        return optimizer\n","    \n","    def train_dataloader(self):\n","        train_ds = getTrainDs(self.args['train_tr'])\n","        loader= DataLoader(train_ds,batch_size = self.args['batch_size'],num_workers = 4,shuffle=True)\n","        return loader\n","    \n","    def val_dataloader(self):\n","        val_ds = getValDs(self.args['val_tr'])\n","        loader= DataLoader(val_ds,batch_size = self.args['batch_size'],num_workers = 4)\n","        return loader\n","    \n","    def test_dataloader(self):\n","        val_ds = getValDs(self.args['val_tr'])\n","        loader= DataLoader(val_ds,batch_size = self.args['batch_size'],num_workers = 4)\n","        return loader\n","    \n","    def predict_dataloader(self):\n","        val_ds = getTestDs(self.args['val_tr'])\n","        loader= DataLoader(val_ds,batch_size = self.args['batch_size'],num_workers = 4)\n","        return loader\n","\n","\n","def writeSub(p):\n","    test_df = pd.read_csv('data/sample_submission.csv')\n","    output_list = p.int().tolist()\n","    test_df['label'] = output_list\n","    test_df.to_csv('submission.csv',index = False)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"OYx_PZK6EgC4"},"source":["import albumentations as A\n","import matplotlib.pyplot as plt\n","import pandas as pd\n","import torch\n","from torch import nn\n","from torch.nn import functional as F\n","import numpy as np\n","import random, os"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"E7vZkfniEgC7"},"source":["def set_seed(seed: int = 42):\n","    random.seed(seed)\n","    np.random.seed(seed)\n","    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n","    torch.manual_seed(seed)\n","    torch.cuda.manual_seed(seed)  # type: ignore\n","    torch.backends.cudnn.deterministic = True  # type: ignore\n","    torch.backends.cudnn.benchmark = True  # type: ignore\n","    \n","set_seed(0)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"sbzG-AZFEgC-"},"source":["train = pd.read_csv('data/train.csv')\n","val = pd.read_csv('data/val.csv')\n","\n","train.ImageID = [f'train/{i}' for i in train.ImageID]\n","val.ImageID = [f'val/{i}' for i in val.ImageID]\n","trainval = pd.concat((train,val))\n","trainval.to_csv('data/trainval.csv')"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"7zxwv_8hEgC-","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1621771483175,"user_tz":-120,"elapsed":549,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"d2d9db1f-4a6e-49f9-b115-454197e9d866"},"source":["\n","import albumentations as A\n","from albumentations.augmentations.transforms import Flip, Blur, ChannelShuffle\n","import pytorch_lightning as pl\n","from pytorch_lightning import Trainer\n","from pytorch_lightning.callbacks.model_checkpoint import ModelCheckpoint\n","from pytorch_lightning.callbacks.early_stopping import EarlyStopping\n","\n","if __name__ == '__main__':\n","    ckpt = [ModelCheckpoint(monitor = 'val_mse',save_top_k = -1,mode = 'min')\n","           ]\n","    trainer = Trainer(max_epochs = 100,gpus = 1, callbacks = ckpt, precision=16,deterministic=True,fast_dev_run = False)\n","    \n","    train_tr = A.Compose([\n","        \n","    ])\n","    \n","    val_tr = A.Compose([\n","        #A.CenterCrop(128,128),\n","        #A.Resize(32,100)\n","    ])\n","    \n","    model = Classifier({'lr':3e-4,'batch_size':64,'train_tr':train_tr,'val_tr':val_tr})\n","    \n","    #trainer.fit(model) # UNCOMMENT HERE TO TRAIN\n","    #trainer.test(model)"],"execution_count":null,"outputs":[{"output_type":"stream","text":["GPU available: True, used: True\n","TPU available: False, using: 0 TPU cores\n","Using native 16bit precision.\n"],"name":"stderr"}]},{"cell_type":"code","metadata":{"id":"RckhLsqnHzzP","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1621771692610,"user_tz":-120,"elapsed":19539,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"2a8518eb-4120-4847-efe7-6d13e3b25fee"},"source":["!gdown --id 1mwQxonsEpuIngo-XH42ZxgH-c1mOkKRl"],"execution_count":null,"outputs":[{"output_type":"stream","text":["Downloading...\n","From: https://drive.google.com/uc?id=1mwQxonsEpuIngo-XH42ZxgH-c1mOkKRl\n","To: /content/epoch=71-step=49535.ckpt\n","573MB [00:15, 36.6MB/s]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"H1QzZq2HEgDA","colab":{"base_uri":"https://localhost:8080/","height":124,"referenced_widgets":["e697bfdb6d1c481180984038332bd5e3","2777eabbb42c41e996f1f64efb0a2f9d","3b7c172fd82045a980b6fba38dad58ef","80f82e0611704cb0bafd907f015ff46c","5dfc15a57856467c92feffb7f7ccd6f6","562b61a5effa446ca5b94f77c43d66ed","a99f91736e6b47f8b349b6baadc17742","8a5fa7d1298b40009b6eaca328e65aa6","f6d32c8d19b6489f88b2933cb663ffba","25ca847206dd482a9ad35c66bbf05080","99bbfd47a9df4dcaa8494a53bbaf523a"]},"executionInfo":{"status":"ok","timestamp":1621771802062,"user_tz":-120,"elapsed":13308,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"c845ea02-3953-4009-c358-a9981bbedb5e"},"source":["\n","model = Classifier({'lr':3e-4,'batch_size':32,'train_tr':train_tr,'val_tr':val_tr})\n","\n","ckpt = torch.load('epoch=71-step=49535.ckpt') # PATH OF CHECKPOINT\n","model.load_state_dict(ckpt['state_dict'])\n","out = trainer.predict(model)"],"execution_count":null,"outputs":[{"output_type":"stream","text":["LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]\n","/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py:477: UserWarning: This DataLoader will create 4 worker processes in total. Our suggested max number of worker in current system is 2, which is smaller than what this DataLoader is going to create. Please be aware that excessive worker creation might get DataLoader running slow or even freeze, lower the worker number to avoid potential slowness/freeze if necessary.\n","  cpuset_checked))\n"],"name":"stderr"},{"output_type":"display_data","data":{"application/vnd.jupyter.widget-view+json":{"model_id":"e697bfdb6d1c481180984038332bd5e3","version_minor":0,"version_major":2},"text/plain":["Predicting: 0it [00:00, ?it/s]"]},"metadata":{"tags":[]}}]},{"cell_type":"code","metadata":{"id":"8engzLlNEgDB"},"source":["def removeBatches(t):\n","    concat = torch.tensor([])\n","    for i in range(len(t)):\n","        concat = torch.cat((concat,torch.tensor(t[i]).cpu()))\n","    return concat"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"Ggz6mRTDEgDC","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1621771802817,"user_tz":-120,"elapsed":411,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"24a08c10-7fb3-4885-9dd7-3b954275e5d8"},"source":["\n","out = removeBatches(out)\n","\n","concat = toNum(out).squeeze()"],"execution_count":null,"outputs":[{"output_type":"stream","text":["/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n","  after removing the cwd from sys.path.\n"],"name":"stderr"}]},{"cell_type":"code","metadata":{"id":"QC6n9ivGEgDC"},"source":["\n","writeSub(concat) "],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"wZAVpZ9SEgDD","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1621771823832,"user_tz":-120,"elapsed":5430,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"eeeee749-95d5-4602-c1ea-482045db495e"},"source":["!aicrowd submission create -c f1-speed-recognition -f submission.csv"],"execution_count":null,"outputs":[{"output_type":"stream","text":["\u001b[2K\u001b[1;34msubmission.csv\u001b[0m \u001b[90m━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[35m100.0%\u001b[0m • \u001b[32m95.1/93.5 KB\u001b[0m • \u001b[31m530.6 kB/s\u001b[0m • \u001b[36m0:00:00\u001b[0m\n","\u001b[?25h                                                   ╭─────────────────────────╮                                                    \n","                                                   │ \u001b[1mSuccessfully submitted!\u001b[0m │                                                    \n","                                                   ╰─────────────────────────╯                                                    \n","\u001b[3m                                                         Important links                                                          \u001b[0m\n","┌──────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐\n","│  This submission │ https://www.aicrowd.com/challenges/ai-blitz-8/problems/f1-speed-recognition/submissions/140303              │\n","│                  │                                                                                                             │\n","│  All submissions │ https://www.aicrowd.com/challenges/ai-blitz-8/problems/f1-speed-recognition/submissions?my_submissions=true │\n","│                  │                                                                                                             │\n","│      Leaderboard │ https://www.aicrowd.com/challenges/ai-blitz-8/problems/f1-speed-recognition/leaderboards                    │\n","│                  │                                                                                                             │\n","│ Discussion forum │ https://discourse.aicrowd.com/c/ai-blitz-8                                                                  │\n","│                  │                                                                                                             │\n","│   Challenge page │ https://www.aicrowd.com/challenges/ai-blitz-8/problems/f1-speed-recognition                                 │\n","└──────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────┘\n","{'submission_id': 140303, 'created_at': '2021-05-23T12:10:22.887Z'}\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"LTCf_5cVEgDE"},"source":[""],"execution_count":null,"outputs":[]}]}