{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# Importing Libraries\nimport os,re,random\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom keras import layers, models, optimizers\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import backend as K\nfrom keras.preprocessing.image import img_to_array, load_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Reducing the size of img_width and img_height to 150\nimg_width = 96\nimg_height = 96\nTRAIN_DIR = '../input/train/'\nTEST_DIR = '../input/test/'\n\ntrain_images_dogs_cats = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset\ntest_images_dogs_cats = [TEST_DIR+i for i in os.listdir(TEST_DIR)]\n# print(train_images_dogs_cats)\n# print(test_images_dogs_cats)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# We have to sort the values as the list is random\ndef atoi(text):\n    return int(text) if text.isdigit() else text\n\ndef natural_keys(text):\n    return [ atoi(c) for c in re.split('(\\d+)', text) ]\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_dogs_cats.sort(key=natural_keys)\n# print(train_images_dogs_cats)\n# as First all starting images have cats and then all the images are of dogs \n# we take significant amount of data and train and test the model on it","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_images_dogs_cats = train_images_dogs_cats[0:1000] + train_images_dogs_cats[12800:13800] \ntest_images_dogs_cats.sort(key=natural_keys)\n# print(train_images_dogs_cats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(train_images_dogs_cats))\nprint(len(test_images_dogs_cats))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# preparing data to linear data to feed to neural network\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    # there is a test name file in test folder which is not a image\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":{"trusted":true},"cell_type":"code","source":"for im in train_images_dogs_cats:\n    try:\n        cv2.resize(cv2.imread(im), (img_width,img_height), interpolation=cv2.INTER_CUBIC)\n    except Exception as e:\n        print(str(e))\n        print(str(im))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_dogs_cats.remove('../input/train/train')\nX , Y = prepare_data(train_images_dogs_cats)\nX_train , X_test , Y_train , Y_test = train_test_split(X , Y , test_size = 0.2 , random_state = 101)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"nb_train_samples = len(X_train)\nnb_test_samples = len(X_test)\nbatch_size = 16","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# for convulaitional 2d network\n# initailise CNN\nmodel = models.Sequential()\n# defining 2D Network with 32 features and of shape =3*3 and input size of image\nmodel.add(layers.Conv2D(32  ,(3 , 3), input_shape= (img_width, img_height , 3)))\n# adding the activation as Rectified Linear Unit.\nmodel.add(layers.Activation('relu'))\n# max pooling with window size of 2*2\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# Flattening the CNN\nmodel.add(layers.Flatten())\n# Output of activation as relu of 64 output\nmodel.add(layers.Dense(64))\nmodel.add(layers.Activation('relu'))   \n# Using dropout as 0.5\nmodel.add(layers.Dropout(0.5))\n# Using output as 1\nmodel.add(layers.Dense(1))\n# As using Sigmoid Activation to get probability output\nmodel.add(layers.Activation('sigmoid'))\n\n# Compiling options\nmodel.compile(loss = 'binary_crossentropy' , optimizer = 'rmsprop' , metrics =['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# ImageDataGenerator class\n# Generate batches of tensor image data with real-time data augmentation. The data will be looped over (in batches).\n\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)\nval_datagen  = ImageDataGenerator(\n    rescale = 1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generated = train_datagen.flow(np.array(X_train), Y_train  , batch_size = batch_size)\nval_generated =  val_datagen.flow(np.array(X_test) , Y_test , batch_size = batch_size)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(\n    train_generated, \n    steps_per_epoch=nb_train_samples // batch_size,\n    epochs=50,\n    validation_data=val_generated,\n    validation_steps=nb_test_samples // batch_size\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for im in test_images_dogs_cats:\n    try:\n        cv2.resize(cv2.imread(im), (img_width,img_height), interpolation=cv2.INTER_CUBIC)\n    except Exception as e:\n        print(str(e))\n        print(str(im))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save_weights('model_wieghts.h5')\nmodel.save('model_keras.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images_dogs_cats.remove('../input/test/test')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_t, Y_t = prepare_data(test_images_dogs_cats)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1. / 255)\ntest_g = val_datagen.flow(np.array(X_t), batch_size=batch_size)\nprint(test_g)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction_probabilities = model.predict_generator(test_g , verbose=1 , steps = len(test_g))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(prediction_probabilities.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"counter = range(1, len(test_images_dogs_cats) + 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":{"name":"python3","language":"python","display_name":"Python 3"},"language_info":{"file_extension":".py","name":"python","codemirror_mode":{"version":3,"name":"ipython"},"version":"3.6.4","mimetype":"text/x-python","pygments_lexer":"ipython3","nbconvert_exporter":"python"}},"nbformat":4,"nbformat_minor":1}