{"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":"# Lets go on a small adventure looking for starfishes \n\nThis notebook houses some basic EDA which will be updated frequently as the competition goes on with a simple aim of getting to the depths of data and extracting any key insights which could shape the solution\n\n\n<img src=\"https://media.giphy.com/media/QvSkfOVGFEH7Nydll2/giphy.gif\">","metadata":{}},{"cell_type":"markdown","source":"## Time to gear up! : Lets Import","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport os\nimport pathlib\nimport PIL\nfrom pathlib import Path\nfrom PIL import Image, ImageDraw\nfrom math import sqrt\nimport ast\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nsns.set()\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:28:54.198592Z","iopub.execute_input":"2021-11-23T18:28:54.198975Z","iopub.status.idle":"2021-11-23T18:28:54.209391Z","shell.execute_reply.started":"2021-11-23T18:28:54.198933Z","shell.execute_reply":"2021-11-23T18:28:54.208781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\ntest = pd.read_csv(\"../input/tensorflow-great-barrier-reef/test.csv\")\nsub = pd.read_csv(\"../input/tensorflow-great-barrier-reef/example_sample_submission.csv\")\n\npath = Path('../input/tensorflow-great-barrier-reef/train_images')\nfilepaths = list(path.glob(r'**/*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:19:53.038009Z","iopub.execute_input":"2021-11-23T18:19:53.038296Z","iopub.status.idle":"2021-11-23T18:20:06.134721Z","shell.execute_reply.started":"2021-11-23T18:19:53.038263Z","shell.execute_reply":"2021-11-23T18:20:06.133914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let us Begin!!!","metadata":{}},{"cell_type":"code","source":"# checking the train test lengths\nprint(\"Number of training samples: \", len(train))\nprint(\"Number of testing samples: \", len(test))","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:20:06.136613Z","iopub.execute_input":"2021-11-23T18:20:06.136911Z","iopub.status.idle":"2021-11-23T18:20:06.141740Z","shell.execute_reply.started":"2021-11-23T18:20:06.136880Z","shell.execute_reply":"2021-11-23T18:20:06.140891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(150)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:20:06.143263Z","iopub.execute_input":"2021-11-23T18:20:06.144140Z","iopub.status.idle":"2021-11-23T18:20:06.171694Z","shell.execute_reply.started":"2021-11-23T18:20:06.144097Z","shell.execute_reply":"2021-11-23T18:20:06.170766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Looks like a lot of these frames dont have our starfishes \n\n\n<img src=\"https://media.giphy.com/media/jsN192JGdyWvS1gqTb/giphy.gif\">","metadata":{}},{"cell_type":"code","source":"# lets see how many frames with no starfishes\ntrain_clean = train.loc[train[\"annotations\"] != \"[]\"]\nprint(f\"No starfishes in {len(train)-len(train_clean)} samples.\")\nprint(f\"The clean train set has {len(train_clean)} images for us to work with.\")","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:20:06.173314Z","iopub.execute_input":"2021-11-23T18:20:06.173626Z","iopub.status.idle":"2021-11-23T18:20:06.187321Z","shell.execute_reply.started":"2021-11-23T18:20:06.173592Z","shell.execute_reply":"2021-11-23T18:20:06.186409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:20:06.188794Z","iopub.execute_input":"2021-11-23T18:20:06.189268Z","iopub.status.idle":"2021-11-23T18:20:06.202410Z","shell.execute_reply.started":"2021-11-23T18:20:06.189214Z","shell.execute_reply":"2021-11-23T18:20:06.201551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of Sequences","metadata":{}},{"cell_type":"code","source":"# Checking out the number of sequences\nlen(train_clean.sequence.value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:20:41.842224Z","iopub.execute_input":"2021-11-23T18:20:41.842484Z","iopub.status.idle":"2021-11-23T18:20:41.852088Z","shell.execute_reply.started":"2021-11-23T18:20:41.842455Z","shell.execute_reply":"2021-11-23T18:20:41.851167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rows per each sequence\nprint(\"Sequence Samples\")\nprint(train_clean.sequence.value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:26:27.069194Z","iopub.execute_input":"2021-11-23T18:26:27.069490Z","iopub.status.idle":"2021-11-23T18:26:27.076819Z","shell.execute_reply.started":"2021-11-23T18:26:27.069463Z","shell.execute_reply":"2021-11-23T18:26:27.075828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seq_df = train_clean.sequence.value_counts().to_frame()\nplt.figure(figsize=(16, 9))\nsns.barplot(x=seq_df.index, y=list(seq_df.sequence), palette=\"Greens_d\")\nplt.title(\"Distribution of Sequences\")\nplt.xlabel(\"Sequence Id\")\nplt.ylabel(\"Frequency\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:23:18.756312Z","iopub.execute_input":"2021-11-23T18:23:18.756649Z","iopub.status.idle":"2021-11-23T18:23:19.173058Z","shell.execute_reply.started":"2021-11-23T18:23:18.756614Z","shell.execute_reply":"2021-11-23T18:23:19.172149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Number Of Boxes","metadata":{}},{"cell_type":"code","source":"num_boxes = []\nannotations_clean = []\nfor elem in train_clean.annotations:\n    ann = ast.literal_eval(elem)\n    num_boxes.append(len(ann))\n    annotations_clean.append(ann)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:26:43.329963Z","iopub.execute_input":"2021-11-23T18:26:43.330798Z","iopub.status.idle":"2021-11-23T18:26:43.535723Z","shell.execute_reply.started":"2021-11-23T18:26:43.330758Z","shell.execute_reply":"2021-11-23T18:26:43.534814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# adding num boxes per row and changing the annotations column to a proper python parseable list of dictionaries\ntrain_clean[\"num_boxes\"] = num_boxes\ntrain_clean[\"annotations\"] = annotations_clean","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:29:00.435499Z","iopub.execute_input":"2021-11-23T18:29:00.436167Z","iopub.status.idle":"2021-11-23T18:29:00.448595Z","shell.execute_reply.started":"2021-11-23T18:29:00.436131Z","shell.execute_reply":"2021-11-23T18:29:00.447482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:29:06.086730Z","iopub.execute_input":"2021-11-23T18:29:06.087031Z","iopub.status.idle":"2021-11-23T18:29:06.102250Z","shell.execute_reply.started":"2021-11-23T18:29:06.086998Z","shell.execute_reply":"2021-11-23T18:29:06.101384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"#box Frequency\")\nprint(train_clean.num_boxes.value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:29:55.891790Z","iopub.execute_input":"2021-11-23T18:29:55.892077Z","iopub.status.idle":"2021-11-23T18:29:55.898463Z","shell.execute_reply.started":"2021-11-23T18:29:55.892042Z","shell.execute_reply":"2021-11-23T18:29:55.897632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of bounding boxes in the clean train datasets\nprint(f\"Number of Bounding Boxes in the dataset: {train_clean.num_boxes.sum()}\")","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:30:08.146427Z","iopub.execute_input":"2021-11-23T18:30:08.146722Z","iopub.status.idle":"2021-11-23T18:30:08.151067Z","shell.execute_reply.started":"2021-11-23T18:30:08.146692Z","shell.execute_reply":"2021-11-23T18:30:08.150445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of number of bounding boxes","metadata":{}},{"cell_type":"code","source":"box_count = train_clean.num_boxes.value_counts().to_frame()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:30:14.522309Z","iopub.execute_input":"2021-11-23T18:30:14.522629Z","iopub.status.idle":"2021-11-23T18:30:14.528248Z","shell.execute_reply.started":"2021-11-23T18:30:14.522564Z","shell.execute_reply":"2021-11-23T18:30:14.527613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16, 9))\nsns.barplot(x=box_count.index, y=list(box_count.num_boxes), palette=\"Greens_d\")\nplt.title(\"Distribution of Num_boxes\")\nplt.xlabel(\"# of Boxes\")\nplt.ylabel(\"Frequency\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:30:20.165889Z","iopub.execute_input":"2021-11-23T18:30:20.166291Z","iopub.status.idle":"2021-11-23T18:30:20.540415Z","shell.execute_reply.started":"2021-11-23T18:30:20.166260Z","shell.execute_reply":"2021-11-23T18:30:20.539621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So looks like 57% of the data points have only one bounding box, followed by 19.1% with 2 bounding boxes \n","metadata":{}},{"cell_type":"markdown","source":"## Looking at the boxes","metadata":{}},{"cell_type":"code","source":"#structure of a annotation\nlist(train_clean[\"annotations\"])[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:31:30.287772Z","iopub.execute_input":"2021-11-23T18:31:30.288183Z","iopub.status.idle":"2021-11-23T18:31:30.294760Z","shell.execute_reply.started":"2021-11-23T18:31:30.288154Z","shell.execute_reply":"2021-11-23T18:31:30.294153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# generating paths for input images\nsrc = '../input/tensorflow-great-barrier-reef/train_images'\npaths = []\nfor row in train_clean.image_id:\n    vid_num = row.split('-')[0]\n    img_num = row.split('-')[1]\n    paths.append(os.path.join(src,f'video_{vid_num}',img_num+'.jpg'))\n","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:31:31.173990Z","iopub.execute_input":"2021-11-23T18:31:31.174297Z","iopub.status.idle":"2021-11-23T18:31:31.192086Z","shell.execute_reply.started":"2021-11-23T18:31:31.174260Z","shell.execute_reply":"2021-11-23T18:31:31.191512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean['paths'] = paths","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:31:31.881685Z","iopub.execute_input":"2021-11-23T18:31:31.882613Z","iopub.status.idle":"2021-11-23T18:31:31.887567Z","shell.execute_reply.started":"2021-11-23T18:31:31.882557Z","shell.execute_reply":"2021-11-23T18:31:31.886612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classic way of iterating through and drawing the bounding boxes on an image\ndef vis_boxes(img_path, bboxes):\n    coords = []\n    for box in bboxes:\n        x1 = box['x']\n        y1 = box['y']\n        x2 = x1 + box['width']\n        y2 = y1 + box['height']\n        coords.append([x1, y1, x2, y2])\n        \n    img = Image.open(img_path)\n    img1 = img.copy()\n    draw = ImageDraw.Draw(img1)\n    for elem in coords:\n        draw.rectangle(elem, outline='red', width=7)\n    \n    return img1","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:31:32.738177Z","iopub.execute_input":"2021-11-23T18:31:32.738841Z","iopub.status.idle":"2021-11-23T18:31:32.744676Z","shell.execute_reply.started":"2021-11-23T18:31:32.738798Z","shell.execute_reply":"2021-11-23T18:31:32.744020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:31:35.154475Z","iopub.execute_input":"2021-11-23T18:31:35.154782Z","iopub.status.idle":"2021-11-23T18:31:35.170188Z","shell.execute_reply.started":"2021-11-23T18:31:35.154743Z","shell.execute_reply":"2021-11-23T18:31:35.169246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sequences with max bounding boxes","metadata":{}},{"cell_type":"code","source":"# number of bounding boxes per each sequence\ntrain_clean.groupby('sequence').num_boxes.sum().to_frame()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:32:26.944768Z","iopub.execute_input":"2021-11-23T18:32:26.945614Z","iopub.status.idle":"2021-11-23T18:32:26.958816Z","shell.execute_reply.started":"2021-11-23T18:32:26.945547Z","shell.execute_reply":"2021-11-23T18:32:26.957786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Lets look at some samples ","metadata":{}},{"cell_type":"code","source":"# lets plot a few\n# some inspiration from https://www.kaggle.com/sjyangkevin/eda-bounding-box-analysis-annotated-videos\n\nplt.figure(figsize=(16, 9))\nn_images = 9\ncount = 0\nr,c = int(sqrt(n_images)), int(sqrt(n_images))\ntrain_plot = train_clean.sample(n = n_images)\n\nfor _, row in train_plot.iterrows():\n    img_path = row['paths']\n    bboxes = row['annotations']\n    plt.subplot(r, c, count + 1)\n    img_out = vis_boxes(img_path, bboxes)\n    plt.imshow(img_out)\n    count+=1\n\nplt.show()\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:32:35.138465Z","iopub.execute_input":"2021-11-23T18:32:35.138891Z","iopub.status.idle":"2021-11-23T18:32:38.260420Z","shell.execute_reply.started":"2021-11-23T18:32:35.138860Z","shell.execute_reply":"2021-11-23T18:32:38.259632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What Next?\n- Some Advanced EDA\n- Baseline model\n- Error Analysis of Baseline\n- Advanced model","metadata":{}},{"cell_type":"markdown","source":"**If you like it so far, consider upvoting 😄** \n\n<img src=\"https://media.giphy.com/media/eunrMjB8lBUKeL1fqD/giphy-downsized.gif\">","metadata":{}}]}