{"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":"# **Importing libraries**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom PIL import Image\n\nimport matplotlib.pyplot as plt\nfrom matplotlib import patches\n\nimport torch\nfrom torch.utils.data import Dataset\n\nimport os\nimport json","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:14:54.158089Z","iopub.execute_input":"2021-11-27T12:14:54.158388Z","iopub.status.idle":"2021-11-27T12:14:54.163272Z","shell.execute_reply.started":"2021-11-27T12:14:54.158356Z","shell.execute_reply":"2021-11-27T12:14:54.162558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = \"/kaggle/input/tensorflow-great-barrier-reef/\"","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:56:34.489733Z","iopub.execute_input":"2021-11-27T11:56:34.489960Z","iopub.status.idle":"2021-11-27T11:56:34.494759Z","shell.execute_reply.started":"2021-11-27T11:56:34.489933Z","shell.execute_reply":"2021-11-27T11:56:34.493731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Looking at data**","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(DATA_DIR, \"train.csv\"))\ntrain_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T11:56:34.496315Z","iopub.execute_input":"2021-11-27T11:56:34.496623Z","iopub.status.idle":"2021-11-27T11:56:34.682013Z","shell.execute_reply.started":"2021-11-27T11:56:34.496593Z","shell.execute_reply":"2021-11-27T11:56:34.681176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-27T12:03:11.261890Z","iopub.execute_input":"2021-11-27T12:03:11.262463Z","iopub.status.idle":"2021-11-27T12:03:11.268050Z","shell.execute_reply.started":"2021-11-27T12:03:11.262428Z","shell.execute_reply":"2021-11-27T12:03:11.267322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vizualize(img, bboxes, class_name, color):\n    fig, ax = plt.subplots(1, 1, figsize=(20, 20))\n    plt.axis(\"off\")\n    ax.imshow(img)\n    for box in bboxes:\n        x, y, w, h = box\n        ax.add_patch(patches.Rectangle((x, y), w, h, edgecolor=color, fill=False, linewidth=2))\n        ax.text(x, y, class_name, bbox={\"facecolor\": color, \"alpha\": 0.9}, fontsize=11)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T13:17:44.838253Z","iopub.execute_input":"2021-11-27T13:17:44.838560Z","iopub.status.idle":"2021-11-27T13:17:44.845318Z","shell.execute_reply.started":"2021-11-27T13:17:44.838526Z","shell.execute_reply":"2021-11-27T13:17:44.844323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _, row in train_df[20:22].iterrows():\n    bboxes = []\n    vid = row[\"video_id\"]\n    frame = row[\"video_frame\"]\n    annots = json.loads(row[\"annotations\"].replace(\"'\", '\"'))\n    for annot in annots:\n        x = annot[\"x\"]\n        y = annot[\"y\"]\n        w = annot[\"width\"]\n        h = annot[\"height\"]\n        bboxes.append([x, y, w, h])\n    img = np.array(Image.open(os.path.join(DATA_DIR, f\"train_images/video_{vid}/{frame}.jpg\")))\n    vizualize(img, bboxes, \"starfish\", \"orange\")","metadata":{"execution":{"iopub.status.busy":"2021-11-27T13:26:06.426033Z","iopub.execute_input":"2021-11-27T13:26:06.426455Z","iopub.status.idle":"2021-11-27T13:26:08.135019Z","shell.execute_reply.started":"2021-11-27T13:26:06.426410Z","shell.execute_reply":"2021-11-27T13:26:08.133473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data for net**","metadata":{}},{"cell_type":"code","source":"class StarfishDataset(Dataset):\n    def __init__(self, df, data_dir):\n        self.df = df.copy()\n        self.data_dir = data_dir\n    \n    def __getitem__(self, idx):\n        row = self.df.iloc[idx]\n        vid = row[\"video_id\"]\n        fid = row[\"video_frame\"]\n        annots = json.loads(row[\"annotations\"].replace(\"'\", '\"'))\n        \n        img = Image.open(os.path.join(self.data_dir, f\"video_{vid}/{fid}.jpg\"))\n        img = np.array(img, dtype=np.float32) / 255\n        img = torch.from_numpy(img)\n        \n        bboxes = []\n        \n        for annot in annots:\n            x = annot[\"x\"]\n            y = annot[\"y\"]\n            w = annot[\"width\"]\n            h = annot[\"height\"]\n            bboxes.append([x, y, w, h])\n        \n        labels = torch.ones((len(bboxes), ))\n        \n        return img, bboxes\n    \n    def __len__(self):\n        return len(self.df)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T13:27:36.250606Z","iopub.execute_input":"2021-11-27T13:27:36.250920Z","iopub.status.idle":"2021-11-27T13:27:36.260750Z","shell.execute_reply.started":"2021-11-27T13:27:36.250883Z","shell.execute_reply":"2021-11-27T13:27:36.259935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = StarfishDataset(train_df, os.path.join(DATA_DIR, \"train_images\"))","metadata":{"execution":{"iopub.status.busy":"2021-11-27T13:27:38.111232Z","iopub.execute_input":"2021-11-27T13:27:38.111855Z","iopub.status.idle":"2021-11-27T13:27:38.116991Z","shell.execute_reply.started":"2021-11-27T13:27:38.111812Z","shell.execute_reply":"2021-11-27T13:27:38.116383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[20]","metadata":{"execution":{"iopub.status.busy":"2021-11-27T13:28:02.893378Z","iopub.execute_input":"2021-11-27T13:28:02.893785Z","iopub.status.idle":"2021-11-27T13:28:02.931296Z","shell.execute_reply.started":"2021-11-27T13:28:02.893754Z","shell.execute_reply":"2021-11-27T13:28:02.930634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}