{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-08T02:36:14.307361Z","iopub.execute_input":"2021-12-08T02:36:14.307627Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.core.display import display, HTML\n\nimport pandas as pd\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport glob\nimport os\nimport gc\nimport cv2\n\nfrom joblib import Parallel, delayed\n\nfrom sklearn import preprocessing, model_selection\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.preprocessing import QuantileTransformer\nfrom sklearn.metrics import r2_score\n\nimport matplotlib.pyplot as plt \nimport seaborn as sns\nimport numpy.matlib\nimport warnings\nfrom PIL import Image, ImageFilter\nwarnings.simplefilter('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:08:24.437286Z","iopub.execute_input":"2021-12-08T03:08:24.437566Z","iopub.status.idle":"2021-12-08T03:08:24.446040Z","shell.execute_reply.started":"2021-12-08T03:08:24.437532Z","shell.execute_reply":"2021-12-08T03:08:24.444658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"训练数据读取","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:08:27.745023Z","iopub.execute_input":"2021-12-08T03:08:27.745973Z","iopub.status.idle":"2021-12-08T03:08:27.797454Z","shell.execute_reply.started":"2021-12-08T03:08:27.745915Z","shell.execute_reply":"2021-12-08T03:08:27.796644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"image_id - 图像的 ID 代码，格式为 '{video_id}-{video_frame}' 注释 - 任何海星检测的边界框，字符串格式，可以直接用 Python 评估。","metadata":{}},{"cell_type":"code","source":"train.annotations.unique()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:08:48.937464Z","iopub.execute_input":"2021-12-08T03:08:48.937796Z","iopub.status.idle":"2021-12-08T03:08:48.947476Z","shell.execute_reply.started":"2021-12-08T03:08:48.937762Z","shell.execute_reply":"2021-12-08T03:08:48.946825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/tensorflow-great-barrier-reef/test.csv')\ntest","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:09:48.881171Z","iopub.execute_input":"2021-12-08T03:09:48.881830Z","iopub.status.idle":"2021-12-08T03:09:48.901430Z","shell.execute_reply.started":"2021-12-08T03:09:48.881791Z","shell.execute_reply":"2021-12-08T03:09:48.900447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"提交文件","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv('../input/tensorflow-great-barrier-reef/example_sample_submission.csv')\nsub","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:09:52.592708Z","iopub.execute_input":"2021-12-08T03:09:52.593190Z","iopub.status.idle":"2021-12-08T03:09:52.608280Z","shell.execute_reply.started":"2021-12-08T03:09:52.593152Z","shell.execute_reply":"2021-12-08T03:09:52.606806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_test = np.load('../input/tensorflow-great-barrier-reef/example_test.npy') \nsample_test[0][0]","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:13:19.806607Z","iopub.execute_input":"2021-12-08T03:13:19.807243Z","iopub.status.idle":"2021-12-08T03:13:19.825195Z","shell.execute_reply.started":"2021-12-08T03:13:19.807199Z","shell.execute_reply":"2021-12-08T03:13:19.824319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(30,20))\nfor i in range(3):\n    fig.add_subplot(4, 5, i+1)\n    plt.title(i)\n    plt.imshow(sample_test[i])","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:13:22.226384Z","iopub.execute_input":"2021-12-08T03:13:22.227011Z","iopub.status.idle":"2021-12-08T03:13:23.471150Z","shell.execute_reply.started":"2021-12-08T03:13:22.226970Z","shell.execute_reply":"2021-12-08T03:13:23.470055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"预计在测试集中看到大约 13,000 张图像。","metadata":{}},{"cell_type":"code","source":"len(train)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:13:27.501376Z","iopub.execute_input":"2021-12-08T03:13:27.502317Z","iopub.status.idle":"2021-12-08T03:13:27.509347Z","shell.execute_reply.started":"2021-12-08T03:13:27.502261Z","shell.execute_reply":"2021-12-08T03:13:27.508273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"anno = train[train.annotations!='[]']\nanno","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:13:32.264567Z","iopub.execute_input":"2021-12-08T03:13:32.265532Z","iopub.status.idle":"2021-12-08T03:13:32.290976Z","shell.execute_reply.started":"2021-12-08T03:13:32.265489Z","shell.execute_reply":"2021-12-08T03:13:32.290129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"按视频划分数据","metadata":{}},{"cell_type":"code","source":"video0 = anno[anno.video_id==0].reset_index(drop=True)\nvideo0","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:13:35.642426Z","iopub.execute_input":"2021-12-08T03:13:35.642791Z","iopub.status.idle":"2021-12-08T03:13:35.662171Z","shell.execute_reply.started":"2021-12-08T03:13:35.642752Z","shell.execute_reply":"2021-12-08T03:13:35.661226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"video1 = anno[anno.video_id==1].reset_index(drop=True)\nvideo1","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:14:30.792494Z","iopub.execute_input":"2021-12-08T03:14:30.792805Z","iopub.status.idle":"2021-12-08T03:14:30.809764Z","shell.execute_reply.started":"2021-12-08T03:14:30.792772Z","shell.execute_reply":"2021-12-08T03:14:30.808900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"video2 = anno[anno.video_id==2].reset_index(drop=True)\nvideo2","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:14:33.765324Z","iopub.execute_input":"2021-12-08T03:14:33.766120Z","iopub.status.idle":"2021-12-08T03:14:33.781316Z","shell.execute_reply.started":"2021-12-08T03:14:33.766064Z","shell.execute_reply":"2021-12-08T03:14:33.780765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = video0.annotations.loc[0][2:-2]\na = a.split(',')\na","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:14:36.441973Z","iopub.execute_input":"2021-12-08T03:14:36.442298Z","iopub.status.idle":"2021-12-08T03:14:36.449359Z","shell.execute_reply.started":"2021-12-08T03:14:36.442261Z","shell.execute_reply":"2021-12-08T03:14:36.448654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = int(a[0].split(':')[1])\ny = int(a[1].split(':')[1])\nwidth = int(a[2].split(':')[1])\nheight = int(a[3].split(':')[1])","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:14:39.395200Z","iopub.execute_input":"2021-12-08T03:14:39.395692Z","iopub.status.idle":"2021-12-08T03:14:39.400728Z","shell.execute_reply.started":"2021-12-08T03:14:39.395657Z","shell.execute_reply":"2021-12-08T03:14:39.400080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(30,20))\n\nvideo_imid = ('../input/tensorflow-great-barrier-reef/train_images/video_0/16' +'.jpg')\nimg = cv2.imread(video_imid)\n\nimg = cv2.rectangle(img,\n              pt1=(x, y),\n              pt2=(x+height, y-width),\n              color=(0, 255, 0),\n              thickness=3,\n              lineType=cv2.LINE_4,\n              shift=0)\n\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:14:42.640854Z","iopub.execute_input":"2021-12-08T03:14:42.641457Z","iopub.status.idle":"2021-12-08T03:14:44.438851Z","shell.execute_reply.started":"2021-12-08T03:14:42.641409Z","shell.execute_reply":"2021-12-08T03:14:44.437922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"把注解和照片合并起来展示一下","metadata":{}},{"cell_type":"code","source":"for i in range(15):\n    video0[f'x_{i}'] =str()\n    video0[f'y_{i}'] = str()\n    video0[f'width_{i}'] = str()\n    video0[f'height_{i}'] = str()\nvideo0['path'] =str()\n\nfor x in video0.index:\n    a = video0.annotations.loc[x][2:-2]\n    a = a.split(',')\n    \n    for i in range(int(len(a)/4)):\n        video0[f'x_{i}'][x] = int(a[0].split(':')[1])\n        video0[f'y_{i}'][x] = int(a[1].split(':')[1])\n        video0[f'width_{i}'][x] = int(a[2].split(':')[1])\n        video0[f'height_{i}'][x] = int(a[3].split(':')[1].split('}')[0])\n    video0['path'][x] = '../input/tensorflow-great-barrier-reef/train_images/video_0/' + str(video0.video_id.loc[i]) +'.jpg'","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:14:54.756865Z","iopub.execute_input":"2021-12-08T03:14:54.757141Z","iopub.status.idle":"2021-12-08T03:15:00.918872Z","shell.execute_reply.started":"2021-12-08T03:14:54.757113Z","shell.execute_reply":"2021-12-08T03:15:00.917960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(30,20))\nfor i in range(12):\n    fig.add_subplot(4, 3, i+1)\n    plt.title(video0.image_id[i])\n    img = cv2.imread(video0.path[i])\n    for z in range(15):\n        if video0[f'x_{z}'][i] !=str():\n            x = video0[f'x_{z}'][i]\n            y = video0[f'x_{z}'][i]\n            width=  video0[f'width_{z}'][i]\n            height= video0[f'height_{z}'][i]\n            img = cv2.rectangle(img,\n              pt1=(x, y),\n              pt2=(x+height, y-width),\n              color=(0, 255, 0),\n              thickness=3,\n              lineType=cv2.LINE_4,\n              shift=0)\n\n    plt.imshow(img)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:16:20.802806Z","iopub.execute_input":"2021-12-08T03:16:20.803515Z","iopub.status.idle":"2021-12-08T03:16:25.018290Z","shell.execute_reply.started":"2021-12-08T03:16:20.803467Z","shell.execute_reply":"2021-12-08T03:16:25.016921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(25):\n    video1[f'x_{i}'] =str()\n    video1[f'y_{i}'] = str()\n    video1[f'width_{i}'] = str()\n    video1[f'height_{i}'] = str()\nvideo1['path'] =str()\n\nfor x in video1.index:\n    a = video1.annotations.loc[x][2:-2]\n    a = a.split(',')\n    \n    for i in range(int(len(a)/4)):\n        video1[f'x_{i}'][x] = int(a[0].split(':')[1])\n        video1[f'y_{i}'][x] = int(a[1].split(':')[1])\n        video1[f'width_{i}'][x] = int(a[2].split(':')[1])\n        video1[f'height_{i}'][x] = int(a[3].split(':')[1].split('}')[0])\n    video1['path'][x] = '../input/tensorflow-great-barrier-reef/train_images/video_0/' + str(video0.video_id.loc[i]) +'.jpg'","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:16:36.215690Z","iopub.execute_input":"2021-12-08T03:16:36.216082Z","iopub.status.idle":"2021-12-08T03:16:50.802653Z","shell.execute_reply.started":"2021-12-08T03:16:36.216045Z","shell.execute_reply":"2021-12-08T03:16:50.801773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"video1[video1[f'x_{17}'] !=str()] ","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:17:08.764043Z","iopub.execute_input":"2021-12-08T03:17:08.764378Z","iopub.status.idle":"2021-12-08T03:17:08.795712Z","shell.execute_reply.started":"2021-12-08T03:17:08.764342Z","shell.execute_reply":"2021-12-08T03:17:08.795052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=1759\nfor z in range(25):\n            x = video1[f'x_{z}'][i]\n            y = video1[f'x_{z}'][i]\n            width=  video1[f'width_{z}'][i]\n            height= video1[f'height_{z}'][i]\n            print(x,y,width,height)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:17:12.113323Z","iopub.execute_input":"2021-12-08T03:17:12.113643Z","iopub.status.idle":"2021-12-08T03:17:12.142170Z","shell.execute_reply.started":"2021-12-08T03:17:12.113611Z","shell.execute_reply":"2021-12-08T03:17:12.141223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=1760\nfor z in range(25):\n            x = video1[f'x_{z}'][i]\n            y = video1[f'x_{z}'][i]\n            width=  video1[f'width_{z}'][i]\n            height= video1[f'height_{z}'][i]\n            print(x,y,width,height)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:17:17.485575Z","iopub.execute_input":"2021-12-08T03:17:17.485944Z","iopub.status.idle":"2021-12-08T03:17:17.504902Z","shell.execute_reply.started":"2021-12-08T03:17:17.485910Z","shell.execute_reply":"2021-12-08T03:17:17.504297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(30,20))\nfor i in range(12):\n    fig.add_subplot(4, 3, i+1)\n    plt.title(video1.image_id[i])\n    img = cv2.imread(video1.path[i])\n    for z in range(25):\n        if video1[f'x_{z}'][i] !=str():\n            x = video1[f'x_{z}'][i]\n            y = video1[f'x_{z}'][i]\n            width=  video1[f'width_{z}'][i]\n            height= video1[f'height_{z}'][i]\n            img = cv2.rectangle(img,\n              pt1=(x, y),\n              pt2=(x+height, y-width),\n              color=(0, 255, 0),\n              thickness=3,\n              lineType=cv2.LINE_4,\n              shift=0)\n    print(x,y,width,height)\n\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:17:21.668877Z","iopub.execute_input":"2021-12-08T03:17:21.669602Z","iopub.status.idle":"2021-12-08T03:17:26.149114Z","shell.execute_reply.started":"2021-12-08T03:17:21.669562Z","shell.execute_reply":"2021-12-08T03:17:26.147687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(25):\n    video2[f'x_{i}'] =str()\n    video2[f'y_{i}'] = str()\n    video2[f'width_{i}'] = str()\n    video2[f'height_{i}'] = str()\nvideo2['path'] =str()\n\nfor x in video2.index:\n    a = video2.annotations.loc[x][2:-2]\n    a = a.split(',')\n    \n    for i in range(int(len(a)/4)):\n        video2[f'x_{i}'][x] = int(a[0].split(':')[1])\n        video2[f'y_{i}'][x] = int(a[1].split(':')[1])\n        video2[f'width_{i}'][x] = int(a[2].split(':')[1])\n        video2[f'height_{i}'][x] = int(a[3].split(':')[1].split('}')[0])\n    video2['path'][x] = '../input/tensorflow-great-barrier-reef/train_images/video_0/' + str(video0.video_id.loc[i]) +'.jpg'","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:17:26.150665Z","iopub.execute_input":"2021-12-08T03:17:26.150951Z","iopub.status.idle":"2021-12-08T03:17:31.644345Z","shell.execute_reply.started":"2021-12-08T03:17:26.150919Z","shell.execute_reply":"2021-12-08T03:17:31.643294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(30,20))\nfor i in range(12):\n    fig.add_subplot(4, 3, i+1)\n    plt.title(video2.image_id[i])\n    img = cv2.imread(video2.path[i])\n    for z in range(25):\n        if video2[f'x_{z}'][i] !=str():\n            x = video2[f'x_{z}'][i]\n            y = video2[f'x_{z}'][i]\n            width=  video2[f'width_{z}'][i]\n            height= video2[f'height_{z}'][i]\n            img = cv2.rectangle(img,\n              pt1=(x, y),\n              pt2=(x+height, y-width),\n              color=(0, 255, 0),\n              thickness=3,\n              lineType=cv2.LINE_4,\n              shift=0)\n\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T03:17:31.646124Z","iopub.execute_input":"2021-12-08T03:17:31.646392Z","iopub.status.idle":"2021-12-08T03:17:35.780285Z","shell.execute_reply.started":"2021-12-08T03:17:31.646358Z","shell.execute_reply":"2021-12-08T03:17:35.778860Z"},"trusted":true},"execution_count":null,"outputs":[]}]}