{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-01T05:22:07.361600Z","iopub.execute_input":"2022-08-01T05:22:07.362649Z","iopub.status.idle":"2022-08-01T05:22:07.391452Z","shell.execute_reply.started":"2022-08-01T05:22:07.362542Z","shell.execute_reply":"2022-08-01T05:22:07.390564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch","metadata":{"execution":{"iopub.status.busy":"2022-08-01T05:30:14.206352Z","iopub.execute_input":"2022-08-01T05:30:14.207255Z","iopub.status.idle":"2022-08-01T05:30:14.213621Z","shell.execute_reply.started":"2022-08-01T05:30:14.207194Z","shell.execute_reply":"2022-08-01T05:30:14.212415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Option 1: passing weights param as string\nmodel = torch.hub.load(\"pytorch/vision\", \"regnet_y_128gf\", weights=\"IMAGENET1K_SWAG_E2E_V1\")","metadata":{"execution":{"iopub.status.busy":"2022-08-01T05:23:33.265080Z","iopub.execute_input":"2022-08-01T05:23:33.266161Z","iopub.status.idle":"2022-08-01T05:26:01.597624Z","shell.execute_reply.started":"2022-08-01T05:23:33.266116Z","shell.execute_reply":"2022-08-01T05:26:01.596682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nfrom torchvision import models\nfrom torchvision import transforms\nimport torch.nn.functional as F","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyModel(nn.Module):\n    def __init__(self, target_size=[224, 224]):\n        super().__init__()\n        self.target_size = target_size\n\n        self.encoder = torch.hub.load(\"pytorch/vision\", \"regnet_y_128gf\", weights=\"IMAGENET1K_SWAG_E2E_V1\")    \n        self.final = nn.AdaptiveAvgPool1d(64)\n        \n    def forward(self, x):\n        x = x #.to(torch.device('cuda:0'))\n        x = transforms.functional.resize(x, size=self.target_size, interpolation=transforms.InterpolationMode.BICUBIC)\n        x = x/255.0\n        x = transforms.functional.normalize(x,\n                                            mean=[0.48145466, 0.4578275, 0.40821073], \n                                            std=[0.26862954, 0.26130258, 0.27577711])\n        \n        x = self.encoder(x)\n        x = self.final(x)\n\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-08-01T05:30:19.759212Z","iopub.execute_input":"2022-08-01T05:30:19.759619Z","iopub.status.idle":"2022-08-01T05:30:19.771253Z","shell.execute_reply.started":"2022-08-01T05:30:19.759587Z","shell.execute_reply":"2022-08-01T05:30:19.769988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = MyModel(target_size=[128, 128]) #.to(torch.device('cuda:0'))\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T05:30:23.091850Z","iopub.execute_input":"2022-08-01T05:30:23.092672Z","iopub.status.idle":"2022-08-01T05:30:39.818608Z","shell.execute_reply.started":"2022-08-01T05:30:23.092635Z","shell.execute_reply":"2022-08-01T05:30:39.817418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = torch.randn(1, 3, 4545, 2324)\nprint(x.shape)\n# Let's print it\nmodel(x).shape","metadata":{"execution":{"iopub.status.busy":"2022-08-01T05:30:39.820662Z","iopub.execute_input":"2022-08-01T05:30:39.821601Z","iopub.status.idle":"2022-08-01T05:30:41.299743Z","shell.execute_reply.started":"2022-08-01T05:30:39.821561Z","shell.execute_reply":"2022-08-01T05:30:41.298853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"saved_model = torch.jit.script(model)\nsaved_model.save('saved_model.pt')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T05:31:01.892012Z","iopub.execute_input":"2022-08-01T05:31:01.892975Z","iopub.status.idle":"2022-08-01T05:31:09.334227Z","shell.execute_reply.started":"2022-08-01T05:31:01.892936Z","shell.execute_reply":"2022-08-01T05:31:09.332940Z"},"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":{"execution":{"iopub.status.busy":"2022-08-01T05:31:12.006220Z","iopub.execute_input":"2022-08-01T05:31:12.007122Z","iopub.status.idle":"2022-08-01T05:31:29.747906Z","shell.execute_reply.started":"2022-08-01T05:31:12.007078Z","shell.execute_reply":"2022-08-01T05:31:29.746781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}