{"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","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The dataset contains two numpy arrays:\n\n  - images.py, shape=(3422,256,256,3)\n  - masks.py, shape=(3422,256,256,1)\n  \nImages and masks are values between 0 and 1"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom scipy.ndimage import zoom\nimport ast\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ORIG_SIZE = 1024\nSCALE = 4\nIMG_SIZE = ORIG_SIZE//SCALE\n\ndef get_mask(fname, train):\n    '''\n    Returns an image with ones inside bboxes and zeros outside\n    '''\n    \n    fname = fname.split('.')[0]\n    train = train[train['image_id']==fname]\n    \n    boxes = train['bbox'].tolist()\n    \n    mask = np.zeros((IMG_SIZE,IMG_SIZE))\n    \n    for box in boxes:\n        box = ast.literal_eval(box)\n        box = [int(i) for i in box]\n        \n        x1, x2 = box[1]//SCALE, (box[1] + box[3])//SCALE\n        y1, y2 = box[0]//SCALE, (box[0] + box[2])//SCALE\n        \n        mask[x1:x2,y1:y2] = 1\n        \n    return mask\n        \n\ndef create_dataset():\n    \n    images = os.listdir('/kaggle/input/global-wheat-detection/train')\n    train = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\n    \n    N = len(images)\n    x = np.zeros((N,IMG_SIZE,IMG_SIZE,3))\n    y = np.zeros((N,IMG_SIZE,IMG_SIZE,1))\n    \n    for i, fname in enumerate(tqdm(images)):\n        \n        img = mpimg.imread('/kaggle/input/global-wheat-detection/train/'+ fname)/255\n        img = zoom(img, (1/SCALE,1/SCALE,1))\n        x[i,:,:,:] = img\n        \n        mask = get_mask(fname, train)\n        y[i,:,:,:] = np.expand_dims(mask, axis=2)\n    \n    # because of zoom some values are lower than zero or higher than one.\n    x = (x - x.min())/(x.max() - x.min())\n    \n    # save some memory\n    x, y = np.float32(x), np.float32(y)\n    \n    return x, y\n    \n\nimages, masks = create_dataset()\n\n\nnp.save('/kaggle/working/images.npy', images)\nnp.save('/kaggle/working/masks.npy', masks)\n    \nprint('images.shape=', images.shape)\nprint('masks.shape=', masks.shape)\nprint('images.max=', images.max())\nprint('masks.max=', masks.max())\nprint('images.min=', images.min())\nprint('masks.min=', masks.min())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot a random figure figure with its masks\nn = np.random.randint(low=0, high=3422)\n\nfig = plt.figure(figsize=(12,12))\nplt.imshow(images[n] + masks[n]*0.3)","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}