{"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":"# Great Barrier Reef Object Detection","metadata":{}},{"cell_type":"markdown","source":"## Initial Settings","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport random\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib\nimport ast\nimport os\nimport cv2\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom matplotlib.transforms import Bbox\n\npd.set_option(\"display.max_colwidth\",None)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-07T11:24:41.941272Z","iopub.execute_input":"2023-10-07T11:24:41.941626Z","iopub.status.idle":"2023-10-07T11:24:43.367121Z","shell.execute_reply.started":"2023-10-07T11:24:41.941603Z","shell.execute_reply":"2023-10-07T11:24:43.366154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/tensorflow-great-barrier-reef/train.csv')\ntest = pd.read_csv('/kaggle/input/tensorflow-great-barrier-reef/test.csv')\nsample_submission = pd.read_csv('/kaggle/input/tensorflow-great-barrier-reef/example_sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:43.371996Z","iopub.execute_input":"2023-10-07T11:24:43.374113Z","iopub.status.idle":"2023-10-07T11:24:43.489029Z","shell.execute_reply.started":"2023-10-07T11:24:43.374082Z","shell.execute_reply":"2023-10-07T11:24:43.488090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploring Dataset","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:43.493684Z","iopub.execute_input":"2023-10-07T11:24:43.494372Z","iopub.status.idle":"2023-10-07T11:24:43.528690Z","shell.execute_reply.started":"2023-10-07T11:24:43.494344Z","shell.execute_reply":"2023-10-07T11:24:43.527830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:43.534908Z","iopub.execute_input":"2023-10-07T11:24:43.537494Z","iopub.status.idle":"2023-10-07T11:24:43.554029Z","shell.execute_reply.started":"2023-10-07T11:24:43.537461Z","shell.execute_reply":"2023-10-07T11:24:43.552557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"train dataset Info:\")\nprint(train.info())\nprint(\"\\n\")\nprint(\"test dataset Info:\")\nprint(test.info())","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:43.565571Z","iopub.execute_input":"2023-10-07T11:24:43.567526Z","iopub.status.idle":"2023-10-07T11:24:43.630540Z","shell.execute_reply.started":"2023-10-07T11:24:43.567497Z","shell.execute_reply":"2023-10-07T11:24:43.629797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Boxplot (to find Outliers)\nplt.figure(figsize=(15,15))\npos = 1\nfor i in train.columns:\n        if(type(train[i][0]) != str):\n                plt.subplot(4, 3, pos)\n                sns.boxplot(train[i])\n                pos += 1","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:43.634051Z","iopub.execute_input":"2023-10-07T11:24:43.636188Z","iopub.status.idle":"2023-10-07T11:24:44.778458Z","shell.execute_reply.started":"2023-10-07T11:24:43.636159Z","shell.execute_reply":"2023-10-07T11:24:44.777754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Correlation Matrix ( help to choose columns to drop: the most correlated!)\ncorrelation_matrix = train.corr()\nsns.heatmap(correlation_matrix, cmap=\"YlGnBu\", annot=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:44.780087Z","iopub.execute_input":"2023-10-07T11:24:44.780691Z","iopub.status.idle":"2023-10-07T11:24:45.156724Z","shell.execute_reply.started":"2023-10-07T11:24:44.780659Z","shell.execute_reply":"2023-10-07T11:24:45.155780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### No noticeable positive or negative correlation detected","metadata":{}},{"cell_type":"code","source":"#check if there are null val\nplt.figure(figsize=(10,10))\nsns.heatmap(train.isna())","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:45.158063Z","iopub.execute_input":"2023-10-07T11:24:45.159020Z","iopub.status.idle":"2023-10-07T11:24:46.606726Z","shell.execute_reply.started":"2023-10-07T11:24:45.158988Z","shell.execute_reply":"2023-10-07T11:24:46.605860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### As we can see from the plot here are no nulls detected","metadata":{}},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:46.607687Z","iopub.execute_input":"2023-10-07T11:24:46.608034Z","iopub.status.idle":"2023-10-07T11:24:46.615653Z","shell.execute_reply.started":"2023-10-07T11:24:46.608005Z","shell.execute_reply":"2023-10-07T11:24:46.614656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Number of video_frame per video\ntrain.groupby(\"video_id\")[\"video_frame\"].count()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:46.619615Z","iopub.execute_input":"2023-10-07T11:24:46.620885Z","iopub.status.idle":"2023-10-07T11:24:46.639062Z","shell.execute_reply.started":"2023-10-07T11:24:46.620853Z","shell.execute_reply":"2023-10-07T11:24:46.638260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot video_id distribution ( number of images per video)\nplt.figure(figsize=(10,10))\nsns.histplot(data=train, x=\"video_id\")","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:46.640284Z","iopub.execute_input":"2023-10-07T11:24:46.640778Z","iopub.status.idle":"2023-10-07T11:24:47.098967Z","shell.execute_reply.started":"2023-10-07T11:24:46.640750Z","shell.execute_reply":"2023-10-07T11:24:47.097958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adding image path from train_images folder to the dataset images (train folder)\ntrain[\"image_path\"] = '/kaggle/input/tensorflow-great-barrier-reef/train_images/video_'+train[\"video_id\"].astype(str)+'/'+train[\"image_id\"].apply(lambda x: x.split(\"-\")[1])+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:47.103073Z","iopub.execute_input":"2023-10-07T11:24:47.105283Z","iopub.status.idle":"2023-10-07T11:24:47.156136Z","shell.execute_reply.started":"2023-10-07T11:24:47.105250Z","shell.execute_reply":"2023-10-07T11:24:47.155096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:47.160659Z","iopub.execute_input":"2023-10-07T11:24:47.162817Z","iopub.status.idle":"2023-10-07T11:24:47.178238Z","shell.execute_reply.started":"2023-10-07T11:24:47.162762Z","shell.execute_reply":"2023-10-07T11:24:47.177383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting random images to check previous step\n\nrows, cols = 2, 2\nfig, axs = plt.subplots(rows, cols, figsize=(15,10))\nfor i,ax in zip(train, axs.ravel()):\n    random_image = random.randint(0,len(train)-1)\n    img = mpimg.imread(train[\"image_path\"][random_image])\n    ax.imshow(img)\n    ax.set_title(f'Image ID: {train[\"image_id\"][random_image]}',{\"fontsize\": 20})","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:47.181845Z","iopub.execute_input":"2023-10-07T11:24:47.182577Z","iopub.status.idle":"2023-10-07T11:24:49.714734Z","shell.execute_reply.started":"2023-10-07T11:24:47.182547Z","shell.execute_reply":"2023-10-07T11:24:49.713930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploring Annotations\n\n-  Annotations have the following format: [{'x': 645, 'y': 182, 'width': 41, 'height': 45}]\n-  There can be multiple records in one list\n\n","metadata":{}},{"cell_type":"code","source":"len(train[\"annotations\"])  ","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:49.715837Z","iopub.execute_input":"2023-10-07T11:24:49.716632Z","iopub.status.idle":"2023-10-07T11:24:49.722686Z","shell.execute_reply.started":"2023-10-07T11:24:49.716602Z","shell.execute_reply":"2023-10-07T11:24:49.722006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"annotations\"].dtype","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:49.723842Z","iopub.execute_input":"2023-10-07T11:24:49.724600Z","iopub.status.idle":"2023-10-07T11:24:49.736887Z","shell.execute_reply.started":"2023-10-07T11:24:49.724569Z","shell.execute_reply":"2023-10-07T11:24:49.736175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.annotations.describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:49.738297Z","iopub.execute_input":"2023-10-07T11:24:49.738993Z","iopub.status.idle":"2023-10-07T11:24:49.760919Z","shell.execute_reply.started":"2023-10-07T11:24:49.738965Z","shell.execute_reply":"2023-10-07T11:24:49.760115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.annotations.unique()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:49.762262Z","iopub.execute_input":"2023-10-07T11:24:49.762844Z","iopub.status.idle":"2023-10-07T11:24:49.772702Z","shell.execute_reply.started":"2023-10-07T11:24:49.762815Z","shell.execute_reply":"2023-10-07T11:24:49.771863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#calculate records with and without annotations\nrecords_with_annotations = train[train[\"annotations\"] != \"[]\"]\nnum_records_with_annotations = records_with_annotations[\"annotations\"].count()\n\nrecords_without_annotations = train[train[\"annotations\"] == \"[]\"]\nnum_records_without_annotations = records_without_annotations[\"annotations\"].count()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:49.774242Z","iopub.execute_input":"2023-10-07T11:24:49.774917Z","iopub.status.idle":"2023-10-07T11:24:49.791365Z","shell.execute_reply.started":"2023-10-07T11:24:49.774889Z","shell.execute_reply":"2023-10-07T11:24:49.790481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot records annotation distribution\nsns.barplot(x = [\"record with annotations\", \"record without annotations\"], y = [num_records_with_annotations, num_records_without_annotations], palette = \"colorblind\")\nplt.title(\"Records Annotation Distribution\", fontsize = 30)\nplt.xlabel(\"Annotation\", fontsize = 15)\nplt.ylabel(\"Count\", fontsize = 15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:49.792669Z","iopub.execute_input":"2023-10-07T11:24:49.792994Z","iopub.status.idle":"2023-10-07T11:24:50.071896Z","shell.execute_reply.started":"2023-10-07T11:24:49.792967Z","shell.execute_reply":"2023-10-07T11:24:50.071091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Records with annotations\nrecords_with_annotations","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:50.073016Z","iopub.execute_input":"2023-10-07T11:24:50.073823Z","iopub.status.idle":"2023-10-07T11:24:50.094827Z","shell.execute_reply.started":"2023-10-07T11:24:50.073773Z","shell.execute_reply":"2023-10-07T11:24:50.093656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Number of records without annotations\nprint(\"There are \"+ str(num_records_without_annotations)+ \" records without annotations and \"+ str(num_records_with_annotations)+ \" records with annotations.\")\n            ","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:50.096200Z","iopub.execute_input":"2023-10-07T11:24:50.096598Z","iopub.status.idle":"2023-10-07T11:24:50.109154Z","shell.execute_reply.started":"2023-10-07T11:24:50.096553Z","shell.execute_reply":"2023-10-07T11:24:50.107667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calculate the number of total annotations within the frame and adding to the data\ntrain[\"no_annotations\"] = train[\"annotations\"].apply(lambda x: len(eval(x)))","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:50.110484Z","iopub.execute_input":"2023-10-07T11:24:50.110922Z","iopub.status.idle":"2023-10-07T11:24:50.414541Z","shell.execute_reply.started":"2023-10-07T11:24:50.110891Z","shell.execute_reply":"2023-10-07T11:24:50.413644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Total number of annotations\ntot=0\nfor i in train[\"video_frame\"]:\n    if train[\"no_annotations\"][i] != 0:\n        tot = tot+train[\"no_annotations\"][i]\n\nprint(\"There are \" + str(tot)+ \" annotations in total\")","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:50.415981Z","iopub.execute_input":"2023-10-07T11:24:50.416290Z","iopub.status.idle":"2023-10-07T11:24:50.570958Z","shell.execute_reply.started":"2023-10-07T11:24:50.416262Z","shell.execute_reply":"2023-10-07T11:24:50.569963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### There are a lot of records without annotations, this can make it difficult creating a good model. But, previous information are useful to check upon the model training part.","metadata":{}},{"cell_type":"code","source":"#Plot annotation length distribution for records that do have annotations\n\nsns.countplot(x=records_with_annotations[\"annotations\"].apply(lambda x: len(x)).value_counts(),palette = \"colorblind\")\nplt.title(\"Annotation Length Distribution\", fontsize = 30)\nplt.xlabel(\"Annotation\", fontsize = 15)\nplt.ylabel(\"Count\", fontsize = 15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:50.572131Z","iopub.execute_input":"2023-10-07T11:24:50.572934Z","iopub.status.idle":"2023-10-07T11:24:51.048825Z","shell.execute_reply.started":"2023-10-07T11:24:50.572903Z","shell.execute_reply":"2023-10-07T11:24:51.047986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Annotation distribution per video_id\n\nvideo_1 = (records_with_annotations[records_with_annotations[\"video_id\"] == 0][\"annotations\"]).count()\nvideo_2 = (records_with_annotations[records_with_annotations[\"video_id\"] == 1][\"annotations\"]).count()\nvideo_3 = (records_with_annotations[records_with_annotations[\"video_id\"] == 2][\"annotations\"]).count()\n\nax = sns.barplot(x=['Video id: 0', 'Video id: 1', 'Video id: 2'], y=[video_1, video_2, video_3])\nax.set_ylabel('Count')","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:51.049943Z","iopub.execute_input":"2023-10-07T11:24:51.050821Z","iopub.status.idle":"2023-10-07T11:24:51.244426Z","shell.execute_reply.started":"2023-10-07T11:24:51.050771Z","shell.execute_reply":"2023-10-07T11:24:51.243518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Video with ID 2 has the least amount of annotations record.","metadata":{}},{"cell_type":"markdown","source":"## Detecting object with the help of a bounding box from a random image file","metadata":{}},{"cell_type":"markdown","source":"Each record in the annotation represents a bounding box. We can see from above exploration that one annotation list can have multiple records. So, this correspond to multiple bounding boxes.\nIn order to draw bounding box we must transform the annotations to list. annotations is in a string format. For bounding box the indices must be integers.","metadata":{}},{"cell_type":"code","source":"#Show image and annotations if applicable\ndef show_image(path, annot, axs=None):\n    '''Shows an image and marks any COTS annotated within the frame.\n    path: full path to the .jpg image\n    annot: string of the annotation for the coordinates of COTS'''\n    \n    # This is in case we plot only 1 image\n    if axs==None:\n        fig, axs = plt.subplots(figsize=(23, 8))\n    \n    img = plt.imread(path)\n    axs.imshow(img)\n\n    if annot:\n        for a in eval(annot):\n            rect = patches.Rectangle((a[\"x\"], a[\"y\"]), a[\"width\"], a[\"height\"], \n                                     linewidth=3, edgecolor=\"#FF6103\", facecolor='none')\n            axs.add_patch(rect)\n\n    axs.axis(\"off\")\n ","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:51.245557Z","iopub.execute_input":"2023-10-07T11:24:51.246054Z","iopub.status.idle":"2023-10-07T11:24:51.252689Z","shell.execute_reply.started":"2023-10-07T11:24:51.246025Z","shell.execute_reply":"2023-10-07T11:24:51.251904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show only 1 image as example\npath = list(train[train[\"no_annotations\"]==0][\"image_path\"])[0]\nannot = list(train[train[\"no_annotations\"]==0][\"annotations\"])[0]\n\n\nprint(\"Path:\",path)\nprint(\"Annotation:\", annot)\nprint(\"Frame:\")\nshow_image(path, annot, axs=None)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:51.258236Z","iopub.execute_input":"2023-10-07T11:24:51.258512Z","iopub.status.idle":"2023-10-07T11:24:51.980938Z","shell.execute_reply.started":"2023-10-07T11:24:51.258492Z","shell.execute_reply":"2023-10-07T11:24:51.979832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show only 1 image as example\npath = list(train[train[\"no_annotations\"]==18][\"image_path\"])[0]\nannot = list(train[train[\"no_annotations\"]==18][\"annotations\"])[0]\n\n\nprint(\"Path:\",path)\nprint(\"Annotation:\", annot)\nprint(\"Frame:\")\nshow_image(path, annot, axs=None)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:51.982543Z","iopub.execute_input":"2023-10-07T11:24:51.983248Z","iopub.status.idle":"2023-10-07T11:24:52.700333Z","shell.execute_reply.started":"2023-10-07T11:24:51.983219Z","shell.execute_reply":"2023-10-07T11:24:52.699235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_multiple_images(seq_id, frame_no):\n    '''Shows multiple images within a sequence.\n    seq_id: a number corresponding with the sequence unique ID\n    frame_no: a list containing the first and last frame to plot'''\n    \n    # Select image paths & their annotations\n    paths = list(train[(train[\"sequence\"]==seq_id) & \n                 (train[\"sequence_frame\"]>=frame_no[0]) & \n                 (train[\"sequence_frame\"]<=frame_no[1])][\"image_path\"])\n    annotations = list(train[(train[\"sequence\"]==seq_id) & \n                 (train[\"sequence_frame\"]>=frame_no[0]) & \n                 (train[\"sequence_frame\"]<=frame_no[1])][\"annotations\"])\n\n    # Plot\n    fig, axs = plt.subplots(2, 3, figsize=(23, 10))\n    axs = axs.flatten()\n    fig.suptitle(f\"Showing consecutive frames for Sequence ID: {seq_id}\", fontsize = 20)\n\n    for k, (path, annot) in enumerate(zip(paths, annotations)):\n        axs[k].set_title(f\"Frame No: {frame_no[0]+k}\", fontsize = 12)\n        show_image(path, annot, axs[k])\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:52.702175Z","iopub.execute_input":"2023-10-07T11:24:52.702729Z","iopub.status.idle":"2023-10-07T11:24:52.710556Z","shell.execute_reply.started":"2023-10-07T11:24:52.702700Z","shell.execute_reply":"2023-10-07T11:24:52.709692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#example\nseq_id = 44160\nframe_no = [51, 56]\n\nshow_multiple_images(seq_id, frame_no)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:52.711976Z","iopub.execute_input":"2023-10-07T11:24:52.712538Z","iopub.status.idle":"2023-10-07T11:24:55.107258Z","shell.execute_reply.started":"2023-10-07T11:24:52.712511Z","shell.execute_reply":"2023-10-07T11:24:55.106091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#example\nseq_id = 59337\nframe_no = [38, 43]\n\nshow_multiple_images(seq_id, frame_no)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:55.111319Z","iopub.execute_input":"2023-10-07T11:24:55.111952Z","iopub.status.idle":"2023-10-07T11:24:57.478452Z","shell.execute_reply.started":"2023-10-07T11:24:55.111910Z","shell.execute_reply":"2023-10-07T11:24:57.477304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#compare images with the same number of annotations\ndef plot_comparison(no_annot, state=24):\n    \n    # Select image paths & their annotations\n    paths_compare = list(train[train[\"no_annotations\"]==no_annot]\\\n                         .sample(n=9, random_state=state)[\"image_path\"])\n    annotations_compare = list(train[train[\"no_annotations\"]==no_annot]\\\n                               .sample(n=9, random_state=state)[\"annotations\"])\n\n    # Plot\n    fig, axs = plt.subplots(3, 3, figsize=(23, 13))\n    axs = axs.flatten()\n    fig.suptitle(f\"{no_annot} annotations\", fontsize = 20)\n\n    for k, (path, annot) in enumerate(zip(paths_compare, annotations_compare)):\n        video_id = path.split(\"/\")[4]\n        frame_id = path.split(\"/\")[-1].split(\".\")[0]\n        \n        axs[k].set_title(f\"{video_id} | Frame {frame_id}\",\n                         fontsize = 12)\n        show_image(path, annot, axs[k])\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:57.480322Z","iopub.execute_input":"2023-10-07T11:24:57.481045Z","iopub.status.idle":"2023-10-07T11:24:57.492471Z","shell.execute_reply.started":"2023-10-07T11:24:57.481003Z","shell.execute_reply":"2023-10-07T11:24:57.491694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# No annotations example\nno_annot = 0\nplot_comparison(no_annot, state=24)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:24:57.493763Z","iopub.execute_input":"2023-10-07T11:24:57.494275Z","iopub.status.idle":"2023-10-07T11:25:00.549189Z","shell.execute_reply.started":"2023-10-07T11:24:57.494242Z","shell.execute_reply":"2023-10-07T11:25:00.548024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#10 annotations example\nno_annot = 10\nplot_comparison(no_annot, state=24)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:25:00.551063Z","iopub.execute_input":"2023-10-07T11:25:00.551779Z","iopub.status.idle":"2023-10-07T11:25:03.794810Z","shell.execute_reply.started":"2023-10-07T11:25:00.551736Z","shell.execute_reply":"2023-10-07T11:25:03.793575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#formatting annotations\ndef format_annotations(x):\n    '''Changes annotations from format {x, y, width, height} to {x1, y1, x2, y2}.\n    x: a string of the initial format.'''\n    \n    annotations = eval(x)\n    new_annotations = []\n\n    if annotations:\n        for annot in annotations:\n            new_annotations.append([annot[\"x\"],\n                                    annot[\"y\"],\n                                    annot[\"x\"]+annot[\"width\"],\n                                    annot[\"y\"]+annot[\"height\"]\n                                   ])\n    \n    if new_annotations: return str(new_annotations)\n    else: return \"[]\"","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:25:03.796446Z","iopub.execute_input":"2023-10-07T11:25:03.797139Z","iopub.status.idle":"2023-10-07T11:25:03.807014Z","shell.execute_reply.started":"2023-10-07T11:25:03.797098Z","shell.execute_reply":"2023-10-07T11:25:03.806022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a new column with the new formated annotations\ntrain[\"f_annotations\"] = train[\"annotations\"].apply(lambda x: format_annotations(x))","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:25:03.808293Z","iopub.execute_input":"2023-10-07T11:25:03.809504Z","iopub.status.idle":"2023-10-07T11:25:04.171699Z","shell.execute_reply.started":"2023-10-07T11:25:03.809466Z","shell.execute_reply":"2023-10-07T11:25:04.170837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### We will be using Yolov8 model, so we need the right format for annotations (labels):","metadata":{}},{"cell_type":"code","source":"def format_yolo_annotations(x):\n    '''Changes annotations from format {x, y, width, height} to {x1, y1, x2, y2}.\n    x: a string of the initial format.'''\n    \n    annotations = eval(x)\n    new_annotations = ''\n    img_h=720\n    img_w=1280\n\n    if annotations:\n        for annot in annotations:\n            current_category = 0\n            x = annot[\"x\"]\n            y = annot[\"y\"]\n            w = annot[\"width\"]\n            h = annot[\"height\"]\n            \n            # Finding midpoints\n            x_centre = (x + (x+w))/2\n            y_centre = (y + (y+h))/2\n\n            # Normalization\n            x_centre = x_centre / img_w\n            y_centre = y_centre / img_h\n            w = w / img_w\n            h = h / img_h\n\n            # Limiting upto fix number of decimal places\n            x_centre = format(x_centre, '.6f')\n            y_centre = format(y_centre, '.6f')\n            w = format(w, '.6f')\n            h = format(h, '.6f')\n        \n            new_annotations = new_annotations + (f\"{current_category} {x_centre} {y_centre} {w} {h}\\n\")\n\n    \n    if new_annotations: return new_annotations\n    else: return ''","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:25:04.173126Z","iopub.execute_input":"2023-10-07T11:25:04.173453Z","iopub.status.idle":"2023-10-07T11:25:04.180072Z","shell.execute_reply.started":"2023-10-07T11:25:04.173423Z","shell.execute_reply":"2023-10-07T11:25:04.178851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a new column with the new formated annotations\ntrain[\"yolo_annot\"] = train[\"annotations\"].apply(lambda x: format_yolo_annotations(x))","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:25:04.181282Z","iopub.execute_input":"2023-10-07T11:25:04.181830Z","iopub.status.idle":"2023-10-07T11:25:04.460880Z","shell.execute_reply.started":"2023-10-07T11:25:04.181802Z","shell.execute_reply":"2023-10-07T11:25:04.460006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Using Yolov8 model","metadata":{}},{"cell_type":"code","source":"train.tail()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:25:04.462067Z","iopub.execute_input":"2023-10-07T11:25:04.462392Z","iopub.status.idle":"2023-10-07T11:25:04.474009Z","shell.execute_reply.started":"2023-10-07T11:25:04.462363Z","shell.execute_reply":"2023-10-07T11:25:04.473167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create images directory for yolov8\nnewpath = '/kaggle/working/images/train/'\nif not os.path.exists(newpath):\n    os.makedirs(newpath)\n    \n#select img with annotations for .jpg\nimg_annot_jpg = []\nfor i,data in train.iterrows() :\n    if not data[\"annotations\"] == \"[]\":\n        img_annot_jpg.append(data[\"image_path\"])\n\nprint(len(img_annot_jpg)) #must be the same as before","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:25:04.475166Z","iopub.execute_input":"2023-10-07T11:25:04.475713Z","iopub.status.idle":"2023-10-07T11:25:05.292045Z","shell.execute_reply.started":"2023-10-07T11:25:04.475683Z","shell.execute_reply":"2023-10-07T11:25:05.291144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#copy images to right directory for yolov8\nimport shutil\ndir = '/kaggle/input/tensorflow-great-barrier-reef/train_images/'\ndest = '/kaggle/working/images/train/'\n\ncount=0\n\nfor file in os.listdir(dir):\n        folder = dir + file\n        for img in os.listdir(folder):\n            img_path = os.path.join(folder,img)\n            if img_path in img_annot_jpg:\n                count=count+1\n                shutil.copy(img_path, dest)\nprint(count) #must be the same as before","metadata":{"execution":{"iopub.status.busy":"2023-10-07T11:25:05.293302Z","iopub.execute_input":"2023-10-07T11:25:05.294101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create labels directory for yolov8\nnewpath = '/kaggle/working/labels/train/'\nif not os.path.exists(newpath):\n    os.makedirs(newpath)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#create labels directory for yolov8\ndest_labels = '/kaggle/working/labels/train/'\nif not os.path.exists(dest_labels):\n    os.makedirs(dest_labels)\n\ncount=0\n#select records with annotations for .txt and copy them in the right directory\nfor i,data in train.iterrows() :\n    if not data[\"annotations\"] == \"[]\":\n        count=count+1\n        file =  str(data[\"video_frame\"]) + \".txt\"\n        f = open(os.path.join(dest_labels,file),\"w\")\n        f.write(data[\"yolo_annot\"])\n        f.close()\n        \nprint(count) #must be the same as before","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check specific image for annotations/labels\nnum= 100 #put the choosen number to be checked\npath_l = '/kaggle/working/labels/train/'\npath_i = '/kaggle/working/images/train/'\nannot =  list(train[train[\"video_frame\"]==num][\"annotations\"])[0]\n\n\nlabel = str(num)+\".txt\"\nimage = str(num)+\".jpg\"\nprint (\"Image real path: \" + train[\"image_path\"][num])\nprint (\"Image yolo path: \" + os.path.join(path_i,image))\nprint(\"Annotations: \" + train[\"annotations\"][num])\nprint(\"\\n\")\nprint(\"Label yolo path: \" + os.path.join(path_l,label))\nf = open(os.path.join(path_l, label), 'r')\nprint(\"Annotations yolo format: \" +f.read())\nf.close()\nshow_image(os.path.join(path_i,image), annot, axs=None)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show only 1 image as example\npath = '/kaggle/working/images/train/9470.jpg'\nannot =  list(train[train[\"video_frame\"]==9470][\"annotations\"])[0]\n\n\nprint(\"Path:\",path)\nprint(\"Annotation:\", annot)\nprint(\"Frame:\")\nshow_image(path, annot, axs=None)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training:","metadata":{}},{"cell_type":"code","source":"!pip install ultralytics\nimport ultralytics\nultralytics.checks()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\n\n\n#Load model\nmodel = YOLO(\"yolov8n.yaml\") #build a new model from scratch\n\n#Use the model\nresults = model.train(data = \"/kaggle/input/data-settings/config.yml\", epochs=100) #train the model","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show Results","metadata":{}},{"cell_type":"code","source":"for file in os.listdir('/kaggle/working/runs/detect/train/'):\n    print(file)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = pd.read_csv('/kaggle/working/runs/detect/train/results.csv')\nres","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npath = '/kaggle/working/runs/detect/train/'\ndef show_res(file):\n    path = '/kaggle/working/runs/detect/train/'\n    fig, axs = plt.subplots(figsize=(23, 8))\n    file_path = os.path.join(path,file)\n    img = plt.imread(file_path)\n    fig.tight_layout()\n\n    axs.set_title(file, fontsize=20)\n    axs.imshow(img)\n\n\nfor file in os.listdir(path):\n    if file.endswith(('.png', '.jpg', '.jpeg')):\n        show_res(file)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}