{"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)\nimport os,cv2,re,random\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing.image import img_to_array , load_img\nfrom keras import layers,models,optimizers\nfrom keras import backend as K\nfrom sklearn.model_selection import train_test_split\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.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"\ntrain_dir = \"../input/train/\"\ntest_dir = \"../input/test/\"\ntrain_img_dogs_cat = [train_dir + i for i in os.listdir(train_dir)]#use this f or use whole dataset\ntest_img_dogs_cat = [test_dir + i for i in os.listdir(test_dir)]\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Helper function to sort the image files based on the numeric value in each file name."},{"metadata":{"trusted":true},"cell_type":"code","source":"def atoi(text):\n    return int(text) if text.isdigit() else text\ndef natural_keys(text):\n    return [atoi(c) for c in re.split('(\\d+)', text) ]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Sort the traning set. Use 1300 images each of cats and dogs instead of all 25000 to speed up the learning process.\n\nSort the test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_img_dogs_cat.sort(key = natural_keys)\ntrain_img_dogs_cat = train_img_dogs_cat[0:1300] + train_img_dogs_cat[12500 :13800]\ntest_img_dogs_cat.sort(key = natural_keys)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now the images have to be represented in numbers. For this, using the openCV library read and resize the image.\n\nGenerate labels for the supervised learning set.\n\nBelow is the helper function to do so."},{"metadata":{"trusted":true},"cell_type":"code","source":"img_height = 150\nimg_width = 150\n\n\ndef prepare_data(list_of_images):\n    \"\"\"\n    Returns two arrays: \n        x is an array of resized images\n        y is an array of labels\n    \"\"\"\n    x = [] # images as arrays\n    y = [] # labels\n    \n    for image in list_of_images:\n        x.append(cv2.resize(cv2.imread(image), (img_width,img_height), interpolation=cv2.INTER_CUBIC))\n    \n    for i in list_of_images:\n        if 'dog' in i:\n            y.append(1)\n        elif 'cat' in i:\n            y.append(0)\n        #else:\n            #print('neither cat nor dog name present in images')\n            \n    return x, y\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Generate X and Y using the helper function above\n\nSince K.image_data_format() is channel_last, input_shape to the first keras layer will be (img_width, img_height, 3). '3' since it is a color image"},{"metadata":{"trusted":true},"cell_type":"code","source":"X,Y = prepare_data(train_img_dogs_cat)\nprint(K.image_data_format())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Split the data set containing 2600 images into 2 parts, training set and validation set. Later, you will see that accuracy and loss on the validation set will also be reported while fitting the model using training set.****"},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val ,y_train, y_val = train_test_split(X,Y,test_size = 0.2 ,random_state = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"nb_train_samples = len(X_train)\nnb_validation_samples = len(X_val)\nbatch_size = 16","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We will be using the Sequential model from Keras to form the Neural Network. Sequential Model is used to construct simple models with linear stack of layers.\n\nMore info on Sequential model and Keras in general at https://keras.io/getting-started/sequential-model-guide/ and https://github.com/keras-team/keras"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32,(3,3),input_shape=(img_width,img_height,3)))\nmodel.add(layers.Activation('relu'))\nmodel.add(layers.MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(layers.Conv2D(32,(3,3)))\nmodel.add(layers.Activation('relu'))\nmodel.add(layers.MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(layers.Conv2D(64,(3,3)))\nmodel.add(layers.Activation('relu'))\nmodel.add(layers.MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64))\nmodel.add(layers.Activation('relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(1))\nmodel.add(layers.Activation('sigmoid'))\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer='rmsprop',\n              metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This is the augmentation configuration we will use for training and validation\n\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale=1. / 255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)\n\nval_datagen = ImageDataGenerator(\n    rescale=1. / 255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Prepare generators for training and validation sets"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = train_datagen.flow(np.array(X_train),y_train,batch_size = batch_size)\nvalidation_generator = train_datagen.flow(np.array(X_val),y_val,batch_size = batch_size)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Start training the model!\n\nFor better accuracy and lower loss, we are using an epoch of 30. Epoch value can be increased for better results."},{"metadata":{"trusted":true},"cell_type":"code","source":"hystory = model.fit_generator(\n                train_generator,\n                steps_per_epoch = nb_train_samples // batch_size,\n                epochs = 30,\n                validation_data = validation_generator,\n                validation_steps=nb_validation_samples // batch_size\n                 )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test, Y_test = prepare_data(test_img_dogs_cat) #Y_test in this case will be []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1. / 255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"steps = nb_train_samples // batch_size\ntest_generator = val_datagen.flow(np.array(X_test), batch_size=batch_size)\n\nprediction_probabilities = model.predict_generator(test_generator, verbose=1,steps=782)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"counter = range(1, len(test_img_dogs_cat) + 1)\nsolution = pd.DataFrame({\"id\": counter, \"label\":list(prediction_probabilities)})\ncols = ['label']\n\nfor col in cols:\n    solution[col] = solution[col].map(lambda x: str(x).lstrip('[').rstrip(']')).astype(float)\n\nsolution.to_csv(\"dogsVScats.csv\", index = False)","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}