{"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":"!python -m pip install timm","metadata":{"execution":{"iopub.status.busy":"2022-10-20T05:05:48.258326Z","iopub.execute_input":"2022-10-20T05:05:48.258876Z","iopub.status.idle":"2022-10-20T05:06:00.349385Z","shell.execute_reply.started":"2022-10-20T05:05:48.258832Z","shell.execute_reply":"2022-10-20T05:06:00.348144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ftfy regex tqdm\n!pip install git+https://github.com/openai/CLIP.git","metadata":{"execution":{"iopub.status.busy":"2022-10-20T05:06:00.352227Z","iopub.execute_input":"2022-10-20T05:06:00.352710Z","iopub.status.idle":"2022-10-20T05:06:28.686964Z","shell.execute_reply.started":"2022-10-20T05:06:00.352662Z","shell.execute_reply":"2022-10-20T05:06:28.685176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\nclass ConvertToDtype(torch.nn.Module):\n    def __init__(self, dtype):\n        super().__init__()\n        self.dtype = dtype\n    def forward(self, sample):\n        return sample.to(dtype=self.dtype)\n\nclass Normalize255To01(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n    def forward(self, sample):\n        return sample / 255.0","metadata":{"execution":{"iopub.status.busy":"2022-10-20T05:06:28.689325Z","iopub.execute_input":"2022-10-20T05:06:28.689984Z","iopub.status.idle":"2022-10-20T05:06:28.700490Z","shell.execute_reply.started":"2022-10-20T05:06:28.689933Z","shell.execute_reply":"2022-10-20T05:06:28.698659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torchvision import transforms\n\nclass DeitModel(torch.nn.Module):\n  def __init__(self):\n    super().__init__()\n    self.preprocess = torch.nn.Sequential(\n        ConvertToDtype(dtype=torch.float32),\n        Normalize255To01(),\n        transforms.Resize(size=(256, 256)),\n        transforms.CenterCrop(224),\n        transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225]),\n    )\n    self.deit = torch.hub.load('facebookresearch/deit:main', 'deit_base_patch16_224', pretrained=True)\n\n  def forward(self, x):\n    image_processed = self.preprocess(x)\n    image_features = self.deit(image_processed)\n    reduced_features = image_features[:, :64]\n    return reduced_features","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-20T05:06:28.702315Z","iopub.execute_input":"2022-10-20T05:06:28.702801Z","iopub.status.idle":"2022-10-20T05:06:28.720226Z","shell.execute_reply.started":"2022-10-20T05:06:28.702760Z","shell.execute_reply":"2022-10-20T05:06:28.719150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch import nn\nfrom torchvision.transforms import Resize, CenterCrop, Normalize\nimport clip\n\nclass ClipVisionOnlyModel(nn.Module):\n  def __init__(self):\n    super().__init__()\n    model, _ = clip.load(\"RN50\", jit=True)\n    self.preprocess = torch.nn.Sequential(\n        ConvertToDtype(dtype=torch.float32),\n        Normalize255To01(),\n        Resize(size=(224, 224)),\n        CenterCrop(size=(224, 224)),\n        Normalize(mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711)),\n    )\n    self.clip_model = model.visual\n\n  def forward(self, x):\n    image_processed = self.preprocess(x)\n    image_features = self.clip_model(image_processed)\n    reduced_features = torch.sum(image_features.view(image_features.shape[0], image_features.shape[1] // 16, 16), dim=-1)\n    return reduced_features","metadata":{"execution":{"iopub.status.busy":"2022-10-20T05:06:28.724369Z","iopub.execute_input":"2022-10-20T05:06:28.725477Z","iopub.status.idle":"2022-10-20T05:06:28.737714Z","shell.execute_reply.started":"2022-10-20T05:06:28.725417Z","shell.execute_reply":"2022-10-20T05:06:28.736582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = DeitModel()\nmodel.eval()\nsaved_model = torch.jit.script(model)\nsaved_model.save('saved_model.pt')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T05:06:28.739786Z","iopub.execute_input":"2022-10-20T05:06:28.740350Z","iopub.status.idle":"2022-10-20T05:06:32.441900Z","shell.execute_reply.started":"2022-10-20T05:06:28.740297Z","shell.execute_reply":"2022-10-20T05:06:32.440328Z"},"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-10-20T05:06:32.443982Z","iopub.execute_input":"2022-10-20T05:06:32.445715Z","iopub.status.idle":"2022-10-20T05:06:33.925018Z","shell.execute_reply.started":"2022-10-20T05:06:32.445648Z","shell.execute_reply":"2022-10-20T05:06:33.923684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluation\nfrom PIL import Image\nimport torch\nfrom torchvision import transforms\nfrom torchvision.datasets import CIFAR10\nimport sys\n\nsaved_model_path = 'saved_model.pt'\n\n# Model loading.\nmodel = torch.jit.load(saved_model_path)\nmodel.eval()\nembedding_fn = model\n\n# Load image and extract its embedding.\ninput_image = CIFAR10(root='.', download=True)[0][0]\nconvert_to_tensor = transforms.Compose([transforms.PILToTensor()])\ninput_tensor = convert_to_tensor(input_image)\ninput_batch = input_tensor.unsqueeze(0)\nwith torch.no_grad():\n  embedding = torch.flatten(embedding_fn(input_batch)[0]).cpu().data.numpy()\n  print(embedding)\n  print(embedding.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T05:06:33.926957Z","iopub.execute_input":"2022-10-20T05:06:33.928409Z","iopub.status.idle":"2022-10-20T05:06:36.273739Z","shell.execute_reply.started":"2022-10-20T05:06:33.928337Z","shell.execute_reply":"2022-10-20T05:06:36.272558Z"},"trusted":true},"execution_count":null,"outputs":[]}]}