{"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":"# Introduction","metadata":{}},{"cell_type":"markdown","source":"In this notebook i have used self created** CNN** Model and pretrained model **VGG16 **for classifying the Dog vs Cat datasets.\nFirst, i used only CNN model with augmentation but this time i didnot get good accuracy and thats why i use pretrained model VGG16 for classifying the dog vs cat dataset.","metadata":{}},{"cell_type":"markdown","source":"# Import required libraries:","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:35:42.976046Z","iopub.execute_input":"2022-07-29T18:35:42.977059Z","iopub.status.idle":"2022-07-29T18:35:48.642683Z","shell.execute_reply.started":"2022-07-29T18:35:42.976938Z","shell.execute_reply":"2022-07-29T18:35:48.641642Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Collecting and Precossing","metadata":{}},{"cell_type":"code","source":"print(os.listdir(\"../input/dogs-vs-cats\"))","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:35:52.292535Z","iopub.execute_input":"2022-07-29T18:35:52.293570Z","iopub.status.idle":"2022-07-29T18:35:52.298791Z","shell.execute_reply.started":"2022-07-29T18:35:52.293529Z","shell.execute_reply":"2022-07-29T18:35:52.297851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip  ../input/dogs-vs-cats/train.zip\n! unzip  ../input/dogs-vs-cats/test1.zip","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-29T18:35:55.773178Z","iopub.execute_input":"2022-07-29T18:35:55.773530Z","iopub.status.idle":"2022-07-29T18:36:12.668898Z","shell.execute_reply.started":"2022-07-29T18:35:55.773500Z","shell.execute_reply":"2022-07-29T18:36:12.667795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imgae into pandas Dataframe","metadata":{}},{"cell_type":"code","source":"filenames= os.listdir(\"./train\")\n\ncategories=[]\n\nfor filename in filenames:\n    \n    category=filename.split(\".\")[0]\n    if category==\"dog\":\n        categories.append(1)\n    else:   \n       categories.append(0)\n        \n        \ndf=pd.DataFrame({'filename':filenames,\n                  'category':categories\n                })    ","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:41:32.182555Z","iopub.execute_input":"2022-07-29T18:41:32.182964Z","iopub.status.idle":"2022-07-29T18:41:32.228959Z","shell.execute_reply.started":"2022-07-29T18:41:32.182931Z","shell.execute_reply":"2022-07-29T18:41:32.228053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-29T18:41:35.751192Z","iopub.execute_input":"2022-07-29T18:41:35.751628Z","iopub.status.idle":"2022-07-29T18:41:35.781643Z","shell.execute_reply.started":"2022-07-29T18:41:35.751588Z","shell.execute_reply":"2022-07-29T18:41:35.780762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:41:42.970217Z","iopub.execute_input":"2022-07-29T18:41:42.970649Z","iopub.status.idle":"2022-07-29T18:41:42.978360Z","shell.execute_reply.started":"2022-07-29T18:41:42.970610Z","shell.execute_reply":"2022-07-29T18:41:42.977310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:41:46.240730Z","iopub.execute_input":"2022-07-29T18:41:46.241402Z","iopub.status.idle":"2022-07-29T18:41:46.255092Z","shell.execute_reply.started":"2022-07-29T18:41:46.241362Z","shell.execute_reply":"2022-07-29T18:41:46.254067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:41:49.734161Z","iopub.execute_input":"2022-07-29T18:41:49.734983Z","iopub.status.idle":"2022-07-29T18:41:50.000277Z","shell.execute_reply.started":"2022-07-29T18:41:49.734932Z","shell.execute_reply":"2022-07-29T18:41:49.998364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_Img=random.choice(filenames)\nimage=load_img(\"./train/\"+random_Img)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:41:54.116539Z","iopub.execute_input":"2022-07-29T18:41:54.117013Z","iopub.status.idle":"2022-07-29T18:41:54.451089Z","shell.execute_reply.started":"2022-07-29T18:41:54.116971Z","shell.execute_reply":"2022-07-29T18:41:54.444347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using Convolution Neural Network","metadata":{}},{"cell_type":"markdown","source":"This self made convulotion neural network . First i trained this model with train data set and the validate .Observe the accuracy","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\n\nmodel1 = Sequential()\n\nmodel1.add(Conv2D(128, (3, 3), activation='relu', input_shape=(128,128,3)))\nmodel1.add(BatchNormalization())\nmodel1.add(MaxPooling2D(pool_size=(2, 2)))\nmodel1.add(Dropout(0.25))\n\nmodel1.add(Conv2D(64, (3, 3), activation='relu'))\nmodel1.add(BatchNormalization())\nmodel1.add(MaxPooling2D(pool_size=(2, 2)))\nmodel1.add(Dropout(0.25))\n\nmodel1.add(Conv2D(64, (3, 3), activation='relu'))\nmodel1.add(BatchNormalization())\nmodel1.add(MaxPooling2D(pool_size=(2, 2)))\nmodel1.add(Dropout(0.25))\n\nmodel1.add(Flatten())\nmodel1.add(Dense(512, activation='relu'))\nmodel1.add(BatchNormalization())\nmodel1.add(Dropout(0.2))\nmodel1.add(Dense(2, activation='softmax')) # 2 because we have cat and dog classes\n\nmodel1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:41:59.916159Z","iopub.execute_input":"2022-07-29T18:41:59.916743Z","iopub.status.idle":"2022-07-29T18:42:02.748277Z","shell.execute_reply.started":"2022-07-29T18:41:59.916704Z","shell.execute_reply":"2022-07-29T18:42:02.747330Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing data for model1","metadata":{}},{"cell_type":"code","source":"df['category']=df[\"category\"].replace({0:'cat',1:'dog'})","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:42:17.465737Z","iopub.execute_input":"2022-07-29T18:42:17.466400Z","iopub.status.idle":"2022-07-29T18:42:17.474716Z","shell.execute_reply.started":"2022-07-29T18:42:17.466361Z","shell.execute_reply":"2022-07-29T18:42:17.473789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split data in train and validation","metadata":{}},{"cell_type":"code","source":"train_df,validate_df=train_test_split(df,test_size=.20,random_state=4)\ntrain_df=train_df.reset_index(drop=True)\nvalidate_df=validate_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:42:21.392946Z","iopub.execute_input":"2022-07-29T18:42:21.393301Z","iopub.status.idle":"2022-07-29T18:42:21.406320Z","shell.execute_reply.started":"2022-07-29T18:42:21.393272Z","shell.execute_reply":"2022-07-29T18:42:21.405210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train=train_df.shape[0]\ntotal_validate=validate_df.shape[0]\nbatch_size=16","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:42:26.014136Z","iopub.execute_input":"2022-07-29T18:42:26.014619Z","iopub.status.idle":"2022-07-29T18:42:26.021688Z","shell.execute_reply.started":"2022-07-29T18:42:26.014564Z","shell.execute_reply":"2022-07-29T18:42:26.019532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_validate","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:42:29.791649Z","iopub.execute_input":"2022-07-29T18:42:29.792609Z","iopub.status.idle":"2022-07-29T18:42:29.799331Z","shell.execute_reply.started":"2022-07-29T18:42:29.792559Z","shell.execute_reply":"2022-07-29T18:42:29.798399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Augmentation and prepare images for our model1","metadata":{}},{"cell_type":"markdown","source":"1. What do you mean by data augmentation?\n* Image result for augmentation in dl\nData augmentation in data analysis are techniques used to increase the amount of data by adding slightly modified copies of already existing data or newly created synthetic data from existing data. It acts as a regularizer and helps reduce overfitting when training a machine learning model.\n","metadata":{}},{"cell_type":"markdown","source":"2. How basic dataset augmentation works\n* Flipping (both vertically and horizontally)\n* Rotating.\n* Zooming and scaling.\n* Cropping.\n* Translating (moving along the x or y axis)\n* Adding Gaussian noise (distortion of high frequency features)\n","metadata":{}},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(\n    rotation_range=30,\n    rescale=1./255,\n    shear_range=0.4,\n    zoom_range=0.4,\n    horizontal_flip=True,\n    width_shift_range=0.4,\n    height_shift_range=0.4\n)\n\ntrain_generator=train_datagen.flow_from_dataframe(\n    \n    train_df,\n    \"./train\",\n    x_col='filename',\n    y_col='category',\n    target_size=(128,128),\n    class_mode='categorical',\n    batch_size=batch_size\n    \n)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:42:34.573301Z","iopub.execute_input":"2022-07-29T18:42:34.574243Z","iopub.status.idle":"2022-07-29T18:42:34.770920Z","shell.execute_reply.started":"2022-07-29T18:42:34.574191Z","shell.execute_reply":"2022-07-29T18:42:34.769936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ****No need not apply augmentation in validation datasets","metadata":{}},{"cell_type":"code","source":"validate_datagen=ImageDataGenerator(\n    rescale=1./255\n)\n\nvalidate_generator=validate_datagen.flow_from_dataframe(\n    validate_df,\n    \"./train\",\n    x_col='filename',\n    y_col='category',\n    target_size=(128,128),\n    class_mode='categorical',\n    batch_size=batch_size\n    \n)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:42:39.243243Z","iopub.execute_input":"2022-07-29T18:42:39.243584Z","iopub.status.idle":"2022-07-29T18:42:39.303238Z","shell.execute_reply.started":"2022-07-29T18:42:39.243555Z","shell.execute_reply":"2022-07-29T18:42:39.302185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Callbacks:","metadata":{}},{"cell_type":"markdown","source":"* What are callbacks in ML?\n* A 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","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:42:44.684033Z","iopub.execute_input":"2022-07-29T18:42:44.684667Z","iopub.status.idle":"2022-07-29T18:42:44.690151Z","shell.execute_reply.started":"2022-07-29T18:42:44.684628Z","shell.execute_reply":"2022-07-29T18:42:44.689128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"\nearlystop = EarlyStopping(patience=10)\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\ncallbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:42:49.297647Z","iopub.execute_input":"2022-07-29T18:42:49.298022Z","iopub.status.idle":"2022-07-29T18:42:49.303534Z","shell.execute_reply.started":"2022-07-29T18:42:49.297988Z","shell.execute_reply":"2022-07-29T18:42:49.302139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train and lets see the accuracy rate our model1","metadata":{}},{"cell_type":"code","source":"history1=model1.fit_generator(\n    train_generator,\n    steps_per_epoch=total_train//batch_size, \n    epochs=10, \n    verbose=1,\n    callbacks=callbacks, \n    validation_data=validate_generator,\n    validation_steps=total_validate//batch_size,\n    \n\n    \n)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T18:42:56.906501Z","iopub.execute_input":"2022-07-29T18:42:56.906851Z","iopub.status.idle":"2022-07-29T19:07:03.945649Z","shell.execute_reply.started":"2022-07-29T18:42:56.906822Z","shell.execute_reply":"2022-07-29T19:07:03.944716Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It is observed that the train and validation accuracy not satisfying.\n\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's try pretrained model:","metadata":{}},{"cell_type":"markdown","source":"# Importing libraries for pretrained model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense,Dropout\nfrom keras.backend import pool2d\nfrom keras.layers.pooling import GlobalAveragePooling2D\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:26:59.544314Z","iopub.execute_input":"2022-07-29T17:26:59.544897Z","iopub.status.idle":"2022-07-29T17:26:59.555177Z","shell.execute_reply.started":"2022-07-29T17:26:59.544826Z","shell.execute_reply":"2022-07-29T17:26:59.553996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pretrained model vGG16","metadata":{}},{"cell_type":"markdown","source":"* What is a pre-trained model?\n* \nA pre-trained model is a saved network that was previously trained on a large dataset, typically on a large-scale image-classification task. You either use the pretrained model as is or use transfer learning to customize this model to a given task.","metadata":{}},{"cell_type":"code","source":"base_model = VGG16(input_shape = (128, 128, 3), # Shape of our images\ninclude_top = False, # Leave out the last fully connected layer\nweights = 'imagenet')","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-29T17:27:04.058225Z","iopub.execute_input":"2022-07-29T17:27:04.058583Z","iopub.status.idle":"2022-07-29T17:27:08.073262Z","shell.execute_reply.started":"2022-07-29T17:27:04.058554Z","shell.execute_reply":"2022-07-29T17:27:08.072211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We dont need to trained above layers because this layers are pretrained ","metadata":{}},{"cell_type":"code","source":"base_model.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:28:36.199737Z","iopub.execute_input":"2022-07-29T17:28:36.200391Z","iopub.status.idle":"2022-07-29T17:28:36.205778Z","shell.execute_reply.started":"2022-07-29T17:28:36.200355Z","shell.execute_reply":"2022-07-29T17:28:36.204775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Adding top_layer","metadata":{}},{"cell_type":"markdown","source":"Only this layers will be trained ","metadata":{}},{"cell_type":"code","source":"\nmodel=base_model.output\n\nmodel=Dense(512, activation='relu')(model)\nmodel= GlobalAveragePooling2D()(model)\nmodel=Dropout(rate=0.5)(model)\nmodel=Dense(2, activation='softmax')(model)\nmodel=Model(inputs=base_model.inputs, outputs=model)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:28:58.780356Z","iopub.execute_input":"2022-07-29T17:28:58.780705Z","iopub.status.idle":"2022-07-29T17:28:58.829370Z","shell.execute_reply.started":"2022-07-29T17:28:58.780675Z","shell.execute_reply":"2022-07-29T17:28:58.828468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compiling the model","metadata":{}},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:02.790462Z","iopub.execute_input":"2022-07-29T17:29:02.791546Z","iopub.status.idle":"2022-07-29T17:29:02.810839Z","shell.execute_reply.started":"2022-07-29T17:29:02.791501Z","shell.execute_reply":"2022-07-29T17:29:02.809705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-29T17:29:06.239220Z","iopub.execute_input":"2022-07-29T17:29:06.239653Z","iopub.status.idle":"2022-07-29T17:29:06.252237Z","shell.execute_reply.started":"2022-07-29T17:29:06.239618Z","shell.execute_reply":"2022-07-29T17:29:06.251153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\nimport cv2\nfrom sklearn.utils import shuffle","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:16.304542Z","iopub.execute_input":"2022-07-29T17:29:16.304903Z","iopub.status.idle":"2022-07-29T17:29:16.495813Z","shell.execute_reply.started":"2022-07-29T17:29:16.304867Z","shell.execute_reply":"2022-07-29T17:29:16.494575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"category\"] = df[\"category\"].replace({0: 'cat', 1: 'dog'})","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:20.187340Z","iopub.execute_input":"2022-07-29T17:29:20.187938Z","iopub.status.idle":"2022-07-29T17:29:20.196649Z","shell.execute_reply.started":"2022-07-29T17:29:20.187903Z","shell.execute_reply":"2022-07-29T17:29:20.195595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split the dataset","metadata":{}},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.20, random_state=42)\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:23.877620Z","iopub.execute_input":"2022-07-29T17:29:23.877996Z","iopub.status.idle":"2022-07-29T17:29:23.889763Z","shell.execute_reply.started":"2022-07-29T17:29:23.877963Z","shell.execute_reply":"2022-07-29T17:29:23.888560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]\nbatch_size=4","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:27.739159Z","iopub.execute_input":"2022-07-29T17:29:27.740066Z","iopub.status.idle":"2022-07-29T17:29:27.744683Z","shell.execute_reply.started":"2022-07-29T17:29:27.740030Z","shell.execute_reply":"2022-07-29T17:29:27.743681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing data for fed the model","metadata":{}},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(\n    rescale=1/255\n)\n\n\ntrain_generator=train_datagen.flow_from_dataframe(train_df,\n                                                      './train',\n                                                      x_col='filename', \n                                                      y_col='category',\n                                                      weight_col=None,\n                                                      target_size=(128, 128),\n                                                      class_mode='categorical', \n                                                      batch_size=batch_size,\n                                                      \n                                                      \n                                                     )\n                                                 \n                                                 ","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:32.425305Z","iopub.execute_input":"2022-07-29T17:29:32.425696Z","iopub.status.idle":"2022-07-29T17:29:32.620245Z","shell.execute_reply.started":"2022-07-29T17:29:32.425667Z","shell.execute_reply":"2022-07-29T17:29:32.619301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"./train\", \n    x_col='filename',\n    y_col='category',\n    target_size=(128,128),\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:36.474432Z","iopub.execute_input":"2022-07-29T17:29:36.474788Z","iopub.status.idle":"2022-07-29T17:29:36.534944Z","shell.execute_reply.started":"2022-07-29T17:29:36.474760Z","shell.execute_reply":"2022-07-29T17:29:36.533845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:40.087747Z","iopub.execute_input":"2022-07-29T17:29:40.088748Z","iopub.status.idle":"2022-07-29T17:29:40.094287Z","shell.execute_reply.started":"2022-07-29T17:29:40.088695Z","shell.execute_reply":"2022-07-29T17:29:40.092457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Callbacks:","metadata":{}},{"cell_type":"markdown","source":"A callback is an object that can perform actions at various stages of training (e.g. at the start or end of an epoch, before or after a single batch, etc). You can use callbacks to: Write TensorBoard logs after every batch of training to monitor your metrics. Periodically save your model to disk.","metadata":{}},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\ncallbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:45.302987Z","iopub.execute_input":"2022-07-29T17:29:45.303337Z","iopub.status.idle":"2022-07-29T17:29:45.308469Z","shell.execute_reply.started":"2022-07-29T17:29:45.303310Z","shell.execute_reply":"2022-07-29T17:29:45.307528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\n    train_generator, \n    epochs=12,\n    validation_data=validation_generator,\n    validation_steps=total_validate//batch_size,\n    steps_per_epoch=total_train//batch_size,\n    callbacks=callbacks,\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T17:29:49.324261Z","iopub.execute_input":"2022-07-29T17:29:49.324608Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" Got a reasonable accuracy for both train and test datasets.If i only used the CNN model then ,it would be quite imposible to get this type accuracy.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}}]}