{"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":"import torch\nimport torch.nn as nn\nfrom torchvision import models\nfrom torchvision import transforms","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:23:44.295219Z","iopub.execute_input":"2022-07-13T18:23:44.295965Z","iopub.status.idle":"2022-07-13T18:23:45.164641Z","shell.execute_reply.started":"2022-07-13T18:23:44.295591Z","shell.execute_reply":"2022-07-13T18:23:45.163655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Code example given by competition host to produce model\n# For PyTorch, see the example below, \n# where we take a pretrained Inception-v3 model, \n# add a linear layer to project to 64D, \n# and export the model in the TorchScript format:\n\nclass MyModel(nn.Module):\n  def __init__(self):\n    super().__init__()\n    inception_model = models.inception_v3(pretrained=True)\n    inception_model.fc = nn.Linear(2048, 64)\n    self.feature_extractor = inception_model\n\n  def forward(self, x):\n    x = transforms.functional.resize(x,size=[224, 224])\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    return self.feature_extractor(x).logits","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:23:52.745491Z","iopub.execute_input":"2022-07-13T18:23:52.745829Z","iopub.status.idle":"2022-07-13T18:23:52.752647Z","shell.execute_reply.started":"2022-07-13T18:23:52.745802Z","shell.execute_reply":"2022-07-13T18:23:52.751835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = MyModel()\nmodel.eval()\nsaved_model = torch.jit.script(model)\nsaved_model.save('saved_model.pt')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:23:56.124967Z","iopub.execute_input":"2022-07-13T18:23:56.125510Z","iopub.status.idle":"2022-07-13T18:24:06.066855Z","shell.execute_reply.started":"2022-07-13T18:23:56.125480Z","shell.execute_reply":"2022-07-13T18:24:06.065850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Notebook for submission: https://www.kaggle.com/code/yashvrdnjain/guie-pytorch-submission/notebook ","metadata":{}},{"cell_type":"code","source":"# # Code for submission notebook\n# from zipfile import ZipFile\n\n# with ZipFile('submission.zip','w') as zip:           \n#   zip.write('../input/pytorchuecinceptionv3baseline/saved_model.pt', arcname='saved_model.pt') ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Feature extraction code\n# When your model is submitted, \n# we run it in the competition images in order \n# to extract embeddings. \n# Please find below code snippets that \n# show how the model is used in detail. \n# These could be helpful for participants to \n# make sure that their models are extracting \n# embeddings correctly.\n\n\n# # from PIL import Image\n# import torch\n# from torchvision import transforms\n\n# # Model loading.\n# model = torch.jit.load(saved_model_path)\n# model.eval()\n# embedding_fn = model\n\n# # Load image and extract its embedding.\n# input_image = Image.open(image_path).convert(\"RGB\")\n# convert_to_tensor = transforms.Compose([transforms.PILToTensor()])\n# input_tensor = convert_to_tensor(input_image)\n# input_batch = input_tensor.unsqueeze(0)\n# with torch.no_grad():\n#   embedding = torch.flatten(embedding_fn(input_batch)[0]).cpu().data.numpy()","metadata":{},"execution_count":null,"outputs":[]}]}