{"cells":[{"metadata":{"_uuid":"24ec9ae15faa9dcd87c40218ff0d1489ec141e67"},"cell_type":"markdown","source":"We are going to use the transfer learning technique where we are going to use the InceptionResnetV2 weights and count to get an accurate of **0.95%.**\n\nFor this kernel and for reasons of memory, we will reduce the number of training images and the number of iterations and still get a good result."},{"metadata":{"_uuid":"f67dd98dfed8f9b119dd6540f5b88c9d08302fd3","_cell_guid":"47f7faed-9d02-4151-9dfd-0cf9198c02e4","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport random\nimport cv2\nimport os\nimport gc\n\nfrom sklearn.model_selection import train_test_split\nfrom keras.applications import InceptionResNetV2\nfrom keras import layers\nfrom keras import models\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing.image import img_to_array, load_img","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a8c79bf32d7b86862260c19f094a54f0a4058e84","trusted":true},"cell_type":"code","source":"print(os.listdir(\"../input/\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"49cf9aa30372f03d8b1c6b9112abf11185e5f27c","trusted":true},"cell_type":"code","source":"train_dir = '../input/train'\ntest_dir = '../input/test'\n\n# train_imgs = ['../input/train/{}'.format(i) for i in os.listdir(train_dir)]  #get full data set\ntrain_dogs = ['../input/train/{}'.format(i) for i in os.listdir(train_dir) if 'dog' in i]  #get dog images\ntrain_cats = ['../input/train/{}'.format(i) for i in os.listdir(train_dir) if 'cat' in i]  #get cat images\n\ntest_imgs = ['../input/test/{}'.format(i) for i in os.listdir(test_dir)] #get test images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e1a493935bdc9506f55590f17553da8ddae23811"},"cell_type":"code","source":"size=4000\ntrain_imgs = train_dogs[0:size] + train_cats[0:size]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44496504784012612152dd4de8ff1529842bb632"},"cell_type":"code","source":"random.shuffle(train_imgs)  # shuffle it randomly","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80d626e2fc5892f55a4433b2859f794543f36715"},"cell_type":"code","source":"img_size = 150","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7945fb14b24c2b0b46785f8f7b373e1ff4e7311"},"cell_type":"code","source":"def read_and_process_image(list_of_images):\n    \"\"\"\n    Returns three arrays: \n        X is an array of resized images\n        y is an array of labels\n        l_id an array of Ids for submission\n    \"\"\"\n    X = [] # images\n    y = [] # labels\n    l_id = [] # id for submission\n    \n    for image in list_of_images:\n        X.append(cv2.resize(cv2.imread(image, cv2.IMREAD_COLOR), (img_size,img_size), interpolation=cv2.INTER_CUBIC))  #Read the image\n        basename = os.path.basename(image)\n        img_num = basename.split('.')[0]\n        l_id.append(img_num)\n        #get the labels\n        if 'dog' in image:\n            y.append(1)\n        elif 'cat' in image:\n            y.append(0)\n    \n    return X, y, l_id","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1336341d339cd25f9c1150e40137a8bebf62c302"},"cell_type":"code","source":"X, y, l_id = read_and_process_image(train_imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a54bd2785e0fdfddfafbc4e5f8c01ffb27696c78"},"cell_type":"code","source":"plt.figure(figsize=(20,10))\ncolumns = 5\nfor i in range(columns):\n    plt.subplot(5 / columns + 1, columns, i + 1)\n    plt.imshow(X[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7e06004ed7b69515132ebe922d3a15153ec026f"},"cell_type":"code","source":"X = np.array(X)\ny = np.array(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"81b55f8702fc30c37cf286c725dc1ff56fcbb8fd"},"cell_type":"code","source":"sns.countplot(y)\nplt.title('Labels for Cats and Dogs')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1b8101022ce7c1639ff21fd2f4cc08d96b553573"},"cell_type":"code","source":"print(\"Shape of train images is:\", X.shape)\nprint(\"Shape of labels is:\", y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3d6b84f15b7d58c56e0bf335001e2730f777099"},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.15, random_state=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99bf1939ee9f752a384a4a10730ca3eeaba3eb0a"},"cell_type":"code","source":"del X\ndel y\ndel train_imgs\ndel train_dogs\ndel train_cats\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af1b6c124744b92df1ca45c7c359f3bb8f9bc5cd"},"cell_type":"code","source":"print(\"Shape of X_train\",X_train.shape)\nprint(\"Shape of X_val\", X_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88847ec66872ae33e258b1fb12be96042f94b688"},"cell_type":"code","source":"ntrain = len(X_train)\nnval = len(X_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e097f759562bee5d2d2a8c7224ef6056a11e8e5"},"cell_type":"code","source":"conv_base = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=[150, 150, 3]) \nconv_base.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5a2cf5d1d9d105acb4c493c8d6532efff1bcf2d"},"cell_type":"code","source":"model = models.Sequential()\nmodel.add(conv_base)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))   \n\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7d3b71fd5d90461e9bc47cf6ff94d8c4f764039"},"cell_type":"code","source":"#model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0428ec5f16a01732c608bada194c49053cfbbcfe","trusted":true},"cell_type":"code","source":"batch_size = 128  \n\ntrain_datagen = ImageDataGenerator(rescale=1./255,   #Scale the image between 0 and 1\n                                    #rotation_range=30,\n                                    #width_shift_range=0.2,\n                                    #height_shift_range=0.2,\n                                    #shear_range=0.2,\n                                    #zoom_range=0.2,\n                                    #horizontal_flip=True,\n                                    fill_mode='nearest')\n\nval_datagen = ImageDataGenerator(rescale=1./255) \n\ntrain_generator = train_datagen.flow(X_train, y_train,  batch_size=batch_size)\nval_generator = val_datagen.flow(X_val, y_val, batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d7255e9632e08fd2f5e6a23a148a51d368d77fd6","trusted":true},"cell_type":"code","source":"epochs = 10\nhistory = model.fit_generator(train_generator,\n                              steps_per_epoch=ntrain // batch_size,\n                              epochs=epochs,\n                              validation_data=val_generator,\n                              validation_steps=nval // batch_size)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c659f54a47abec86066431895f1a2d0322e293ec","trusted":true},"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\n#Train and validation accuracy\nplt.plot(epochs, acc, 'b', label='Training accurarcy')\nplt.plot(epochs, val_acc, 'r', label='Validation accurarcy')\nplt.title('Training and Validation accurarcy')\nplt.legend()\n\nplt.figure()\n#Train and validation loss\nplt.plot(epochs, loss, 'b', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Validation loss')\nplt.title('Training and Validation loss')\nplt.legend()\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3eba8d0d996da12f65cc31dd101958d0b89c60b"},"cell_type":"code","source":"X_test, y_test, l_id = read_and_process_image(test_imgs[10:20]) #Y_test in this case will be empty.\nx = np.array(X_test)\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ni = 0\ncolumns = 5\ntext_labels = []\nplt.figure(figsize=(30,20))\nfor batch in test_datagen.flow(x, batch_size=1):\n    pred = model.predict(batch)\n    pred = np.float(pred)\n    if pred > 0.5:\n        text_labels.append('dog ({:.3f})'.format(pred))\n    else:\n        text_labels.append('cat ({:.3f})'.format(pred))\n    plt.subplot(5 / columns + 1, columns, i + 1)\n    plt.title('This is a ' + text_labels[i])\n    imgplot = plt.imshow(batch[0])\n    i += 1\n    if i % 10 == 0:\n        break\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6356ad17c42a6f014342decb20a958a6a26afec2"},"cell_type":"code","source":"del X_train\ndel X_val\ndel y_train\ndel y_val\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3ab1ddaf503c3690d306a72d50ca4268f1b14544"},"cell_type":"code","source":"X_test, y_test, l_id = read_and_process_image(test_imgs) \nx = np.array(X_test) / 255\ndel X_test\n\npredictions = model.predict(x)\npred=pd.DataFrame(predictions, columns=['label'])\nlid =pd.DataFrame(l_id, columns=['id'])\n\nsubmission = pd.concat([lid,pred],axis = 1)\nsubmission = submission.sort_values(['id'])\nsubmission.to_csv(\"cats_IncepRes.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f68a08fd0aea7cae7e4a45e5eb9803468aec7a4"},"cell_type":"code","source":"binary_pred=predictions\nbinary_pred[predictions>0.5] = 1\nbinary_pred[predictions<=0.5] = 0\n\npred=pd.DataFrame(binary_pred, columns=['label'])\nlid =pd.DataFrame(l_id, columns=['id'])\n\nsubmission = pd.concat([lid,pred],axis = 1)\nsubmission = submission.sort_values(['id'])\nsubmission.to_csv(\"cats_IncepRes_bp.csv\",index=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f7298f1f973eacb5b46983289c4b65a66e9a896f"},"cell_type":"code","source":"binary_pred=predictions\nbinary_pred[predictions>0.80] = 1\nbinary_pred[predictions<=0.2] = 0\n\npred=pd.DataFrame(binary_pred, columns=['label'])\nlid =pd.DataFrame(l_id, columns=['id'])\n\nsubmission = pd.concat([lid,pred],axis = 1)\nsubmission = submission.sort_values(['id'])\nsubmission.to_csv(\"cats_IncepRes_bp2.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"81e9dd4fdd20221a12631d5ae43f1d38b5d358f6"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"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"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}