{"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":"code","source":"import pandas as pd\n\nDATA_PATH = '/kaggle/input/tensorflow-great-barrier-reef/'\ntrain = pd.read_csv(DATA_PATH + 'train.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:18:06.093432Z","iopub.execute_input":"2021-12-03T04:18:06.094106Z","iopub.status.idle":"2021-12-03T04:18:06.196862Z","shell.execute_reply.started":"2021-12-03T04:18:06.094002Z","shell.execute_reply":"2021-12-03T04:18:06.196297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Basic information about train data","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:18:11.809801Z","iopub.execute_input":"2021-12-03T04:18:11.810095Z","iopub.status.idle":"2021-12-03T04:18:11.83754Z","shell.execute_reply.started":"2021-12-03T04:18:11.810058Z","shell.execute_reply":"2021-12-03T04:18:11.836918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check duplicated data","metadata":{}},{"cell_type":"code","source":"train.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:19:02.750117Z","iopub.execute_input":"2021-12-03T04:19:02.75091Z","iopub.status.idle":"2021-12-03T04:19:02.770973Z","shell.execute_reply.started":"2021-12-03T04:19:02.750863Z","shell.execute_reply":"2021-12-03T04:19:02.770091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There is no duplicated data in train data","metadata":{}},{"cell_type":"markdown","source":"## Feature summary","metadata":{}},{"cell_type":"code","source":"def resumetable(df):\n    '''function to create feature summary'''\n    print(f'Shape: {df.shape}')\n    summary = pd.DataFrame(df.dtypes, columns=['Data Type'])\n    summary = summary.reset_index()\n    summary = summary.rename(columns={'index': 'Features'})\n    summary['Num of Null Value'] = df.isnull().sum().values\n    summary['Num of Unique Value'] = df.nunique().values\n    summary['1st Value'] = df.loc[0].values\n    summary['2nd Value'] = df.loc[1].values\n    summary['3rd Value'] = df.loc[2].values\n    return summary","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:19:11.521269Z","iopub.execute_input":"2021-12-03T04:19:11.522018Z","iopub.status.idle":"2021-12-03T04:19:11.528269Z","shell.execute_reply.started":"2021-12-03T04:19:11.521976Z","shell.execute_reply":"2021-12-03T04:19:11.527469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resumetable(train)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:19:16.426001Z","iopub.execute_input":"2021-12-03T04:19:16.426309Z","iopub.status.idle":"2021-12-03T04:19:16.463489Z","shell.execute_reply.started":"2021-12-03T04:19:16.426264Z","shell.execute_reply":"2021-12-03T04:19:16.462692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Downcast","metadata":{}},{"cell_type":"code","source":"def downcast(df, verbose=True):\n    start_mem = df.memory_usage().sum() / 1024**2\n    for col in df.columns:\n        dtype_name = df[col].dtype.name\n        if dtype_name == 'object':\n            pass\n        elif dtype_name == 'bool':\n            df[col] = df[col].astype('int8')\n        elif dtype_name.startswith('int') or (df[col].round() == df[col]).all():\n            df[col] = pd.to_numeric(df[col], downcast='integer')\n        else:\n            df[col] = pd.to_numeric(df[col], downcast='float')\n    end_mem = df.memory_usage().sum() / 1024**2\n    if verbose:\n        print('{:.1f}% Compressed'.format(100 * (start_mem - end_mem) / start_mem))\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:19:20.120124Z","iopub.execute_input":"2021-12-03T04:19:20.120556Z","iopub.status.idle":"2021-12-03T04:19:20.128254Z","shell.execute_reply.started":"2021-12-03T04:19:20.120521Z","shell.execute_reply":"2021-12-03T04:19:20.127308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = downcast(train)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:19:23.2772Z","iopub.execute_input":"2021-12-03T04:19:23.277889Z","iopub.status.idle":"2021-12-03T04:19:23.293609Z","shell.execute_reply.started":"2021-12-03T04:19:23.277854Z","shell.execute_reply":"2021-12-03T04:19:23.29294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Engineering","metadata":{}},{"cell_type":"code","source":"import ast\n\n# Convert String to List Type\ntrain['annotations'] = train['annotations'].apply(ast.literal_eval)\n\n# Get the number of bounding boxes for each image\ntrain['num_bboxes'] = train['annotations'].apply(lambda x: len(x))","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:19:41.48217Z","iopub.execute_input":"2021-12-03T04:19:41.482559Z","iopub.status.idle":"2021-12-03T04:19:41.858095Z","shell.execute_reply.started":"2021-12-03T04:19:41.482529Z","shell.execute_reply":"2021-12-03T04:19:41.857297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:19:45.030406Z","iopub.execute_input":"2021-12-03T04:19:45.030672Z","iopub.status.idle":"2021-12-03T04:19:45.042989Z","shell.execute_reply.started":"2021-12-03T04:19:45.030644Z","shell.execute_reply":"2021-12-03T04:19:45.042185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check number of frames with bounding boxes","metadata":{}},{"cell_type":"code","source":"train[train['num_bboxes'] > 0]","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:19:48.143343Z","iopub.execute_input":"2021-12-03T04:19:48.143877Z","iopub.status.idle":"2021-12-03T04:19:48.171025Z","shell.execute_reply.started":"2021-12-03T04:19:48.143843Z","shell.execute_reply":"2021-12-03T04:19:48.170039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os import listdir\nfrom PIL import Image\n\ndef verify_images(video_id):\n    path = DATA_PATH + f'train_images/video_{video_id}/'    \n    for filename in listdir(path):\n        if filename.endswith('.jpg'):\n            try:\n                img = Image.open(path + filename)\n                img.verify() # Verify it is in fact an image\n            except (IOError, SyntaxError) as e:\n                print('Bad file:', filename) # Print out the names of corrupt files\n    print(f'Video {video_id} has all valid images. Verified!')\n\nfor video_id in range(3):\n    verify_images(video_id)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:19:51.733135Z","iopub.execute_input":"2021-12-03T04:19:51.73446Z","iopub.status.idle":"2021-12-03T04:20:22.478957Z","shell.execute_reply.started":"2021-12-03T04:19:51.734413Z","shell.execute_reply":"2021-12-03T04:20:22.477505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load sequence of images with annotations","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom PIL import ImageDraw\n\ndef fetch_image(df, video_id, frame_id):\n    # get frame\n    frame = df[(df['video_id'] == video_id) & (df['video_frame'] == frame_id)].iloc[0]\n    # get bounding_boxes\n    bounding_boxes = frame['annotations']\n    # open image\n    img = Image.open(DATA_PATH + f'train_images/video_{video_id}/{frame_id}.jpg')\n\n    for box in bounding_boxes:\n        x0, y0, x1, y1 = (box['x'], box['y'], box['x']+box['width'], box['y']+box['height'])\n        draw = ImageDraw.Draw(img)\n        draw.rectangle( (x0, y0, x1, y1), outline=180, width=5)\n    return img\n\ndef fetch_image_list(df, video_id, num_images, start_frame_idx):\n    image_list = [np.array(fetch_image(df, video_id, start_frame_idx + index)) for index in range(num_images)]\n\n    return image_list","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:20:24.30938Z","iopub.execute_input":"2021-12-03T04:20:24.310004Z","iopub.status.idle":"2021-12-03T04:20:24.320408Z","shell.execute_reply.started":"2021-12-03T04:20:24.309965Z","shell.execute_reply":"2021-12-03T04:20:24.319652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = fetch_image_list(train, video_id=0, num_images=80, start_frame_idx=25)\n\nprint(f'Number of images: {len(images)}')","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:20:28.435795Z","iopub.execute_input":"2021-12-03T04:20:28.436067Z","iopub.status.idle":"2021-12-03T04:20:31.89179Z","shell.execute_reply.started":"2021-12-03T04:20:28.43604Z","shell.execute_reply":"2021-12-03T04:20:31.890841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.gridspec as gridspec \n\ngrid = gridspec.GridSpec(4, 2) \nplt.figure(figsize=(18, 20))\n\nidx_list = [0, 5, 10, 15, 20, 25, 30, 35] \n\nfor i, idx in enumerate(idx_list): \n    ax = plt.subplot(grid[i])\n    plt.imshow(images[idx], interpolation='nearest')\n    ax.set_title(f'frame index {idx}')\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:20:34.936504Z","iopub.execute_input":"2021-12-03T04:20:34.93677Z","iopub.status.idle":"2021-12-03T04:20:36.547216Z","shell.execute_reply.started":"2021-12-03T04:20:34.936742Z","shell.execute_reply":"2021-12-03T04:20:36.546532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\ndef create_animation(imgs, frame_interval=130):\n    fig = plt.figure(figsize=(7, 4))\n    plt.axis('off')\n    img = plt.imshow(imgs[0])\n\n    def animate(i):\n        img.set_array(imgs[i])\n        return [img]\n\n    return animation.FuncAnimation(fig, animate, frames=len(imgs), interval=frame_interval)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:20:43.8201Z","iopub.execute_input":"2021-12-03T04:20:43.820382Z","iopub.status.idle":"2021-12-03T04:20:43.827264Z","shell.execute_reply.started":"2021-12-03T04:20:43.820354Z","shell.execute_reply":"2021-12-03T04:20:43.826236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frame_interval = 130 # set smaller number if you want to play fast, otherwise set bigger\n\ncreate_animation(images, frame_interval=frame_interval)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T04:20:57.713799Z","iopub.execute_input":"2021-12-03T04:20:57.714379Z","iopub.status.idle":"2021-12-03T04:21:09.46644Z","shell.execute_reply.started":"2021-12-03T04:20:57.714328Z","shell.execute_reply":"2021-12-03T04:21:09.465747Z"},"trusted":true},"execution_count":null,"outputs":[]}]}