{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","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.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"915ddeac4d7cc8a1523cf8261e8b9130bab38f4a","trusted":true},"cell_type":"code","source":"import cv2\nfrom keras import layers\nfrom keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D\nfrom keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D\nfrom keras.models import Model\nfrom keras.preprocessing import image\nfrom keras.utils import layer_utils\nfrom keras.utils.data_utils import get_file\nfrom keras.applications.imagenet_utils import preprocess_input\nimport pydot\nfrom IPython.display import SVG\nfrom keras.utils.vis_utils import model_to_dot\nfrom keras.utils import plot_model\nfrom keras.utils import to_categorical\nimport matplotlib.pyplot as plt \nfrom random import shuffle\nnp.set_printoptions(suppress=True)\n#np.set_printoptions(threshold=np.inf)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f3b68753e0732708180d300fada74ce3f4d374aa","trusted":true},"cell_type":"code","source":"IMG_SIZE = 120\nX_Train_orig = []\nY_Train_orig = []\nfor i in os.listdir('../input/train/'):\n    label = i.split('.')[-3]\n    if label == 'cat':\n        label = 0\n    elif label == 'dog':\n        label = 1\n    img = cv2.imread('../input/train/'+i, cv2.IMREAD_COLOR)\n    img = cv2.resize(img,(IMG_SIZE,IMG_SIZE), interpolation=cv2.INTER_CUBIC)\n    #img = img / 255\n    X_Train_orig.append([np.array(img), np.array(label)])\n\nnp.save('Training_Data.npy', X_Train_orig)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"57117442faa3e8505d8352914056eefe18818d91"},"cell_type":"code","source":"shuffle(X_Train_orig)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"252c1bdc4906c04df7181e5df23e61c47f4cce96","trusted":true},"cell_type":"code","source":"X = np.array([i[0] for i in X_Train_orig]).reshape(-1,IMG_SIZE,IMG_SIZE,3)\nY = np.array([i[1] for i in X_Train_orig])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d91fb23ee9689f792adb9973bd483ae3c6bc0faf","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size = 0.1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3650249df7955cab4983d03f4252a02489409185","trusted":true},"cell_type":"code","source":"print('Shape of X_train is :', X_train.shape)\nprint('Shape of Y_train is :', Y_train.shape)\nprint('Shape of X_val is :', X_val.shape)\nprint('Shape of Y_val is :', Y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2ba6838f1f1e4b7e33c9e35c7807ce128094015b","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt \nplt.figure(figsize=(20,20))   # to fix a shape for each image print\nfor i in range(50):          # using a for loop to display a number of images\n    plt.subplot(5, 10, i+1) # we need to use this function to print an array of pictures \n    plt.imshow(X_val[i,:,:,:]) # this will call the images from train set one by one\n    plt.title('DOG' if Y_val[i] == 1 else 'CAT')  # Lets also look into the labels \n    plt.axis('off') ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"130838688d5b7e0cb66e3e6936f9a7042f62a2d3","trusted":true},"cell_type":"code","source":"def Keras_Model(input_shape):    \n    \n    X_input = Input(input_shape)\n    \n    # First Layer\n    X = Conv2D(64, (3, 3), strides = (1, 1), padding = 'same', name = 'conv0')(X_input) \n    X = BatchNormalization(axis = 3, name = 'bn0')(X)\n    X = Activation('relu')(X) \n    \n    X = Conv2D(64, (3, 3), strides = (1, 1), padding = 'same', name = 'conv1')(X) \n    X = BatchNormalization(axis = 3, name = 'bn1')(X)\n    X = Activation('relu')(X)\n    \n    X = MaxPooling2D((3, 3), name='max_pool_0')(X)\n    X = Dropout(0.3)(X)\n    \n    # Second Layer\n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv3')(X) \n    X = BatchNormalization(axis = 3, name = 'bn3')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv4')(X) \n    X = BatchNormalization(axis = 3, name = 'bn4')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv5')(X) \n    X = BatchNormalization(axis = 3, name = 'bn5')(X)\n    X = Activation('relu')(X)\n     \n    X = MaxPooling2D((3, 3), name='max_pool_1')(X)\n    X = Dropout(0.3)(X)\n    \n    # Fourth Convolutional Layer\n    X = Conv2D(256, (3, 3), strides = (1, 1), padding = 'same', name = 'conv6')(X) \n    X = BatchNormalization(axis = 3, name = 'bn6')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(256, (3, 3), strides = (1, 1), padding = 'same', name = 'conv7')(X) \n    X = BatchNormalization(axis = 3, name = 'bn7')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(256, (3, 3), strides = (1, 1), padding = 'same', name = 'conv8')(X) \n    X = BatchNormalization(axis = 3, name = 'bn8')(X)\n    X = Activation('relu')(X)\n\n \n    X = MaxPooling2D((3, 3), name='max_pool_2')(X)\n    X = Dropout(0.3)(X)\n    \n    X = Conv2D(512, (3, 3), strides = (1, 1), padding = 'same', name = 'conv10')(X) \n    X = BatchNormalization(axis = 3, name = 'bn10')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(512, (3, 3), strides = (1, 1), padding = 'same', name = 'conv11')(X) \n    X = BatchNormalization(axis = 3, name = 'bn11')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(512, (3, 3), strides = (1, 1), padding = 'same', name = 'conv12')(X) \n    X = BatchNormalization(axis = 3, name = 'bn12')(X)\n    X = Activation('relu')(X)\n\n    \n    X = MaxPooling2D((3, 3), name='max_pool_3')(X)\n    X = Dropout(0.3)(X)\n    \n    # Flatten the data.\n    X = Flatten()(X)\n    # Dense Layer\n    X = Dense(4096, activation='relu', name='fc1')(X)\n    X = Dropout(0.5)(X)\n    X = Dense(1024, activation='relu', name='fc2')(X)\n    X = Dropout(0.5)(X)\n    X = Dense(256, activation='relu', name='fc3')(X)\n    # Using softmax function to get the output\n    X = Dense(1, activation='sigmoid', name='fc4')(X)\n    \n    model = Model(inputs = X_input, outputs = X, name='model')\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f8ca985f73d74c07d9b9b7c8f3b73b4f41643f93","trusted":true},"cell_type":"code","source":"Keras_Model = Keras_Model(X_train.shape[1:4])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3738ac4a4e7b8fb7dd2ad230c9f733237203aaac","trusted":true},"cell_type":"code","source":"from keras.optimizers import Adam\nepochs = 100\nbatch_size = 64\nlrate = 0.001\ndecay = lrate/epochs\noptimizer = Adam(lr=lrate, epsilon=1e-08, decay = decay)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2c7e680adbf89ea985de03ba05ebaebaaa5b077a","trusted":true},"cell_type":"code","source":"Keras_Model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ed853eff65ed21eb0cc72eb46933d830b4d192a"},"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=1, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.0000001)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a59c0ba29efd8d758aa122222f4787b711e0800"},"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, Callback, EarlyStopping\nearly_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1, mode='auto')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33d59bcc39c4a474f1696555e6f82101727eeb0c"},"cell_type":"code","source":"history = Keras_Model.fit(x = X_train, y = Y_train, batch_size = batch_size, \n                        epochs=epochs, verbose=1, \n                        validation_data = (X_val, Y_val),\n                          shuffle = True, \n                          steps_per_epoch= None, validation_steps=None,\n                                  callbacks=[learning_rate_reduction, early_stopping] )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"459602bc76cc1966a0f017f7ad5a0ac22c05caae","trusted":true},"cell_type":"code","source":"preds = Keras_Model.evaluate(X_train, Y_train)\nprint (\"Loss = \" + str(preds[0]))\nprint (\"Train set Accuracy = \" + str(preds[1]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0a25e3afbc805f2bf3fdcba48a671dce68c3544"},"cell_type":"code","source":"preds_val = Keras_Model.evaluate(X_val, Y_val)\nprint (\"Loss = \" + str(preds_val[0]))\nprint (\"Validation Set Accuracy = \" + str(preds_val[1]))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3798d66e9c624f98b1f83462d849e8b1b8d73ec1","trusted":true},"cell_type":"code","source":"history_dict = history.history\nhistory_dict.keys()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"36f0df8ccfd64876915bb3524ce5ade194e7ba3d","trusted":true},"cell_type":"code","source":"val_loss = history_dict['val_loss']\nval_acc = history_dict['val_acc']\nloss = history_dict['loss']\nacc = history_dict['acc']\nepochs = range(1,len(history_dict['val_loss'])+1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"391dd25d33d15a17da3ace56d44a91639fbab50b","trusted":true,"scrolled":true},"cell_type":"code","source":"plt.plot(epochs,acc,'b-')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\n\nplt.plot(epochs,val_acc,'b-', color = 'red')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f617e562701a140372075c8159c31947f3b90bb7","trusted":true},"cell_type":"code","source":"plt.plot(epochs,loss,'b-')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('Loss')\n\nplt.plot(epochs,val_loss,'b-', color = 'red')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('val_loss')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c1e62b41eca359540b11ad6404a16d8f1784a3d"},"cell_type":"code","source":"predicted_val_probability = Keras_Model.predict(X_val, batch_size=64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16b5c003ab117486c42bdde80f25c1e41ead387d"},"cell_type":"code","source":"Y_val_pred_label = np.round(predicted_val_probability)\nl = []\nfor i in range(len(Y_val_pred_label)):\n    if Y_val[i] != Y_val_pred_label[i]:\n        l.append(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"420a62d80b6556b910d8d8119173d8e56b3e9b37"},"cell_type":"code","source":"m = []\nfor t in l:\n    if predicted_val_probability[t] >= 0.99:\n        m.append(t)\n    elif predicted_val_probability[t] < 0.01:\n        m.append(t)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f0c136df87aef4f01625808ef79fc6ee25959fa7"},"cell_type":"code","source":"len(m)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2e6144870ed37a0766ef8005eccef28526c2af6"},"cell_type":"code","source":"plt.figure(figsize=(35,35))\nc = 1\nfor i in m[:]:\n    plt.subplot(10,5, c)\n    plt.imshow(X_val[i])\n    plt.title('DOG:{}\\nTrue label:{}'.format(predicted_val_probability[i], Y_val[i])\n              if predicted_val_probability[i]>= 0.5 else 'CAT:{}\\nTrue label:{}'.format(predicted_val_probability[i],Y_val[i]))\n    plt.axis('off')\n    c = c+1","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c25765553e1db2d44ebcb91fcb7b981a00603be8","trusted":true},"cell_type":"code","source":"X_Test_orig = []\nfor i in os.listdir('../input/test/'):\n    label = i.split('.')[-2]\n    img = cv2.imread('../input/test/'+i, cv2.IMREAD_COLOR)\n    img = cv2.resize(img,(IMG_SIZE,IMG_SIZE), interpolation = cv2.INTER_CUBIC)\n    #img = img / 255\n    X_Test_orig.append([np.array(img), np.array(label)])\n\nnp.save('Test_Data.npy', X_Train_orig)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"076a820fa95d5987acf8ac655460c2e694e9bfb4","trusted":true},"cell_type":"code","source":"X_test = np.array([i[0] for i in X_Test_orig]).reshape(-1,IMG_SIZE, IMG_SIZE, 3)\nLabel = np.array([i[1] for i in X_Test_orig])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29fb0a8ed8e1de26575c8d1748a10155f104eab8"},"cell_type":"code","source":"X_test = X_test / 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb8b4ac08954ebc942a4f158aae441b5df88c532"},"cell_type":"code","source":"classes = Keras_Model.predict(X_test, batch_size = batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"187383744f503a192f31139351b4280f54379827"},"cell_type":"code","source":"prediction = pd.DataFrame()\nprediction['id'] = Label\nprediction['label'] = classes\n\nprediction.to_csv('submission.csv', index = False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}