{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nfrom PIL import Image, ImageDraw\nfrom ast import literal_eval\nimport matplotlib.pyplot as plt\n\nimport plotly.express as px\nimport plotly.graph_objects as go\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Read data"},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ../input/global-wheat-detection/","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"root_path = \"../input/global-wheat-detection/\"\ntrain_folder = os.path.join(root_path, \"train\")\ntest_folder = os.path.join(root_path, \"test\")\ntrain_csv_path = os.path.join(root_path, \"train.csv\")\nsample_submission = os.path.join(root_path, \"sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(train_csv_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Compute Area of bbox"},{"metadata":{"trusted":true},"cell_type":"code","source":"import ast\ntrain_df['bbox_area'] = train_df.bbox.apply(lambda x: ast.literal_eval(x)[2] * ast.literal_eval(x)[3])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Sources\n**1. How many sources are there and what's the distribution wheat heads looks like w.r.t sources?**  \nThere are 7 sources, **ethz_1** and **arvalid_1** being the top post ones  \n  \n**2. What's the distribution of images w.r.t sources?**  \n- **ethz_1** has 747 images with 51k wheat heads, that's ~69 wheat heads per image. That's a lot!  \n- It looks like **ethz_1** has the smallest wheat heads too, with median bbox area of 4050, which is lowest in all sources.\n- It makes sense, most likely either **ethz_1** consists of baby wheat heads and a lot of them together in the image or the angle at which the are taken makes image with many wheat heads.\n  \n**3. Are there images linked to same source? Ideally that should not be the case**  \nThat's not the case."},{"metadata":{"trusted":true},"cell_type":"code","source":"source_vc = train_df.source.value_counts()\n\nprint(f\"There are {len(source_vc)} sources.\\n\")\nprint(source_vc)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"summary_df = train_df.groupby(['source']).agg(unq_images=('image_id','nunique'), wheat_heads=('image_id','size'), mean_bbox_area=('bbox_area','mean'), median_bbox_area=('bbox_area','median'))\nsummary_df.reset_index(inplace=True,drop=False)\nsummary_df['mean_bbox_area'] = np.round(summary_df.mean_bbox_area, 2)\nsummary_df['median_bbox_area'] = np.round(summary_df.median_bbox_area, 2)\nsummary_df['mean_wheat_heads_per_img'] = np.round(summary_df.wheat_heads / summary_df.unq_images, 2)\n\nsummary_df\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"summary_df = train_df.groupby(['image_id']).agg(unq_source=('source','nunique'))\nsummary_df.reset_index(inplace=True,drop=False)\nprint(f\"Images with >1 sources : {summary_df.loc[summary_df.unq_source > 1].shape[0]}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Images\n\n1- Check if Width and Height differ for images, or are they same for all images?  \nYes, all images are 1024 by 1024  \n\n2- "},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"Unique Widths: {pd.unique(train_df.width)}\")\nprint(f\"Unique HeightsA: {pd.unique(train_df.height)}\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nsummary_df = train_df.groupby(['source','image_id']).agg(wheat_heads=('image_id','size'), mean_bbox_area=('bbox_area','mean'))\nsummary_df.reset_index(inplace=True,drop=False)\nsummary_df\n\n\n\n\ncolors = ['rgba(93, 164, 214, 1.0)', 'rgba(255, 144, 14, 1.0)', 'rgba(44, 160, 101, 1.0)',\n          'rgba(255, 65, 54, 1.0)', 'rgba(207, 114, 255, 1.0)', 'rgba(127, 96, 0, 1.0)', 'rgba(200, 200, 0, 1.0)']\n\nfig = go.Figure()\nindex = 0\nfor cls in pd.unique(summary_df.source):\n    fig.add_trace(go.Box(y=summary_df.loc[summary_df.source==cls,'wheat_heads'], name=cls,\n                marker_color = colors[index]))\n    index = index + 1\n\nfig.update_yaxes(range=[0, 100])\nfig.update_layout(title='Box-Plot Wheatheads w.r.t source',xaxis_title=\"Source\",yaxis_title=\"No. Wheatheads per image\")\nfig.show()\n\n\n\nfig = go.Figure()\nindex = 0\nfor cls in pd.unique(summary_df.source):\n    fig.add_trace(go.Box(y=summary_df.loc[summary_df.source==cls,'mean_bbox_area'], name=cls,\n                marker_color = colors[index]))\n    index = index + 1\n\nfig.update_yaxes(range=[0, 40000])\nfig.update_layout(title='Box-Plot BBox Area w.r.t source',xaxis_title=\"Source\",yaxis_title=\"Mean BBox Area\")\nfig.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualizing images\n\nCopied code from https://www.kaggle.com/devvindan/wheat-detection-eda\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_images(images, num = 5):\n    \n    images_to_show = np.random.choice(images, num)\n\n    for image_id in images_to_show:\n\n        image_path = os.path.join(train_folder, image_id + \".jpg\")\n        image = Image.open(image_path)\n\n        # get all bboxes for given image in [xmin, ymin, width, height]\n        bboxes = [literal_eval(box) for box in train_df[train_df['image_id'] == image_id]['bbox']]\n\n        # visualize them\n        draw = ImageDraw.Draw(image)\n        for bbox in bboxes:    \n            draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], width=3)\n\n        plt.figure(figsize = (15,15))\n        plt.imshow(image)\n        plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for source in pd.unique(train_df.source):\n    print(f\"Showing images for {source}:\")\n    show_images(train_df[train_df['source'] == source]['image_id'].unique(), num = 1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}