{"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":"markdown","source":"<br>\n\n**BASIC NOTEBOOK FOR <a href=\"https://objectnet.dev/\">OBJECTNET</a> DATASET DEMONSTRATING:**\n* How to combine all csvs\n* How to add in image paths\n* Plot a few from each class","metadata":{}},{"cell_type":"markdown","source":"<br>\n\n**IMPORTS**","metadata":{}},{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-15T22:23:14.499575Z","iopub.execute_input":"2022-07-15T22:23:14.499933Z","iopub.status.idle":"2022-07-15T22:23:14.504735Z","shell.execute_reply.started":"2022-07-15T22:23:14.499903Z","shell.execute_reply":"2022-07-15T22:23:14.503872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**LOAD CSVS**","metadata":{}},{"cell_type":"code","source":"ONET_DIR_MAP","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:15:01.457380Z","iopub.execute_input":"2022-07-15T22:15:01.457787Z","iopub.status.idle":"2022-07-15T22:15:01.466422Z","shell.execute_reply.started":"2022-07-15T22:15:01.457755Z","shell.execute_reply":"2022-07-15T22:15:01.465308Z"},"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-15T22:17:00.398926Z","iopub.execute_input":"2022-07-15T22:17:00.399365Z","iopub.status.idle":"2022-07-15T22:17:00.607964Z","shell.execute_reply.started":"2022-07-15T22:17:00.399329Z","shell.execute_reply":"2022-07-15T22:17:00.606888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**COMBINE CSVS**","metadata":{}},{"cell_type":"code","source":"onet_df = pd.concat(list(ONET_CSV_MAP.values())).reset_index(drop=True)\nonet_df","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:17:18.828665Z","iopub.execute_input":"2022-07-15T22:17:18.829044Z","iopub.status.idle":"2022-07-15T22:17:18.870186Z","shell.execute_reply.started":"2022-07-15T22:17:18.829013Z","shell.execute_reply":"2022-07-15T22:17:18.869134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**ADD PATH INFORMATION**","metadata":{}},{"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}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:31:54.695135Z","iopub.execute_input":"2022-07-15T22:31:54.695650Z","iopub.status.idle":"2022-07-15T22:31:54.900198Z","shell.execute_reply.started":"2022-07-15T22:31:54.695606Z","shell.execute_reply":"2022-07-15T22:31:54.899032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**PLOT RANDOM IMAGES**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,30))\nfor i, (_, row) in enumerate(onet_df.sample(49).iterrows()):\n    img = cv2.imread(row.img_path)[..., ::-1]\n    str_lbl = row.onet_str_label\n    int_lbl = row.int_label\n    plt.subplot(7,7,i+1)\n    plt.imshow(img)\n    plt.title(f\"{str_lbl} ({int_lbl})\", fontweight=\"bold\")\n    plt.axis(False)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:32:09.506199Z","iopub.execute_input":"2022-07-15T22:32:09.506589Z","iopub.status.idle":"2022-07-15T22:32:34.499492Z","shell.execute_reply.started":"2022-07-15T22:32:09.506553Z","shell.execute_reply":"2022-07-15T22:32:34.497871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**PLOT ONE IMAGE FOR EACH CLASS (313)**\n","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,120))\nfor i, (_, row) in enumerate(onet_df.sample(len(onet_df)).drop_duplicates(\"label\").iterrows()):\n    img = cv2.imread(row.img_path)[..., ::-1]\n    str_lbl = row.onet_str_label\n    plt.subplot(32,10,i+1)\n    plt.imshow(img)\n    plt.title(f\"{str_lbl}\", fontweight=\"bold\")\n    plt.axis(False)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:39:38.579237Z","iopub.execute_input":"2022-07-15T22:39:38.579762Z","iopub.status.idle":"2022-07-15T22:42:34.178736Z","shell.execute_reply.started":"2022-07-15T22:39:38.579729Z","shell.execute_reply":"2022-07-15T22:42:34.177469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<br>\n\n**SAVE UPDATED CSV FOR EASY LOADING**","metadata":{}},{"cell_type":"code","source":"onet_df.to_csv(\"full_onet.csv\", index=False)\nprint(os.listdir(\"/kaggle/working\"))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:42:34.181371Z","iopub.execute_input":"2022-07-15T22:42:34.182073Z","iopub.status.idle":"2022-07-15T22:42:34.548726Z","shell.execute_reply.started":"2022-07-15T22:42:34.182024Z","shell.execute_reply":"2022-07-15T22:42:34.547849Z"},"trusted":true},"execution_count":null,"outputs":[]}]}