{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install timm","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:44:10.259140Z","iopub.execute_input":"2022-07-12T19:44:10.260136Z","iopub.status.idle":"2022-07-12T19:44:24.266040Z","shell.execute_reply.started":"2022-07-12T19:44:10.260027Z","shell.execute_reply":"2022-07-12T19:44:24.264855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport torch\nimport timm\nfrom torchvision import transforms\nfrom typing import Optional","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:44:24.268089Z","iopub.execute_input":"2022-07-12T19:44:24.268644Z","iopub.status.idle":"2022-07-12T19:44:36.301681Z","shell.execute_reply.started":"2022-07-12T19:44:24.268604Z","shell.execute_reply":"2022-07-12T19:44:36.300360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyModel(torch.nn.Module):\n    def __init__(\n        self,\n        model_name: str,\n        embedding_size: Optional[int] = 64\n    ):\n        super().__init__()\n\n        self.backbone = timm.create_model(\n            model_name,\n            pretrained=True,\n            num_classes=0)\n\n        self.neck = torch.nn.Sequential(\n            torch.nn.Linear(\n                self.backbone.num_features,\n                embedding_size,\n                bias=True),\n            torch.nn.BatchNorm1d(embedding_size),\n            torch.nn.PReLU()\n        )\n\n    def forward(self, x):\n        x = x/255.0\n        x = transforms.functional.normalize(\n            x,\n            mean=[0.485, 0.456, 0.406],\n            std=[0.229, 0.224, 0.225])\n\n        return self.neck(self.backbone(x))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:44:36.303362Z","iopub.execute_input":"2022-07-12T19:44:36.304293Z","iopub.status.idle":"2022-07-12T19:44:36.317153Z","shell.execute_reply.started":"2022-07-12T19:44:36.304240Z","shell.execute_reply":"2022-07-12T19:44:36.315910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pprint import pprint\nmodel_names = timm.list_models('tf_efficientnet_b7_ap', pretrained=True)\npprint(model_names)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T19:58:16.617626Z","iopub.execute_input":"2022-07-12T19:58:16.618079Z","iopub.status.idle":"2022-07-12T19:58:16.625104Z","shell.execute_reply.started":"2022-07-12T19:58:16.618043Z","shell.execute_reply":"2022-07-12T19:58:16.622767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom zipfile import ZipFile\n\nmodel_name = 'tf_efficientnet_b7_ap'\n\nfor model_name in model_names:\n    model = MyModel(\n        model_name=model_name,\n        embedding_size=64)\n\n    model.eval()\n    saved_model = torch.jit.script(model)\n    os.makedirs(model_name, exist_ok=True)\n    saved_model.save(os.path.join(model_name, \"saved_model.pt\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-12T19:58:17.906343Z","iopub.execute_input":"2022-07-12T19:58:17.906711Z","iopub.status.idle":"2022-07-12T19:58:50.832532Z","shell.execute_reply.started":"2022-07-12T19:58:17.906681Z","shell.execute_reply":"2022-07-12T19:58:50.831517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with ZipFile('submission.zip','w') as zip:           \n    zip.write(os.path.join(model_name, 'saved_model.pt'), arcname='saved_model.pt') ","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:03:55.440273Z","iopub.execute_input":"2022-07-12T20:03:55.440754Z","iopub.status.idle":"2022-07-12T20:03:57.002222Z","shell.execute_reply.started":"2022-07-12T20:03:55.440709Z","shell.execute_reply":"2022-07-12T20:03:57.000912Z"},"trusted":true},"execution_count":null,"outputs":[]}]}