{"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":"# IMPORTING LIBRARIES","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport os\nimport random \n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom keras.preprocessing import image\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:02:17.432095Z","iopub.execute_input":"2022-10-16T12:02:17.432472Z","iopub.status.idle":"2022-10-16T12:02:23.025215Z","shell.execute_reply.started":"2022-10-16T12:02:17.432393Z","shell.execute_reply":"2022-10-16T12:02:23.024374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PREPARING DATA","metadata":{}},{"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('./')","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:08:36.572999Z","iopub.execute_input":"2022-10-16T12:08:36.573356Z","iopub.status.idle":"2022-10-16T12:08:54.577497Z","shell.execute_reply.started":"2022-10-16T12:08:36.573324Z","shell.execute_reply":"2022-10-16T12:08:54.576604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read input files to create training dataset\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-10-16T12:09:03.009141Z","iopub.execute_input":"2022-10-16T12:09:03.009466Z","iopub.status.idle":"2022-10-16T12:09:03.042946Z","shell.execute_reply.started":"2022-10-16T12:09:03.009434Z","shell.execute_reply":"2022-10-16T12:09:03.042162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-10-16T12:09:07.687059Z","iopub.execute_input":"2022-10-16T12:09:07.687384Z","iopub.status.idle":"2022-10-16T12:09:07.708856Z","shell.execute_reply.started":"2022-10-16T12:09:07.687352Z","shell.execute_reply":"2022-10-16T12:09:07.707786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:09:13.0115Z","iopub.execute_input":"2022-10-16T12:09:13.011866Z","iopub.status.idle":"2022-10-16T12:09:13.021081Z","shell.execute_reply.started":"2022-10-16T12:09:13.011834Z","shell.execute_reply":"2022-10-16T12:09:13.020113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:09:18.431363Z","iopub.execute_input":"2022-10-16T12:09:18.431707Z","iopub.status.idle":"2022-10-16T12:09:18.44216Z","shell.execute_reply.started":"2022-10-16T12:09:18.431654Z","shell.execute_reply":"2022-10-16T12:09:18.441128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**VISUALIZING TRAINING DATASET** 📊","metadata":{}},{"cell_type":"code","source":"df['category'].value_counts().plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:09:21.83135Z","iopub.execute_input":"2022-10-16T12:09:21.831697Z","iopub.status.idle":"2022-10-16T12:09:21.982479Z","shell.execute_reply.started":"2022-10-16T12:09:21.831636Z","shell.execute_reply":"2022-10-16T12:09:21.98157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample image\n\nsample = random.choice(filenames)\n\nimage = image.load_img('./train/' + sample)\n\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:09:29.890472Z","iopub.execute_input":"2022-10-16T12:09:29.890842Z","iopub.status.idle":"2022-10-16T12:09:30.080261Z","shell.execute_reply.started":"2022-10-16T12:09:29.890807Z","shell.execute_reply":"2022-10-16T12:09:30.079368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SPLITTING DATASET INTO TRAINING AND VALIDATION SET","metadata":{}},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, 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-10-16T12:09:39.311755Z","iopub.execute_input":"2022-10-16T12:09:39.312084Z","iopub.status.idle":"2022-10-16T12:09:39.323089Z","shell.execute_reply.started":"2022-10-16T12:09:39.312053Z","shell.execute_reply":"2022-10-16T12:09:39.322296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:09:43.819442Z","iopub.execute_input":"2022-10-16T12:09:43.81979Z","iopub.status.idle":"2022-10-16T12:09:43.828472Z","shell.execute_reply.started":"2022-10-16T12:09:43.819757Z","shell.execute_reply":"2022-10-16T12:09:43.82769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:09:49.271082Z","iopub.execute_input":"2022-10-16T12:09:49.27142Z","iopub.status.idle":"2022-10-16T12:09:49.280768Z","shell.execute_reply.started":"2022-10-16T12:09:49.271388Z","shell.execute_reply":"2022-10-16T12:09:49.279762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['category'].value_counts().plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:09:53.283555Z","iopub.execute_input":"2022-10-16T12:09:53.283919Z","iopub.status.idle":"2022-10-16T12:09:53.393301Z","shell.execute_reply.started":"2022-10-16T12:09:53.283884Z","shell.execute_reply":"2022-10-16T12:09:53.392535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df['category'].value_counts().plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:09:56.579437Z","iopub.execute_input":"2022-10-16T12:09:56.579788Z","iopub.status.idle":"2022-10-16T12:09:56.687523Z","shell.execute_reply.started":"2022-10-16T12:09:56.579753Z","shell.execute_reply":"2022-10-16T12:09:56.6868Z"},"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-10-16T12:10:00.539587Z","iopub.execute_input":"2022-10-16T12:10:00.539964Z","iopub.status.idle":"2022-10-16T12:10:00.545821Z","shell.execute_reply.started":"2022-10-16T12:10:00.53993Z","shell.execute_reply":"2022-10-16T12:10:00.544709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# BUILD A PREDICTIVE MODEL","metadata":{}},{"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-10-16T12:10:12.659342Z","iopub.execute_input":"2022-10-16T12:10:12.659794Z","iopub.status.idle":"2022-10-16T12:10:12.663975Z","shell.execute_reply.started":"2022-10-16T12:10:12.659748Z","shell.execute_reply":"2022-10-16T12:10:12.663113Z"},"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()\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_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        \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\n    \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:10:17.232569Z","iopub.execute_input":"2022-10-16T12:10:17.232911Z","iopub.status.idle":"2022-10-16T12:10:17.244677Z","shell.execute_reply.started":"2022-10-16T12:10:17.23288Z","shell.execute_reply":"2022-10-16T12:10:17.243853Z"},"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-10-16T12:10:25.264114Z","iopub.execute_input":"2022-10-16T12:10:25.264429Z","iopub.status.idle":"2022-10-16T12:10:25.268846Z","shell.execute_reply.started":"2022-10-16T12:10:25.264398Z","shell.execute_reply":"2022-10-16T12:10:25.267749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1 = model.create_model_one()\n\nmodel_1.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:10:30.851492Z","iopub.execute_input":"2022-10-16T12:10:30.85185Z","iopub.status.idle":"2022-10-16T12:10:33.442218Z","shell.execute_reply.started":"2022-10-16T12:10:30.851815Z","shell.execute_reply":"2022-10-16T12:10:33.441354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import optimizers\n\nmodel_1.compile(loss = 'binary_crossentropy', optimizer = optimizers.RMSprop(lr = 1e-4), metrics =['acc'])","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:10:41.652083Z","iopub.execute_input":"2022-10-16T12:10:41.652407Z","iopub.status.idle":"2022-10-16T12:10:41.666775Z","shell.execute_reply.started":"2022-10-16T12:10:41.652377Z","shell.execute_reply":"2022-10-16T12:10:41.66589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:10:48.765144Z","iopub.execute_input":"2022-10-16T12:10:48.765465Z","iopub.status.idle":"2022-10-16T12:10:48.771091Z","shell.execute_reply.started":"2022-10-16T12:10:48.765433Z","shell.execute_reply":"2022-10-16T12:10:48.77003Z"},"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-10-16T12:10:53.543467Z","iopub.execute_input":"2022-10-16T12:10:53.543828Z","iopub.status.idle":"2022-10-16T12:10:53.548718Z","shell.execute_reply.started":"2022-10-16T12:10:53.543794Z","shell.execute_reply":"2022-10-16T12:10:53.547767Z"},"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                  )\n","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:10:57.611917Z","iopub.execute_input":"2022-10-16T12:10:57.612249Z","iopub.status.idle":"2022-10-16T12:10:57.850081Z","shell.execute_reply.started":"2022-10-16T12:10:57.612218Z","shell.execute_reply":"2022-10-16T12:10:57.84918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-10-16T12:11:05.132443Z","iopub.execute_input":"2022-10-16T12:11:05.132822Z","iopub.status.idle":"2022-10-16T12:11:05.192947Z","shell.execute_reply.started":"2022-10-16T12:11:05.132785Z","shell.execute_reply":"2022-10-16T12:11:05.191698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-10-16T12:12:14.912308Z","iopub.execute_input":"2022-10-16T12:12:14.912629Z","iopub.status.idle":"2022-10-16T12:12:14.918595Z","shell.execute_reply.started":"2022-10-16T12:12:14.912597Z","shell.execute_reply":"2022-10-16T12:12:14.917679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Callbacks","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:12:19.548385Z","iopub.execute_input":"2022-10-16T12:12:19.548731Z","iopub.status.idle":"2022-10-16T12:12:19.552704Z","shell.execute_reply.started":"2022-10-16T12:12:19.548697Z","shell.execute_reply":"2022-10-16T12:12:19.551654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:12:26.497074Z","iopub.execute_input":"2022-10-16T12:12:26.497403Z","iopub.status.idle":"2022-10-16T12:12:26.502944Z","shell.execute_reply.started":"2022-10-16T12:12:26.497371Z","shell.execute_reply":"2022-10-16T12:12:26.501803Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:12:29.751353Z","iopub.execute_input":"2022-10-16T12:12:29.751695Z","iopub.status.idle":"2022-10-16T12:12:29.75616Z","shell.execute_reply.started":"2022-10-16T12:12:29.751639Z","shell.execute_reply":"2022-10-16T12:12:29.754887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:12:32.913282Z","iopub.execute_input":"2022-10-16T12:12:32.913601Z","iopub.status.idle":"2022-10-16T12:12:32.917784Z","shell.execute_reply.started":"2022-10-16T12:12:32.91357Z","shell.execute_reply":"2022-10-16T12:12:32.916522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_3.compile(loss = 'binary_crossentropy', optimizer = optimizers.RMSprop(lr = 1e-5), metrics =['acc'])","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:12:36.871546Z","iopub.execute_input":"2022-10-16T12:12:36.871889Z","iopub.status.idle":"2022-10-16T12:12:36.882406Z","shell.execute_reply.started":"2022-10-16T12:12:36.871856Z","shell.execute_reply":"2022-10-16T12:12:36.881569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# FIT THE MODEL ONTO THE TRAINING DATA","metadata":{}},{"cell_type":"code","source":"from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:12:44.30927Z","iopub.execute_input":"2022-10-16T12:12:44.309586Z","iopub.status.idle":"2022-10-16T12:12:44.314592Z","shell.execute_reply.started":"2022-10-16T12:12:44.309554Z","shell.execute_reply":"2022-10-16T12:12:44.313612Z"},"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-10-16T12:12:46.085076Z","iopub.execute_input":"2022-10-16T12:12:46.085488Z","iopub.status.idle":"2022-10-16T12:17:47.960834Z","shell.execute_reply.started":"2022-10-16T12:12:46.085449Z","shell.execute_reply":"2022-10-16T12:17:47.95987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(history_1.history['acc']))","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:18:40.460573Z","iopub.execute_input":"2022-10-16T12:18:40.460958Z","iopub.status.idle":"2022-10-16T12:18:40.466062Z","shell.execute_reply.started":"2022-10-16T12:18:40.460923Z","shell.execute_reply":"2022-10-16T12:18:40.464768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(history_1.history['val_acc']))","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:18:44.94696Z","iopub.execute_input":"2022-10-16T12:18:44.947302Z","iopub.status.idle":"2022-10-16T12:18:44.95246Z","shell.execute_reply.started":"2022-10-16T12:18:44.947268Z","shell.execute_reply":"2022-10-16T12:18:44.951333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Saving the model","metadata":{}},{"cell_type":"code","source":"model_1.save('model_1.h5')","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:18:53.754067Z","iopub.execute_input":"2022-10-16T12:18:53.754392Z","iopub.status.idle":"2022-10-16T12:18:53.836028Z","shell.execute_reply.started":"2022-10-16T12:18:53.754362Z","shell.execute_reply":"2022-10-16T12:18:53.834796Z"},"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-10-16T12:18:59.467392Z","iopub.execute_input":"2022-10-16T12:18:59.467747Z","iopub.status.idle":"2022-10-16T12:18:59.731177Z","shell.execute_reply.started":"2022-10-16T12:18:59.467709Z","shell.execute_reply":"2022-10-16T12:18:59.730197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **6. PREPARE TESTING DATASET AND VISULAIZE IT** 📷","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:19:39.432625Z","iopub.execute_input":"2022-10-16T12:19:39.432988Z","iopub.status.idle":"2022-10-16T12:19:39.436838Z","shell.execute_reply.started":"2022-10-16T12:19:39.432955Z","shell.execute_reply":"2022-10-16T12:19:39.435892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model('model_1.h5')","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:20:00.055325Z","iopub.execute_input":"2022-10-16T12:20:00.055703Z","iopub.status.idle":"2022-10-16T12:20:00.174563Z","shell.execute_reply.started":"2022-10-16T12:20:00.055644Z","shell.execute_reply":"2022-10-16T12:20:00.173754Z"},"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-10-16T12:20:05.00213Z","iopub.execute_input":"2022-10-16T12:20:05.002463Z","iopub.status.idle":"2022-10-16T12:20:05.016437Z","shell.execute_reply.started":"2022-10-16T12:20:05.002432Z","shell.execute_reply":"2022-10-16T12:20:05.015649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:20:09.485194Z","iopub.execute_input":"2022-10-16T12:20:09.485545Z","iopub.status.idle":"2022-10-16T12:20:09.494414Z","shell.execute_reply.started":"2022-10-16T12:20:09.485508Z","shell.execute_reply":"2022-10-16T12:20:09.493539Z"},"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-10-16T12:20:17.213181Z","iopub.execute_input":"2022-10-16T12:20:17.213496Z","iopub.status.idle":"2022-10-16T12:20:17.240875Z","shell.execute_reply.started":"2022-10-16T12:20:17.213464Z","shell.execute_reply":"2022-10-16T12:20:17.239763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **7. PREDICTING ON TESTING DATA** 📈🤞","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:20:24.296397Z","iopub.execute_input":"2022-10-16T12:20:24.296766Z","iopub.status.idle":"2022-10-16T12:20:54.187311Z","shell.execute_reply.started":"2022-10-16T12:20:24.296719Z","shell.execute_reply":"2022-10-16T12:20:54.186377Z"},"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-10-16T12:21:49.256188Z","iopub.execute_input":"2022-10-16T12:21:49.256508Z","iopub.status.idle":"2022-10-16T12:21:49.30197Z","shell.execute_reply.started":"2022-10-16T12:21:49.256476Z","shell.execute_reply":"2022-10-16T12:21:49.30107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:21:53.894082Z","iopub.execute_input":"2022-10-16T12:21:53.894422Z","iopub.status.idle":"2022-10-16T12:21:54.001162Z","shell.execute_reply.started":"2022-10-16T12:21:53.89439Z","shell.execute_reply":"2022-10-16T12:21:54.000368Z"},"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-10-16T12:21:57.655121Z","iopub.execute_input":"2022-10-16T12:21:57.655812Z","iopub.status.idle":"2022-10-16T12:21:57.6643Z","shell.execute_reply.started":"2022-10-16T12:21:57.65577Z","shell.execute_reply":"2022-10-16T12:21:57.663327Z"},"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-10-16T12:22:03.523324Z","iopub.execute_input":"2022-10-16T12:22:03.523639Z","iopub.status.idle":"2022-10-16T12:22:03.535439Z","shell.execute_reply.started":"2022-10-16T12:22:03.523607Z","shell.execute_reply":"2022-10-16T12:22:03.534542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T12:22:08.452419Z","iopub.execute_input":"2022-10-16T12:22:08.452821Z","iopub.status.idle":"2022-10-16T12:22:08.558338Z","shell.execute_reply.started":"2022-10-16T12:22:08.452774Z","shell.execute_reply":"2022-10-16T12:22:08.557379Z"},"trusted":true},"execution_count":null,"outputs":[]}]}