{"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)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-03T19:02:27.724872Z","iopub.execute_input":"2022-08-03T19:02:27.725240Z","iopub.status.idle":"2022-08-03T19:02:27.752528Z","shell.execute_reply.started":"2022-08-03T19:02:27.725156Z","shell.execute_reply":"2022-08-03T19:02:27.751662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ftfy regex tqdm","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:02:27.754888Z","iopub.execute_input":"2022-08-03T19:02:27.755547Z","iopub.status.idle":"2022-08-03T19:02:40.827920Z","shell.execute_reply.started":"2022-08-03T19:02:27.755511Z","shell.execute_reply":"2022-08-03T19:02:40.826708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install git+https://github.com/openai/CLIP.git","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:02:40.830142Z","iopub.execute_input":"2022-08-03T19:02:40.830640Z","iopub.status.idle":"2022-08-03T19:02:53.532750Z","shell.execute_reply.started":"2022-08-03T19:02:40.830593Z","shell.execute_reply":"2022-08-03T19:02:53.531588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport clip","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:02:53.535590Z","iopub.execute_input":"2022-08-03T19:02:53.536252Z","iopub.status.idle":"2022-08-03T19:02:56.100854Z","shell.execute_reply.started":"2022-08-03T19:02:53.536209Z","shell.execute_reply":"2022-08-03T19:02:56.099829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clip.available_models()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:02:56.102559Z","iopub.execute_input":"2022-08-03T19:02:56.103217Z","iopub.status.idle":"2022-08-03T19:02:56.111549Z","shell.execute_reply.started":"2022-08-03T19:02:56.103178Z","shell.execute_reply":"2022-08-03T19:02:56.110619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torchvision import models\nfrom torchvision import transforms\nfrom torchvision.transforms.functional import InterpolationMode","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:15:44.040858Z","iopub.execute_input":"2022-08-03T19:15:44.041551Z","iopub.status.idle":"2022-08-03T19:15:44.046766Z","shell.execute_reply.started":"2022-08-03T19:15:44.041516Z","shell.execute_reply":"2022-08-03T19:15:44.045526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:15:44.558860Z","iopub.execute_input":"2022-08-03T19:15:44.559938Z","iopub.status.idle":"2022-08-03T19:15:44.564814Z","shell.execute_reply.started":"2022-08-03T19:15:44.559901Z","shell.execute_reply":"2022-08-03T19:15:44.563433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:15:45.441048Z","iopub.execute_input":"2022-08-03T19:15:45.441418Z","iopub.status.idle":"2022-08-03T19:15:45.446357Z","shell.execute_reply.started":"2022-08-03T19:15:45.441369Z","shell.execute_reply":"2022-08-03T19:15:45.444851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nmodel, preprocess = clip.load('ViT-L/14', device, jit=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:15:57.284686Z","iopub.execute_input":"2022-08-03T19:15:57.285129Z","iopub.status.idle":"2022-08-03T19:16:12.056561Z","shell.execute_reply.started":"2022-08-03T19:15:57.285096Z","shell.execute_reply":"2022-08-03T19:16:12.055446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:16:12.058950Z","iopub.execute_input":"2022-08-03T19:16:12.059241Z","iopub.status.idle":"2022-08-03T19:16:12.195345Z","shell.execute_reply.started":"2022-08-03T19:16:12.059214Z","shell.execute_reply":"2022-08-03T19:16:12.194414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class VitModel(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.vit_model, _ = clip.load('ViT-L/14', device, jit=True)\n        self.pool = nn.AdaptiveAvgPool1d(64)\n        \n    def forward(self, x):\n        x = x.to(torch.device('cuda:0'))\n        x = transforms.functional.resize(x,size=[224, 224], 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.vit_model.encode_image(x)\n        return self.pool(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:16:43.894453Z","iopub.execute_input":"2022-08-03T19:16:43.895130Z","iopub.status.idle":"2022-08-03T19:16:43.902175Z","shell.execute_reply.started":"2022-08-03T19:16:43.895093Z","shell.execute_reply":"2022-08-03T19:16:43.901125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_model():\n    model = VitModel().to(torch.device('cuda:0'))\n    model = model.eval()\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:16:44.778793Z","iopub.execute_input":"2022-08-03T19:16:44.779167Z","iopub.status.idle":"2022-08-03T19:16:44.784592Z","shell.execute_reply.started":"2022-08-03T19:16:44.779133Z","shell.execute_reply":"2022-08-03T19:16:44.783340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_image_model()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T19:16:45.564362Z","iopub.execute_input":"2022-08-03T19:16:45.565139Z","iopub.status.idle":"2022-08-03T19:16:51.579903Z","shell.execute_reply.started":"2022-08-03T19:16:45.565100Z","shell.execute_reply":"2022-08-03T19:16:51.578920Z"},"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-03T19:16:51.581622Z","iopub.execute_input":"2022-08-03T19:16:51.581987Z","iopub.status.idle":"2022-08-03T19:16:53.069852Z","shell.execute_reply.started":"2022-08-03T19:16:51.581948Z","shell.execute_reply":"2022-08-03T19:16:53.068836Z"},"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-03T19:16:53.584619Z","iopub.execute_input":"2022-08-03T19:16:53.585282Z","iopub.status.idle":"2022-08-03T19:16:55.289633Z","shell.execute_reply.started":"2022-08-03T19:16:53.585246Z","shell.execute_reply":"2022-08-03T19:16:55.288633Z"},"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-03T19:17:30.965800Z","iopub.execute_input":"2022-08-03T19:17:30.966162Z","iopub.status.idle":"2022-08-03T19:17:33.236647Z","shell.execute_reply.started":"2022-08-03T19:17:30.966131Z","shell.execute_reply":"2022-08-03T19:17:33.235641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}