{"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":"gpu","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"},{"sourceId":7797349,"sourceType":"datasetVersion","datasetId":4565043}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-03-09T04:54:48.622193Z","iopub.execute_input":"2024-03-09T04:54:48.622468Z","iopub.status.idle":"2024-03-09T04:54:48.641892Z","shell.execute_reply.started":"2024-03-09T04:54:48.622445Z","shell.execute_reply":"2024-03-09T04:54:48.641075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nimport os\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n\nfrom PIL import Image\nfrom IPython.display import display\n\n\n# Set up directories\nworking_dir = \"/kaggle/working\"\ndata_dir = \"/kaggle/input/dogs-vs-cats-redux-kernels-edition\"\ntrain_zip_path = os.path.join(data_dir, \"train.zip\")\ntest_zip_path = os.path.join(data_dir, \"test.zip\")\n\n# Create directories for train and test data\ntrain_dir = os.path.join(working_dir, \"train\")\ntest_dir = os.path.join(working_dir, \"test\")\n\nos.makedirs(train_dir, exist_ok=True)\nos.makedirs(test_dir, exist_ok=True)\n\n# Unzip train data\nwith zipfile.ZipFile(train_zip_path, 'r') as train_zip:\n    train_zip.extractall(train_dir)\n\n# Unzip test data\nwith zipfile.ZipFile(test_zip_path, 'r') as test_zip:\n    test_zip.extractall(test_dir)\n\nprint(\"Data extraction complete.\")","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:39:26.672346Z","iopub.execute_input":"2024-03-09T10:39:26.673269Z","iopub.status.idle":"2024-03-09T10:39:44.995018Z","shell.execute_reply.started":"2024-03-09T10:39:26.673233Z","shell.execute_reply":"2024-03-09T10:39:44.994020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Training Directory :--->\\n\")\ntraining_dir = \"/kaggle/working/train/train\"\nfiles_and_folders_in_training_dir = [item for item in os.listdir(training_dir)]\nprint(files_and_folders_in_training_dir[:2])\nprint(\"Total files Are :->\", len(files_and_folders_in_training_dir))\n\nprint(\"-\"*100, \"\\n\")\n\nprint(\"Testing Directory :--->\\n\")\ntesting_dir = \"/kaggle/working/test/test\"\nfiles_and_folders_in_testing_dir = [item for item in os.listdir(testing_dir)]\nprint(files_and_folders_in_testing_dir[:4])\nprint(\"Total files Are :->\", len(testing_dir))","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:39:44.996687Z","iopub.execute_input":"2024-03-09T10:39:44.996982Z","iopub.status.idle":"2024-03-09T10:39:45.029220Z","shell.execute_reply.started":"2024-03-09T10:39:44.996957Z","shell.execute_reply":"2024-03-09T10:39:45.028332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's Visualize The Image From The Training And Testing Directory ","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\ndef plot_random_images(dir_path, num_images, folder_name):\n    # Get a list of all the image file names in the directory\n    image_files = [f for f in os.listdir(dir_path) if f.endswith('.jpg') or f.endswith('.png')]\n\n    # Shuffle the list of image file names\n    random.shuffle(image_files)\n\n    # Get the first num_images from the shuffled list\n    image_files = image_files[:num_images]\n\n    # Calculate the number of rows and columns for the plot\n    num_rows = (num_images + 3) // 4\n    num_cols = min(num_images, 4)\n\n    # Create a figure to plot the images\n    fig = plt.figure(figsize=(10, 10*num_rows/num_cols))\n\n    # Loop through the selected images and plot each one\n    for i in range(num_images):\n        # Open the image file\n        img_path = os.path.join(dir_path, image_files[i])\n        img = Image.open(img_path)\n\n        # Add a subplot to the figure and plot the image\n        ax = fig.add_subplot(num_rows, num_cols, i+1)\n        ax.imshow(img,cmap=\"gray\")\n\n        # Remove the axis labels\n        ax.set_xticks([])\n        ax.set_yticks([])\n\n        # Add the image name as x-axis label\n        ax.set_xlabel(image_files[i], fontsize=8, wrap=True)\n\n    # Set the title of the plot to the folder name\n    plt.suptitle(folder_name, fontsize=16)\n\n    # Adjust the spacing between the subplots\n    plt.subplots_adjust(wspace=0.05, hspace=0.05)\n\n    # Show the plot\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:39:45.032577Z","iopub.execute_input":"2024-03-09T10:39:45.032872Z","iopub.status.idle":"2024-03-09T10:39:45.043211Z","shell.execute_reply.started":"2024-03-09T10:39:45.032847Z","shell.execute_reply":"2024-03-09T10:39:45.042269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training Directory Image","metadata":{}},{"cell_type":"code","source":"plot_random_images(training_dir, 3, \"Train_Images\")","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:39:45.044958Z","iopub.execute_input":"2024-03-09T10:39:45.045309Z","iopub.status.idle":"2024-03-09T10:39:45.449799Z","shell.execute_reply.started":"2024-03-09T10:39:45.045284Z","shell.execute_reply":"2024-03-09T10:39:45.448665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Testing Directory Image","metadata":{}},{"cell_type":"code","source":"plot_random_images(testing_dir, 3, \"Test_images\")","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:39:45.451004Z","iopub.execute_input":"2024-03-09T10:39:45.451347Z","iopub.status.idle":"2024-03-09T10:39:45.845801Z","shell.execute_reply.started":"2024-03-09T10:39:45.451320Z","shell.execute_reply":"2024-03-09T10:39:45.844851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creating DataFrame Of The Training_dataset Images","metadata":{}},{"cell_type":"code","source":"df = pd.DataFrame([training_dir+\"/\"+i for i in os.listdir(training_dir)], columns = ['ImagePath'])\ndf['Actual'] = df['ImagePath'].apply(lambda x: x.split('/')[-1].split('.')[0])","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:40:15.058920Z","iopub.execute_input":"2024-03-09T10:40:15.059526Z","iopub.status.idle":"2024-03-09T10:40:15.115662Z","shell.execute_reply.started":"2024-03-09T10:40:15.059498Z","shell.execute_reply":"2024-03-09T10:40:15.114887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(3)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:40:17.242656Z","iopub.execute_input":"2024-03-09T10:40:17.243277Z","iopub.status.idle":"2024-03-09T10:40:17.256209Z","shell.execute_reply.started":"2024-03-09T10:40:17.243243Z","shell.execute_reply":"2024-03-09T10:40:17.255204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Actual'].value_counts(normalize = True)*100","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:40:19.282398Z","iopub.execute_input":"2024-03-09T10:40:19.283376Z","iopub.status.idle":"2024-03-09T10:40:19.302363Z","shell.execute_reply.started":"2024-03-09T10:40:19.283326Z","shell.execute_reply":"2024-03-09T10:40:19.301332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###  Test Train Split","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_dataset, test_dataset = train_test_split(df ,test_size = 0.2 , random_state = 42, shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:40:23.097018Z","iopub.execute_input":"2024-03-09T10:40:23.097515Z","iopub.status.idle":"2024-03-09T10:40:23.604815Z","shell.execute_reply.started":"2024-03-09T10:40:23.097472Z","shell.execute_reply":"2024-03-09T10:40:23.603638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Data Generator","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.xception import preprocess_input\n\n\n# Define data generators for training and validation data\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,            # Rescale pixel values to the range [0, 1]\n    preprocessing_function=preprocess_input,\n    rotation_range=40,         # Randomly rotate images by up to 40 degrees\n    width_shift_range=0.3,     # Randomly shift images horizontally by up to 30% of the width\n    height_shift_range=0.3,    # Randomly shift images vertically by up to 30% of the height\n    shear_range=0.3,           # Shear intensity\n    zoom_range=0.3,            # Randomly zoom images by up to 30%\n    horizontal_flip=True,      # Randomly flip images horizontally\n    vertical_flip=True,        # Randomly flip images vertically\n    brightness_range=[0.6, 1.4],  # Randomly adjust brightness\n    channel_shift_range=100.0,  # Randomly shift channels\n    fill_mode='nearest',       # Strategy for filling in newly created pixels\n)\n\ntest_datagen = ImageDataGenerator(rescale=1./255,   # Rescale pixel values for the test set\n                                  preprocessing_function=preprocess_input)\n\nprint(\"-\"*80)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:40:54.543081Z","iopub.execute_input":"2024-03-09T10:40:54.544597Z","iopub.status.idle":"2024-03-09T10:40:54.558474Z","shell.execute_reply.started":"2024-03-09T10:40:54.544556Z","shell.execute_reply":"2024-03-09T10:40:54.557427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set = train_datagen.flow_from_dataframe(\n                        dataframe = train_dataset,\n                        x_col = 'ImagePath',\n                        y_col = 'Actual',\n                        target_size=(299,299),\n                        class_mode = 'binary',\n                        batch_size= 64)\n\nvalidation_set = test_datagen.flow_from_dataframe(\n                dataframe = test_dataset,\n                x_col = 'ImagePath',\n                y_col = 'Actual',\n                target_size = (299, 299),\n                class_mode = 'binary',\n                batch_size = 64)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:40:56.746377Z","iopub.execute_input":"2024-03-09T10:40:56.747045Z","iopub.status.idle":"2024-03-09T10:40:57.049312Z","shell.execute_reply.started":"2024-03-09T10:40:56.747015Z","shell.execute_reply":"2024-03-09T10:40:57.048281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import (Input, Lambda , Dense, Flatten, \n                                     ReLU, LeakyReLU, PReLU, BatchNormalization,\n                                    Conv2D, MaxPool2D, Dropout, \n                                     GlobalAveragePooling2D)\n\nfrom tensorflow.keras.models import (Model, Sequential)\n\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.applications.xception import Xception\n","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:40:59.145128Z","iopub.execute_input":"2024-03-09T10:40:59.145822Z","iopub.status.idle":"2024-03-09T10:40:59.154967Z","shell.execute_reply.started":"2024-03-09T10:40:59.145790Z","shell.execute_reply":"2024-03-09T10:40:59.154197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Building ","metadata":{}},{"cell_type":"code","source":"exception_model = Xception(input_shape = (299,299,3), weights = 'imagenet', include_top = False, pooling = 'average')\n\nprint(\"No Of Layers in exception_model\", len(exception_model.layers))\n\n# Fine-tune from this layer\nfine_tune_at = 80\n\n# Freeze all the layers before the `fine_tune_at` layer\nfor layer in exception_model.layers[:fine_tune_at]:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:41:02.148104Z","iopub.execute_input":"2024-03-09T10:41:02.148933Z","iopub.status.idle":"2024-03-09T10:41:03.764704Z","shell.execute_reply.started":"2024-03-09T10:41:02.148902Z","shell.execute_reply":"2024-03-09T10:41:03.763707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\n\n#learning_rate reduce module\nlr_reduce = ReduceLROnPlateau('val_loss', patience=3, \n                                              factor=0.5, min_lr=1e-6)\n\n# Stop early if model doesn't improve after n epochs\nearly_stopper = EarlyStopping(monitor='val_loss', patience=4,\n                              verbose=0, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:41:05.097787Z","iopub.execute_input":"2024-03-09T10:41:05.098377Z","iopub.status.idle":"2024-03-09T10:41:05.105723Z","shell.execute_reply.started":"2024-03-09T10:41:05.098339Z","shell.execute_reply":"2024-03-09T10:41:05.104780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import GlobalAveragePooling2D\n\n# Define the rest of your model\nx = GlobalAveragePooling2D()(exception_model.output)\nx = Dense(1024, activation='relu')(x)\nx = BatchNormalization()(x)\n\nx = Dropout(0.4)(x)\nx = Dense(512, activation='relu')(x)\nx = BatchNormalization()(x)\n\nx = Dropout(0.4)(x)\noutput = Dense(1, activation='sigmoid')(x)\n\n# Create a new model\nmodel = Model(inputs=exception_model.input, outputs=output)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:41:07.043663Z","iopub.execute_input":"2024-03-09T10:41:07.044347Z","iopub.status.idle":"2024-03-09T10:41:07.113845Z","shell.execute_reply.started":"2024-03-09T10:41:07.044311Z","shell.execute_reply":"2024-03-09T10:41:07.112974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:41:08.842670Z","iopub.execute_input":"2024-03-09T10:41:08.843321Z","iopub.status.idle":"2024-03-09T10:41:08.851897Z","shell.execute_reply.started":"2024-03-09T10:41:08.843288Z","shell.execute_reply":"2024-03-09T10:41:08.851043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(training_set,\n                         batch_size = 512,\n                         epochs=20,\n                         validation_data=validation_set,\n                         callbacks = [lr_reduce, early_stopper],\n                         verbose=1, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T17:00:38.433199Z","iopub.execute_input":"2024-03-08T17:00:38.434021Z","iopub.status.idle":"2024-03-08T17:54:32.942455Z","shell.execute_reply.started":"2024-03-08T17:00:38.433985Z","shell.execute_reply":"2024-03-08T17:54:32.941341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,5))\n\nplt.subplot(1,2,1)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], 'ro', label='Validation Loss')\nplt.title('Loss')\nplt.grid(True)\nplt.legend()\n\nplt.subplot(1,2,2)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], 'ro', label='Validation Accuracy')\nplt.title('Accuracy')\nplt.grid(True)\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-08T17:54:32.944322Z","iopub.execute_input":"2024-03-08T17:54:32.944636Z","iopub.status.idle":"2024-03-08T17:54:33.478169Z","shell.execute_reply.started":"2024-03-08T17:54:32.944609Z","shell.execute_reply":"2024-03-08T17:54:33.477042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:41:22.418786Z","iopub.execute_input":"2024-03-09T10:41:22.419659Z","iopub.status.idle":"2024-03-09T10:41:22.424346Z","shell.execute_reply.started":"2024-03-09T10:41:22.419616Z","shell.execute_reply":"2024-03-09T10:41:22.423435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Class Indeces Are :-->\", validation_set.class_indices)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:41:23.200897Z","iopub.execute_input":"2024-03-09T10:41:23.201316Z","iopub.status.idle":"2024-03-09T10:41:23.207147Z","shell.execute_reply.started":"2024-03-09T10:41:23.201284Z","shell.execute_reply":"2024-03-09T10:41:23.205996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image\nimport numpy as np\n\ndef predict_cat_or_dog(img_path, model):\n    # Load and preprocess the Image\n    img = image.load_img(img_path, target_size=(299, 299))\n    # Load and preprocess the image\n    img = Image.open(img_path)\n    img = img.resize((224, 224))  # Resize the image if needed\n    img_array = np.array(img) / 255.0  # Normalize pixel values\n\n    # Make prediction using the model\n    prediction = model.predict(np.expand_dims(img_array, axis=0))[0][0]\n\n    # Calculate confidence\n    if prediction > 0.5:\n        confidence = \"{:.4f}\".format(prediction * 100)\n        class_label = \"Dog\"\n        return \"I Am {0}% Sure This Is A {1}\".format(confidence, class_label)\n    else:\n        confidence = \"{:.4f}\".format((1 - prediction) * 100)\n        class_label = \"Cat\"\n        return \"I Am {0}% Sure This Is A {1}\".format(confidence, class_label)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:42:18.143142Z","iopub.execute_input":"2024-03-09T10:42:18.143801Z","iopub.status.idle":"2024-03-09T10:42:18.152013Z","shell.execute_reply.started":"2024-03-09T10:42:18.143769Z","shell.execute_reply":"2024-03-09T10:42:18.150737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '/kaggle/working/train/train/cat.9792.jpg'\nresult = predict_cat_or_dog(img_path, model)\nresult","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:19:31.533021Z","iopub.execute_input":"2024-03-09T10:19:31.533829Z","iopub.status.idle":"2024-03-09T10:19:31.606423Z","shell.execute_reply.started":"2024-03-09T10:19:31.533796Z","shell.execute_reply":"2024-03-09T10:19:31.605645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.head(3)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:19:34.174084Z","iopub.execute_input":"2024-03-09T10:19:34.174474Z","iopub.status.idle":"2024-03-09T10:19:34.183450Z","shell.execute_reply.started":"2024-03-09T10:19:34.174443Z","shell.execute_reply":"2024-03-09T10:19:34.182569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\ntqdm.pandas()\ntest_dataset['ModelProba'] = test_dataset['ImagePath'].progress_apply(lambda x: predict_cat_or_dog(x, model))\ntest_dataset['Prediction'] = test_dataset['ModelProba'].progress_apply(lambda x: \"dog\" if \"dog\" in x.lower() else \"cat\" if \"cat\" in x.lower() else None)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T18:09:46.045158Z","iopub.execute_input":"2024-03-08T18:09:46.045525Z","iopub.status.idle":"2024-03-08T18:15:46.051113Z","shell.execute_reply.started":"2024-03-08T18:09:46.045496Z","shell.execute_reply":"2024-03-08T18:15:46.050183Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset['Prediction'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-08T18:15:46.052713Z","iopub.execute_input":"2024-03-08T18:15:46.053017Z","iopub.status.idle":"2024-03-08T18:15:46.061696Z","shell.execute_reply.started":"2024-03-08T18:15:46.052990Z","shell.execute_reply":"2024-03-08T18:15:46.060775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset","metadata":{"execution":{"iopub.status.busy":"2024-03-08T18:19:54.126905Z","iopub.execute_input":"2024-03-08T18:19:54.127298Z","iopub.status.idle":"2024-03-08T18:19:54.139730Z","shell.execute_reply.started":"2024-03-08T18:19:54.127268Z","shell.execute_reply":"2024-03-08T18:19:54.138842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport random\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n\ndef plot_image_with_prediction(img_path, img_name, model):\n    # Load and preprocess the image\n    img = Image.open(img_path)\n    img = img.resize((299, 299))  # Resize the image if needed\n    img_array = np.array(img) / 255.0  # Normalize pixel values\n\n    # Make prediction using the model\n    prediction = model.predict(np.expand_dims(img_array, axis=0))[0][0]\n\n    # Calculate confidence\n    if prediction > 0.5:\n        confidence = \"{:.4f}\".format(prediction * 100)\n        class_label = \"Dog\"\n    else:\n        confidence = \"{:.4f}\".format((1 - prediction) * 100)\n        class_label = \"Cat\"\n\n    # Plot the image with the class label and confidence\n    plt.imshow(img)\n    plt.title(f\"Actual: {img_name}\")\n    plt.axis('off')\n    \n    # Add prediction text to the plot\n    plt.text(10, 30, f\"Prediction: I am {confidence}% sure this is a {class_label}\", fontsize=10, color='black', bbox=dict(facecolor='white', alpha=0.8))\n\n    plt.show()\n\ndef plot_random_images_with_prediction(dir_path, num_images, folder_name, model):\n    # Get a list of all the image file names in the directory\n    image_files = [f for f in os.listdir(dir_path) if f.endswith('.jpg') or f.endswith('.png')]\n\n    # Shuffle the list of image file names\n    random.shuffle(image_files)\n\n    # Get the first num_images from the shuffled list\n    image_files = image_files[:num_images]\n\n    # Calculate the number of rows and columns for the plot\n    num_rows = (num_images + 3) // 4\n    num_cols = min(num_images, 4)\n\n    # Create a figure to plot the images\n    fig = plt.figure(figsize=(10, 10*num_rows/num_cols))\n\n    # Loop through the selected images and plot each one\n    for i, image_file in enumerate(image_files):\n        # Open the image file\n        img_path = os.path.join(dir_path, image_file)\n        \n        # Plot the image with its prediction\n        ax = fig.add_subplot(num_rows, num_cols, i+1)\n        plot_image_with_prediction(img_path, image_file, model)\n\n    # Set the title of the plot to the folder name\n    plt.suptitle(folder_name, fontsize=16)\n\n    # Adjust the spacing between the subplots\n    plt.subplots_adjust(wspace=0.05, hspace=0.4)\n\n    # Show the plot\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:47:58.927891Z","iopub.execute_input":"2024-03-09T10:47:58.928287Z","iopub.status.idle":"2024-03-09T10:47:58.941522Z","shell.execute_reply.started":"2024-03-09T10:47:58.928256Z","shell.execute_reply":"2024-03-09T10:47:58.940357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_random_images_with_prediction(testing_dir, 3, 'Training_Image', model)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:48:03.276649Z","iopub.execute_input":"2024-03-09T10:48:03.277406Z","iopub.status.idle":"2024-03-09T10:48:10.064404Z","shell.execute_reply.started":"2024-03-09T10:48:03.277370Z","shell.execute_reply":"2024-03-09T10:48:10.063417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finally We Achieved 98% Accuaray  Inception Model","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Load Model \n\nfrom tensorflow.keras.models import load_model\nmodel = load_model(\"/kaggle/input/cat-dog-xception-model-h5/cat_dog_xception_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:42:46.944714Z","iopub.execute_input":"2024-03-09T10:42:46.945414Z","iopub.status.idle":"2024-03-09T10:42:48.780078Z","shell.execute_reply.started":"2024-03-09T10:42:46.945383Z","shell.execute_reply":"2024-03-09T10:42:48.779278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submit To Competition","metadata":{}},{"cell_type":"code","source":"SampleSubmission = pd.read_csv(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv\")\nprint(\"Shape Of THe Sample Data :-->\",SampleSubmission.shape)\n[SampleSubmission].head(1)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:42:51.927975Z","iopub.execute_input":"2024-03-09T10:42:51.928372Z","iopub.status.idle":"2024-03-09T10:42:51.950909Z","shell.execute_reply.started":"2024-03-09T10:42:51.928341Z","shell.execute_reply":"2024-03-09T10:42:51.949919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\ntqdm.pandas()\n\ntest_df = pd.DataFrame([testing_dir+\"/\"+i for i in os.listdir(testing_dir)], columns = ['ImagePath'])\ntest_df['Actual'] = test_df['ImagePath'].apply(lambda x: x.split('/')[-1].split('.')[0])\n\nprint(\"Shape Of THe test_df Data :-->\",test_df.shape)\n\n# test_df['ModelProba'] = test_df['ImagePath'].progress_apply(lambda x: predict_cat_or_dog(x, model))\n# test_df['Prediction'] = test_df['ModelProba'].progress_apply(lambda x: \"dog\" if \"dog\" in x.lower() else \"cat\" if \"cat\" in x.lower() else None)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:48:24.600694Z","iopub.execute_input":"2024-03-09T10:48:24.601416Z","iopub.status.idle":"2024-03-09T10:48:24.632528Z","shell.execute_reply.started":"2024-03-09T10:48:24.601381Z","shell.execute_reply":"2024-03-09T10:48:24.631590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###  Defining Function Which Will Calculate  Model Probability so that we could submit  To Competition","metadata":{}},{"cell_type":"code","source":"def model_probability(img_path, model):\n    # Load and preprocess the image\n    img = Image.open(img_path)\n    img = img.resize((299, 299))  # Resize the image if needed\n    img_array = np.array(img) / 255.0  # Normalize pixel values\n\n    # Make prediction using the model\n    prediction = model.predict(np.expand_dims(img_array, axis=0))[0][0]\n    return prediction","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:52:09.596973Z","iopub.execute_input":"2024-03-09T10:52:09.597398Z","iopub.status.idle":"2024-03-09T10:52:09.603822Z","shell.execute_reply.started":"2024-03-09T10:52:09.597364Z","shell.execute_reply":"2024-03-09T10:52:09.602734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x = test_df[test_df['Actual']=='4914']\ntest_df['ModelProba'] = test_df['ImagePath'].progress_apply(lambda x: model_probability(x, model))","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:53:04.069599Z","iopub.execute_input":"2024-03-09T10:53:04.070401Z","iopub.status.idle":"2024-03-09T11:08:40.806294Z","shell.execute_reply.started":"2024-03-09T10:53:04.070367Z","shell.execute_reply":"2024-03-09T11:08:40.805389Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2024-03-09T11:13:34.299830Z","iopub.execute_input":"2024-03-09T11:13:34.300860Z","iopub.status.idle":"2024-03-09T11:13:34.314025Z","shell.execute_reply.started":"2024-03-09T11:13:34.300826Z","shell.execute_reply":"2024-03-09T11:13:34.312847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SampleSubmission['label'] = test_df['ModelProba'].to_list()","metadata":{"execution":{"iopub.status.busy":"2024-03-09T11:13:34.327029Z","iopub.execute_input":"2024-03-09T11:13:34.327408Z","iopub.status.idle":"2024-03-09T11:13:34.334606Z","shell.execute_reply.started":"2024-03-09T11:13:34.327382Z","shell.execute_reply":"2024-03-09T11:13:34.333266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SampleSubmission.to_csv(\"SampleSubmission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-03-09T11:15:43.961665Z","iopub.execute_input":"2024-03-09T11:15:43.962620Z","iopub.status.idle":"2024-03-09T11:15:44.009746Z","shell.execute_reply.started":"2024-03-09T11:15:43.962584Z","shell.execute_reply":"2024-03-09T11:15:44.008990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## If you found this notebook helpful, please consider upvoting it!","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}