{"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-26T14:30:12.726958Z","iopub.execute_input":"2022-07-26T14:30:12.727436Z","iopub.status.idle":"2022-07-26T14:30:25.695763Z","shell.execute_reply.started":"2022-07-26T14:30:12.727399Z","shell.execute_reply":"2022-07-26T14:30:25.694499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm\nimport torch\nimport torch.nn as nn\nfrom torchvision import models\nfrom torchvision import transforms\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:30:25.698598Z","iopub.execute_input":"2022-07-26T14:30:25.699128Z","iopub.status.idle":"2022-07-26T14:30:25.706365Z","shell.execute_reply.started":"2022-07-26T14:30:25.699065Z","shell.execute_reply":"2022-07-26T14:30:25.705387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyModel(nn.Module):\n  def __init__(self, model_name, target_size=[224, 224], normalize=True):\n    super().__init__()\n    self.target_size = target_size\n    \n    self.encoder = timm.create_model(model_name, pretrained=True, num_classes=0)    \n    self.final = nn.AdaptiveAvgPool1d(64)\n    self.normalize = normalize\n\n  def forward(self, x):\n    x = transforms.functional.resize(x,size=self.target_size)\n    x = x / 255.0\n    x = transforms.functional.normalize(x, \n                                            mean=[0.485, 0.456, 0.406], \n                                            std=[0.229, 0.224, 0.225])\n    x = self.encoder(x)/2.0 + self.encoder(x.flip([-1]))/2.0\n    x = self.final(x)\n    if self.normalize:\n        x = torch.nn.functional.normalize(x)\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:30:25.707437Z","iopub.execute_input":"2022-07-26T14:30:25.707768Z","iopub.status.idle":"2022-07-26T14:30:25.724592Z","shell.execute_reply.started":"2022-07-26T14:30:25.707737Z","shell.execute_reply":"2022-07-26T14:30:25.723512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = MyModel(\"swin_large_patch4_window12_384_in22k\", target_size=[384, 384], normalize=True)\nm.eval();","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:30:25.727257Z","iopub.execute_input":"2022-07-26T14:30:25.728068Z","iopub.status.idle":"2022-07-26T14:30:30.689044Z","shell.execute_reply.started":"2022-07-26T14:30:25.728013Z","shell.execute_reply":"2022-07-26T14:30:30.688048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"saved_model = torch.jit.script(m)\nsaved_model.save('saved_model.pt')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:30:30.690316Z","iopub.execute_input":"2022-07-26T14:30:30.691244Z","iopub.status.idle":"2022-07-26T14:30:34.526895Z","shell.execute_reply.started":"2022-07-26T14:30:30.691199Z","shell.execute_reply":"2022-07-26T14:30:34.525428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -lh","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:30:34.529382Z","iopub.execute_input":"2022-07-26T14:30:34.530316Z","iopub.status.idle":"2022-07-26T14:30:35.331983Z","shell.execute_reply.started":"2022-07-26T14:30:34.530263Z","shell.execute_reply":"2022-07-26T14:30:35.330539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\n\nwith ZipFile('submission.zip','w') as zip:           \n  zip.write('saved_model.pt', arcname='saved_model.pt') ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-26T14:30:35.335067Z","iopub.execute_input":"2022-07-26T14:30:35.335574Z","iopub.status.idle":"2022-07-26T14:30:42.742923Z","shell.execute_reply.started":"2022-07-26T14:30:35.335523Z","shell.execute_reply":"2022-07-26T14:30:42.740603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -lh","metadata":{"execution":{"iopub.status.busy":"2022-07-26T14:30:42.748177Z","iopub.execute_input":"2022-07-26T14:30:42.748758Z","iopub.status.idle":"2022-07-26T14:30:43.609658Z","shell.execute_reply.started":"2022-07-26T14:30:42.748703Z","shell.execute_reply":"2022-07-26T14:30:43.608314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}