{"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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-07-17T05:48:35.414744Z","iopub.execute_input":"2022-07-17T05:48:35.415123Z","iopub.status.idle":"2022-07-17T05:48:37.705826Z","shell.execute_reply.started":"2022-07-17T05:48:35.415093Z","shell.execute_reply":"2022-07-17T05:48:37.705023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:37.707734Z","iopub.execute_input":"2022-07-17T05:48:37.708529Z","iopub.status.idle":"2022-07-17T05:48:37.713502Z","shell.execute_reply.started":"2022-07-17T05:48:37.708497Z","shell.execute_reply":"2022-07-17T05:48:37.712525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ONET_DIR_MAP = {\n    int(_dir.split(\"-\",2)[1]):os.path.join(\"/kaggle/input\", _dir, f'split_{_dir.split(\"-\", 2)[1]}') \\\n    for _dir in os.listdir(\"/kaggle/input\") if \"objectnet\" in _dir\n}\nONET_CSV_MAP = {k:pd.read_csv(os.path.join(v, \"onet.csv\")) for k,v in ONET_DIR_MAP.items()}\nfor split_n, split_df in sorted(ONET_CSV_MAP.items(), key=lambda x: x[0]):\n    print(f\"\\n\\n... SPLIT #{split_n:>02} DATAFRAME (LENGTH={len(split_df)}) ...\\n\\n\")\n    display(split_df.head(3))","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:37.714725Z","iopub.execute_input":"2022-07-17T05:48:37.715410Z","iopub.status.idle":"2022-07-17T05:48:37.892752Z","shell.execute_reply.started":"2022-07-17T05:48:37.715374Z","shell.execute_reply":"2022-07-17T05:48:37.891661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onet_df = pd.concat(list(ONET_CSV_MAP.values())).reset_index(drop=True)\nonet_df\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:37.895278Z","iopub.execute_input":"2022-07-17T05:48:37.896194Z","iopub.status.idle":"2022-07-17T05:48:37.928799Z","shell.execute_reply.started":"2022-07-17T05:48:37.896164Z","shell.execute_reply":"2022-07-17T05:48:37.927837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onet_df['onet_str_label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:37.930733Z","iopub.execute_input":"2022-07-17T05:48:37.931032Z","iopub.status.idle":"2022-07-17T05:48:37.948832Z","shell.execute_reply.started":"2022-07-17T05:48:37.931004Z","shell.execute_reply":"2022-07-17T05:48:37.948121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onet_df[\"img_path\"] = \"/kaggle/input/objectnet-\"+\\\n                      onet_df[\"split\"].astype(str)+\"-of-10/\"\\\n                      \"split_\"+onet_df[\"split\"].astype(str)+\\\n                      \"/images/\"+onet_df[\"label\"]+\"/\"+\\\n                      onet_df[\"img_name\"]\n\nprint(\"\\n... FULL LOADED OBJECTNET DATAFRAME ...\\n\")\ndisplay(onet_df)\n\nprint(f\"\\n... OBJECTNET CLASS (N_CLASSES={onet_df.label.nunique()}) DISTRIBUTION ...\\n\")\nfor k,v in onet_df.label.value_counts().items(): print(f\"\\t{k:<20}\\t-->\\tCOUNT={v}\")\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:37.949611Z","iopub.execute_input":"2022-07-17T05:48:37.949846Z","iopub.status.idle":"2022-07-17T05:48:38.064715Z","shell.execute_reply.started":"2022-07-17T05:48:37.949823Z","shell.execute_reply":"2022-07-17T05:48:38.063696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onet_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:38.065854Z","iopub.execute_input":"2022-07-17T05:48:38.066148Z","iopub.status.idle":"2022-07-17T05:48:38.079063Z","shell.execute_reply.started":"2022-07-17T05:48:38.066126Z","shell.execute_reply":"2022-07-17T05:48:38.077753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,30))\n\nfor i in range(20):\n    img=cv2.imread(onet_df['img_path'][i])\n    plt.subplot(7,7,i+1)\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:38.080855Z","iopub.execute_input":"2022-07-17T05:48:38.081386Z","iopub.status.idle":"2022-07-17T05:48:51.938128Z","shell.execute_reply.started":"2022-07-17T05:48:38.081351Z","shell.execute_reply":"2022-07-17T05:48:51.937114Z"},"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\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\n\nmodel = MyModel()\nmodel.eval()\nsaved_model = torch.jit.script(model)\nsaved_model.save('saved_model.pt')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:51.939661Z","iopub.execute_input":"2022-07-17T05:48:51.939933Z","iopub.status.idle":"2022-07-17T05:48:53.924704Z","shell.execute_reply.started":"2022-07-17T05:48:51.939874Z","shell.execute_reply":"2022-07-17T05:48:53.923450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport torch\nfrom torchvision import transforms\nsaved_model_path='/kaggle/working/saved_model.pt'\n# Model loading.\nmodel = torch.jit.load(saved_model_path)\nmodel.eval()\nembedding_fn = model\nimage_path=onet_df['img_path'][0]\n# Load image and extract its embedding.\ninput_image = Image.open(image_path).convert(\"RGB\")\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()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:53.929031Z","iopub.execute_input":"2022-07-17T05:48:53.929428Z","iopub.status.idle":"2022-07-17T05:48:55.138877Z","shell.execute_reply.started":"2022-07-17T05:48:53.929399Z","shell.execute_reply":"2022-07-17T05:48:55.137946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(embedding)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:55.140468Z","iopub.execute_input":"2022-07-17T05:48:55.141102Z","iopub.status.idle":"2022-07-17T05:48:55.149485Z","shell.execute_reply.started":"2022-07-17T05:48:55.141056Z","shell.execute_reply":"2022-07-17T05:48:55.148288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfor i in range(50):\n    im = Image.open(onet_df['img_path'][i])\n    width, height = im.size\n    print(width,height)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:55.151288Z","iopub.execute_input":"2022-07-17T05:48:55.151942Z","iopub.status.idle":"2022-07-17T05:48:55.405366Z","shell.execute_reply.started":"2022-07-17T05:48:55.151883Z","shell.execute_reply":"2022-07-17T05:48:55.404708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"width,height","metadata":{"execution":{"iopub.status.busy":"2022-07-17T05:48:55.406302Z","iopub.execute_input":"2022-07-17T05:48:55.406833Z","iopub.status.idle":"2022-07-17T05:48:55.411376Z","shell.execute_reply.started":"2022-07-17T05:48:55.406808Z","shell.execute_reply":"2022-07-17T05:48:55.410735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = Image.open(onet_df['img_path'][6])\nplt.imshow(x)\nx = transforms.functional.resize(x,size=[299, 299])","metadata":{"execution":{"iopub.status.busy":"2022-07-17T06:08:58.641355Z","iopub.execute_input":"2022-07-17T06:08:58.641708Z","iopub.status.idle":"2022-07-17T06:08:59.270353Z","shell.execute_reply.started":"2022-07-17T06:08:58.641680Z","shell.execute_reply":"2022-07-17T06:08:59.269242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T06:08:59.272489Z","iopub.execute_input":"2022-07-17T06:08:59.272853Z","iopub.status.idle":"2022-07-17T06:08:59.446601Z","shell.execute_reply.started":"2022-07-17T06:08:59.272816Z","shell.execute_reply":"2022-07-17T06:08:59.444598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_height=224\nimage_width=224\nbatch_size=256\nnum_classes=313","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\n\nwith ZipFile('submission.zip','w') as zip:           \n  zip.write('/kaggle/working/saved_model.pt', arcname='saved_model.pt') ","metadata":{"execution":{"iopub.status.busy":"2022-07-17T06:19:29.664142Z","iopub.execute_input":"2022-07-17T06:19:29.665132Z","iopub.status.idle":"2022-07-17T06:19:30.181940Z","shell.execute_reply.started":"2022-07-17T06:19:29.665091Z","shell.execute_reply":"2022-07-17T06:19:30.181075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}