{"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\n#for 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 5GB 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\n\n\n# credit: https://gist.github.com/soply/f3eec2e79c165e39c9d540e916142ae1\nimport matplotlib.pyplot as plt\nimport numpy as np\n\ndef show_images(images, cols = 1, titles = None):\n    \"\"\"Display a list of images in a single figure with matplotlib.\n    \n    Parameters\n    ---------\n    images: List of np.arrays compatible with plt.imshow.\n    \n    cols (Default = 1): Number of columns in figure (number of rows is \n                        set to np.ceil(n_images/float(cols))).\n    \n    titles: List of titles corresponding to each image. Must have\n            the same length as titles.\n    \"\"\"\n    assert((titles is None)or (len(images) == len(titles)))\n    n_images = len(images)\n    if titles is None: titles = ['Image (%d)' % i for i in range(1,n_images + 1)]\n    fig = plt.figure()\n    for n, (image, title) in enumerate(zip(images, titles)):\n        a = fig.add_subplot(cols, np.ceil(n_images/float(cols)), n + 1)\n        if image.ndim == 2:\n            plt.gray()\n        plt.imshow(image)\n        a.set_title(title)\n    fig.set_size_inches(np.array(fig.get_size_inches()) * n_images)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/global-wheat-detection/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 147,793 training bounding boxes, on 3,373 images\ntrain_df.shape, len(train_df.image_id.unique())\nprint(\"There are {} unique bounding boxes on {} unique training images (with boxes) from {} different sources. \".format(train_df.shape[0], len(train_df.image_id.unique()),len(train_df.source.unique()) ))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* The train set contains 3422 images, so that means there are 49 images in the set that contain 0 bounding boxes."},{"metadata":{"trusted":true},"cell_type":"code","source":"# Shape of training images\ntrain_df.height.unique(), train_df.width.unique()\n# All images are the same width and height, and more importanntly square which could help with certain object detector types","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Regions"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Distribution of source images is somewhat skewed, lets see if there are any visual differences by region\n#(or if the average size of the bounding box varies by region)\ntrain_df[[\"image_id\", \"source\"]].groupby(\"source\").count().sort_values(\"image_id\", ascending = False).reset_index()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Keep this in mind for when you start to look for patterns in detection accuracy or mAP by source as some regions are under represented in teh train set.  \n"},{"metadata":{"trusted":true},"cell_type":"code","source":"### How many bounding boxes do images have?\ndf_by_id = train_df[[\"image_id\",\"bbox\"]].groupby(\"image_id\").count().reset_index()\nboxplot = df_by_id.boxplot(column=['bbox'])\nprint(\"The image with the most bboxes has {} and the one with the least (besides the ones with zero) has {}\".format(df_by_id.bbox.max(), df_by_id.bbox.min()))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Median number of bounding boxes in each image seems to be around 42. Let's keep this is mind in case your detector uses anchors and there might be a need to recalculate anchors that are smaller than perhaps ImageNet (if you plan on using pre trained weights)\n\n* There are **49 images in the train set without bounding boxes.**\n\n* Keep in mind some detectors are better/worse for small objects and some detectors might have performance issues around grid if they use a grid at the detection stage. "},{"metadata":{"trusted":true},"cell_type":"code","source":"import random\nfrom matplotlib import image","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Checking some images from different sources:"},{"metadata":{"trusted":true},"cell_type":"code","source":"img_subset = train_df[train_df[\"source\"]==\"ethz_1\"]\nimg_subset_imageids = img_subset.image_id.unique()\nrandom.shuffle(img_subset_imageids)\nrs = [\"../input/global-wheat-detection/train/{}.jpg\".format(i) for i in img_subset_imageids[:5]]\n\nimage_list = []\nfor i in rs:\n    img = image.imread(i)\n    image_list.append(img)\n    \nshow_images(image_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_subset = train_df[train_df[\"source\"]==\"arvalis_1\"]\nimg_subset_imageids = img_subset.image_id.unique()\nrandom.shuffle(img_subset_imageids)\nrs = [\"../input/global-wheat-detection/train/{}.jpg\".format(i) for i in img_subset_imageids[:5]]\n\nimage_list = []\nfor i in rs:\n    img = image.imread(i)\n    image_list.append(img)\n    \nshow_images(image_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_subset = train_df[train_df[\"source\"]==\"rres_1\"]\nimg_subset_imageids = img_subset.image_id.unique()\nrandom.shuffle(img_subset_imageids)\nrs = [\"../input/global-wheat-detection/train/{}.jpg\".format(i) for i in img_subset_imageids[:5]]\n\nimage_list = []\nfor i in rs:\n    img = image.imread(i)\n    image_list.append(img)\n    \nshow_images(image_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_subset = train_df[train_df[\"source\"]==\"arvalis_3\"]\nimg_subset_imageids = img_subset.image_id.unique()\nrandom.shuffle(img_subset_imageids)\nrs = [\"../input/global-wheat-detection/train/{}.jpg\".format(i) for i in img_subset_imageids[:5]]\n\nimage_list = []\nfor i in rs:\n    img = image.imread(i)\n    image_list.append(img)\n    \nshow_images(image_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_subset = train_df[train_df[\"source\"]==\"usask_1\"]\nimg_subset_imageids = img_subset.image_id.unique()\nrandom.shuffle(img_subset_imageids)\nrs = [\"../input/global-wheat-detection/train/{}.jpg\".format(i) for i in img_subset_imageids[:5]]\n\nimage_list = []\nfor i in rs:\n    img = image.imread(i)\n    image_list.append(img)\n    \nshow_images(image_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_subset = train_df[train_df[\"source\"]==\"inrae_1\"]\nimg_subset_imageids = img_subset.image_id.unique()\nrandom.shuffle(img_subset_imageids)\nrs = [\"../input/global-wheat-detection/train/{}.jpg\".format(i) for i in img_subset_imageids[:5]]\n\nimage_list = []\nfor i in rs:\n    img = image.imread(i)\n    image_list.append(img)\n    \nshow_images(image_list)","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}