{"cells":[{"metadata":{"_uuid":"3a3985b58aad59338cedb4ab9813960736f6ce77"},"cell_type":"markdown","source":"> In this kernel, we'll use Transfer learning to achieve an accuracy of about 96%  on the Dog VS. Cat classification challenge.\n**\nA [link](https://medium.com/@risingdeveloper/transfer-learning-on-image-classification-using-keras-and-kaggle-kernels-c76d3b030649) to my Medium post with full explanation of this Notebook and transfer learning in general**"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#Lets take a look at our directory\nimport os, shutil\nprint(os.listdir(\"../input\"))\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"The train data contains 25,000 images of both dogs and cats. We are going to sample a small portion of  the data for training because of memory and Ram limits on kaggle kernels. So therefore, we will use Data Augmentation to reduce overfitting."},{"metadata":{"trusted":true,"_uuid":"cf9b6bdfbb7bbeff9f5da51eadb71faf8a451603"},"cell_type":"code","source":"import random\nimport gc   #Gabage collector for cleaning deleted data from memory\n\ntrain_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\n\ntrain_imgs = train_dogs[:2000] + train_cats[:2000]  # slice the dataset and use 2000 in each class\nrandom.shuffle(train_imgs)  # shuffle it randomly\n\n#Clear list that are useless\ndel train_dogs\ndel train_cats\ngc.collect()   #collect garbage to save memory","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"18ea669e7f0ede7c486c1d49b5b6e8256d7a807e"},"cell_type":"code","source":"#Import some packages to use\nimport cv2\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nfrom matplotlib import ticker\nimport seaborn as sns\n%matplotlib inline ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f4f8d5cba1748b3c9a800aef21b3665da8d4615"},"cell_type":"code","source":"#Lets declare our image dimensions\n#we are using coloured images. \nnrows = 150\nncolumns = 150\nchannels = 3  #change to 1 if you want to use grayscale image\n\n\n#A function to read and process the images to an acceptable format for our model\ndef read_and_process_image(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\n    y = [] # labels\n    \n    for image in list_of_images:\n        X.append(cv2.resize(cv2.imread(image, cv2.IMREAD_COLOR), (nrows,ncolumns), interpolation=cv2.INTER_CUBIC))  #Read the image\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00cc550b88aaf9185b76a222b1e71334852c96e4"},"cell_type":"code","source":"#get the train and label data\nX, y = read_and_process_image(train_imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66b3ba8fa1cc19c4a074ce8fa3de2861aa15d14e"},"cell_type":"code","source":"#Lets view some of the pics\nplt.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":{"_uuid":"f802b89c6f5c0d5375abc406f5e06f5484ecf184","trusted":true},"cell_type":"code","source":"del train_imgs\ngc.collect()\n#Convert list to numpy array\nX = np.array(X)\ny = np.array(y)\n\n#Lets plot the label to be sure we just have two class\nsns.countplot(y)\nplt.title('Labels for Cats and Dogs')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"64e9d91fcf06646078638b92f03e6fc190017b12"},"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":"1be578474747d0b493dd38b65422e53b524463ef"},"cell_type":"code","source":"#Lets split the data into train and test set\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.20, random_state=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad1ba887aa06009efabcec6b6d4c015c671b0dda"},"cell_type":"code","source":"#clear memory\ndel X\ndel y\ngc.collect()\n\n#get the length of the train and validation data\nntrain = len(X_train)\nnval = len(X_val)\n\n#We will use a batch size of 32. Note: batch size should be a factor of 2.***4,8,16,32,64...***\nbatch_size = 32  \n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8776a451cd2055acfa494764934ee6e9dcb49fbe"},"cell_type":"markdown","source":"Now lets create our model."},{"metadata":{"trusted":true,"_uuid":"232be44fb2282fd93a783c999a86145c199963d8"},"cell_type":"code","source":"from keras.applications import InceptionResNetV2\n\nconv_base = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(150,150,3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"516f101110b3b7705bde4c4d1fba77af96fd4cd3"},"cell_type":"code","source":"conv_base.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"996596d2355a4c1e57f79e477dd6b2f75d868176"},"cell_type":"code","source":"from keras import layers\nfrom keras import models\n\nmodel = models.Sequential()\nmodel.add(conv_base)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))  #Sigmoid function at the end because we have just two classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3819c1880f1b9b85bebe70d60747fbcacfdd2198"},"cell_type":"code","source":"#Lets see our model\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"89c5b84a4485be583f71a809bb936fe71fb6b023"},"cell_type":"code","source":"print('Number of trainable weights before freezing the conv base:', len(model.trainable_weights))\nconv_base.trainable = False\nprint('Number of trainable weights after freezing the conv base:', len(model.trainable_weights))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0353019267ca5cfffba2d72ff55dfc931cb6d3b"},"cell_type":"code","source":"#We'll use the RMSprop optimizer with a learning rate of 0.0001\n#We'll use binary_crossentropy loss because its a binary classification\nfrom keras import optimizers\n\nmodel.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=2e-5), metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e76686be36b4cf570402eb52db63901e4d42df84"},"cell_type":"code","source":"#Lets create the augmentation configuration\n#This helps prevent overfitting, since we are using a small dataset\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing.image import img_to_array, load_img\n\ntrain_datagen = ImageDataGenerator(rescale=1./255,   #Scale the image between 0 and 1\n                                    rotation_range=40,\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)  #We do not augment validation data. we only perform rescale","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d375212932d85aba68c73c0b02103c04bdfe60e"},"cell_type":"code","source":"#Create the image generators\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":{"trusted":true,"_uuid":"576d8d10a4b6f59fa39e9bc44b954aae887d2954","scrolled":false},"cell_type":"code","source":"#The training part\n#We train for 64 epochs with about 100 steps per epoch\nhistory = model.fit_generator(train_generator,\n                              steps_per_epoch=ntrain // batch_size,\n                              epochs=20,\n                              validation_data=val_generator,\n                              validation_steps=nval // batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"770dacdaf6156a5d45737faafc7b9b03c34d2b66"},"cell_type":"code","source":"#Save the model\nmodel.save_weights('model_wieghts.h5')\nmodel.save('model_keras.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"02e8aaf73583b328f76b025240eb9914cdf12bf0"},"cell_type":"code","source":"#lets plot the train and val curve\nimport matplotlib.pyplot as plt\n\n#get the details form the history object\nacc = 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()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"86589b00cdb6e366298ddb4d18edf89c1ba54435"},"cell_type":"markdown","source":"Let's create a function that makes our plot looks smoother and cleaner."},{"metadata":{"trusted":true,"_uuid":"5c3c9ad9891dae2d5a3f24c460a1c3e322dc6089"},"cell_type":"code","source":"def smooth_plot(points, factor=0.7):\n    smooth_pts = []\n    for point in points:\n        if smooth_pts:\n            previous = smooth_pts[-1]\n            smooth_pts.append(previous * factor + point * (1 - factor))\n        else:\n            smooth_pts.append(point)\n    return smooth_pts","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"badb83ae16e7e826575fedcaf0b684d099aebc7e"},"cell_type":"code","source":"#Plot figure\nplt.plot(epochs, smooth_plot(acc), 'b', label='Training accurarcy')\nplt.plot(epochs, smooth_plot(val_acc), 'r', label='Validation accurarcy')\nplt.title('Training and Validation accurarcy')\nplt.legend()\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a3e841ea9e128d78d441116a446d2dcfe87b088"},"cell_type":"code","source":"#Now lets predict on the first 10 Images of the test set\nX_test, y_test = read_and_process_image(test_imgs[0:10]) #Y_test in this case will be empty.\nx = np.array(X_test)\ntest_datagen = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3cb40f6f11ea6124330eb9c41774fc74ad3c266d"},"cell_type":"code","source":"i = 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    if pred > 0.5:\n        text_labels.append('dog')\n    else:\n        text_labels.append('cat')\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,"collapsed":true,"_uuid":"0b649af20dbb34d9875309a1fe04a520c758903e"},"cell_type":"markdown","source":"A [link](https://medium.com/@risingdeveloper/transfer-learning-on-image-classification-using-keras-and-kaggle-kernels-c76d3b030649) to my Medium post with full explanation of the code and transfer learning in general"},{"metadata":{"trusted":true,"_uuid":"d7c04358bfb77ec5fc96552259b25ecf98b02be1"},"cell_type":"code","source":"","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}