{"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":"# Intro\nWelcome to the [Tensorflow - Help Protect the Great Barrier Reef](https://www.kaggle.com/c/tensorflow-great-barrier-reef) compedition.\n\n![](https://storage.googleapis.com/kaggle-competitions/kaggle/31703/logos/header.png)\n\n<span style=\"color: royalblue;\">Please vote the notebook up if it helps you. Feel free to leave a comment above the notebook. Thank you. </span>","metadata":{}},{"cell_type":"markdown","source":"# Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport ast\nimport numpy as np\nimport pandas as pd\nimport matplotlib\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.262029Z","iopub.execute_input":"2021-11-22T21:45:18.262361Z","iopub.status.idle":"2021-11-22T21:45:18.302135Z","shell.execute_reply.started":"2021-11-22T21:45:18.262306Z","shell.execute_reply":"2021-11-22T21:45:18.301315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Path","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/tensorflow-great-barrier-reef/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.308532Z","iopub.execute_input":"2021-11-22T21:45:18.309072Z","iopub.status.idle":"2021-11-22T21:45:18.317519Z","shell.execute_reply.started":"2021-11-22T21:45:18.309027Z","shell.execute_reply":"2021-11-22T21:45:18.316878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train.csv')\ntest_data = pd.read_csv(path+'test.csv')\nsamp_subm = pd.read_csv(path+'example_sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.318717Z","iopub.execute_input":"2021-11-22T21:45:18.319345Z","iopub.status.idle":"2021-11-22T21:45:18.366571Z","shell.execute_reply.started":"2021-11-22T21:45:18.319312Z","shell.execute_reply":"2021-11-22T21:45:18.365716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview","metadata":{}},{"cell_type":"code","source":"print('Number train samples:', len(train_data))\nprint('Number test samples:', len(test_data))","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.368739Z","iopub.execute_input":"2021-11-22T21:45:18.368975Z","iopub.status.idle":"2021-11-22T21:45:18.374513Z","shell.execute_reply.started":"2021-11-22T21:45:18.368946Z","shell.execute_reply":"2021-11-22T21:45:18.373877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* video_id - ID number of the video the image was part of. The video ids are not meaningfully ordered.\n* video_frame - The frame number of the image within the video. Expect to see occasional gaps in the frame number from when the diver surfaced.\n* sequence - ID of a gap-free subset of a given video. The sequence ids are not meaningfully ordered.\n* sequence_frame - The frame number within a given sequence.\n* image_id - ID code for the image, in the format '{video_id}-{video_frame}'\n* annotations - The bounding boxes of any starfish detections in a string format that can be evaluated directly with Python. Does not use the same format as the predictions you will submit. Not available in test.csv. A bounding box is described by the pixel coordinate (x_min, y_min) of its lower left corner within the image together with its width and height in pixels.","metadata":{}},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.375702Z","iopub.execute_input":"2021-11-22T21:45:18.376352Z","iopub.status.idle":"2021-11-22T21:45:18.407229Z","shell.execute_reply.started":"2021-11-22T21:45:18.376309Z","shell.execute_reply":"2021-11-22T21:45:18.406601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA\nThere are 3 values for the feature video_id which represent the number of the underlying folder.","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:50:56.65513Z","iopub.execute_input":"2021-11-22T20:50:56.655934Z","iopub.status.idle":"2021-11-22T20:50:56.659374Z"}}},{"cell_type":"code","source":"train_data['video_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.408439Z","iopub.execute_input":"2021-11-22T21:45:18.408711Z","iopub.status.idle":"2021-11-22T21:45:18.416009Z","shell.execute_reply.started":"2021-11-22T21:45:18.408674Z","shell.execute_reply":"2021-11-22T21:45:18.415213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 20 different sequences:","metadata":{}},{"cell_type":"code","source":"train_data['sequence'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.417528Z","iopub.execute_input":"2021-11-22T21:45:18.41775Z","iopub.status.idle":"2021-11-22T21:45:18.428207Z","shell.execute_reply.started":"2021-11-22T21:45:18.417723Z","shell.execute_reply":"2021-11-22T21:45:18.427127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Image Files\n**train_images/** - Folder containing training set photos of the form video_{video_id}/{video_frame_number}.jpg.\n\nWe consider the image with the video_frame id 7981:","metadata":{}},{"cell_type":"code","source":"video_frame = 7981\nfile_name = str(video_frame)+'.jpg'\ntrain_data[train_data['video_frame']==video_frame]","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.429259Z","iopub.execute_input":"2021-11-22T21:45:18.429915Z","iopub.status.idle":"2021-11-22T21:45:18.446769Z","shell.execute_reply.started":"2021-11-22T21:45:18.429883Z","shell.execute_reply":"2021-11-22T21:45:18.445878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see there are 2 images with this video_frame id. One in folder video_0 and another one in folder video_2:","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:02:34.853561Z","iopub.execute_input":"2021-11-22T21:02:34.854101Z","iopub.status.idle":"2021-11-22T21:02:34.869723Z","shell.execute_reply.started":"2021-11-22T21:02:34.854067Z","shell.execute_reply":"2021-11-22T21:02:34.868819Z"}}},{"cell_type":"code","source":"print('file 7981.jpg in folder video_0:', file_name in os.listdir(path+'train_images/video_0'))\nprint('file 7981.jpg in folder video_2:', file_name in os.listdir(path+'train_images/video_2'))","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.447908Z","iopub.execute_input":"2021-11-22T21:45:18.448497Z","iopub.status.idle":"2021-11-22T21:45:18.46405Z","shell.execute_reply.started":"2021-11-22T21:45:18.448429Z","shell.execute_reply":"2021-11-22T21:45:18.463195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We load the images:","metadata":{}},{"cell_type":"code","source":"image_folder_0 = cv2.imread(path+'train_images/video_0/'+file_name)\nimage_folder_2 = cv2.imread(path+'train_images/video_2/'+file_name)","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.465153Z","iopub.execute_input":"2021-11-22T21:45:18.465507Z","iopub.status.idle":"2021-11-22T21:45:18.521912Z","shell.execute_reply.started":"2021-11-22T21:45:18.46547Z","shell.execute_reply":"2021-11-22T21:45:18.520864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('shape of image in folder video_0:', image_folder_0.shape)\nprint('shape of image in folder video_2:', image_folder_2.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:18.523151Z","iopub.execute_input":"2021-11-22T21:45:18.523395Z","iopub.status.idle":"2021-11-22T21:45:18.528673Z","shell.execute_reply.started":"2021-11-22T21:45:18.523366Z","shell.execute_reply":"2021-11-22T21:45:18.527936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Plot images**\n\nThe image in folder video_0 has no annotations.","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(20, 8))\nax.imshow(image_folder_0);\nax.set_xticklabels([])\nax.set_yticklabels([])\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-22T21:45:18.529704Z","iopub.execute_input":"2021-11-22T21:45:18.530031Z","iopub.status.idle":"2021-11-22T21:45:19.105901Z","shell.execute_reply.started":"2021-11-22T21:45:18.530003Z","shell.execute_reply":"2021-11-22T21:45:19.10519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The image in the folder video_2 has one annotation:","metadata":{}},{"cell_type":"code","source":"train_data[(train_data['video_frame']==video_frame)&(train_data['video_id']==2)]['annotations']","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:19.108563Z","iopub.execute_input":"2021-11-22T21:45:19.109281Z","iopub.status.idle":"2021-11-22T21:45:19.118063Z","shell.execute_reply.started":"2021-11-22T21:45:19.109241Z","shell.execute_reply":"2021-11-22T21:45:19.117219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = 20722","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:19.119109Z","iopub.execute_input":"2021-11-22T21:45:19.119562Z","iopub.status.idle":"2021-11-22T21:45:19.128926Z","shell.execute_reply.started":"2021-11-22T21:45:19.119518Z","shell.execute_reply":"2021-11-22T21:45:19.127944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We extract the boxes:","metadata":{}},{"cell_type":"code","source":"boxes = ast.literal_eval(train_data.loc[row, 'annotations'])","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:19.130476Z","iopub.execute_input":"2021-11-22T21:45:19.131172Z","iopub.status.idle":"2021-11-22T21:45:19.139351Z","shell.execute_reply.started":"2021-11-22T21:45:19.131128Z","shell.execute_reply":"2021-11-22T21:45:19.138562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(20, 8))\nax.imshow(image_folder_2)\nfor box in boxes:\n    p = matplotlib.patches.Rectangle((box['x'], box['y']), box['width'], box['height'],\n                                     ec='r', fc='none', lw=2.)\n    ax.add_patch(p)\nax.set_xticklabels([])\nax.set_yticklabels([])\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-22T21:45:19.140607Z","iopub.execute_input":"2021-11-22T21:45:19.141006Z","iopub.status.idle":"2021-11-22T21:45:19.671219Z","shell.execute_reply.started":"2021-11-22T21:45:19.140956Z","shell.execute_reply":"2021-11-22T21:45:19.670302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Example With Multi Boxes","metadata":{}},{"cell_type":"code","source":"row = 5454\nfile_name = str(train_data.loc[row, 'video_frame'])+'.jpg'\nvideo_folder = 'video_'+str(train_data.loc[row, 'video_id'])\nboxes = ast.literal_eval(train_data.loc[row, 'annotations'])","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:19.672567Z","iopub.execute_input":"2021-11-22T21:45:19.672806Z","iopub.status.idle":"2021-11-22T21:45:19.678215Z","shell.execute_reply.started":"2021-11-22T21:45:19.672777Z","shell.execute_reply":"2021-11-22T21:45:19.677364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('video folder:', video_folder)\nprint('file name:', file_name)","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:19.679782Z","iopub.execute_input":"2021-11-22T21:45:19.680357Z","iopub.status.idle":"2021-11-22T21:45:19.691849Z","shell.execute_reply.started":"2021-11-22T21:45:19.680305Z","shell.execute_reply":"2021-11-22T21:45:19.691023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(path+'train_images/'+video_folder+'/'+file_name)\nimage.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:19.693114Z","iopub.execute_input":"2021-11-22T21:45:19.69384Z","iopub.status.idle":"2021-11-22T21:45:19.72869Z","shell.execute_reply.started":"2021-11-22T21:45:19.693798Z","shell.execute_reply":"2021-11-22T21:45:19.72769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot image and annotations:","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(20, 8))\nax.imshow(image)\nfor box in boxes:\n    p = matplotlib.patches.Rectangle((box['x'], box['y']), box['width'], box['height'],\n                                     ec='r', fc='none', lw=2.)\n    ax.add_patch(p)\nax.set_xticklabels([])\nax.set_yticklabels([])\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-22T21:45:19.73016Z","iopub.execute_input":"2021-11-22T21:45:19.730685Z","iopub.status.idle":"2021-11-22T21:45:20.311913Z","shell.execute_reply.started":"2021-11-22T21:45:19.73064Z","shell.execute_reply":"2021-11-22T21:45:20.311329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Use API\nTo use the api we follow the instructions of [this notebook](https://www.kaggle.com/sohier/great-barrier-reef-api-tutorial/notebook). ","metadata":{}},{"cell_type":"code","source":"import PIL.Image\nimport greatbarrierreef\n\nenv = greatbarrierreef.make_env()\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:20.312966Z","iopub.execute_input":"2021-11-22T21:45:20.313359Z","iopub.status.idle":"2021-11-22T21:45:20.323791Z","shell.execute_reply.started":"2021-11-22T21:45:20.313329Z","shell.execute_reply":"2021-11-22T21:45:20.322744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixel_array, sample_prediction_df = next(iter_test)\npixel_array","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:20.324942Z","iopub.execute_input":"2021-11-22T21:45:20.325171Z","iopub.status.idle":"2021-11-22T21:45:20.344552Z","shell.execute_reply.started":"2021-11-22T21:45:20.325145Z","shell.execute_reply":"2021-11-22T21:45:20.343808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PIL.Image.fromarray(pixel_array)","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:20.345962Z","iopub.execute_input":"2021-11-22T21:45:20.346409Z","iopub.status.idle":"2021-11-22T21:45:20.891431Z","shell.execute_reply.started":"2021-11-22T21:45:20.346375Z","shell.execute_reply":"2021-11-22T21:45:20.890745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Export Data","metadata":{}},{"cell_type":"code","source":"samp_subm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-22T21:45:20.892683Z","iopub.execute_input":"2021-11-22T21:45:20.893335Z","iopub.status.idle":"2021-11-22T21:45:20.899069Z","shell.execute_reply.started":"2021-11-22T21:45:20.893297Z","shell.execute_reply":"2021-11-22T21:45:20.898506Z"},"trusted":true},"execution_count":null,"outputs":[]}]}