{"cells":[{"metadata":{},"cell_type":"markdown","source":"![Wheat](http://cdn-a.william-reed.com/var/wrbm_gb_food_pharma/storage/images/1/9/4/3/2903491-1-eng-GB/Global-wheat-production-to-fall-in-2016-season-reports-FAO_wrbm_large.jpg)"},{"metadata":{},"cell_type":"markdown","source":"\"About this Competition\"\n\"Supporting the shit for sake of the breads to have for the dinner\"\n\n> In this competition, you’ll detect wheat heads from outdoor images of wheat plants, including wheat datasets from around the globe. Using worldwide data, you will focus on a generalized solution to estimate the number and size of wheat heads. To better gauge the performance for unseen genotypes, environments, and observational conditions, the training dataset covers multiple regions. You will use more than 3,000 images from Europe (France, UK, Switzerland) and North America (Canada). The test data includes about 1,000 images from Australia, Japan, and China.\n\nWheat is a staple across the globe, which is why this competition must account for different growing conditions. Models developed for wheat phenotyping need to be able to generalize between environments. If successful, researchers can accurately estimate the density and size of wheat heads in different varieties. With improved detection farmers can better assess their crops, ultimately bringing cereal, toast, and other favorite dishes to your table.\n\n"},{"metadata":{},"cell_type":"markdown","source":"**Urbanization, rising incomes and working women are driving a rapid rise in global wheat consumption. Models predict that by 2050 consumers will require 60 percent more wheat than today. Challenges are big: this demand must be met without opening new land and with better use of fertilizer, water, and labor.**"},{"metadata":{},"cell_type":"markdown","source":"![Wheat](https://wheat.org/wp-content/uploads/sites/4/2014/10/LoadingOven-08-890x1024.jpg)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#BASIC\nimport numpy as np \nimport pandas as pd \nimport os\nimport cv2\nimport re\nfrom tqdm.notebook import tqdm\nfrom PIL import Image\nimport hashlib\nimport plotly.graph_objects as go\nimport matplotlib.patches as patches\nfrom plotly.subplots import make_subplots\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom IPython.display import Image, display\nfrom plotly import graph_objs as go\nimport plotly.express as px\nimport plotly.figure_factory as ff","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"DIR = \"../input/global-wheat-detection/\"\nTRAIN = \"train.csv\"\n\nTRAIN_IMG = \"train\"\nTEST_IMG= \"test\"\nWIDTH = 1024\nHEIGHT = 1024\n\nTRAIN_IMAGES = [os.path.join(DIR, \"train\", fname) for fname in os.listdir(os.path.join(DIR, \"train\"))]\nTEST_IMAGES = [os.path.join(DIR, \"test\", fname) for fname in os.listdir(os.path.join(DIR, \"test\"))]\n\ntrain_df = pd.read_csv(os.path.join(DIR, TRAIN))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"unique ids : \", len(train_df.image_id.unique()))\nprint(\"unique width : \", len(train_df.width.unique()))\nprint(\"unique height : \", len(train_df.height.unique()))\nprint(\"unique source : \", len(train_df.source.unique()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.source.value_counts()\n#7 unique sources of wheat head images","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> **Total Training & Testing  Images **"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"Total training images: {len(TRAIN_IMAGES)}\")\nprint(f\"Total test images: {len(TEST_IMAGES)}\")\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**What only ten training images**"},{"metadata":{},"cell_type":"markdown","source":"> **EDA**"},{"metadata":{"trusted":true},"cell_type":"code","source":"bbox_wrt_source = train_df.groupby([\"source\"]).apply(lambda x:x[\"image_id\"].value_counts().mean())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bbox_wrt_source","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"source=train_df['source'].value_counts()\nfig = go.Figure(data=[\n    go.Pie(labels=source.index, values=source.values)\n])\n\nfig.update_layout(title='Source distribution for data')\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Spread of bounding boxes per image is:**"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12, 8))\nsns.distplot(train_df['image_id'].value_counts().values)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12, 8))\nbbox_wrt_source.plot(kind='bar')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> Area per image"},{"metadata":{"trusted":true},"cell_type":"code","source":"area_per_image = train_df.groupby(\"image_id\").apply(lambda x: (x[\"width\"]*x[\"height\"]).sum()/(WIDTH*HEIGHT))\nplt.figure(figsize=(10, 6))\nplt.title(\"Area % for each image.\")\nprint(f\"Min area per image: {area_per_image.min()}%\")\nprint(f\"Max area per image: {area_per_image.max()}%\")\nprint(f\"Mean area per image: {area_per_image.mean()}%\")\nprint(f\"Std area per image: {area_per_image.std()}%\")\nsns.distplot(area_per_image)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> Normally/ Gaussian distribution of % area"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualize few samples of current training dataset\nfig, ax = plt.subplots(nrows=2, ncols=2, figsize=(20, 10))\ncount=0\nfor row in ax:\n    for col in row:\n        img = plt.imread(f'{os.path.join(DIR, \"train\", train_df[\"image_id\"].unique()[count])}.jpg')\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        col.imshow(img)\n        count += 1\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> Extracting Dimensions of bounding boxes in data frame"},{"metadata":{"trusted":true},"cell_type":"code","source":"import ast\ntrain_df[['x_min','y_min', 'width', 'height']] = pd.DataFrame([ast.literal_eval(x) for x in train_df.bbox.tolist()], index= train_df.index)\ntrain_df = train_df[['image_id', 'bbox', 'source', 'x_min', 'y_min', 'width', 'height']]\ntrain_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"*Let **Visulize** BBOX For Corresponding Images of Wheat Heads *"},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_bbox(image_id, df, col, color='white'):\n    bboxes = df[df['image_id'] == image_id]\n    \n    for i in range(len(bboxes)):\n        # Create a Rectangle patch\n        rect = patches.Rectangle(\n            (bboxes['x_min'].iloc[i], bboxes['y_min'].iloc[i]),\n            bboxes['width'].iloc[i], \n            bboxes['height'].iloc[i], \n            linewidth=2, \n            edgecolor=color, \n            facecolor='none')\n\n        # Add the patch to the Axes\n        col.add_patch(rect)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualize few samples of current training dataset\nfig, ax = plt.subplots(nrows=2, ncols=2, figsize=(20, 20))\ncount=0\nfor row in ax:\n    for col in row:\n        img_id = train_df[\"image_id\"].unique()[count]\n        img = plt.imread(f'{os.path.join(DIR, \"train\", img_id)}.jpg')\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        get_bbox(img_id, train_df, col, color='red')\n        col.imshow(img)\n        count += 1\nplt.show()\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}