{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_file = \"../input/global-wheat-detection/train.csv\"\ntrain_csv = pd.read_csv(train_file)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#initial look at the metadata\ntrain_csv.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Firstly, we can see that the height and width of all the images shown are the same. Is this the case for the whole data set?"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(set(train_csv['height']), set(train_csv['width']))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Yes! All the images have the same dimentions. Perhaps this could be manipulated to see how this affects the accuracy of the model."},{"metadata":{"trusted":true},"cell_type":"code","source":"# Dimensions of  metadata\ntrain_csv.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Tells me that there are 147793 bounding boxes in the training dataset. Will now see how many images don't have any bounding boxes."},{"metadata":{"trusted":true},"cell_type":"code","source":"# Number of unique image_id's containing bounding boxes\nprint(len(set(train_csv['image_id'])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"3422-3373","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There are 3373 images containing bounding boxes. We know that the total number of images was 3422. Therefore, 49 images don't have bounding boxes."},{"metadata":{"trusted":true},"cell_type":"code","source":"print(set(train_csv['source']))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The images come from 7 different sources."},{"metadata":{},"cell_type":"markdown","source":"# Data Visulisation"},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom matplotlib.pyplot import figure\nfrom tqdm import tqdm\nimport csv\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom PIL import Image, ImageDraw, ImageEnhance\nimport albumentations as albu\nfrom tqdm.notebook import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = \"../input/global-wheat-detection/train/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#loading the bounding boxes\nbboxes = {}\nfor i, image_id in enumerate(train_csv['image_id']):\n    bbox = train_csv['bbox'][i][1:-1]\n    bbox = bbox.split(',')\n    xmin = float(bbox[0])\n    ymin = float(bbox[1])\n    width = float(bbox[2])\n    height = float(bbox[3])\n    \n    if image_id not in bboxes:\n        bboxes[image_id] = []\n        bboxes[image_id].append([xmin, ymin, width, height])\n    else:\n        bboxes[image_id].append([xmin, ymin, width, height])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# visulising the images with the bounding boxes\nfor i, image_id in enumerate(bboxes):\n    image_path = train + image_id + '.jpg'\n    im = np.array(Image.open(image_path), dtype=np.uint8)\n    fig,ax = plt.subplots(1)\n    ax.imshow(im)\n    \n    for xmin, ymin, width, height in bboxes[image_id]:\n        rect = patches.Rectangle( (xmin ,ymin), width, height, linewidth=1, edgecolor='r', facecolor='none')\n        ax.add_patch(rect)\n    \n    if i == 5:\n        break","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Here we can see 5 different images showing that the bounding boxes are picking up the wheat heads. There are some annomilies,on the 4th picture, in the top left of the image there is a very large bounding box with several smaller ones within it. On the 5th image, there are some tiny bounding boxes which is difficult to make out with the human eye whether there is a wheat head there or not. \n\nI will now run a model to see what the initial accuracy of detecting the wheat head is, then I can make small adjustments to try and improve the accuracy."}],"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}