{"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":"#导入数据","metadata":{"papermill":{"duration":0.015354,"end_time":"2021-11-24T12:02:38.250327","exception":false,"start_time":"2021-11-24T12:02:38.234973","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import pandas as pd\n\ntrain = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ntrain","metadata":{"papermill":{"duration":0.093043,"end_time":"2021-11-24T12:02:38.358878","exception":false,"start_time":"2021-11-24T12:02:38.265835","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:27.795891Z","iopub.execute_input":"2021-12-03T13:05:27.796187Z","iopub.status.idle":"2021-12-03T13:05:27.863911Z","shell.execute_reply.started":"2021-12-03T13:05:27.796147Z","shell.execute_reply":"2021-12-03T13:05:27.862983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 观察数据","metadata":{"papermill":{"duration":0.016208,"end_time":"2021-11-24T12:02:38.391639","exception":false,"start_time":"2021-11-24T12:02:38.375431","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## 列车数据基本信息","metadata":{"papermill":{"duration":0.016817,"end_time":"2021-11-24T12:02:38.424858","exception":false,"start_time":"2021-11-24T12:02:38.408041","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train.info()","metadata":{"papermill":{"duration":0.045379,"end_time":"2021-11-24T12:02:38.486598","exception":false,"start_time":"2021-11-24T12:02:38.441219","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:27.865297Z","iopub.execute_input":"2021-12-03T13:05:27.865705Z","iopub.status.idle":"2021-12-03T13:05:27.885270Z","shell.execute_reply.started":"2021-12-03T13:05:27.865668Z","shell.execute_reply":"2021-12-03T13:05:27.884548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 检查重复数据","metadata":{"papermill":{"duration":0.016115,"end_time":"2021-11-24T12:02:38.520915","exception":false,"start_time":"2021-11-24T12:02:38.5048","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train.duplicated().sum()","metadata":{"papermill":{"duration":0.037545,"end_time":"2021-11-24T12:02:38.575126","exception":false,"start_time":"2021-11-24T12:02:38.537581","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:27.886521Z","iopub.execute_input":"2021-12-03T13:05:27.886746Z","iopub.status.idle":"2021-12-03T13:05:27.903261Z","shell.execute_reply.started":"2021-12-03T13:05:27.886718Z","shell.execute_reply":"2021-12-03T13:05:27.902535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 功能概括","metadata":{"papermill":{"duration":0.016274,"end_time":"2021-11-24T12:02:38.641417","exception":false,"start_time":"2021-11-24T12:02:38.625143","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.026054,"end_time":"2021-11-24T12:02:38.684304","exception":false,"start_time":"2021-11-24T12:02:38.65825","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:27.904479Z","iopub.execute_input":"2021-12-03T13:05:27.904698Z","iopub.status.idle":"2021-12-03T13:05:27.911019Z","shell.execute_reply.started":"2021-12-03T13:05:27.904672Z","shell.execute_reply":"2021-12-03T13:05:27.910212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resumetable(train)","metadata":{"papermill":{"duration":0.049292,"end_time":"2021-11-24T12:02:38.750352","exception":false,"start_time":"2021-11-24T12:02:38.70106","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:27.912857Z","iopub.execute_input":"2021-12-03T13:05:27.913463Z","iopub.status.idle":"2021-12-03T13:05:27.956949Z","shell.execute_reply.started":"2021-12-03T13:05:27.913428Z","shell.execute_reply":"2021-12-03T13:05:27.956306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 基础工程","metadata":{"papermill":{"duration":0.01679,"end_time":"2021-11-24T12:02:38.784598","exception":false,"start_time":"2021-11-24T12:02:38.767808","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## 向下转换","metadata":{"papermill":{"duration":0.016691,"end_time":"2021-11-24T12:02:38.818357","exception":false,"start_time":"2021-11-24T12:02:38.801666","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.026618,"end_time":"2021-11-24T12:02:38.862076","exception":false,"start_time":"2021-11-24T12:02:38.835458","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:27.957937Z","iopub.execute_input":"2021-12-03T13:05:27.958528Z","iopub.status.idle":"2021-12-03T13:05:27.966210Z","shell.execute_reply.started":"2021-12-03T13:05:27.958495Z","shell.execute_reply":"2021-12-03T13:05:27.965388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = downcast(train)","metadata":{"papermill":{"duration":0.036196,"end_time":"2021-11-24T12:02:38.915663","exception":false,"start_time":"2021-11-24T12:02:38.879467","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:27.967568Z","iopub.execute_input":"2021-12-03T13:05:27.968119Z","iopub.status.idle":"2021-12-03T13:05:27.987065Z","shell.execute_reply.started":"2021-12-03T13:05:27.968074Z","shell.execute_reply":"2021-12-03T13:05:27.986441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 特征工程","metadata":{"papermill":{"duration":0.017068,"end_time":"2021-11-24T12:02:38.950563","exception":false,"start_time":"2021-11-24T12:02:38.933495","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.327369,"end_time":"2021-11-24T12:02:39.295514","exception":false,"start_time":"2021-11-24T12:02:38.968145","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:27.988344Z","iopub.execute_input":"2021-12-03T13:05:27.989140Z","iopub.status.idle":"2021-12-03T13:05:28.699230Z","shell.execute_reply.started":"2021-12-03T13:05:27.989096Z","shell.execute_reply":"2021-12-03T13:05:28.698288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"papermill":{"duration":0.041067,"end_time":"2021-11-24T12:02:39.354639","exception":false,"start_time":"2021-11-24T12:02:39.313572","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:28.700423Z","iopub.execute_input":"2021-12-03T13:05:28.700770Z","iopub.status.idle":"2021-12-03T13:05:28.716346Z","shell.execute_reply.started":"2021-12-03T13:05:28.700736Z","shell.execute_reply":"2021-12-03T13:05:28.715414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 检查带有边框的帧数","metadata":{"papermill":{"duration":0.021109,"end_time":"2021-11-24T12:02:39.40369","exception":false,"start_time":"2021-11-24T12:02:39.382581","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train[train['num_bboxes'] > 0]","metadata":{"papermill":{"duration":0.051725,"end_time":"2021-11-24T12:02:39.475147","exception":false,"start_time":"2021-11-24T12:02:39.423422","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:28.717950Z","iopub.execute_input":"2021-12-03T13:05:28.718165Z","iopub.status.idle":"2021-12-03T13:05:28.747078Z","shell.execute_reply.started":"2021-12-03T13:05:28.718139Z","shell.execute_reply":"2021-12-03T13:05:28.746108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 验证是否有损坏的数据","metadata":{"papermill":{"duration":0.0193,"end_time":"2021-11-24T12:02:39.514482","exception":false,"start_time":"2021-11-24T12:02:39.495182","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from os import listdir\nfrom PIL import Image\nDATA_PATH = '/kaggle/input/tensorflow-great-barrier-reef/'\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":{"papermill":{"duration":232.121284,"end_time":"2021-11-24T12:06:31.654769","exception":false,"start_time":"2021-11-24T12:02:39.533485","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:05:28.749434Z","iopub.execute_input":"2021-12-03T13:05:28.749791Z","iopub.status.idle":"2021-12-03T13:07:59.687039Z","shell.execute_reply.started":"2021-12-03T13:05:28.749744Z","shell.execute_reply":"2021-12-03T13:07:59.686234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 用边框绘制帧图像","metadata":{"papermill":{"duration":0.018866,"end_time":"2021-11-24T12:06:31.692985","exception":false,"start_time":"2021-11-24T12:06:31.674119","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## 加载带有注释的图像序列","metadata":{"papermill":{"duration":0.018411,"end_time":"2021-11-24T12:06:31.730386","exception":false,"start_time":"2021-11-24T12:06:31.711975","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.031604,"end_time":"2021-11-24T12:06:31.78101","exception":false,"start_time":"2021-11-24T12:06:31.749406","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:07:59.688677Z","iopub.execute_input":"2021-12-03T13:07:59.688942Z","iopub.status.idle":"2021-12-03T13:07:59.697973Z","shell.execute_reply.started":"2021-12-03T13:07:59.688909Z","shell.execute_reply":"2021-12-03T13:07:59.697453Z"},"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":{"papermill":{"duration":2.814046,"end_time":"2021-11-24T12:06:34.61438","exception":false,"start_time":"2021-11-24T12:06:31.800334","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:07:59.698961Z","iopub.execute_input":"2021-12-03T13:07:59.699270Z","iopub.status.idle":"2021-12-03T13:08:01.987690Z","shell.execute_reply.started":"2021-12-03T13:07:59.699242Z","shell.execute_reply":"2021-12-03T13:08:01.986725Z"},"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":{"papermill":{"duration":1.723974,"end_time":"2021-11-24T12:06:36.359607","exception":false,"start_time":"2021-11-24T12:06:34.635633","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:08:01.989261Z","iopub.execute_input":"2021-12-03T13:08:01.989603Z","iopub.status.idle":"2021-12-03T13:08:03.375402Z","shell.execute_reply.started":"2021-12-03T13:08:01.989556Z","shell.execute_reply":"2021-12-03T13:08:03.374397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 图像动画","metadata":{"papermill":{"duration":0.106289,"end_time":"2021-11-24T12:06:36.576856","exception":false,"start_time":"2021-11-24T12:06:36.470567","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.117912,"end_time":"2021-11-24T12:06:36.800948","exception":false,"start_time":"2021-11-24T12:06:36.683036","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:08:03.376877Z","iopub.execute_input":"2021-12-03T13:08:03.377260Z","iopub.status.idle":"2021-12-03T13:08:03.385956Z","shell.execute_reply.started":"2021-12-03T13:08:03.377214Z","shell.execute_reply":"2021-12-03T13:08:03.384667Z"},"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":{"papermill":{"duration":11.976804,"end_time":"2021-11-24T12:06:48.884476","exception":false,"start_time":"2021-11-24T12:06:36.907672","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-03T13:08:03.387146Z","iopub.execute_input":"2021-12-03T13:08:03.387422Z","iopub.status.idle":"2021-12-03T13:08:15.212898Z","shell.execute_reply.started":"2021-12-03T13:08:03.387392Z","shell.execute_reply":"2021-12-03T13:08:15.211970Z"},"trusted":true},"execution_count":null,"outputs":[]}]}