{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<a href=\"https://colab.research.google.com/github/sidharth178/Cat-Dog-Classification-Flask-App/blob/master/cat_dog_classification.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>","metadata":{"id":"view-in-github"}},{"cell_type":"markdown","source":"# **Table of Contents**\n\n1. Introduction\n1. Import library\n1. Fetch datasets from kaggle\n1. Prepare training data\n1. Data pre-processing\n1. Model building\n1. Prepare testing data\n1. Find prediction\n\n","metadata":{"id":"9SoBSzMC5TJp"}},{"cell_type":"markdown","source":"# **Introduction**\n\nIn this project, we'll write an algorithm to classify whether images contain either a dog or a cat.  This is easy for humans, dogs, and cats but our computer will find it a bit more difficult.\n### Data Description\nThe folder \"train\" contains two sub-folders \"cats\" & \"dogs\" which contain images of cats and dogs respectively.The folder \"test1\" contain unknown images which we have to classify.\n### Data\nTo download the Dataset click [here](https://www.kaggle.com/c/dogs-vs-cats/data)\n### Objective\nTo build a deep learning classification model which classify whether images contain either a dog or a cat. \n\n","metadata":{"id":"Ta4h4I0S5isk"}},{"cell_type":"markdown","source":"# **1. Import Library**","metadata":{"id":"orb_gVwmfpLh"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport keras\nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\n\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom keras.wrappers.scikit_learn import KerasClassifier\nfrom sklearn.model_selection import GridSearchCV\n","metadata":{"id":"Z-2zU9fQGN33","execution":{"iopub.status.busy":"2021-07-03T11:12:10.926128Z","iopub.execute_input":"2021-07-03T11:12:10.926566Z","iopub.status.idle":"2021-07-03T11:12:10.936321Z","shell.execute_reply.started":"2021-07-03T11:12:10.926527Z","shell.execute_reply":"2021-07-03T11:12:10.935490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **2. Fetch datasets from kaggle**","metadata":{"id":"TonGIKhwgO90"}},{"cell_type":"code","source":"# unzip the dataset\nfrom zipfile import ZipFile\nzf = ZipFile('../input/dogs-vs-cats/train.zip')\nzf.extractall('/kaggle/working/') # save files in selected folder\nzf.close()","metadata":{"id":"rMI1s41-42vS","execution":{"iopub.status.busy":"2021-07-03T11:12:10.938225Z","iopub.execute_input":"2021-07-03T11:12:10.938643Z","iopub.status.idle":"2021-07-03T11:12:19.770616Z","shell.execute_reply.started":"2021-07-03T11:12:10.938605Z","shell.execute_reply":"2021-07-03T11:12:19.769821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **3. Prepare Training Data**\nHere we will create dataframe which will store all file names in \"filenames\" column and value \"1\" for \"dog\" and \"0\" for \"cat\". ","metadata":{"id":"nqCWR2LMi5gq"}},{"cell_type":"code","source":"filenames = os.listdir(\"/kaggle/working/train\")\ncategories = []\nfor name in filenames:\n    category = name.split('.')[0]\n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n\nimage_df = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})","metadata":{"id":"XsDw9ifS6lEa","execution":{"iopub.status.busy":"2021-07-03T11:12:19.772667Z","iopub.execute_input":"2021-07-03T11:12:19.773004Z","iopub.status.idle":"2021-07-03T11:12:19.819408Z","shell.execute_reply.started":"2021-07-03T11:12:19.772969Z","shell.execute_reply":"2021-07-03T11:12:19.818761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# See top 5 rows of the dataset\nimage_df.head()","metadata":{"id":"hHSjDnUIEUSy","outputId":"039ddb2a-b288-477e-87c7-677400726ec6","execution":{"iopub.status.busy":"2021-07-03T11:12:19.821589Z","iopub.execute_input":"2021-07-03T11:12:19.821935Z","iopub.status.idle":"2021-07-03T11:12:19.833887Z","shell.execute_reply.started":"2021-07-03T11:12:19.821900Z","shell.execute_reply":"2021-07-03T11:12:19.832903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# See buttom 5 cell of the dataset\nimage_df.tail()","metadata":{"id":"Hu8hKYbxEUVX","outputId":"22a8ef24-8d20-46e3-b20b-d8134c34a578","execution":{"iopub.status.busy":"2021-07-03T11:12:19.835614Z","iopub.execute_input":"2021-07-03T11:12:19.836018Z","iopub.status.idle":"2021-07-03T11:12:19.846602Z","shell.execute_reply.started":"2021-07-03T11:12:19.835941Z","shell.execute_reply":"2021-07-03T11:12:19.845614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df.shape","metadata":{"id":"9ar1pBtXehW_","outputId":"1bffa524-10f9-46c7-9b26-dc285986eb6d","execution":{"iopub.status.busy":"2021-07-03T11:12:19.848345Z","iopub.execute_input":"2021-07-03T11:12:19.849012Z","iopub.status.idle":"2021-07-03T11:12:19.854780Z","shell.execute_reply.started":"2021-07-03T11:12:19.848974Z","shell.execute_reply":"2021-07-03T11:12:19.853876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualize number of cats and dogs present in the dataframe\nimage_df['category'].value_counts().plot.bar()","metadata":{"id":"BCw7mwtYEUZK","outputId":"f464c8b8-fb83-4b18-fb45-847a3da03eee","execution":{"iopub.status.busy":"2021-07-03T11:12:19.856606Z","iopub.execute_input":"2021-07-03T11:12:19.856966Z","iopub.status.idle":"2021-07-03T11:12:19.975259Z","shell.execute_reply.started":"2021-07-03T11:12:19.856932Z","shell.execute_reply":"2021-07-03T11:12:19.974481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **3.1. Sample Image**","metadata":{"id":"TCcsRCqSvMnG"}},{"cell_type":"code","source":"sample = random.choice(filenames)\nimage = load_img(\"/kaggle/working/train/\"+sample)\nplt.imshow(image)","metadata":{"id":"UPO4nreu6lI2","outputId":"46fb02ec-35a7-4ae5-f5b6-4e95bee58a7b","execution":{"iopub.status.busy":"2021-07-03T11:12:19.976887Z","iopub.execute_input":"2021-07-03T11:12:19.977213Z","iopub.status.idle":"2021-07-03T11:12:20.235089Z","shell.execute_reply.started":"2021-07-03T11:12:19.977180Z","shell.execute_reply":"2021-07-03T11:12:20.234159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **4.Data Pre-processing**\nAs we use image genaretor `with class_mode=\"categorical\"`. We need to convert column category into string. Then imagenerator will convert it one-hot encoding. \n\nSo we will convert 1 to \"dog\" and 0 to \"cat\"","metadata":{"id":"xNQEl8h_evKN"}},{"cell_type":"code","source":"# replace 0 as \"cat\" & 1 as \"dog\"\nimage_df[\"category\"] = image_df[\"category\"].replace({0: 'cat', 1: 'dog'}) ","metadata":{"id":"XzrVsIhtjbGg","execution":{"iopub.status.busy":"2021-07-03T11:12:20.238162Z","iopub.execute_input":"2021-07-03T11:12:20.239610Z","iopub.status.idle":"2021-07-03T11:12:20.250693Z","shell.execute_reply.started":"2021-07-03T11:12:20.239568Z","shell.execute_reply":"2021-07-03T11:12:20.249866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the dataset for training & testing\ntrain_df, validate_df = train_test_split(image_df, test_size=0.20, random_state=23)\n# training dataset\ntrain_df = train_df.reset_index(drop=True)\n# testing dataset\nvalidate_df = validate_df.reset_index(drop=True)","metadata":{"id":"CyfT4zIEeotm","execution":{"iopub.status.busy":"2021-07-03T11:12:20.255168Z","iopub.execute_input":"2021-07-03T11:12:20.257107Z","iopub.status.idle":"2021-07-03T11:12:20.270750Z","shell.execute_reply.started":"2021-07-03T11:12:20.257072Z","shell.execute_reply":"2021-07-03T11:12:20.269869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# see frequency of class variables in target column of training dataset\ntrain_df['category'].value_counts().plot.bar(color=[\"cyan\",\"pink\"])","metadata":{"id":"vq91A8n9eopt","outputId":"863b76fb-08e2-4cb1-9347-5cb02d621640","execution":{"iopub.status.busy":"2021-07-03T11:12:20.274287Z","iopub.execute_input":"2021-07-03T11:12:20.276271Z","iopub.status.idle":"2021-07-03T11:12:20.466749Z","shell.execute_reply.started":"2021-07-03T11:12:20.276236Z","shell.execute_reply":"2021-07-03T11:12:20.460585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# see frequency of class variables in target column of testing dataset\nvalidate_df['category'].value_counts().plot.bar(color=[\"cyan\",\"pink\"])","metadata":{"id":"0X02h2_seonY","outputId":"bb6d1660-9caf-45a5-a19b-9ac34bcdc0e9","execution":{"iopub.status.busy":"2021-07-03T11:12:20.473928Z","iopub.execute_input":"2021-07-03T11:12:20.476191Z","iopub.status.idle":"2021-07-03T11:12:20.641498Z","shell.execute_reply.started":"2021-07-03T11:12:20.476151Z","shell.execute_reply":"2021-07-03T11:12:20.640560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **4.1. Define Constants**","metadata":{"id":"aRN9TCGX4aZ0"}},{"cell_type":"code","source":"FAST_RUN = False\nIMAGE_WIDTH=128\nIMAGE_HEIGHT=128\nIMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)\nIMAGE_CHANNELS=3\nbatch_size=15","metadata":{"id":"J2UC7W8z4Y-9","execution":{"iopub.status.busy":"2021-07-03T11:12:20.644685Z","iopub.execute_input":"2021-07-03T11:12:20.645027Z","iopub.status.idle":"2021-07-03T11:12:20.655132Z","shell.execute_reply.started":"2021-07-03T11:12:20.644994Z","shell.execute_reply":"2021-07-03T11:12:20.653297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_size = train_df.shape[0]\nvalidate_df_size = validate_df.shape[0]","metadata":{"id":"IggLeioteojP","execution":{"iopub.status.busy":"2021-07-03T11:12:20.656990Z","iopub.execute_input":"2021-07-03T11:12:20.657415Z","iopub.status.idle":"2021-07-03T11:12:20.667062Z","shell.execute_reply.started":"2021-07-03T11:12:20.657339Z","shell.execute_reply":"2021-07-03T11:12:20.666022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **4.2. Training Generator**","metadata":{"id":"IIwB4HgSh7Dj"}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/kaggle/working/train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"id":"CYuB_AtkhgAs","outputId":"496b68b7-5e55-4ace-8b3b-0ebe2c7eca9b","execution":{"iopub.status.busy":"2021-07-03T11:12:20.669165Z","iopub.execute_input":"2021-07-03T11:12:20.670315Z","iopub.status.idle":"2021-07-03T11:12:21.031154Z","shell.execute_reply.started":"2021-07-03T11:12:20.670270Z","shell.execute_reply":"2021-07-03T11:12:21.030173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **4.3. Validation Generator**","metadata":{"id":"KK2uWRUfkDm0"}},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"/kaggle/working/train\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"id":"aFYGTVc_hf-i","outputId":"5f655130-7ece-4f60-8847-47bee91f1466","execution":{"iopub.status.busy":"2021-07-03T11:12:21.035523Z","iopub.execute_input":"2021-07-03T11:12:21.036035Z","iopub.status.idle":"2021-07-03T11:12:21.134944Z","shell.execute_reply.started":"2021-07-03T11:12:21.035996Z","shell.execute_reply":"2021-07-03T11:12:21.134050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **4.4. See how generator work**","metadata":{"id":"L52VPDiyk45o"}},{"cell_type":"code","source":"example_df = train_df.sample(n=1).reset_index(drop=True)\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df, \n    \"/kaggle/working/train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical'\n)","metadata":{"id":"SA3D-5Erhf7Q","outputId":"6533639b-d944-47fe-cd47-2a036e259f2e","execution":{"iopub.status.busy":"2021-07-03T11:12:21.136223Z","iopub.execute_input":"2021-07-03T11:12:21.136679Z","iopub.status.idle":"2021-07-03T11:12:21.148056Z","shell.execute_reply.started":"2021-07-03T11:12:21.136645Z","shell.execute_reply":"2021-07-03T11:12:21.147133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualize generated images from one image\nplt.figure(figsize=(12, 12))\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        break\nplt.tight_layout()\nplt.show()","metadata":{"id":"coHhX4KGhf2S","outputId":"d1e66c63-bddd-4221-b697-1aa74e3ddc64","execution":{"iopub.status.busy":"2021-07-03T11:12:21.149090Z","iopub.execute_input":"2021-07-03T11:12:21.149526Z","iopub.status.idle":"2021-07-03T11:12:23.537177Z","shell.execute_reply.started":"2021-07-03T11:12:21.149493Z","shell.execute_reply":"2021-07-03T11:12:23.536317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **5. Model Building**\n<img src=\"https://media.geeksforgeeks.org/wp-content/uploads/cat-vs-dog.jpg\" width=\"100%\"/>","metadata":{"id":"7ZNxidiB1iZU"}},{"cell_type":"markdown","source":"* **Conv Layer**: Convolutional layers are the layers where filters are applied to the original image, or to other feature maps in a deep CNN.\n* **Conv2D Layer**: Conv2D is a 2D Convolution Layer, this layer creates a convolution kernel that is wind with layers input which helps produce a tensor of outputs.\n* **Pooling Layer**: Its function is to progressively reduce the spatial size of the representation to reduce the amount of parameters and computation in the network.\n* **Fully Connected Layer**: It connect the network from a layer to another layer\n* **Output Layer**: It is the predicted values layer. \n* **BatchNormalization**: Layer that normalizes its inputs.Batch normalization applies a transformation that maintains the mean output close to 0 and the output standard deviation close to 1.","metadata":{"id":"SuMaaygR3VSN"}},{"cell_type":"code","source":"# initializing the CNN\nmodel = Sequential()\n\n# Convolution layer-1\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\n# Convolution layer-2\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\n# Convolution layer-3\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\n# flattening layer\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n\n# Output layer\nmodel.add(Dense(2, activation='softmax')) # 2 because we have cat and dog classes\n\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n# summary of model\nmodel.summary()","metadata":{"id":"4hv2gUqL0GCP","outputId":"f04f967f-8c9d-4cd4-fe2b-b7c642a91876","execution":{"iopub.status.busy":"2021-07-03T11:12:23.538804Z","iopub.execute_input":"2021-07-03T11:12:23.539145Z","iopub.status.idle":"2021-07-03T11:12:23.663475Z","shell.execute_reply.started":"2021-07-03T11:12:23.539110Z","shell.execute_reply":"2021-07-03T11:12:23.662027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **5.1. Callback Functions**\nA callback is a set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training.\n- **EarlyStopping** : Used to avoid overfitting.Here we'll stop the training if there is no improvement in 3 conjecutive epochs.\n- **ModelCheckpoint** : This callback saves the model after every epoch.The model'll save in a particular location with minimun 'val_loss'.\n- **ReduceLROnPlateau** : It reduces learning rate when a metric has stopped improving.\nFor more about callback function click [here](https://www.kdnuggets.com/2019/08/keras-callbacks-explained-three-minutes.html) [here](https://keras.io/api/callbacks/)","metadata":{"id":"19tgVb2IHR9L"}},{"cell_type":"code","source":"# EarlyStopping\nearlystop = EarlyStopping(monitor = 'val_loss',\n                          min_delta = 0,\n                          patience = 7,\n                          verbose = 1,\n                          restore_best_weights = True)\n\n# ModelCheckPoint\ncheckPoint = keras.callbacks.ModelCheckpoint(filepath=\"/content/sample_data/cd_model.h5\",\n                             monitor='val_loss',\n                             mode='min',\n                             save_best_only=True,\n                             verbose=1)\n\n# ReduceLROnPlateau\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\n\n# TBoard = tf.keras.callbacks.TensorBoard(log_dir='./logs')","metadata":{"id":"ThR7UrmD0F7p","execution":{"iopub.status.busy":"2021-07-03T11:12:23.664917Z","iopub.execute_input":"2021-07-03T11:12:23.665378Z","iopub.status.idle":"2021-07-03T11:12:23.675270Z","shell.execute_reply.started":"2021-07-03T11:12:23.665333Z","shell.execute_reply":"2021-07-03T11:12:23.674518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [earlystop , checkPoint, learning_rate_reduction]","metadata":{"id":"Cd7RbBux0giI","execution":{"iopub.status.busy":"2021-07-03T11:12:23.676554Z","iopub.execute_input":"2021-07-03T11:12:23.676948Z","iopub.status.idle":"2021-07-03T11:12:23.684164Z","shell.execute_reply.started":"2021-07-03T11:12:23.676890Z","shell.execute_reply":"2021-07-03T11:12:23.683157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this code is to show how much time required to train the model using different algorithms\nfrom datetime import datetime\ndef timer(start_time= None):\n  if not start_time:\n    start_time=datetime.now()\n    return start_time\n  elif start_time:\n    thour,temp_sec=divmod((datetime.now()-start_time).total_seconds(),3600)\n    tmin,tsec=divmod(temp_sec,60)\n    print('\\n Time taken: %i hours %i minutes and %s seconds. '% (thour,tmin,round(tsec,2)))","metadata":{"id":"AqUs4venmRPZ","execution":{"iopub.status.busy":"2021-07-03T11:12:23.685924Z","iopub.execute_input":"2021-07-03T11:12:23.686284Z","iopub.status.idle":"2021-07-03T11:12:23.695011Z","shell.execute_reply.started":"2021-07-03T11:12:23.686250Z","shell.execute_reply":"2021-07-03T11:12:23.693952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **5.2. Model Fitting**","metadata":{"id":"OW2iNaWZOVgP"}},{"cell_type":"code","source":"start_time=timer(None)\nepochs=3 if FAST_RUN else 40   \nclassifier = model.fit_generator(\n    train_generator, \n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=validate_df_size//batch_size,\n    steps_per_epoch=train_df_size//batch_size,\n    callbacks=callbacks\n)\ntimer(start_time)","metadata":{"id":"uf55JHzZ0tXZ","outputId":"3e046580-d545-48a3-c36d-a68467ee459b","execution":{"iopub.status.busy":"2021-07-03T11:12:23.696393Z","iopub.execute_input":"2021-07-03T11:12:23.696969Z","iopub.status.idle":"2021-07-03T12:12:14.828517Z","shell.execute_reply.started":"2021-07-03T11:12:23.696932Z","shell.execute_reply":"2021-07-03T12:12:14.826520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **5.3. Save the Model**","metadata":{"id":"-gWys7g9eEO6"}},{"cell_type":"code","source":"# As we have used ModelCheckpoint callback function.So no need to save again if your model trained completely.\n#Saving Scikitlearn models\nmodel.save(\"/kaggle/working/cat_dog_classifierr.h5\")","metadata":{"id":"uEqjZndN0tUu","execution":{"iopub.status.busy":"2021-07-03T12:12:14.830063Z","iopub.execute_input":"2021-07-03T12:12:14.830472Z","iopub.status.idle":"2021-07-03T12:12:15.144301Z","shell.execute_reply.started":"2021-07-03T12:12:14.830403Z","shell.execute_reply":"2021-07-03T12:12:15.143237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **5.4. Load Model**","metadata":{"id":"mz-h_-tEWzm8"}},{"cell_type":"code","source":"from keras.models import load_model\nnew_model = load_model('/kaggle/working/cat_dog_classifierr.h5')","metadata":{"id":"oFGdq6FNoPGm","execution":{"iopub.status.busy":"2021-07-03T12:12:15.147705Z","iopub.execute_input":"2021-07-03T12:12:15.147966Z","iopub.status.idle":"2021-07-03T12:12:15.509027Z","shell.execute_reply.started":"2021-07-03T12:12:15.147941Z","shell.execute_reply":"2021-07-03T12:12:15.508196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **5.5. Visualize training accuracy and loss**","metadata":{"id":"b5TwHW6TlJM3"}},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\n\n# plot graph of training loss & validation loss\nax1.plot(classifier.history['loss'], color='b', label=\"Training loss\")\nax1.plot(classifier.history['val_loss'], color='r', label=\"validation loss\")\nax1.set_xticks(np.arange(1, 30, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\nax1.set_xlabel(\"epochs\")\nax1.set_ylabel(\"loss\")\nax1.set_title(\"Graph of training loss & validation loss\")\n\n# plot graph of training accuracy & validation accuracy\nax2.plot(classifier.history['accuracy'], color='b', label=\"Training accuracy\")\nax2.plot(classifier.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nax2.set_xticks(np.arange(1, 30, 1))\nax1.set_xlabel(\"epochs\")\nax1.set_ylabel(\"accuracy\")\nax1.set_title(\"Graph of training accuracy & validation accuracy\")\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"id":"3y4ox-UI0tQA","outputId":"b4a37b6f-1008-4188-d4c6-59e78e2bf21e","execution":{"iopub.status.busy":"2021-07-03T12:12:15.510320Z","iopub.execute_input":"2021-07-03T12:12:15.510697Z","iopub.status.idle":"2021-07-03T12:12:16.130041Z","shell.execute_reply.started":"2021-07-03T12:12:15.510660Z","shell.execute_reply":"2021-07-03T12:12:16.129015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **6. Prepare Testing Data**","metadata":{"id":"aX3CGUeaGYH5"}},{"cell_type":"code","source":"# unzip the dataset\nfrom zipfile import ZipFile\nzf = ZipFile('../input/dogs-vs-cats/test1.zip')\nzf.extractall('/kaggle/working') #save files in selected folder\nzf.close()","metadata":{"id":"Y5-BxkY_yxjj","execution":{"iopub.status.busy":"2021-07-03T12:12:16.131577Z","iopub.execute_input":"2021-07-03T12:12:16.131944Z","iopub.status.idle":"2021-07-03T12:12:23.126929Z","shell.execute_reply.started":"2021-07-03T12:12:16.131904Z","shell.execute_reply":"2021-07-03T12:12:23.126042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a dataframe and store all image files\ntest_filenames = os.listdir(\"/kaggle/working/test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","metadata":{"id":"OmQHjj5v0tOJ","execution":{"iopub.status.busy":"2021-07-03T12:12:23.128129Z","iopub.execute_input":"2021-07-03T12:12:23.128462Z","iopub.status.idle":"2021-07-03T12:12:23.143430Z","shell.execute_reply.started":"2021-07-03T12:12:23.128411Z","shell.execute_reply":"2021-07-03T12:12:23.142791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **6.1. Create Testing Generator**","metadata":{"id":"AF5LR5llG5Ld"}},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/working/test1/\",\n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"id":"0ri-pjjIjf9g","outputId":"9ba8571d-c9ac-4a5c-e5c2-7c1ff15e7551","execution":{"iopub.status.busy":"2021-07-03T12:12:23.144958Z","iopub.execute_input":"2021-07-03T12:12:23.145297Z","iopub.status.idle":"2021-07-03T12:12:23.259017Z","shell.execute_reply.started":"2021-07-03T12:12:23.145263Z","shell.execute_reply":"2021-07-03T12:12:23.258112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **7. Find Prediction**","metadata":{"id":"mJWtTv2EHD-0"}},{"cell_type":"code","source":"# find prediction using predict_generator\npredict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","metadata":{"id":"kr77DccAwsYV","outputId":"27b35100-b58a-4aaa-9cae-be58ba4ec40d","execution":{"iopub.status.busy":"2021-07-03T12:12:23.260726Z","iopub.execute_input":"2021-07-03T12:12:23.261119Z","iopub.status.idle":"2021-07-03T12:12:52.761998Z","shell.execute_reply.started":"2021-07-03T12:12:23.261079Z","shell.execute_reply":"2021-07-03T12:12:52.761188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# here \"category\" column store the index of higher predicted value\ntest_df['category'] = np.argmax(predict, axis=-1)","metadata":{"id":"ecKHub2Cwsa-","execution":{"iopub.status.busy":"2021-07-03T12:12:52.765576Z","iopub.execute_input":"2021-07-03T12:12:52.765859Z","iopub.status.idle":"2021-07-03T12:12:52.772832Z","shell.execute_reply.started":"2021-07-03T12:12:52.765832Z","shell.execute_reply":"2021-07-03T12:12:52.771827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# here predicted value will replace by \"cat\" for 0 & \"dog\" for 1\nlabel_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","metadata":{"id":"f4-ttEjBwseq","execution":{"iopub.status.busy":"2021-07-03T12:12:52.775343Z","iopub.execute_input":"2021-07-03T12:12:52.775897Z","iopub.status.idle":"2021-07-03T12:12:52.785215Z","shell.execute_reply.started":"2021-07-03T12:12:52.775858Z","shell.execute_reply":"2021-07-03T12:12:52.784523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# See predicted dataframe\ntest_df.head()","metadata":{"id":"MClpiN8mw-ju","outputId":"11743752-dad2-4db8-96e3-659db31696e4","execution":{"iopub.status.busy":"2021-07-03T12:12:52.786971Z","iopub.execute_input":"2021-07-03T12:12:52.787238Z","iopub.status.idle":"2021-07-03T12:12:52.799194Z","shell.execute_reply.started":"2021-07-03T12:12:52.787214Z","shell.execute_reply":"2021-07-03T12:12:52.798161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **7.1. Visualize Result**","metadata":{"id":"0x-TT6qmHZv9"}},{"cell_type":"code","source":"# see how many images classify into which class \ntest_df['category'].value_counts().plot.bar()","metadata":{"id":"pvC0QwRIHTMO","outputId":"69a98f95-e679-4096-e660-17bc10017316","execution":{"iopub.status.busy":"2021-07-03T12:12:52.800709Z","iopub.execute_input":"2021-07-03T12:12:52.801306Z","iopub.status.idle":"2021-07-03T12:12:52.919742Z","shell.execute_reply.started":"2021-07-03T12:12:52.801268Z","shell.execute_reply":"2021-07-03T12:12:52.919021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **7.2. See Predicted Image With Images**","metadata":{"id":"3AR2_dGaH9J2"}},{"cell_type":"code","source":"sample_test = test_df.head(18)\nsample_test.head()\nplt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = row['category']\n    img = load_img(\"/kaggle/working/test1/\"+filename, target_size=IMAGE_SIZE)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category.upper()) + ')' )\nplt.tight_layout()\nplt.show()","metadata":{"id":"imwhf0TOH6jb","outputId":"1692a430-b538-4baa-880b-90b311e75f68","execution":{"iopub.status.busy":"2021-07-03T12:12:52.922754Z","iopub.execute_input":"2021-07-03T12:12:52.923002Z","iopub.status.idle":"2021-07-03T12:12:55.783187Z","shell.execute_reply.started":"2021-07-03T12:12:52.922977Z","shell.execute_reply":"2021-07-03T12:12:55.782113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If you get any issues during run this notebook.Feel free to contact.\n[Linkedin](https://www.linkedin.com/in/sidharth178),\n[Github](https://github.com/sidharth178)\n\nThank You.\n","metadata":{"id":"25cJKVuHAiNP"}},{"cell_type":"markdown","source":"### The complete code of \"Cat Dog Classification Flask App\" is available on my github profile.\n### Click [here](https://github.com/sidharth178/Cat-Dog-Classification-Flask-App) to access.\n### If you find this notebook useful,don't forget to give a **Upvote**","metadata":{}},{"cell_type":"code","source":"","metadata":{"id":"79sjYRTpILQU"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"1fjWV_TsILTF"},"execution_count":null,"outputs":[]}]}