{"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.\n\n# Importing other required libraries\n\nimport bcolz                        #For compressing the images so that they dont cause a memory outage issue when the code is executed\nfrom tqdm import tqdm               #For visualizing progress bars\nimport matplotlib.pyplot as plt     #Plotting library of Python\n%matplotlib inline\nfrom mpl_toolkits.axes_grid1 import ImageGrid              #To display images in a grid format\nimport random\nimport keras                                               #Deep Learning Library\nfrom keras.preprocessing.image import load_img             #Load image from a directory\nfrom keras.preprocessing.image import img_to_array         #Convert all the images to an array\nfrom keras.preprocessing.image import ImageDataGenerator   #Data Generator class of Keras which will aid in image augmentation","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Reading the data sets present in the working directory of the Kernel"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Reading in the CSVs\n\ndf_train = pd.read_csv('../input/labels.csv')\ndf_test = pd.read_csv('../input/sample_submission.csv')\n\nprint(\"Training data set sample \\n\")\nprint(df_train.head(5))\n\n\nprint(\"\\n Test data set sample \\n\")\nprint(df_test.head(5))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating an empty bcolz array to store the image data"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Before we create the empty bcolz array we need to define the size of each of the image\n\n# Size of the image as given in the problem is 300\n\nim_size = 300\nx_train = bcolz.zeros((0, im_size, im_size, 3), np.float32)\ny_train = []","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating a master array which contains the data of all the images in the training set in the form of a matrix"},{"metadata":{"trusted":true},"cell_type":"code","source":"for i,j in tqdm(df_train.values):\n    image = load_img('../input/train/{}.jpg'.format(i), target_size=(im_size, im_size))\n    x_train.append(img_to_array(image))\n    y_train.append(j)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Viewing the training array (Both in the form of a matrix and a picture) "},{"metadata":{"trusted":true},"cell_type":"code","source":"# Shape of the x_train array is\nx_train.shape\n\n# Output of the x_train.shape suggests that x_train contains the image data of all the 10222 images in the form of a matrix (of the order 300, 300, 3)\n# Lets view how the 100th entry in the x_train matrix looks like\nprint(\"100th Image in the training data set is: \\n\",x_train[99], \"\\n\")\nprint(\"And the lable of the correspoding image is: \\n\", y_train[99])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_train[99]/255)\n\n# Dont know the reason behind: Why the image is being filled with white light if it is not divided by 255. Ask this to any of the buddies ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Defining the function for Data Augmentation (Using ImageDataGenerator Class of Keras) "},{"metadata":{"trusted":true},"cell_type":"code","source":"# The code below is for a sample example, where in we will try to flip the image\n\ndatagen = ImageDataGenerator()\nimg_to_test = 99\ntemp = np.zeros((16, im_size, im_size, 3), dtype = np.float32)\n\nfor i in range(16):\n    if random.randint(1,101) < 50:\n        flip_horizontal = True\n    else:\n        flip_horizontal = False\n    if random.randint(1,101) < 50:\n        flip_vertical = True\n    else:\n        flip_vertical = False\n\n    temp[i] = datagen.apply_transform(x_train[img_to_test],{\n        'flip_horizontal':flip_horizontal,\n        'flip_vertical':flip_vertical\n    })\n\ndef plotImages( images_arr, n_images=2):\n    fig, axes = plt.subplots(n_images, n_images, figsize=(12,12))\n    axes = axes.flatten()\n    for img, ax in zip( images_arr, axes):\n        ax.imshow(img)\n        ax.set_xticks(())\n        ax.set_yticks(())\n    plt.tight_layout()\n\nplotImages(temp[:,]/255.0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def image_augmentation(x_train, y_train, batch_size = 2, re_size_factor = 0.8):\n    \n    \"\"\"\n    \n    x_train = The master matrix containing all the images\n    y_train = The lable vector containing lables of all the images\n    batch_size = default parameter containing batch size of images which will be passed in the function\n    re_size_factor = Proportion by which the cropped images will be resized\n    \n    \"\"\"\n    \n    # Code for randomly selecting any two images from the entire training data set of ~10K images\n    x = list(np.arange(0,x_train.shape[0])) #Generating a list of numbers between 0 and the number of images in the data set\n    rand_samp = random.sample(x, batch_size)\n    \n    ################################################ Cropping Code Begins below ##################################################################\n    \n    # Creating an empty container where the cropped image will be stored\n    crop_container = np.zeros((batch_size, int(x_train.shape[1]*re_size_factor), int(x_train.shape[2]*re_size_factor), 3),dtype = np.float32)\n    for i in range(0,len(rand_samp)):\n        x_lim1 = int(x_train.shape[1]*re_size_factor)\n        y_lim1 = int(x_train.shape[2]*re_size_factor)\n        x_lim2 = int(x_train.shape[1] - int(x_train.shape[1]*re_size_factor))\n        y_lim2 = int(x_train.shape[2] - int(x_train.shape[2]*re_size_factor))\n        center = 30\n        \n        if random.randint(0,101) < 20:\n            #Bottom Left\n            crop_container[i] = x_train[rand_samp[i],0:x_lim1, 0:y_lim1, :]\n        elif random.randint(0,101) < 40:\n            #Top Left\n            crop_container[i] = x_train[rand_samp[i],0:x_lim1, y_lim2:x_train.shape[2], :]\n        elif random.randint(0,101) < 60:\n            #Bottom Right\n            crop_container[i] = x_train[rand_samp[i],x_lim2:x_train.shape[1], 0:y_lim1, :]\n        elif random.randint(0,101) < 80:\n            #Top Right\n            crop_container[i] = x_train[rand_samp[i],x_lim2:x_train.shape[1], y_lim2:x_train.shape[2], :]\n        else:\n            #Centre\n            crop_container[i] = x_train[rand_samp[i],center:(x_train.shape[1] - center), center:(x_train.shape[2] - center), :]\n        ################################################ Flipping Code Begins below ###############################################################\n        \n        datagen = ImageDataGenerator()\n    \n        if random.randint(1,101) < 50:\n            flip_horizontal = True\n        else:\n            flip_horizontal = False\n        if random.randint(1,101) < 50:\n            flip_vertical = True\n        else:\n            flip_vertical = False\n    \n        crop_container[i] = datagen.apply_transform(crop_container[i],{\n        'flip_horizontal':flip_horizontal,\n        'flip_vertical':flip_vertical\n        })\n        \n        ################################################ Flipping Code Ends ###############################################################\n        \n        # Displaying one of the source images\n        plt.title(y_train[rand_samp[i]])\n        plt.imshow(x_train[rand_samp[i],]/255.0)\n        \n        def plotImages(images_arr, n_images=2):\n            fig, axes = plt.subplots(n_images-1, n_images, figsize=(12,12))\n            axes = axes.flatten()\n            for img, ax in zip( images_arr, axes):\n                ax.imshow(img)\n                ax.set_xticks(())\n                ax.set_yticks(())\n                plt.tight_layout()\n        \n        plotImages(crop_container[:,]/255.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_augmentation(x_train, y_train)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}