{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Cats vs Dogs - CNN with InceptionV3 ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"### Project Name: **Cats vs Dogs Image Classifier**\n\n#### Overview:\nThis project involves building a machine learning model to classify images as either cats or dogs. It uses a well-known dataset from Kaggle, where the task is to distinguish between images of cats and dogs. The model is trained using deep learning techniques, with an emphasis on leveraging transfer learning from a pre-trained network (InceptionV3) to achieve high accuracy.\n\n#### Key Features:\n- **Data Preprocessing**: The dataset consists of 25,000 images of cats and dogs, which are divided into training and validation sets. The data is augmented to introduce variations in lighting, rotation, and zoom to improve generalization.\n- **Model Architecture**: The model uses InceptionV3 as the base, with its layers frozen, and custom layers are added on top for binary classification (cat or dog).\n- **Transfer Learning**: By utilizing a pre-trained model, the system can benefit from a robust set of features learned on large datasets like ImageNet, improving classification performance.\n- **Callbacks**: Early stopping and model checkpointing are employed to prevent overfitting and save the best-performing model.\n- **Evaluation**: The model's performance is visualized using training and validation accuracy/loss plots to track the progress.\n- **Test Predictions**: After training, the model is used to make predictions on unseen test images, and the results are stored in a CSV file for submission.\n\n#### Description:\nThe project was motivated by the \"Dogs vs Cats\" Kaggle competition, where the challenge is to develop an algorithm that can distinguish between images of cats and dogs. The dataset is highly diverse, with varying backgrounds, lighting, and poses, making the task challenging for machine learning models. By using advanced image recognition techniques such as Convolutional Neural Networks (CNNs) and Transfer Learning, the project aims to achieve high accuracy in classifying images, even with a large and varied dataset.\n\nThe dataset comes from Microsoft Research’s collaboration with Petfinder.com, which provided over three million images of cats and dogs. This competition has become a standard benchmark for image classification tasks in computer vision.\n\n#### Approach:\n1. **Data Preprocessing**: Images are resized to 150x150 pixels, and the dataset is split into training and validation sets. Data augmentation techniques are used to artificially expand the dataset and improve model performance.\n2. **Model Building**: InceptionV3 is used as a feature extractor, with custom fully connected layers added for the final classification task. The model is compiled with binary cross-entropy loss and accuracy as the evaluation metric.\n3. **Model Training**: The model is trained for 20 epochs with callbacks to prevent overfitting and save the best model.\n4. **Evaluation and Prediction**: After training, the model's performance is evaluated using training and validation accuracy/loss plots, and predictions on the test set are made and saved in a CSV file.\n\n\nThis project highlights the power of deep learning and transfer learning in solving real-world image classification problems, and can serve as a foundation for more complex computer vision tasks in the future.\n\n","metadata":{}},{"cell_type":"code","source":"#import paskages\nimport os\nimport zipfile\nimport random\nfrom sklearn.model_selection import train_test_split\nimport pandas as pd\n\nimport shutil\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import InceptionV3\nfrom tensorflow.keras import layers, Model\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:11.496324Z","iopub.execute_input":"2024-11-28T11:03:11.496643Z","iopub.status.idle":"2024-11-28T11:03:11.501927Z","shell.execute_reply.started":"2024-11-28T11:03:11.496615Z","shell.execute_reply":"2024-11-28T11:03:11.500931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#  Define Paths \nbase_dir = \"dogs_vs_cats\"\ntrain_zip = \"/kaggle/input/dogs-vs-cats/train.zip\"\ntest_zip = \"/kaggle/input/dogs-vs-cats/test1.zip\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:11.510147Z","iopub.execute_input":"2024-11-28T11:03:11.510382Z","iopub.status.idle":"2024-11-28T11:03:11.514241Z","shell.execute_reply.started":"2024-11-28T11:03:11.510358Z","shell.execute_reply":"2024-11-28T11:03:11.513361Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create necessary directories\nif not os.path.exists(base_dir):\n    os.makedirs(base_dir)\ntrain_dir = os.path.join(base_dir, \"train\")\ntest_dir = os.path.join(base_dir, \"test1\")\ntrain_data_dir = os.path.join(base_dir, \"training_data\")\nval_data_dir = os.path.join(base_dir, \"validation_data\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:11.515690Z","iopub.execute_input":"2024-11-28T11:03:11.515981Z","iopub.status.idle":"2024-11-28T11:03:11.522729Z","shell.execute_reply.started":"2024-11-28T11:03:11.515955Z","shell.execute_reply":"2024-11-28T11:03:11.521963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extract train data\nif not os.path.exists(train_dir):\n    with zipfile.ZipFile(train_zip, 'r') as zip_ref:\n        zip_ref.extractall(base_dir)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:11.523575Z","iopub.execute_input":"2024-11-28T11:03:11.523822Z","iopub.status.idle":"2024-11-28T11:03:11.534027Z","shell.execute_reply.started":"2024-11-28T11:03:11.523797Z","shell.execute_reply":"2024-11-28T11:03:11.533230Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extract  test data\nif not os.path.exists(test_dir):\n    with zipfile.ZipFile(test_zip, 'r') as zip_ref:\n        zip_ref.extractall(base_dir)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:11.535365Z","iopub.execute_input":"2024-11-28T11:03:11.535664Z","iopub.status.idle":"2024-11-28T11:03:11.542064Z","shell.execute_reply.started":"2024-11-28T11:03:11.535638Z","shell.execute_reply":"2024-11-28T11:03:11.541287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare directories for training and validation data\nif not os.path.exists(train_data_dir):\n    os.makedirs(train_data_dir)\nif not os.path.exists(val_data_dir):\n    os.makedirs(val_data_dir)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:11.543045Z","iopub.execute_input":"2024-11-28T11:03:11.543626Z","iopub.status.idle":"2024-11-28T11:03:11.551398Z","shell.execute_reply.started":"2024-11-28T11:03:11.543586Z","shell.execute_reply":"2024-11-28T11:03:11.550682Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Split Data","metadata":{}},{"cell_type":"code","source":"# Split Data into Training and Validation Sets\nall_images = os.listdir(train_dir)\ntrain_images, val_images = train_test_split(all_images, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:11.552337Z","iopub.execute_input":"2024-11-28T11:03:11.552585Z","iopub.status.idle":"2024-11-28T11:03:11.577133Z","shell.execute_reply.started":"2024-11-28T11:03:11.552559Z","shell.execute_reply":"2024-11-28T11:03:11.576556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def organize_data(image_list, source_dir, target_dir):\n    for img in image_list:\n        category = \"dog\" if \"dog\" in img else \"cat\"\n        target_path = os.path.join(target_dir, category)\n        if not os.path.exists(target_path):\n            os.makedirs(target_path)\n        shutil.copy(os.path.join(source_dir, img), os.path.join(target_path, img))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:11.577997Z","iopub.execute_input":"2024-11-28T11:03:11.578230Z","iopub.status.idle":"2024-11-28T11:03:11.582759Z","shell.execute_reply.started":"2024-11-28T11:03:11.578206Z","shell.execute_reply":"2024-11-28T11:03:11.581879Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"organize_data(train_images, train_dir, train_data_dir)\norganize_data(val_images, train_dir, val_data_dir)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:11.583889Z","iopub.execute_input":"2024-11-28T11:03:11.584209Z","iopub.status.idle":"2024-11-28T11:03:15.541449Z","shell.execute_reply.started":"2024-11-28T11:03:11.584172Z","shell.execute_reply":"2024-11-28T11:03:15.540759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Display sample images\ndef show_samples(base_folder, category, sample_count=3):\n    path = os.path.join(base_folder, category)\n    images = os.listdir(path)[:sample_count]\n    plt.figure(figsize=(10, 5))\n    for i, img_name in enumerate(images):\n        img = plt.imread(os.path.join(path, img_name))\n        plt.subplot(1, sample_count, i + 1)\n        plt.imshow(img)\n        plt.axis('off')\n        plt.title(category)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:15.543697Z","iopub.execute_input":"2024-11-28T11:03:15.543990Z","iopub.status.idle":"2024-11-28T11:03:15.549311Z","shell.execute_reply.started":"2024-11-28T11:03:15.543964Z","shell.execute_reply":"2024-11-28T11:03:15.548476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Print folder statistics\ndef folder_stats(base_folder):\n    for category in ['dog', 'cat']:\n        path = os.path.join(base_folder, category)\n        print(f\"{category.capitalize()} images in {base_folder}: {len(os.listdir(path))}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:15.550229Z","iopub.execute_input":"2024-11-28T11:03:15.550446Z","iopub.status.idle":"2024-11-28T11:03:15.559130Z","shell.execute_reply.started":"2024-11-28T11:03:15.550423Z","shell.execute_reply":"2024-11-28T11:03:15.558495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Show statistics and sample images\nprint(\"Training Data:\")\nfolder_stats(train_data_dir)\nshow_samples(train_data_dir, 'dog')\nshow_samples(train_data_dir, 'cat')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T11:03:15.560125Z","iopub.execute_input":"2024-11-28T11:03:15.560401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Validation Data:\")\nfolder_stats(val_data_dir)\nshow_samples(val_data_dir, 'dog')\nshow_samples(val_data_dir, 'cat')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Preprocessing the Data","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=40,\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'\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_datagen = ImageDataGenerator(rescale=1./255)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n    train_data_dir,\n    target_size=(150, 150),\n    batch_size=32,\n    class_mode='binary'\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_generator = val_datagen.flow_from_directory(\n    val_data_dir,\n    target_size=(150, 150),\n    batch_size=32,\n    class_mode='binary'\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### : Build  Model Using Transfer Learning\n","metadata":{}},{"cell_type":"code","source":"pretrained_model = InceptionV3(input_shape=(150, 150, 3), include_top=False, weights='imagenet')\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Freeze pre-trained layers\nfor layer in pretrained_model.layers:\n    layer.trainable = False","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Add  layers\nx = layers.Flatten()(pretrained_model.output)\nx = layers.Dense(1024, activation='relu')(x)\nx = layers.Dropout(0.3)(x)\nx = layers.Dense(1, activation='sigmoid')(x)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Model(pretrained_model.input, x)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compile  model\nmodel.compile(\n    optimizer=RMSprop(learning_rate=0.0001),\n    loss='binary_crossentropy',\n    metrics=['accuracy']\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Train the Model","metadata":{}},{"cell_type":"code","source":"# add calback\nearly_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\nmodel_checkpoint = ModelCheckpoint(\"cats_dogs_model.keras\", save_best_only=True)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=20,\n    callbacks=[early_stopping, model_checkpoint],\n    verbose=1\n)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Evaluate ","metadata":{}},{"cell_type":"code","source":"def plot_training_results(history):\n    acc = history.history['accuracy']\n    val_acc = history.history['val_accuracy']\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n    epochs = range(1, len(acc) + 1)\n\n    plt.figure(figsize=(12, 5))\n    \n    plt.subplot(1, 2, 1)\n    plt.plot(epochs, acc, label='Training Accuracy')\n    plt.plot(epochs, val_acc, label='Validation Accuracy')\n    plt.title('Training and Validation Accuracy')\n    plt.legend()\n\n    plt.subplot(1, 2, 2)\n    plt.plot(epochs, loss, label='Training Loss')\n    plt.plot(epochs, val_loss, label='Validation Loss')\n    plt.title('Training and Validation Loss')\n    plt.legend()\n\n    plt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_training_results(history)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Predict on Test Data","metadata":{}},{"cell_type":"code","source":"\n# Get all image file paths in the test directory\nimage_paths = [os.path.join(test_dir, fname) for fname in os.listdir(test_dir) if fname.endswith('.jpg')]\n\n# Create a DataFrame with the file paths\ndf = pd.DataFrame({'filename': image_paths})\n\n\n# Create an ImageDataGenerator for the test data\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\n# Load images using flow_from_dataframe\ntest_generator = test_datagen.flow_from_dataframe(\n    dataframe=df,\n    x_col='filename',  # Image file paths\n    target_size=(150, 150),  # Resize images\n    batch_size=32,\n    class_mode=None,  \n    shuffle=False  \n)\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Print the number of images found\nprint(f\"Found {len(test_generator.filenames)} images in the test directory.\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(test_generator)\npredicted_classes = [1 if pred > 0.5 else 0 for pred in predictions]\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"###### Save Predictions to a CSV File","metadata":{}},{"cell_type":"code","source":"filenames = test_generator.filenames\nresults = pd.DataFrame({\"Filename\": filenames, \"Prediction\": predicted_classes})\nresults.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}