{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-01T14:32:40.901171Z","iopub.execute_input":"2024-04-01T14:32:40.901613Z","iopub.status.idle":"2024-04-01T14:32:42.146712Z","shell.execute_reply.started":"2024-04-01T14:32:40.901578Z","shell.execute_reply":"2024-04-01T14:32:42.145500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Importing package**","metadata":{}},{"cell_type":"code","source":"import os\nimport shutil\nimport random\nimport numpy as np\nimport pandas as pd\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, BatchNormalization, Dropout\nfrom tensorflow.keras.regularizers import L2\nimport warnings\nwarnings.filterwarnings('ignore')\nimport os\nimport zipfile\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom shutil import copyfile","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:34:19.556009Z","iopub.execute_input":"2024-04-01T14:34:19.556618Z","iopub.status.idle":"2024-04-01T14:34:34.226189Z","shell.execute_reply.started":"2024-04-01T14:34:19.556584Z","shell.execute_reply":"2024-04-01T14:34:34.224950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Unzipping Training data**","metadata":{}},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/train.zip","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:35:27.414827Z","iopub.execute_input":"2024-04-01T14:35:27.415571Z","iopub.status.idle":"2024-04-01T14:35:40.597869Z","shell.execute_reply.started":"2024-04-01T14:35:27.415535Z","shell.execute_reply":"2024-04-01T14:35:40.596376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/working/train'\nlen(os.listdir(train_path))","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:36:02.241998Z","iopub.execute_input":"2024-04-01T14:36:02.242460Z","iopub.status.idle":"2024-04-01T14:36:02.271703Z","shell.execute_reply.started":"2024-04-01T14:36:02.242423Z","shell.execute_reply":"2024-04-01T14:36:02.270577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.listdir(train_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:36:07.768093Z","iopub.execute_input":"2024-04-01T14:36:07.768941Z","iopub.status.idle":"2024-04-01T14:36:07.813480Z","shell.execute_reply.started":"2024-04-01T14:36:07.768903Z","shell.execute_reply":"2024-04-01T14:36:07.812670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Some visualization**","metadata":{}},{"cell_type":"code","source":"import matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\nimport random\n\nplt.figure(figsize=(16,16))\n\nimages = os.listdir(train_path)\nfor i in range(8):\n    img = random.choice([x for x in images])\n    fig = plt.subplot(4,4,i+1)\n    fig.axis('off')\n    img = mpimg.imread(os.path.join(train_path, img)) \n    fig.imshow(img) ","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:36:47.980966Z","iopub.execute_input":"2024-04-01T14:36:47.981633Z","iopub.status.idle":"2024-04-01T14:36:49.083848Z","shell.execute_reply.started":"2024-04-01T14:36:47.981601Z","shell.execute_reply":"2024-04-01T14:36:49.082254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(train_path)[:20]","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:37:06.956352Z","iopub.execute_input":"2024-04-01T14:37:06.957032Z","iopub.status.idle":"2024-04-01T14:37:06.983189Z","shell.execute_reply.started":"2024-04-01T14:37:06.956990Z","shell.execute_reply":"2024-04-01T14:37:06.982111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creating Directories to organize our Data**","metadata":{}},{"cell_type":"code","source":"try:\n    parent_dir = '/kaggle/working/cats-v-dogs'\n    os.mkdir(parent_dir)\n\n    for dir in ['training', 'validation']:\n        path = os.path.join(parent_dir , dir)\n        os.mkdir(path)\n        for dir in ['cats', 'dogs']:\n            n_path = os.path.join(path , dir)\n            os.mkdir(n_path)\nexcept OSError:\n    print('Existed')","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:37:51.625899Z","iopub.execute_input":"2024-04-01T14:37:51.627054Z","iopub.status.idle":"2024-04-01T14:37:51.635364Z","shell.execute_reply.started":"2024-04-01T14:37:51.627014Z","shell.execute_reply":"2024-04-01T14:37:51.634103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_dir = train_path\n\ncats_train_dir = '/kaggle/working/cats-v-dogs/training/cats'\ncats_valid_dir = '/kaggle/working/cats-v-dogs/validation/cats'\n\ndogs_train_dir = '/kaggle/working/cats-v-dogs/training/dogs'\ndogs_valid_dir = '/kaggle/working/cats-v-dogs/validation/dogs'\n\n# List of cats names in all training images\ncats =[img for img in os.listdir(src_dir) if img[:3] == 'cat']\n\n# List of dogs names in all training images\ndogs =[img for img in os.listdir(src_dir) if img[:3] == 'dog']\n\n# Take 10% of training images for validation    \nnp.random.seed(100)\ncats_valid_sample = random.sample(cats, int(0.1*len(cats))) \ndogs_valid_sample = random.sample(dogs, int(0.1*len(dogs)))","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:38:19.013927Z","iopub.execute_input":"2024-04-01T14:38:19.014384Z","iopub.status.idle":"2024-04-01T14:38:19.074929Z","shell.execute_reply.started":"2024-04-01T14:38:19.014352Z","shell.execute_reply":"2024-04-01T14:38:19.073690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in os.listdir(src_dir):\n    src_img = os.path.join(src_dir, img)\n\n    if img[:3] == 'cat':\n        if img in cats_valid_sample:\n            dest_img = os.path.join(cats_valid_dir, img)\n        else:\n            dest_img = os.path.join(cats_train_dir, img)\n\n    elif img[:3] == 'dog':\n        if img in dogs_valid_sample:\n            dest_img = os.path.join(dogs_valid_dir, img)\n        else:\n            dest_img = os.path.join(dogs_train_dir, img)\n    if not os.path.exists(dest_img):\n        shutil.copyfile(src_img, dest_img)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:38:33.694401Z","iopub.execute_input":"2024-04-01T14:38:33.694860Z","iopub.status.idle":"2024-04-01T14:38:38.815379Z","shell.execute_reply.started":"2024-04-01T14:38:33.694827Z","shell.execute_reply":"2024-04-01T14:38:38.814365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('No. of ALL cats : ', len(cats))\nprint('No. of cats for training: ', len(os.listdir(cats_train_dir)))\nprint('No. of cats for validation: ', len(os.listdir(cats_valid_dir)))\n\nprint('\\nNo. of ALL dogs : ', len(dogs))\nprint('No. of dogs for training: ', len(os.listdir(dogs_train_dir)))\nprint('No. of dogs for validation: ', len(os.listdir(dogs_valid_dir)))","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:38:53.270967Z","iopub.execute_input":"2024-04-01T14:38:53.271726Z","iopub.status.idle":"2024-04-01T14:38:53.298211Z","shell.execute_reply.started":"2024-04-01T14:38:53.271692Z","shell.execute_reply":"2024-04-01T14:38:53.296843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,16))\n\nimages = os.listdir(cats_train_dir)\nfor i in range(8):\n    img = random.choice([x for x in images])\n    fig = plt.subplot(4,4,i+1)\n    fig.axis('off')\n    img = mpimg.imread(os.path.join(cats_train_dir, img)) \n    fig.imshow(img) ","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:39:18.299058Z","iopub.execute_input":"2024-04-01T14:39:18.299711Z","iopub.status.idle":"2024-04-01T14:39:19.386978Z","shell.execute_reply.started":"2024-04-01T14:39:18.299671Z","shell.execute_reply":"2024-04-01T14:39:19.385808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image Data Generator**","metadata":{}},{"cell_type":"markdown","source":"To flow from directory\n\nAlso, we will use it for rescaling","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1/255)\nvalid_datagen = ImageDataGenerator(rescale=1/255)\n\n\ntrain_dir = '/kaggle/working/cats-v-dogs/training'\ntrain_generator = train_datagen.flow_from_directory(train_dir, target_size=(150, 150), batch_size=32, class_mode='binary')\n\nvalid_dir = '/kaggle/working/cats-v-dogs/validation'\nvalid_generator = valid_datagen.flow_from_directory(valid_dir,target_size=(150, 150), batch_size=32,class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:41:00.869401Z","iopub.execute_input":"2024-04-01T14:41:00.870175Z","iopub.status.idle":"2024-04-01T14:41:01.689671Z","shell.execute_reply.started":"2024-04-01T14:41:00.870138Z","shell.execute_reply":"2024-04-01T14:41:01.688245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(valid_generator.class_indices)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:41:18.127981Z","iopub.execute_input":"2024-04-01T14:41:18.128378Z","iopub.status.idle":"2024-04-01T14:41:18.134210Z","shell.execute_reply.started":"2024-04-01T14:41:18.128350Z","shell.execute_reply":"2024-04-01T14:41:18.132995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**According to Tensorflow Docs:**\n\n**Deprecated: tf.keras.preprocessing.image.ImageDataGenerator is not recommended for new code. Prefer loading images with tf.keras.utils.image_dataset_from_directory and transforming the output tf.data.Dataset with preprocessing layers. Check Here**\n\n**However, we will stick with ImageDataGenerator for its simplicity**","metadata":{}},{"cell_type":"markdown","source":"**Modelling**","metadata":{}},{"cell_type":"markdown","source":"**Base Model**","metadata":{}},{"cell_type":"code","source":"# create model\nmodel = Sequential([    Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),\n                        MaxPooling2D(2, 2),\n                        Conv2D(64, (3,3), activation='relu'),\n                        MaxPooling2D(2,2),\n                        Conv2D(128, (3,3), activation='relu'),\n                        MaxPooling2D(2,2),\n\n                        Flatten(),\n                        Dense(128, activation='relu'),\n                        Dense(1, activation='sigmoid')  ])\n\n# Compile model \nmodel.compile(optimizer='adamax', loss='binary_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:42:29.406815Z","iopub.execute_input":"2024-04-01T14:42:29.407224Z","iopub.status.idle":"2024-04-01T14:42:29.602050Z","shell.execute_reply.started":"2024-04-01T14:42:29.407196Z","shell.execute_reply":"2024-04-01T14:42:29.600912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We will fit the model for very few epochs, just for defining the least accuracy we can get**","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_generator, epochs=5, verbose=1,validation_data=valid_generator)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T14:44:21.673077Z","iopub.execute_input":"2024-04-01T14:44:21.673530Z","iopub.status.idle":"2024-04-01T15:31:13.108370Z","shell.execute_reply.started":"2024-04-01T14:44:21.673502Z","shell.execute_reply":"2024-04-01T15:31:13.107146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:,['loss','val_loss']].plot()\nhistory_df.loc[:,['accuracy','val_accuracy']].plot()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T15:31:27.295553Z","iopub.execute_input":"2024-04-01T15:31:27.296028Z","iopub.status.idle":"2024-04-01T15:31:28.014358Z","shell.execute_reply.started":"2024-04-01T15:31:27.295991Z","shell.execute_reply":"2024-04-01T15:31:28.013129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc = model.evaluate(train_generator)[1]\nvalid_acc = model.evaluate(valid_generator)[1]\nprint(\"Our Base Model Accuracy on Training Data: \", train_acc)\nprint(\"Our Base Model Accuracy on Validation Data: \", valid_acc)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T15:31:37.249467Z","iopub.execute_input":"2024-04-01T15:31:37.249906Z","iopub.status.idle":"2024-04-01T15:34:43.539962Z","shell.execute_reply.started":"2024-04-01T15:31:37.249872Z","shell.execute_reply":"2024-04-01T15:34:43.538986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Some Overfitting**\n\n**We will try using Data Augmentation to overcome it**","metadata":{}},{"cell_type":"markdown","source":"**Data Augmentation on the fly**","metadata":{}},{"cell_type":"markdown","source":"**A technique of artificially increasing the training set by creating modified copies of a dataset using existing data.**\n\n\n**Increase the diversity of your training set by applying random (but realistic) transformations, such as image rotation\nEnlarge dataset**\n\n\n**Avoid Overfitting**","metadata":{}},{"cell_type":"code","source":"aug_train_datagen = ImageDataGenerator(rescale=1./255,\n                                    rotation_range=90,\n                                    width_shift_range=0.2,\n                                    height_shift_range=0.2,\n                                    shear_range=0.2,\n                                    zoom_range=0.2,\n                                    horizontal_flip=True,\n                                    fill_mode='nearest')\nvalid_datagen = ImageDataGenerator(rescale=1/255)\n\ntrain_dir = '/kaggle/working/cats-v-dogs/training'\naug_train_generator = train_datagen.flow_from_directory(train_dir, target_size=(150, 150), batch_size=32, class_mode='binary')\n\nvalid_dir = '/kaggle/working/cats-v-dogs/validation'\nvalid_generator = valid_datagen.flow_from_directory(valid_dir,target_size=(150, 150), batch_size=32,class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2024-04-01T15:35:05.965304Z","iopub.execute_input":"2024-04-01T15:35:05.966352Z","iopub.status.idle":"2024-04-01T15:35:06.753581Z","shell.execute_reply.started":"2024-04-01T15:35:05.966310Z","shell.execute_reply":"2024-04-01T15:35:06.752470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Augmented Model**","metadata":{}},{"cell_type":"code","source":"my_callbacks = [ EarlyStopping(monitor='val_loss', patience=5, min_delta=0.001),\n                 ModelCheckpoint('/kaggle/working/weights.best.hdf5', monitor='val_loss', save_best_only=True)]\n\n# create model\naug_model = Sequential([    Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),\n                            MaxPooling2D(2, 2),\n                            Conv2D(64, (3,3), activation='relu'),\n                            MaxPooling2D(2,2),\n                            Conv2D(128, (3,3), activation='relu'),\n                            MaxPooling2D(2,2),\n\n                            Flatten(),\n                            Dense(128, activation='relu'),\n                            Dense(1, activation='sigmoid')  ])\n\n# load weights if exists\nif os.path.exists(\"/kaggle/working/weights.best.hdf5\"):\n    aug_model.load_weights(\"weights.best.hdf5\")\n\n# Compile model \naug_model.compile(optimizer='adamax', loss='binary_crossentropy', metrics=['accuracy'])\naug_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T15:35:40.361652Z","iopub.execute_input":"2024-04-01T15:35:40.362088Z","iopub.status.idle":"2024-04-01T15:35:41.239617Z","shell.execute_reply.started":"2024-04-01T15:35:40.362056Z","shell.execute_reply":"2024-04-01T15:35:41.237917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_history = aug_model.fit(aug_train_generator, epochs=5, verbose=1,validation_data=valid_generator, callbacks = my_callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T15:35:50.822271Z","iopub.execute_input":"2024-04-01T15:35:50.822767Z","iopub.status.idle":"2024-04-01T15:35:50.867687Z","shell.execute_reply.started":"2024-04-01T15:35:50.822714Z","shell.execute_reply":"2024-04-01T15:35:50.866409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(aug_history.history)\nhistory_df.loc[:,['loss','val_loss']].plot()\nhistory_df.loc[:,['accuracy','val_accuracy']].plot()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T15:35:53.813095Z","iopub.execute_input":"2024-04-01T15:35:53.813542Z","iopub.status.idle":"2024-04-01T15:35:53.858676Z","shell.execute_reply.started":"2024-04-01T15:35:53.813509Z","shell.execute_reply":"2024-04-01T15:35:53.857083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc = aug_model.evaluate(train_generator)[1]\nvalid_acc = aug_model.evaluate(valid_generator)[1]\nprint(\"Our Augmented Base Model Accuracy on Training Data: \", train_acc)\nprint(\"Our Augmented Base Model Accuracy on Validation Data: \", valid_acc)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T15:35:58.806699Z","iopub.execute_input":"2024-04-01T15:35:58.807779Z","iopub.status.idle":"2024-04-01T15:35:58.851447Z","shell.execute_reply.started":"2024-04-01T15:35:58.807727Z","shell.execute_reply":"2024-04-01T15:35:58.849846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Still Overfitting**\n\n**We may try more Regularization**","metadata":{}},{"cell_type":"markdown","source":"**More Complex Model**\n\n**More Layers**\n\n**Batch Normalization**\n\n**Dropout**","metadata":{}},{"cell_type":"code","source":"my_callbacks = [ EarlyStopping(monitor='val_loss', patience=5, min_delta=0.001),\n                 ModelCheckpoint('/kaggle/working/weights2.best.hdf5', monitor='val_loss', save_best_only=True)]\n\n# create model\nnew_model = Sequential([    Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),\n                            MaxPooling2D(2, 2),\n                            Conv2D(64, (3,3), activation='relu'),\n                            MaxPooling2D(2,2),\n                            Conv2D(128, (3,3), activation='relu'),\n                            MaxPooling2D(2,2),\n                            Conv2D(128, (3,3), activation='relu'),\n                            MaxPooling2D(2,2),\n                            Flatten(),\n                            BatchNormalization(),\n                            Dense(128, kernel_regularizer = L2(0.15), bias_regularizer=L2(0.15), activation='relu'),\n                            Dropout(rate = 0.5 , seed = 40),\n                            Dense(1, activation='sigmoid')  ])\n\n# load weights if exists\nif os.path.exists(\"/kaggle/working/weights2.best.hdf5\"):\n    new_model.load_weights(\"weights2.best.hdf5\")\n    \n    # Compile model \nnew_model.compile(optimizer='adamax', loss='binary_crossentropy', metrics=['accuracy'])\nnew_model.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_history = new_model.fit(aug_train_generator, epochs=20, verbose=1,validation_data=valid_generator, callbacks = my_callbacks)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(new_history.history)\nhistory_df.loc[:,['loss','val_loss']].plot()\nhistory_df.loc[:,['accuracy','val_accuracy']].plot()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc = new_model.evaluate(train_generator)[1]\nvalid_acc = new_model.evaluate(valid_generator)[1]\nprint(\"Our New Model Accuracy on Training Data: \", train_acc)\nprint(\"Our New Model Accuracy on Validation Data: \", valid_acc)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Sounds Good!**\n\n**We can go farther use More Epochs**\n\n**As we use ModelCheckpoint callback, the model will start from the final best weights stoed before**","metadata":{}},{"cell_type":"code","source":"new_history = new_model.fit(aug_train_generator, epochs=20, verbose=1,validation_data=valid_generator, callbacks = my_callbacks)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(new_history.history)\nhistory_df.loc[:,['loss','val_loss']].plot()\nhistory_df.loc[:,['accuracy','val_accuracy']].plot()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc = new_model.evaluate(train_generator)[1]\nvalid_acc = new_model.evaluate(valid_generator)[1]\nprint(\"Our New Model Accuracy on Training Data: \", train_acc)\nprint(\"Our New Model Accuracy on Validation Data: \", valid_acc)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Nice!**\n\n**Let's try Transfer Learning**","metadata":{}},{"cell_type":"markdown","source":"**Transfer Learning**\n\n**A technique where knowledge learned from a task is re-used in order to boost performance on a related task.**\n\n**The pre-trained models are usually trained on massive datasets that are a standard benchmark. The weights obtained from the models can be reused in other tasks.**\n\n**Transfer learning is particularly very useful when you have a small training dataset.**","metadata":{}},{"cell_type":"markdown","source":"**Why use transfer learning?**\n\n**training models with high accuracy requires a lot of data. For example, the ImageNet dataset contains over 1 million images. In the real world, you are unlikely to have such a large dataset.**\n\n**assuming that you had that kind of dataset, you might still not have the resources required to train a model on such a large dataset.\neven if you had the compute resources, you still have to wait for days or weeks to train such a model.**","metadata":{}},{"cell_type":"markdown","source":"**Examples of Pre-trained Models:**\n\n**training models with high accuracy requires a lot of data. For example, the ImageNet dataset contains over 1 million images. In the real world, you are unlikely to have such a large dataset.**\n\n**assuming that you had that kind of dataset, you might still not have the resources required to train a model on such a large dataset.**\n\n**even if you had the compute resources, you still have to wait for days or weeks to train such a model.**","metadata":{}},{"cell_type":"markdown","source":"**How you can use pre-trained models**\n\n**There are three ways to use a pre-trained model:**\n\n**Prediction.**\n\n**Feature Extraction.**\n\n**Fine-tuning.**\n\n**Here, we will use it for Feature Extraction**","metadata":{}},{"cell_type":"markdown","source":"**I will use the model included in this Notebook from Tensorflow Specialization on Coursera**","metadata":{}},{"cell_type":"code","source":"# Download the pre-trained weights. No top means it excludes the fully connected layer it uses for classification.\n!wget --no-check-certificate \\\n    https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 \\\n    -O /kaggle/working//inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5","metadata":{"execution":{"iopub.status.busy":"2024-04-01T15:31:13.110607Z","iopub.execute_input":"2024-04-01T15:31:13.110972Z","iopub.status.idle":"2024-04-01T15:31:18.776247Z","shell.execute_reply.started":"2024-04-01T15:31:13.110941Z","shell.execute_reply":"2024-04-01T15:31:18.774801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras import layers\n\n# Set the weights file you downloaded into a variable\nlocal_weights_file = '/kaggle/working/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\n# Initialize the base model.\n# Set the input shape and remove the dense layers.\npre_trained_model = InceptionV3(input_shape = (150, 150, 3), \n                                include_top = False, \n                                weights = None)\n\n# Load the pre-trained weights you downloaded.\npre_trained_model.load_weights(local_weights_file)\n\n# Freeze the weights of the layers.\nfor layer in pre_trained_model.layers:\n  layer.trainable = False","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_trained_model.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Choose `mixed7` as the last layer of your base model\nlast_layer = pre_trained_model.get_layer('mixed7')\nprint('last layer output shape: ', last_layer.output_shape)\nlast_output = last_layer.output","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras import Model\n\n# Flatten the output layer to 1 dimension\nx = layers.Flatten()(last_output)\n\n# Add a fully connected layer with 1,024 hidden units and ReLU activation\nx = layers.Dense(1024, activation='relu')(x)\n\n# Add a dropout rate of 0.2\nx = layers.Dropout(0.2)(x)         \n\n# Add a final sigmoid layer for classification\nx = layers.Dense  (1, activation='sigmoid')(x)           \n\n# Append the dense network to the base model\nmodel = Model(pre_trained_model.input, x) \n\n# Print the model summary. See your dense network connected at the end.\nmodel.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the training parameters\nmodel.compile(optimizer = RMSprop(learning_rate=0.0001), \n              loss = 'binary_crossentropy', \n              metrics = ['accuracy'])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_callbacks = [ EarlyStopping(monitor='val_loss', patience=5, min_delta=0.001),\n                 ModelCheckpoint('/kaggle/working/weights_tf.best.hdf5', monitor='val_loss', save_best_only=True)]\n\n# Train the model.\nhistory = model.fit(aug_train_generator, epochs=20, verbose=1,validation_data=valid_generator, callbacks = my_callbacks)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc = model.evaluate(train_generator)[1]\nvalid_acc = model.evaluate(valid_generator)[1]\nprint(\"Transfer Learning Model Accuracy on Training Data: \", train_acc)\nprint(\"Transfer Learning Model Accuracy on Validation Data: \", valid_acc)\nhistory_df = pd.DataFrame(history.history)\nhistory_df.loc[:,['loss','val_loss']].plot()\nhistory_df.loc[:,['accuracy','val_accuracy']].plot()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/inception_model.keras')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Predictions on Test Data**","metadata":{}},{"cell_type":"code","source":"! unzip -q /kaggle/input/dogs-vs-cats/test1.zip","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = '/kaggle/working/test1'\n\nplt.figure(figsize=(16,16))\nimages = os.listdir(test_dir)\nfor i in range(8):\n    img = random.choice([x for x in images])\n    fig = plt.subplot(4,4,i+1)\n    fig.axis('off')\n    img = mpimg.imread(os.path.join(test_dir, img)) \n    fig.imshow(img) ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Preparing for Test Images**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\n\ndef convert_one_img(img_folder, img):\n  # dimensions of image\n    img_width, img_height = 150, 150\n    img = os.path.join(img_folder, img)\n    img = image.load_img(img, target_size=(img_width, img_height))\n    img = image.img_to_array(img)\n    img = np.expand_dims(img, axis=0)\n    img /= 255\n\n    return img","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\nmodel = load_model('/kaggle/working/inception_model.keras')\n\ndef predict_visualize_img(img_folder, img, model):\n    img_fig = mpimg.imread(os.path.join(img_folder, img)) \n    plt.imshow(img_fig)\n    \n    img_prepared = convert_one_img(img_folder, img)\n    pred = model.predict(img_prepared)[0][0]\n    if pred <= 0.5:\n        result = 'Predicted : Cat'\n    else:\n        result = 'Predicted : Dog'\n    \n    plt.title(result)\n    return pred","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_visualize_img(test_dir, os.listdir(test_dir)[10], model)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_imgs_to_tensors(img_folder):\n\n  # load all images into a list\n  images = []\n\n  for img in os.listdir(img_folder):\n    img = convert_one_img(img_folder, img)\n    images.append(img)\n\n  # stack up images list to pass for model\n  images = np.vstack(images)\n\n  return images","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_prepared = convert_imgs_to_tensors(test_dir)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test_images = (model.predict(test_images_prepared) > 0.5).astype('int')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create DataFrame Similar to Submission CSV File**","metadata":{}},{"cell_type":"code","source":"test_data = os.listdir(test_dir)\ntest_df = pd.DataFrame({ \"id\" : test_data})\ntest_df['id'] = test_df['id'].str.split('.').str[0]\ntest_df['label'] = pred_test_images\ntest_df.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('/kaggle/working/my_first_preds.csv')","metadata":{},"execution_count":null,"outputs":[]}]}