{"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 numpy as np\nimport pandas as pd \nimport os\nimport pathlib\nimport PIL\nfrom pathlib import Path\nfrom PIL import Image, ImageDraw\nfrom math import sqrt\nimport ast\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nsns.set()\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:20.50389Z","iopub.execute_input":"2021-12-08T10:49:20.504431Z","iopub.status.idle":"2021-12-08T10:49:20.51338Z","shell.execute_reply.started":"2021-12-08T10:49:20.504392Z","shell.execute_reply":"2021-12-08T10:49:20.51263Z"},"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\")\ntest = pd.read_csv(\"../input/tensorflow-great-barrier-reef/test.csv\")\nsub = pd.read_csv(\"../input/tensorflow-great-barrier-reef/example_sample_submission.csv\")\n\npath = Path('../input/tensorflow-great-barrier-reef/train_images')\nfilepaths = list(path.glob(r'**/*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:20.514979Z","iopub.execute_input":"2021-12-08T10:49:20.515676Z","iopub.status.idle":"2021-12-08T10:49:28.352614Z","shell.execute_reply.started":"2021-12-08T10:49:20.515629Z","shell.execute_reply":"2021-12-08T10:49:28.351874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking the train test lengths\nprint(\"Number of training samples: \", len(train))\nprint(\"Number of testing samples: \", len(test))","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:28.356555Z","iopub.execute_input":"2021-12-08T10:49:28.356812Z","iopub.status.idle":"2021-12-08T10:49:28.363425Z","shell.execute_reply.started":"2021-12-08T10:49:28.35678Z","shell.execute_reply":"2021-12-08T10:49:28.362336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(150)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:28.36514Z","iopub.execute_input":"2021-12-08T10:49:28.365459Z","iopub.status.idle":"2021-12-08T10:49:28.390679Z","shell.execute_reply.started":"2021-12-08T10:49:28.365418Z","shell.execute_reply":"2021-12-08T10:49:28.389731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"看起来很多这样的相框没有海星","metadata":{}},{"cell_type":"code","source":"# lets see how many frames with no starfishes\ntrain_clean = train.loc[train[\"annotations\"] != \"[]\"]\nprint(f\"No starfishes in {len(train)-len(train_clean)} samples.\")\nprint(f\"The clean train set has {len(train_clean)} images for us to work with.\")","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:28.39418Z","iopub.execute_input":"2021-12-08T10:49:28.394861Z","iopub.status.idle":"2021-12-08T10:49:28.406757Z","shell.execute_reply.started":"2021-12-08T10:49:28.394827Z","shell.execute_reply":"2021-12-08T10:49:28.405911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:28.408073Z","iopub.execute_input":"2021-12-08T10:49:28.408691Z","iopub.status.idle":"2021-12-08T10:49:28.425189Z","shell.execute_reply.started":"2021-12-08T10:49:28.408646Z","shell.execute_reply":"2021-12-08T10:49:28.424571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**序列分布**","metadata":{}},{"cell_type":"code","source":"# Checking out the number of sequences\nlen(train_clean.sequence.value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:28.426627Z","iopub.execute_input":"2021-12-08T10:49:28.427014Z","iopub.status.idle":"2021-12-08T10:49:28.433462Z","shell.execute_reply.started":"2021-12-08T10:49:28.426971Z","shell.execute_reply":"2021-12-08T10:49:28.43278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rows per each sequence\nprint(\"Sequence Samples\")\nprint(train_clean.sequence.value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:28.434621Z","iopub.execute_input":"2021-12-08T10:49:28.435214Z","iopub.status.idle":"2021-12-08T10:49:28.444329Z","shell.execute_reply.started":"2021-12-08T10:49:28.435182Z","shell.execute_reply":"2021-12-08T10:49:28.443733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seq_df = train_clean.sequence.value_counts().to_frame()\nplt.figure(figsize=(16, 9))\nsns.barplot(x=seq_df.index, y=list(seq_df.sequence), palette=\"Greens_d\")\nplt.title(\"Distribution of Sequences\")\nplt.xlabel(\"Sequence Id\")\nplt.ylabel(\"Frequency\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:28.445513Z","iopub.execute_input":"2021-12-08T10:49:28.445953Z","iopub.status.idle":"2021-12-08T10:49:28.965212Z","shell.execute_reply.started":"2021-12-08T10:49:28.44591Z","shell.execute_reply":"2021-12-08T10:49:28.964328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**箱子的数量**","metadata":{}},{"cell_type":"code","source":"num_boxes = []\nannotations_clean = []\nfor elem in train_clean.annotations:\n    ann = ast.literal_eval(elem)\n    num_boxes.append(len(ann))\n    annotations_clean.append(ann)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:50.187482Z","iopub.execute_input":"2021-12-08T10:49:50.187925Z","iopub.status.idle":"2021-12-08T10:49:50.432695Z","shell.execute_reply.started":"2021-12-08T10:49:50.187895Z","shell.execute_reply":"2021-12-08T10:49:50.43178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# adding num boxes per row and changing the annotations column to a proper python parseable list of dictionaries\ntrain_clean[\"num_boxes\"] = num_boxes\ntrain_clean[\"annotations\"] = annotations_clean","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:49:58.419141Z","iopub.execute_input":"2021-12-08T10:49:58.419467Z","iopub.status.idle":"2021-12-08T10:49:58.431553Z","shell.execute_reply.started":"2021-12-08T10:49:58.419431Z","shell.execute_reply":"2021-12-08T10:49:58.430553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:50:08.292118Z","iopub.execute_input":"2021-12-08T10:50:08.292816Z","iopub.status.idle":"2021-12-08T10:50:08.310165Z","shell.execute_reply.started":"2021-12-08T10:50:08.292769Z","shell.execute_reply":"2021-12-08T10:50:08.309286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"#box Frequency\")\nprint(train_clean.num_boxes.value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:50:18.25207Z","iopub.execute_input":"2021-12-08T10:50:18.252333Z","iopub.status.idle":"2021-12-08T10:50:18.258873Z","shell.execute_reply.started":"2021-12-08T10:50:18.252305Z","shell.execute_reply":"2021-12-08T10:50:18.257826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of bounding boxes in the clean train datasets\nprint(f\"Number of Bounding Boxes in the dataset: {train_clean.num_boxes.sum()}\")","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:50:29.062512Z","iopub.execute_input":"2021-12-08T10:50:29.062879Z","iopub.status.idle":"2021-12-08T10:50:29.068107Z","shell.execute_reply.started":"2021-12-08T10:50:29.062838Z","shell.execute_reply":"2021-12-08T10:50:29.067396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**边界框数量的分布**","metadata":{}},{"cell_type":"code","source":"box_count = train_clean.num_boxes.value_counts().to_frame()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:51:01.838843Z","iopub.execute_input":"2021-12-08T10:51:01.839191Z","iopub.status.idle":"2021-12-08T10:51:01.844181Z","shell.execute_reply.started":"2021-12-08T10:51:01.839135Z","shell.execute_reply":"2021-12-08T10:51:01.843593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16, 9))\nsns.barplot(x=box_count.index, y=list(box_count.num_boxes), palette=\"Greens_d\")\nplt.title(\"Distribution of Num_boxes\")\nplt.xlabel(\"# of Boxes\")\nplt.ylabel(\"Frequency\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:51:05.354562Z","iopub.execute_input":"2021-12-08T10:51:05.355294Z","iopub.status.idle":"2021-12-08T10:51:05.741467Z","shell.execute_reply.started":"2021-12-08T10:51:05.355257Z","shell.execute_reply":"2021-12-08T10:51:05.740621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**看着盒子**","metadata":{}},{"cell_type":"code","source":"#structure of a annotation\nlist(train_clean[\"annotations\"])[0]","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:51:29.898276Z","iopub.execute_input":"2021-12-08T10:51:29.898797Z","iopub.status.idle":"2021-12-08T10:51:29.905606Z","shell.execute_reply.started":"2021-12-08T10:51:29.898756Z","shell.execute_reply":"2021-12-08T10:51:29.905004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# generating paths for input images\nsrc = '../input/tensorflow-great-barrier-reef/train_images'\npaths = []\nfor row in train_clean.image_id:\n    vid_num = row.split('-')[0]\n    img_num = row.split('-')[1]\n    paths.append(os.path.join(src,f'video_{vid_num}',img_num+'.jpg'))","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:52:17.014968Z","iopub.execute_input":"2021-12-08T10:52:17.015234Z","iopub.status.idle":"2021-12-08T10:52:17.040902Z","shell.execute_reply.started":"2021-12-08T10:52:17.015206Z","shell.execute_reply":"2021-12-08T10:52:17.039963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean['paths'] = paths","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:52:28.641556Z","iopub.execute_input":"2021-12-08T10:52:28.642101Z","iopub.status.idle":"2021-12-08T10:52:28.646657Z","shell.execute_reply.started":"2021-12-08T10:52:28.642064Z","shell.execute_reply":"2021-12-08T10:52:28.645986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classic way of iterating through and drawing the bounding boxes on an image\ndef vis_boxes(img_path, bboxes):\n    coords = []\n    for box in bboxes:\n        x1 = box['x']\n        y1 = box['y']\n        x2 = x1 + box['width']\n        y2 = y1 + box['height']\n        coords.append([x1, y1, x2, y2])\n        \n    img = Image.open(img_path)\n    img1 = img.copy()\n    draw = ImageDraw.Draw(img1)\n    for elem in coords:\n        draw.rectangle(elem, outline='red', width=7)\n    \n    return img1","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:52:44.517112Z","iopub.execute_input":"2021-12-08T10:52:44.51778Z","iopub.status.idle":"2021-12-08T10:52:44.524279Z","shell.execute_reply.started":"2021-12-08T10:52:44.517739Z","shell.execute_reply":"2021-12-08T10:52:44.523629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:52:53.537736Z","iopub.execute_input":"2021-12-08T10:52:53.538345Z","iopub.status.idle":"2021-12-08T10:52:53.556788Z","shell.execute_reply.started":"2021-12-08T10:52:53.5383Z","shell.execute_reply":"2021-12-08T10:52:53.555759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**具有最大边界框的序列**","metadata":{}},{"cell_type":"code","source":"# number of bounding boxes per each sequence\ntrain_clean.groupby('sequence').num_boxes.sum().to_frame()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:53:02.99338Z","iopub.execute_input":"2021-12-08T10:53:02.993658Z","iopub.status.idle":"2021-12-08T10:53:03.007349Z","shell.execute_reply.started":"2021-12-08T10:53:02.993628Z","shell.execute_reply":"2021-12-08T10:53:03.006457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**来看一些例子**","metadata":{}},{"cell_type":"code","source":"# lets plot a few\n# some inspiration from https://www.kaggle.com/sjyangkevin/eda-bounding-box-analysis-annotated-videos\n\nplt.figure(figsize=(16, 9))\nn_images = 9\ncount = 0\nr,c = int(sqrt(n_images)), int(sqrt(n_images))\ntrain_plot = train_clean.sample(n = n_images)\n\nfor _, row in train_plot.iterrows():\n    img_path = row['paths']\n    bboxes = row['annotations']\n    plt.subplot(r, c, count + 1)\n    img_out = vis_boxes(img_path, bboxes)\n    plt.imshow(img_out)\n    count+=1\n\nplt.show()\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T10:53:14.776361Z","iopub.execute_input":"2021-12-08T10:53:14.777344Z","iopub.status.idle":"2021-12-08T10:53:17.811273Z","shell.execute_reply.started":"2021-12-08T10:53:14.777298Z","shell.execute_reply":"2021-12-08T10:53:17.810698Z"},"trusted":true},"execution_count":null,"outputs":[]}]}