{"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":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport random \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom keras.preprocessing import image\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-20T09:55:29.415894Z","iopub.execute_input":"2022-02-20T09:55:29.416373Z","iopub.status.idle":"2022-02-20T09:55:35.833451Z","shell.execute_reply.started":"2022-02-20T09:55:29.41633Z","shell.execute_reply":"2022-02-20T09:55:35.832456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile('../input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    \n    zip_ref.extractall('./')\n    \nwith zipfile.ZipFile('../input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    \n    zip_ref.extractall('./')\n    ","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:35.836636Z","iopub.execute_input":"2022-02-20T09:55:35.836962Z","iopub.status.idle":"2022-02-20T09:55:52.824344Z","shell.execute_reply.started":"2022-02-20T09:55:35.836926Z","shell.execute_reply":"2022-02-20T09:55:52.823426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading input files to create training dataset","metadata":{}},{"cell_type":"code","source":"\nfilenames = os.listdir('./train')\n\ncategories = []\n\nfor filename in filenames:\n    \n    category = filename.split('.')[0]\n    \n    if(category == 'dog'):\n        \n        categories.append('dog')\n        \n    else:\n        \n        categories.append('cat')\n        \n# create a dataframe\n\ndf = pd.DataFrame({\n        \n        'filename' : filenames,\n    \n        'category' : categories\n    \n    })","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:52.825696Z","iopub.execute_input":"2022-02-20T09:55:52.82594Z","iopub.status.idle":"2022-02-20T09:55:52.865765Z","shell.execute_reply.started":"2022-02-20T09:55:52.825909Z","shell.execute_reply":"2022-02-20T09:55:52.864962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Showing some data files","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:52.867492Z","iopub.execute_input":"2022-02-20T09:55:52.867702Z","iopub.status.idle":"2022-02-20T09:55:52.887316Z","shell.execute_reply.started":"2022-02-20T09:55:52.867676Z","shell.execute_reply":"2022-02-20T09:55:52.8865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:52.888375Z","iopub.execute_input":"2022-02-20T09:55:52.888679Z","iopub.status.idle":"2022-02-20T09:55:52.904009Z","shell.execute_reply.started":"2022-02-20T09:55:52.888642Z","shell.execute_reply":"2022-02-20T09:55:52.903246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" Now taking random sample of 5000 images to df1 out of whole dataset**","metadata":{}},{"cell_type":"code","source":"df1=df.sample(n = 5000)\ndf1.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:52.905225Z","iopub.execute_input":"2022-02-20T09:55:52.90546Z","iopub.status.idle":"2022-02-20T09:55:52.922672Z","shell.execute_reply.started":"2022-02-20T09:55:52.905412Z","shell.execute_reply":"2022-02-20T09:55:52.921587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking which categories of data is present : mainly done to check the balancing of data","metadata":{}},{"cell_type":"code","source":"df1['category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:52.924257Z","iopub.execute_input":"2022-02-20T09:55:52.924573Z","iopub.status.idle":"2022-02-20T09:55:52.933131Z","shell.execute_reply.started":"2022-02-20T09:55:52.924532Z","shell.execute_reply":"2022-02-20T09:55:52.932234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['category'].value_counts().plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:52.935245Z","iopub.execute_input":"2022-02-20T09:55:52.935843Z","iopub.status.idle":"2022-02-20T09:55:53.1438Z","shell.execute_reply.started":"2022-02-20T09:55:52.935801Z","shell.execute_reply":"2022-02-20T09:55:53.142919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sample image","metadata":{}},{"cell_type":"code","source":"sample = random.choice(filenames)\n\nimage = image.load_img('./train/' + sample)\n\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:53.145217Z","iopub.execute_input":"2022-02-20T09:55:53.145465Z","iopub.status.idle":"2022-02-20T09:55:53.441891Z","shell.execute_reply.started":"2022-02-20T09:55:53.145434Z","shell.execute_reply":"2022-02-20T09:55:53.440942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using 20% of your data for testing and 80% for training","metadata":{}},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df1, test_size = 0.20, random_state = 42)\n\ntrain_df = train_df.reset_index(drop=True)\n\nvalidate_df = validate_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:53.444547Z","iopub.execute_input":"2022-02-20T09:55:53.444848Z","iopub.status.idle":"2022-02-20T09:55:53.452072Z","shell.execute_reply.started":"2022-02-20T09:55:53.444817Z","shell.execute_reply":"2022-02-20T09:55:53.451222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:53.453504Z","iopub.execute_input":"2022-02-20T09:55:53.453883Z","iopub.status.idle":"2022-02-20T09:55:53.466197Z","shell.execute_reply.started":"2022-02-20T09:55:53.45384Z","shell.execute_reply":"2022-02-20T09:55:53.465304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:53.467637Z","iopub.execute_input":"2022-02-20T09:55:53.468138Z","iopub.status.idle":"2022-02-20T09:55:53.481262Z","shell.execute_reply.started":"2022-02-20T09:55:53.468073Z","shell.execute_reply":"2022-02-20T09:55:53.480427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['category'].value_counts().plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:53.482404Z","iopub.execute_input":"2022-02-20T09:55:53.483202Z","iopub.status.idle":"2022-02-20T09:55:53.670824Z","shell.execute_reply.started":"2022-02-20T09:55:53.483167Z","shell.execute_reply":"2022-02-20T09:55:53.670063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df['category'].value_counts().plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:53.671806Z","iopub.execute_input":"2022-02-20T09:55:53.67227Z","iopub.status.idle":"2022-02-20T09:55:53.843479Z","shell.execute_reply.started":"2022-02-20T09:55:53.672238Z","shell.execute_reply":"2022-02-20T09:55:53.842226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\n\ntotal_validate = validate_df.shape[0]\n\nprint(\"Total number of example in training dataset : {0}\".format(total_train))\n\nprint(\"Total number of example in validation dataset : {0}\".format(total_validate))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:53.845324Z","iopub.execute_input":"2022-02-20T09:55:53.846011Z","iopub.status.idle":"2022-02-20T09:55:53.851614Z","shell.execute_reply.started":"2022-02-20T09:55:53.845972Z","shell.execute_reply":"2022-02-20T09:55:53.85064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\n\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten\n\nfrom tensorflow.keras import optimizers","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:53.85305Z","iopub.execute_input":"2022-02-20T09:55:53.853401Z","iopub.status.idle":"2022-02-20T09:55:54.209419Z","shell.execute_reply.started":"2022-02-20T09:55:53.853337Z","shell.execute_reply":"2022-02-20T09:55:54.208642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model:\n    \n    def __init__(self, IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS):\n        \n        self.IMG_WIDTH = IMG_WIDTH\n        \n        self.IMG_HEIGHT = IMG_HEIGHT\n               \n        self.IMG_CHANNELS = IMG_CHANNELS\n    \n    def create_model_one(self):\n        \n        model_one = Sequential()   # Adding Layers to create a convolutional mask/kernel that is wind with layers input which helps produce a tensor of outputs\n        \n        # Layer 1\n        model_one.add(Conv2D(32, (3,3), activation = 'relu', input_shape = (150, 150, 3)))\n        \n        model_one.add(MaxPooling2D((2,2)))\n        \n        # Layer 2\n        model_one.add(Conv2D(64, (3,3), activation = 'relu'))\n        \n        model_one.add(MaxPooling2D((2,2)))\n       # Layer 3\n        model_one.add(Conv2D(128, (3,3), activation = 'relu'))\n        \n        model_one.add(MaxPooling2D((2,2)))\n               \n        # Layer 4\n        model_one.add(Conv2D(128, (3,3), activation = 'relu'))\n        \n        model_one.add(MaxPooling2D((2,2)))\n           \n        model_one.add(Flatten())\n                      \n        model_one.add(Dense(512, activation = 'relu'))\n        \n        model_one.add(Dense(1, activation = 'sigmoid'))\n                      \n        return model_one\n    \n    def create_model_two(self):\n        \n        model_two = Sequential()\n        \n        # Adding Layers to create a convolutional mask/kernel that is wind with layers input which helps produce a tensor of outputs\n        \n        # Layer 1\n        model_two.add(Conv2D(32, (3,3), activation = 'relu', input_shape = (150, 150, 3)))\n        \n        model_two.add(MaxPooling2D((2,2)))\n        \n        # Layer 2\n        model_two.add(Conv2D(64, (3,3), activation = 'relu'))\n        \n        model_two.add(MaxPooling2D((2,2)))\n        \n        # Layer 3\n        model_two.add(Conv2D(128, (3,3), activation = 'relu'))\n        \n        model_two.add(MaxPooling2D((2,2)))\n        \n            # Layer 4\n        model_two.add(Conv2D(128, (3,3), activation = 'relu'))\n        \n        model_two.add(MaxPooling2D((2,2)))\n           \n        model_two.add(Flatten())\n        \n        # DropoutLayer for preventing model from overfitting\n        model_two.add(Dropout(0.5))\n                      \n        model_two.add(Dense(512, activation = 'relu'))\n        \n        model_two.add(Dense(1, activation = 'sigmoid'))\n                      \n        return model_two","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.210906Z","iopub.execute_input":"2022-02-20T09:55:54.211233Z","iopub.status.idle":"2022-02-20T09:55:54.23041Z","shell.execute_reply.started":"2022-02-20T09:55:54.21119Z","shell.execute_reply":"2022-02-20T09:55:54.229296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_WIDTH = 150\n\nIMG_HEIGHT = 150\n\nIMG_CHANNELS = 3\n\nmodel = Model(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.231742Z","iopub.execute_input":"2022-02-20T09:55:54.231974Z","iopub.status.idle":"2022-02-20T09:55:54.250391Z","shell.execute_reply.started":"2022-02-20T09:55:54.231947Z","shell.execute_reply":"2022-02-20T09:55:54.249616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model 1**","metadata":{}},{"cell_type":"code","source":"model_1 = model.create_model_one()\n\nmodel_1.summary()\n","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.251825Z","iopub.execute_input":"2022-02-20T09:55:54.252035Z","iopub.status.idle":"2022-02-20T09:55:54.697252Z","shell.execute_reply.started":"2022-02-20T09:55:54.252009Z","shell.execute_reply":"2022-02-20T09:55:54.695761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model1 - Since it is a two class classification problem, therefore using binary_crossentropy as loss and using RMSprop as optimizer because we ended the network with a single sigmoidal unit.**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import optimizers\n\nmodel_1.compile(loss = 'binary_crossentropy', optimizer = optimizers.RMSprop(lr = 1e-4), metrics =['acc'])","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.698642Z","iopub.execute_input":"2022-02-20T09:55:54.698882Z","iopub.status.idle":"2022-02-20T09:55:54.716172Z","shell.execute_reply.started":"2022-02-20T09:55:54.698852Z","shell.execute_reply":"2022-02-20T09:55:54.715299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Currently data is in in jpeg format, so in order to feed those images to Neural Network we need to do the following steps\n\n# Firstly  Read the image file\n\n# then Decode those JPEG content to RGB grids of pixels\n\n# then do Convert these into floating-point tensors\n\n# and finally Rescale the pixel values from range [0-255] to [0-1] as NN prefers to deal with smaller input values.","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n# generator is an object that acts as an iterator. Generators are built using the yield operator.","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.717856Z","iopub.execute_input":"2022-02-20T09:55:54.718245Z","iopub.status.idle":"2022-02-20T09:55:54.722735Z","shell.execute_reply.started":"2022-02-20T09:55:54.718201Z","shell.execute_reply":"2022-02-20T09:55:54.722091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgdatagen = ImageDataGenerator(rescale = 1./255)\n\nvalid_imgdatagen = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.723704Z","iopub.execute_input":"2022-02-20T09:55:54.723917Z","iopub.status.idle":"2022-02-20T09:55:54.733394Z","shell.execute_reply.started":"2022-02-20T09:55:54.723884Z","shell.execute_reply":"2022-02-20T09:55:54.732517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator_m1 = train_imgdatagen.flow_from_dataframe(\n                    train_df,\n                    \n                    \"./train\", \n                    \n                    x_col='filename',\n    \n                    y_col='category',\n                    \n                    target_size = (150, 150), # resize image to 150x150\n                    \n                    batch_size = 20,\n    \n                    class_mode = 'binary'\n                  )\n\n\nvalidation_generator_m1 = train_imgdatagen.flow_from_dataframe(\n                    validate_df,\n    \n                    \"./train\", \n                    \n                    x_col='filename',\n    \n                    y_col='category',\n                    \n                    target_size = (150, 150), # resize image to 150x150\n                    \n                    batch_size = 20,\n    \n                    class_mode = 'binary'\n                  )","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.734669Z","iopub.execute_input":"2022-02-20T09:55:54.73518Z","iopub.status.idle":"2022-02-20T09:55:54.811599Z","shell.execute_reply.started":"2022-02-20T09:55:54.735123Z","shell.execute_reply":"2022-02-20T09:55:54.81105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# That's correct training data contains 4000 images with 2 classes and also validation data contains 1000 images with 2 classes.\n\n# Now lets look at the output of these generators","metadata":{}},{"cell_type":"code","source":"for data_batch, labels_batch in train_generator_m1:\n    \n    print('Data batch shape: {0}'. format(data_batch.shape))\n    \n    print('Labels batch shape: {0}'. format(labels_batch.shape))\n    \n    break","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.812609Z","iopub.execute_input":"2022-02-20T09:55:54.812937Z","iopub.status.idle":"2022-02-20T09:55:54.870226Z","shell.execute_reply.started":"2022-02-20T09:55:54.812908Z","shell.execute_reply":"2022-02-20T09:55:54.869303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" There are 20 samples in each batch.","metadata":{}},{"cell_type":"markdown","source":"# MODEL-2\n\n# how to  get more accuracy?\n\n# Let's try to build one more predictive model using a pretrained model.\n\n # why pretrained model: Because probability of learned features across different problems is a key advantage of deep learning compared to many olders, shallow-learning approaches and it makes deep learning very effective for small-data problems.\n\n","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.871627Z","iopub.execute_input":"2022-02-20T09:55:54.87211Z","iopub.status.idle":"2022-02-20T09:55:54.87787Z","shell.execute_reply.started":"2022-02-20T09:55:54.87204Z","shell.execute_reply":"2022-02-20T09:55:54.876901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base = VGG16(weights = 'imagenet', include_top = False, input_shape = (150,150,3))\n\nconv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:54.879018Z","iopub.execute_input":"2022-02-20T09:55:54.879818Z","iopub.status.idle":"2022-02-20T09:55:55.636055Z","shell.execute_reply.started":"2022-02-20T09:55:54.879772Z","shell.execute_reply":"2022-02-20T09:55:55.634826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The final feature map has a shape (4, 4, 512). That's the feature on top of which we will stick densely connected classifier. Adding a densely connected classifier on top of convolution base.","metadata":{}},{"cell_type":"code","source":"model_3 = Sequential()\n\nmodel_3.add(conv_base)\n\nmodel_3.add(Flatten())\n\nmodel_3.add(Dense(256, activation = 'relu'))\n\nmodel_3.add(Dense(1, activation = 'sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.63885Z","iopub.execute_input":"2022-02-20T09:55:55.639489Z","iopub.status.idle":"2022-02-20T09:55:55.730355Z","shell.execute_reply.started":"2022-02-20T09:55:55.639428Z","shell.execute_reply":"2022-02-20T09:55:55.729531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_3.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.734793Z","iopub.execute_input":"2022-02-20T09:55:55.73505Z","iopub.status.idle":"2022-02-20T09:55:55.74413Z","shell.execute_reply.started":"2022-02-20T09:55:55.735016Z","shell.execute_reply":"2022-02-20T09:55:55.74233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Freezing conv_base","metadata":{}},{"cell_type":"code","source":"print(\"The number of trainable weights before freezing the conv base: \", len(model_3.trainable_weights))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.747366Z","iopub.execute_input":"2022-02-20T09:55:55.747717Z","iopub.status.idle":"2022-02-20T09:55:55.752901Z","shell.execute_reply.started":"2022-02-20T09:55:55.747683Z","shell.execute_reply":"2022-02-20T09:55:55.752303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.trainable = False\nprint(\"The number of trainable weights after freezing the conv base: \", len(model_3.trainable_weights))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.754025Z","iopub.execute_input":"2022-02-20T09:55:55.75456Z","iopub.status.idle":"2022-02-20T09:55:55.764956Z","shell.execute_reply.started":"2022-02-20T09:55:55.754519Z","shell.execute_reply":"2022-02-20T09:55:55.764343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fine Tuning : Freezing layers of conv_base upto specific one . Basically it consists of unfreezing a few of the top layers of frozen model base used for feature extraction and jointly training both the newly added part of the model(in this case, the FCC) and these top layers. This is called fine-tuning because it slightly adjusts the more abstract representations of the model being reused, in order to make them more relevant for the problem.\n\n\n","metadata":{}},{"cell_type":"code","source":"conv_base.trainable = True\n\nset_trainable = False\n\nfor layer in conv_base.layers:\n    \n    if layer.name == 'block5_conv1':\n        \n        set_trainable = True\n        \n    if set_trainable:\n        \n        layer.trainable = True\n        \n    else:\n        \n        layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.766073Z","iopub.execute_input":"2022-02-20T09:55:55.766326Z","iopub.status.idle":"2022-02-20T09:55:55.777878Z","shell.execute_reply.started":"2022-02-20T09:55:55.766296Z","shell.execute_reply":"2022-02-20T09:55:55.777069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Callbacks\n\n","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.779045Z","iopub.execute_input":"2022-02-20T09:55:55.779322Z","iopub.status.idle":"2022-02-20T09:55:55.787524Z","shell.execute_reply.started":"2022-02-20T09:55:55.779289Z","shell.execute_reply":"2022-02-20T09:55:55.78669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# To prevent over fitting we will stop the learning after 10 epochs and val_loss value not decreased","metadata":{}},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.788887Z","iopub.execute_input":"2022-02-20T09:55:55.78934Z","iopub.status.idle":"2022-02-20T09:55:55.801921Z","shell.execute_reply.started":"2022-02-20T09:55:55.789306Z","shell.execute_reply":"2022-02-20T09:55:55.801063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.803528Z","iopub.execute_input":"2022-02-20T09:55:55.804055Z","iopub.status.idle":"2022-02-20T09:55:55.812738Z","shell.execute_reply.started":"2022-02-20T09:55:55.804011Z","shell.execute_reply":"2022-02-20T09:55:55.812032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.814118Z","iopub.execute_input":"2022-02-20T09:55:55.814586Z","iopub.status.idle":"2022-02-20T09:55:55.82252Z","shell.execute_reply.started":"2022-02-20T09:55:55.814543Z","shell.execute_reply":"2022-02-20T09:55:55.821693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_3.compile(loss = 'binary_crossentropy', optimizer = optimizers.RMSprop(lr = 1e-5), metrics =['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.823848Z","iopub.execute_input":"2022-02-20T09:55:55.82434Z","iopub.status.idle":"2022-02-20T09:55:55.839618Z","shell.execute_reply.started":"2022-02-20T09:55:55.824299Z","shell.execute_reply":"2022-02-20T09:55:55.8386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# FITTING THE MODEL ONTO THE TRAINING DATA","metadata":{}},{"cell_type":"code","source":"from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.840822Z","iopub.execute_input":"2022-02-20T09:55:55.841353Z","iopub.status.idle":"2022-02-20T09:55:55.847923Z","shell.execute_reply.started":"2022-02-20T09:55:55.841316Z","shell.execute_reply":"2022-02-20T09:55:55.847032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_1 = model_1.fit(\n            train_generator_m1,\n    \n            epochs = 30,\n    \n            steps_per_epoch = 100,\n            \n            validation_data = validation_generator_m1,\n    \n            validation_steps = 50\n            )","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:55:55.849037Z","iopub.execute_input":"2022-02-20T09:55:55.849543Z","iopub.status.idle":"2022-02-20T10:33:01.049121Z","shell.execute_reply.started":"2022-02-20T09:55:55.849512Z","shell.execute_reply":"2022-02-20T10:33:01.048447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(history_1.history['acc']))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:33:01.05046Z","iopub.execute_input":"2022-02-20T10:33:01.050794Z","iopub.status.idle":"2022-02-20T10:33:01.054895Z","shell.execute_reply.started":"2022-02-20T10:33:01.050765Z","shell.execute_reply":"2022-02-20T10:33:01.05433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(history_1.history['val_acc']))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:33:01.056051Z","iopub.execute_input":"2022-02-20T10:33:01.056506Z","iopub.status.idle":"2022-02-20T10:33:01.073863Z","shell.execute_reply.started":"2022-02-20T10:33:01.056474Z","shell.execute_reply":"2022-02-20T10:33:01.072973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing Loss and accuracy during training","metadata":{}},{"cell_type":"code","source":"plt.plot(history_1.history['acc'], color = 'black')\n\nplt.plot(history_1.history['val_acc'], color = 'blue')\n\nplt.title('Training and validation accuracy of model 1')\n\nplt.xlabel('Epochs')\n\nplt.ylabel('Accuracy')\n\nplt.show()\n\nplt.plot(history_1.history['loss'], color = 'black')\n\nplt.plot(history_1.history['val_loss'], color = 'blue')\n\nplt.title('Training and validation loss of model 1')\n\nplt.xlabel('Epochs')\n\nplt.ylabel('Accuracy')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:33:01.075067Z","iopub.execute_input":"2022-02-20T10:33:01.075509Z","iopub.status.idle":"2022-02-20T10:33:01.468971Z","shell.execute_reply.started":"2022-02-20T10:33:01.075472Z","shell.execute_reply":"2022-02-20T10:33:01.46801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MODEL 2","metadata":{}},{"cell_type":"code","source":"train_imgdatagen_m2 = ImageDataGenerator(\n                     rescale = 1./255,\n    \n                     rotation_range = 15,\n    \n                     width_shift_range = 0.1,\n    \n                     height_shift_range = 0.1,\n    \n                     shear_range = 0.1,\n    \n                     zoom_range = 0.2,\n    \n                     horizontal_flip = True\n                    )\n\nvalid_imgdatagen_m2 = ImageDataGenerator(\n                             rescale = 1./255\n                         )\n\ntrain_generator_m2 = train_imgdatagen_m2.flow_from_dataframe(\n                    train_df,\n                    \n                    \"./train\", \n                    \n                    x_col='filename',\n    \n                    y_col='category',\n                    \n                    target_size = (150, 150), # resize image to 150x150\n                    \n                    batch_size = 32,\n    \n                    class_mode = 'binary'\n                  )\n\n\nvalidation_generator_m2 = valid_imgdatagen_m2.flow_from_dataframe(\n                    validate_df,\n    \n                    \"./train\", \n                    \n                    x_col='filename',\n    \n                    y_col='category',\n                    \n                    target_size = (150, 150), # resize image to 150x150\n                    \n                    batch_size = 32,\n    \n                    class_mode = 'binary'\n                  )","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:33:01.470419Z","iopub.execute_input":"2022-02-20T10:33:01.470716Z","iopub.status.idle":"2022-02-20T10:33:01.546702Z","shell.execute_reply.started":"2022-02-20T10:33:01.470676Z","shell.execute_reply":"2022-02-20T10:33:01.545644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_3 = model_3.fit(\n            train_generator_m2,\n    \n            epochs = 30,\n    \n            steps_per_epoch = 100,\n            \n            validation_data = validation_generator_m2,\n    \n            validation_steps = 50,\n            \n            callbacks = callbacks\n            )","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:51:48.361913Z","iopub.execute_input":"2022-02-20T12:51:48.362288Z","iopub.status.idle":"2022-02-20T16:38:32.999169Z","shell.execute_reply.started":"2022-02-20T12:51:48.362254Z","shell.execute_reply":"2022-02-20T16:38:32.996635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(history_3.history['acc']))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T17:13:36.411899Z","iopub.execute_input":"2022-02-20T17:13:36.41227Z","iopub.status.idle":"2022-02-20T17:13:36.497899Z","shell.execute_reply.started":"2022-02-20T17:13:36.412153Z","shell.execute_reply":"2022-02-20T17:13:36.496654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(history_3.history['val_acc']))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:41:15.762641Z","iopub.execute_input":"2022-02-20T12:41:15.763235Z","iopub.status.idle":"2022-02-20T12:41:15.772347Z","shell.execute_reply.started":"2022-02-20T12:41:15.763184Z","shell.execute_reply":"2022-02-20T12:41:15.77151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_3.history['acc'], color = 'black')\n\nplt.plot(history_3.history['val_acc'], color = 'blue')\n\nplt.title('Training and validation accuracy of model 3')\n\nplt.xlabel('Epochs')\n\nplt.ylabel('Accuracy')\n\nplt.show()\n\nplt.plot(history_3.history['loss'], color = 'black')\n\nplt.plot(history_3.history['val_loss'], color = 'blue')\n\nplt.title('Training and validation loss of model 3')\n\nplt.xlabel('Epochs')\n\nplt.ylabel('Accuracy')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T17:18:10.830478Z","iopub.execute_input":"2022-02-20T17:18:10.830904Z","iopub.status.idle":"2022-02-20T17:18:10.84676Z","shell.execute_reply.started":"2022-02-20T17:18:10.830866Z","shell.execute_reply":"2022-02-20T17:18:10.846102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PREPARE TESTING DATASET AND VISULAIZE IT","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2022-02-20T11:43:57.085494Z","iopub.execute_input":"2022-02-20T11:43:57.085721Z","iopub.status.idle":"2022-02-20T11:43:57.089424Z","shell.execute_reply.started":"2022-02-20T11:43:57.085691Z","shell.execute_reply":"2022-02-20T11:43:57.088863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model('model_3.h5')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T11:43:57.090528Z","iopub.execute_input":"2022-02-20T11:43:57.09123Z","iopub.status.idle":"2022-02-20T11:43:57.466049Z","shell.execute_reply.started":"2022-02-20T11:43:57.091194Z","shell.execute_reply":"2022-02-20T11:43:57.46516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir('./test1')\n\ntest_df = pd.DataFrame(\n         {\n             'id' : test_filenames\n         } \n         )","metadata":{"execution":{"iopub.status.busy":"2022-02-20T11:43:57.466967Z","iopub.execute_input":"2022-02-20T11:43:57.467421Z","iopub.status.idle":"2022-02-20T11:43:57.482017Z","shell.execute_reply.started":"2022-02-20T11:43:57.467385Z","shell.execute_reply":"2022-02-20T11:43:57.481237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T11:43:57.483271Z","iopub.execute_input":"2022-02-20T11:43:57.483508Z","iopub.status.idle":"2022-02-20T11:43:57.492454Z","shell.execute_reply.started":"2022-02-20T11:43:57.483479Z","shell.execute_reply":"2022-02-20T11:43:57.491484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale = 1./255)\n\ntest_generator = test_gen.flow_from_dataframe(    \n                    test_df,\n    \n                    \"./test1\", \n                    \n                    x_col='id',\n    \n                    y_col=None,\n                    \n                    target_size = (150, 150), # resize image to 150x150\n                    \n                    batch_size = 20,\n    \n                    class_mode = None,\n    \n                    shuffle=False,\n    \n                    validate_filenames=False\n                  )","metadata":{"execution":{"iopub.status.busy":"2022-02-20T11:43:57.493551Z","iopub.execute_input":"2022-02-20T11:43:57.493779Z","iopub.status.idle":"2022-02-20T11:43:57.534668Z","shell.execute_reply.started":"2022-02-20T11:43:57.49375Z","shell.execute_reply":"2022-02-20T11:43:57.5338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  PREDICTING ON TESTING DATA","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T11:43:57.535804Z","iopub.execute_input":"2022-02-20T11:43:57.536034Z","iopub.status.idle":"2022-02-20T12:06:19.318298Z","shell.execute_reply.started":"2022-02-20T11:43:57.536005Z","shell.execute_reply":"2022-02-20T12:06:19.317276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = [1 if p > 0.5 else 0 for p in predictions]\n\ntest_df['category'] = pred","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:06:19.320038Z","iopub.execute_input":"2022-02-20T12:06:19.320362Z","iopub.status.idle":"2022-02-20T12:06:19.355564Z","shell.execute_reply.started":"2022-02-20T12:06:19.320319Z","shell.execute_reply":"2022-02-20T12:06:19.354564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:06:19.356815Z","iopub.execute_input":"2022-02-20T12:06:19.357059Z","iopub.status.idle":"2022-02-20T12:06:19.498991Z","shell.execute_reply.started":"2022-02-20T12:06:19.357029Z","shell.execute_reply":"2022-02-20T12:06:19.498158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator_m2.class_indices.items())\n\ntest_df['category'] = test_df['category'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:20:33.20618Z","iopub.execute_input":"2022-02-20T12:20:33.206622Z","iopub.status.idle":"2022-02-20T12:20:33.216364Z","shell.execute_reply.started":"2022-02-20T12:20:33.206581Z","shell.execute_reply":"2022-02-20T12:20:33.215354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:06:19.510677Z","iopub.execute_input":"2022-02-20T12:06:19.511294Z","iopub.status.idle":"2022-02-20T12:06:19.529097Z","shell.execute_reply.started":"2022-02-20T12:06:19.511249Z","shell.execute_reply":"2022-02-20T12:06:19.52797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:06:19.53083Z","iopub.execute_input":"2022-02-20T12:06:19.53148Z","iopub.status.idle":"2022-02-20T12:06:19.658201Z","shell.execute_reply.started":"2022-02-20T12:06:19.531435Z","shell.execute_reply":"2022-02-20T12:06:19.657379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_df.copy()\n\nsubmission_df['id'] = submission_df['id'].str.split('.').str[0]\n\nsubmission_df['label'] = submission_df['category']\n\nsubmission_df.drop(['id', 'category'], axis=1, inplace=True)\n\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T12:06:19.659895Z","iopub.execute_input":"2022-02-20T12:06:19.660462Z","iopub.status.idle":"2022-02-20T12:06:19.714134Z","shell.execute_reply.started":"2022-02-20T12:06:19.660416Z","shell.execute_reply":"2022-02-20T12:06:19.713152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**What is a Pre-trained Model?\nIn this we simply put a pre-trained model is a model created by some-one else to solve a similar problem. Instead of building a model from scratch to solve a similar problem, you use the model trained on other problem as a starting point.\nBy using pre-trained models which have been previously trained on large datasets, we can directly use the weights and architecture obtained and apply the learning on our problem statement. This is known as transfer learning. We transfer the learning of the pre-trained model to our specific problem statement.\nA pre-trained model may not be 100% accurate in your application, but it saves huge efforts required to re-invent the wheel. Let me show this to you with a recent example.**\n","metadata":{}},{"cell_type":"markdown","source":"# How can I use Pre-trained Models?\nWhat is our objective when we train a neural network? We wish to identify the correct weights for the network by multiple forward and backward iterations. By using pre-trained models which have been previously trained on large datasets, we can directly use the weights and architecture obtained and apply the learning on our problem statement. This is known as transfer learning. We “transfer the learning” of the pre-trained model to our specific problem statement.\nWe should be very careful while choosing what pre-trained model you should use in your case. If the problem statement we have at hand is very different from the one on which the pre-trained model was trained – the prediction we would get would beAvery inaccurate. For example, a model previously trained for speech recognition would work horribly if we try to use it to identify objects using it.\nThese pre-trained networks demonstrate a strong ability to generalize to images outside the dataset via transfer learning. We make modifications in the pre-existing model by fine-tuning the model. Since we assume that the pre-trained network has been trained quite well, we would not want to modify the weights too soon and too much. While modifying we generally use a learning rate smaller than the one used for initially training the model.\n******","metadata":{}},{"cell_type":"markdown","source":"# How can I use Pre-trained Models?\n\nOur objective when we train a neural network is to identify the correct weights for the network by multiple forward and backward iterations. By using pre-trained models which have been previously trained on large datasets, we can directly use the weights and architecture obtained and apply the learning on our problem statement. This is known as transfer learning. We “transfer the learning” of the pre-trained model to our specific problem statement.\nWe should be very careful while choosing what pre-trained model you should use in your case. If the problem statement we have at hand is very different from the one on which the pre-trained model was trained – the prediction we would get would be very inaccurate. For example, a model previously trained for speech recognition would work horribly if we try to use it to identify objects using it.\nThese pre-trained networks demonstrate a strong ability to generalize to images outside the dataset via transfer learning. We make modifications in the pre-existing model by fine-tuning the model. Since we assume that the pre-trained network has been trained quite well, we would not want to modify the weights too soon and too much. While modifying we generally use a learning rate smaller than the one used for initially training the model.","metadata":{}},{"cell_type":"markdown","source":"# Ways to Fine tune the model\nFeature extraction – We can use a pre-trained model as a feature extraction mechanism. What we can do is that we can remove the output layer and then use the entire network as a fixed feature extractor for the new data set.\nUse the Architecture of the pre-trained model – What we can do is that we use architecture of the model while we initialize all the weights randomly and train the model according to our dataset again.\nTrain some layers while freeze others – Another way to use a pre-trained model is to train is partially. What we can do is we keep the weights of initial layers of the model frozen while we retrain only the higher layers. We can try and test as to how many layers to be frozen and how many to be trained.","metadata":{}},{"cell_type":"markdown","source":"*****","metadata":{}}]}