{"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Cat vs Dog Image Classification**\n\n![Cats and Dogs](https://images.unsplash.com/photo-1548199973-03cce0bbc87b?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1000&q=80)\n\n\n## Abstract\n\nThis project presents an innovative approach to image classification, specifically targeting the distinction between cats and dogs. By leveraging transfer learning techniques, we combine the power of a pre-trained VGG16 convolutional neural network for feature extraction with a Support Vector Machine (SVM) classifier for final prediction. This hybrid model aims to achieve high accuracy in categorizing images while minimizing computational resources. The workflow encompasses data preparation, feature extraction using VGG16, SVM model training, performance evaluation, and a user-friendly interface for classifying new images. This research contributes to the field of computer vision by demonstrating an effective method for binary image classification that can be extended to various other image recognition tasks.\n \n## Workflow\n\n1. **Setup and Data Preparation**:\n   - Import necessary libraries\n   - Set image size for processing\n   - Load pre-trained VGG16 model (excluding top layers)\n\n2. **Feature Extraction**:\n   - Load and preprocess images from the dataset\n    - Use VGG16 to extract features from each image\n   - Store features and corresponding labels\n\n3. **Model Training**:\n   - Split data into training and testing sets\n   - Train an SVM classifier on the extracted features\n\n4. **Model Evaluation**:\n   - Predict labels for the test set\n   - Calculate and display accuracy and R2 score\n\n5. **Image Classification**:\n   - Implement a function to classify new images\n   - Allow user to select images via file dialog\n   - Preprocess selected images and extract features\n   - Use trained SVM to predict and display results\n","metadata":{}},{"cell_type":"code","source":"# Set the image size\nimage_size = (224, 224)\n\nfrom keras.applications.vgg16 import VGG16\n# Load the VGG16 model\nvgg_model = VGG16(weights='imagenet', include_top=False, input_shape=(image_size[0], image_size[1], 3))","metadata":{"id":"3jObhjj1eP4C","outputId":"e74da547-ae09-42fb-f506-83ec540ba576","execution":{"iopub.status.busy":"2024-09-10T06:19:58.828998Z","iopub.execute_input":"2024-09-10T06:19:58.829802Z","iopub.status.idle":"2024-09-10T06:20:17.701252Z","shell.execute_reply.started":"2024-09-10T06:19:58.829737Z","shell.execute_reply":"2024-09-10T06:20:17.699825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Explanation**\n\n\n1. We're setting up a standardized image size (224x224) that matches VGG16's expected input.\n2. We're importing the pre-trained VGG16 model from Keras for transfer learning.\n3. By using `weights='imagenet'`, we leverage features learned from millions of images.\n4. `include_top=False` removes the final classification layers, allowing us to use VGG16 for feature extraction only.\n5. We specify the `input_shape` to match our desired image dimensions and color channels.\n\n","metadata":{}},{"cell_type":"code","source":"import zipfile\nimport os\n\npath = \"/kaggle/input/dogs-vs-cats\"\nfor zip_file in os.listdir(path)[:2]:\n    zipfile.ZipFile(f\"{path}/{zip_file}\", 'r').extractall(\".\")\n#     print(f\"{path}/{zip_file}\")\n    print(f\"{zip_file} unzipped\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T06:20:17.703479Z","iopub.execute_input":"2024-09-10T06:20:17.704111Z","iopub.status.idle":"2024-09-10T06:20:37.540342Z","shell.execute_reply.started":"2024-09-10T06:20:17.704068Z","shell.execute_reply":"2024-09-10T06:20:37.539009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom tensorflow.keras.utils import load_img\nfrom tensorflow.keras.utils import img_to_array\nimport numpy as np\nfrom keras.applications.vgg16 import preprocess_input\n\n\n# Extract features from images\nX = []\ny = []\ndataset_dir = '/kaggle/working/train'\n\ndog_count = 0\ncat_count = 0\nmax_per_class = 150\ntotal_count = 0\n\nfor file in os.listdir(dataset_dir):\n    if file.endswith('.jpg'):\n        label = file.split('.')[0]\n        if (label == 'dog' and dog_count < max_per_class) or (label == 'cat' and cat_count < max_per_class):\n            if label == 'dog':\n                y.append(1)\n                dog_count += 1\n            else:\n                y.append(0)\n                cat_count += 1\n            \n            image = load_img(os.path.join(dataset_dir, file), target_size=image_size)\n            image = img_to_array(image)\n            image = np.expand_dims(image, axis=0)\n            image = preprocess_input(image)\n            features = vgg_model.predict(image)\n            features = features.flatten()\n            X.append(features)\n            \n            total_count += 1\n            if total_count % 10 == 0:\n                print(f\"{total_count} images processed\")\n        \n        if dog_count >= max_per_class and cat_count >= max_per_class:\n            break\n\nX = np.array(X)\ny = np.array(y)\n\n# Ensure X and y have data\nif len(X) == 0 or len(y) == 0:\n    raise ValueError(\"No data found in X or y. Please check your dataset directory and image loading process.\")\n\nprint(f\"Number of samples in X: {len(X)}\")\nprint(f\"Number of labels in y: {len(y)}\")\nprint(f\"Shape of X: {X.shape}\")\nprint(f\"Shape of y: {y.shape}\")\nprint(f\"Number of dog images: {dog_count}\")\nprint(f\"Number of cat images: {cat_count}\")","metadata":{"id":"2Jyi_LeBka-Z","outputId":"37cb5fb3-0b28-4934-b62b-4ebca29b6b1e","execution":{"iopub.status.busy":"2024-09-10T06:20:37.541987Z","iopub.execute_input":"2024-09-10T06:20:37.542453Z","iopub.status.idle":"2024-09-10T06:22:15.229929Z","shell.execute_reply.started":"2024-09-10T06:20:37.542399Z","shell.execute_reply":"2024-09-10T06:22:15.228830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Explanation**\n1. We're loading and preprocessing a balanced set of cat and dog images (150 each) from the specified directory.\n2. Each image is resized to match the VGG16 input size (224x224) and preprocessed using Keras utilities.\n3. We use the pre-trained VGG16 model to extract features from each image, flattening them for use with our classifier.\n4. Labels are assigned (0 for cats, 1 for dogs) and stored alongside the extracted features.\n5. We're implementing checks to ensure data is loaded correctly and providing informative prints about the dataset.\n6. The resulting X (features) and y (labels) arrays are prepared for subsequent model training and evaluation.\n","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score , r2_score\n\n# Check if X and y are not empty\nif len(X) > 0 and len(y) > 0:\n    # Split the dataset into training and testing sets\n    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n    # Create an SVM classifier\n    svm = SVC(kernel='linear', random_state=42)\n\n    # Train the classifier\n    svm.fit(X_train, y_train)\n\n    # Predict labels for the test set\n    y_pred = svm.predict(X_test)\n    print(X_test.shape)\n    # Calculate the accuracy  \n    accuracy = accuracy_score(y_test, y_pred)\n    r2 = r2_score(y_test, y_pred)\n    print('Accuracy:', accuracy)\n    print('R2 Score:', r2)\nelse:\n    print(\"Error: The dataset is empty. Please ensure that X and y contain data before splitting.\")","metadata":{"id":"WqtvjB_S1IEB","outputId":"435651f0-c1ea-4f33-8d6f-00b4ee64809b","execution":{"iopub.status.busy":"2024-09-10T06:22:15.231793Z","iopub.execute_input":"2024-09-10T06:22:15.232127Z","iopub.status.idle":"2024-09-10T06:22:16.436172Z","shell.execute_reply.started":"2024-09-10T06:22:15.232093Z","shell.execute_reply":"2024-09-10T06:22:16.435088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Explanation**\n1. We're splitting our dataset into training and testing sets to properly evaluate our model's performance.\n2. We're using a Support Vector Machine (SVM) classifier with a linear kernel for our binary classification task.\n3. The SVM is trained on the features extracted by VGG16 from our cat and dog images.\n4. We're predicting labels for the test set to assess how well our model generalizes to unseen data.\n5. We calculate and display the accuracy score to measure the model's overall performance.\n6. We also compute the R2 score, though it's worth noting that R2 is typically more relevant for regression tasks than classification.\n7. We've included error handling to ensure the dataset isn't empty before proceeding with the training process.\n","metadata":{}},{"cell_type":"markdown","source":"# **Conclusion**\n\nIn this project, we successfully implemented a machine learning model to classify images of cats and dogs. Here's a summary of what we accomplished:\n\n1. We used a pre-trained VGG16 model as a feature extractor for our images.\n2. We trained an SVM classifier on these extracted features to distinguish between cats and dogs.\n3. We created a user-friendly interface using tkinter to allow users to select their own images for classification.\n4. The model processes the selected images, resizing and preprocessing them as necessary.\n5. Finally, it predicts whether the input image is a cat or a dog and outputs the result.\n\nThis project demonstrates the power of transfer learning and how we can leverage pre-trained models to create effective classifiers with relatively small datasets. It also showcases the integration of machine learning models with user interfaces, making the technology accessible to end-users.\n\nFuture improvements could include:\n- Expanding the dataset to improve accuracy\n- Fine-tuning the VGG16 model for our specific task\n- Implementing a more sophisticated UI with batch processing capabilities\n- Extending the classifier to recognize more animal species\n\nOverall, this project provides a solid foundation for image classification tasks and can be easily adapted for various other binary classification problems.\n","metadata":{}}]}