{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":" # Importing Required libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport zipfile\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nfrom sklearn.manifold import TSNE\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport plotly.express as px\nimport random\nfrom PIL import Image\nimport plotly.graph_objects as go\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2025-04-10T19:02:58.852482Z","iopub.execute_input":"2025-04-10T19:02:58.852847Z","iopub.status.idle":"2025-04-10T19:03:01.804803Z","shell.execute_reply.started":"2025-04-10T19:02:58.852814Z","shell.execute_reply":"2025-04-10T19:03:01.803858Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Extracting the datasets","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/train')\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/test')","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:01.806433Z","iopub.execute_input":"2025-04-10T19:03:01.806916Z","iopub.status.idle":"2025-04-10T19:03:19.962396Z","shell.execute_reply.started":"2025-04-10T19:03:01.806892Z","shell.execute_reply":"2025-04-10T19:03:19.961550Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Setting up paths","metadata":{}},{"cell_type":"code","source":"train_dir = '/kaggle/working/train/train'\ntest_dir = '/kaggle/working/test/test1'","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:19.963331Z","iopub.execute_input":"2025-04-10T19:03:19.963652Z","iopub.status.idle":"2025-04-10T19:03:19.968207Z","shell.execute_reply.started":"2025-04-10T19:03:19.963620Z","shell.execute_reply":"2025-04-10T19:03:19.967143Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Function to load and preprocess the images","metadata":{}},{"cell_type":"code","source":"def load_images(folder, img_size=(64, 64), sample_count=20, label_filter=None):\n    images = []\n    labels = []\n    filenames = os.listdir(folder)\n    \n    if label_filter is not None:\n        filenames = [fname for fname in filenames if label_filter in fname]\n    \n    random.shuffle(filenames)\n    for img in filenames[:sample_count]:\n        try:\n            img_path = os.path.join(folder, img)\n            img_pil = Image.open(img_path)\n            img_pil = img_pil.resize(img_size)\n            img_np = np.array(img_pil) / 255.0  # Normalize pixel values\n            images.append(img_np)\n            labels.append(0 if 'cat' in img else 1)\n        except Exception as e:\n            print(f\"Error loading image {img}: {e}\")\n    \n    return np.array(images), np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:19.970237Z","iopub.execute_input":"2025-04-10T19:03:19.970555Z","iopub.status.idle":"2025-04-10T19:03:20.007009Z","shell.execute_reply.started":"2025-04-10T19:03:19.970529Z","shell.execute_reply":"2025-04-10T19:03:20.006068Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualize images with labels","metadata":{}},{"cell_type":"code","source":"def visualize_images(images, labels, sample_count=20):\n    fig, axes = plt.subplots(4, 5, figsize=(15, 12))\n    axes = axes.flatten()\n    \n    for i in range(sample_count):\n        axes[i].imshow(images[i])\n        axes[i].set_title('Cat' if labels[i] == 0 else 'Dog')\n        axes[i].axis('off')\n    \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:20.008030Z","iopub.execute_input":"2025-04-10T19:03:20.008339Z","iopub.status.idle":"2025-04-10T19:03:20.028120Z","shell.execute_reply.started":"2025-04-10T19:03:20.008310Z","shell.execute_reply":"2025-04-10T19:03:20.027216Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load and visualize cat images","metadata":{}},{"cell_type":"code","source":"cat_images, cat_labels = load_images(train_dir, sample_count=20, label_filter='cat')\nvisualize_images(cat_images, cat_labels, sample_count=20)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:20.029041Z","iopub.execute_input":"2025-04-10T19:03:20.029329Z","iopub.status.idle":"2025-04-10T19:03:21.865704Z","shell.execute_reply.started":"2025-04-10T19:03:20.029306Z","shell.execute_reply":"2025-04-10T19:03:21.864784Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load and visualize dog images","metadata":{}},{"cell_type":"code","source":"dog_images, dog_labels = load_images(train_dir, sample_count=20, label_filter='dog')\nvisualize_images(dog_images, dog_labels, sample_count=20)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:21.866657Z","iopub.execute_input":"2025-04-10T19:03:21.866948Z","iopub.status.idle":"2025-04-10T19:03:23.522546Z","shell.execute_reply.started":"2025-04-10T19:03:21.866920Z","shell.execute_reply":"2025-04-10T19:03:23.521616Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load and visualize both cat and dog images","metadata":{}},{"cell_type":"code","source":"all_images, all_labels = load_images(train_dir, sample_count=20)\nvisualize_images(all_images, all_labels, sample_count=20)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:23.523265Z","iopub.execute_input":"2025-04-10T19:03:23.523568Z","iopub.status.idle":"2025-04-10T19:03:24.990046Z","shell.execute_reply.started":"2025-04-10T19:03:23.523544Z","shell.execute_reply":"2025-04-10T19:03:24.989212Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Loading and preprocessing the training data","metadata":{}},{"cell_type":"code","source":"images, labels = load_images(train_dir, sample_count=10000) \nimages = images / 255.0","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:24.991187Z","iopub.execute_input":"2025-04-10T19:03:24.991520Z","iopub.status.idle":"2025-04-10T19:03:53.364264Z","shell.execute_reply.started":"2025-04-10T19:03:24.991493Z","shell.execute_reply":"2025-04-10T19:03:53.363390Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Display the shape of the images array","metadata":{}},{"cell_type":"code","source":"print(\"Shape of images array:\", images.shape)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:53.373206Z","iopub.execute_input":"2025-04-10T19:03:53.373495Z","iopub.status.idle":"2025-04-10T19:03:53.378223Z","shell.execute_reply.started":"2025-04-10T19:03:53.373476Z","shell.execute_reply":"2025-04-10T19:03:53.377239Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Display the shape of the first image","metadata":{}},{"cell_type":"code","source":"print(\"Shape of the first image:\", images[0].shape)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:53.379188Z","iopub.execute_input":"2025-04-10T19:03:53.379443Z","iopub.status.idle":"2025-04-10T19:03:53.402810Z","shell.execute_reply.started":"2025-04-10T19:03:53.379423Z","shell.execute_reply":"2025-04-10T19:03:53.401735Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":" # Display the first 2 images","metadata":{}},{"cell_type":"code","source":"print(images[:2]) \nprint(labels[:2])","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:53.403800Z","iopub.execute_input":"2025-04-10T19:03:53.404110Z","iopub.status.idle":"2025-04-10T19:03:53.427766Z","shell.execute_reply.started":"2025-04-10T19:03:53.404087Z","shell.execute_reply":"2025-04-10T19:03:53.426555Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Flattening the images for the SVM input","metadata":{}},{"cell_type":"code","source":"n_samples, img_height, img_width, img_channels = images.shape\nimages_flat = images.reshape(n_samples, -1)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:53.428782Z","iopub.execute_input":"2025-04-10T19:03:53.429137Z","iopub.status.idle":"2025-04-10T19:03:53.446852Z","shell.execute_reply.started":"2025-04-10T19:03:53.429106Z","shell.execute_reply":"2025-04-10T19:03:53.446047Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Standardizing the features","metadata":{}},{"cell_type":"code","source":"scaler = StandardScaler()\nimages_flat_scaled = scaler.fit_transform(images_flat)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:03:53.447838Z","iopub.execute_input":"2025-04-10T19:03:53.448585Z","iopub.status.idle":"2025-04-10T19:03:55.578077Z","shell.execute_reply.started":"2025-04-10T19:03:53.448553Z","shell.execute_reply":"2025-04-10T19:03:55.576667Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Splitting the dataset into training and test sets","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(images_flat_scaled, labels, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:05:47.990025Z","iopub.execute_input":"2025-04-10T19:05:47.990476Z","iopub.status.idle":"2025-04-10T19:05:48.379678Z","shell.execute_reply.started":"2025-04-10T19:05:47.990431Z","shell.execute_reply":"2025-04-10T19:05:48.379079Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:05:48.380863Z","iopub.execute_input":"2025-04-10T19:05:48.381173Z","iopub.status.idle":"2025-04-10T19:05:48.388835Z","shell.execute_reply.started":"2025-04-10T19:05:48.381143Z","shell.execute_reply":"2025-04-10T19:05:48.387782Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_val","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:05:48.389824Z","iopub.execute_input":"2025-04-10T19:05:48.390096Z","iopub.status.idle":"2025-04-10T19:05:48.407862Z","shell.execute_reply.started":"2025-04-10T19:05:48.390076Z","shell.execute_reply":"2025-04-10T19:05:48.406942Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_val","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:05:48.408890Z","iopub.execute_input":"2025-04-10T19:05:48.409877Z","iopub.status.idle":"2025-04-10T19:05:48.429060Z","shell.execute_reply.started":"2025-04-10T19:05:48.409846Z","shell.execute_reply":"2025-04-10T19:05:48.428165Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_val","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:05:48.430136Z","iopub.execute_input":"2025-04-10T19:05:48.430460Z","iopub.status.idle":"2025-04-10T19:05:48.452467Z","shell.execute_reply.started":"2025-04-10T19:05:48.430431Z","shell.execute_reply":"2025-04-10T19:05:48.451535Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Checking class distribution","metadata":{}},{"cell_type":"code","source":"unique, counts = np.unique(labels, return_counts=True)\nprint(f\"Class distribution: {dict(zip(unique, counts))}\")","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:05:48.453437Z","iopub.execute_input":"2025-04-10T19:05:48.453707Z","iopub.status.idle":"2025-04-10T19:05:48.472935Z","shell.execute_reply.started":"2025-04-10T19:05:48.453686Z","shell.execute_reply":"2025-04-10T19:05:48.471910Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training the SVM model","metadata":{}},{"cell_type":"code","source":"svm = SVC(kernel='rbf')\nsvm.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.340753Z","iopub.status.idle":"2025-04-10T19:19:12.341125Z","shell.execute_reply.started":"2025-04-10T19:19:12.340922Z","shell.execute_reply":"2025-04-10T19:19:12.340937Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Making predictions on the validation set","metadata":{}},{"cell_type":"code","source":"y_val_pred = svm.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.313486Z","iopub.status.idle":"2025-04-10T19:19:12.313921Z","shell.execute_reply.started":"2025-04-10T19:19:12.313723Z","shell.execute_reply":"2025-04-10T19:19:12.313741Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_val_pred","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.314977Z","iopub.status.idle":"2025-04-10T19:19:12.315280Z","shell.execute_reply.started":"2025-04-10T19:19:12.315149Z","shell.execute_reply":"2025-04-10T19:19:12.315161Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluating the model","metadata":{}},{"cell_type":"code","source":"val_accuracy = accuracy_score(y_val, y_val_pred)\nval_class_report = classification_report(y_val, y_val_pred, target_names=['Cat', 'Dog'])\nval_conf_matrix = confusion_matrix(y_val, y_val_pred)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.316400Z","iopub.status.idle":"2025-04-10T19:19:12.316763Z","shell.execute_reply.started":"2025-04-10T19:19:12.316579Z","shell.execute_reply":"2025-04-10T19:19:12.316596Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Accuracy","metadata":{}},{"cell_type":"code","source":"print(f'Validation Accuracy: {val_accuracy:.4f}')","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.317941Z","iopub.status.idle":"2025-04-10T19:19:12.318291Z","shell.execute_reply.started":"2025-04-10T19:19:12.318124Z","shell.execute_reply":"2025-04-10T19:19:12.318141Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Classification Report","metadata":{}},{"cell_type":"code","source":"print('Validation Classification Report:')\nprint(val_class_report)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.319068Z","iopub.status.idle":"2025-04-10T19:19:12.319364Z","shell.execute_reply.started":"2025-04-10T19:19:12.319211Z","shell.execute_reply":"2025-04-10T19:19:12.319223Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Confusion Matrix","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import confusion_matrix\nprint('Validation Confusion Matrix:')\nprint(val_conf_matrix)\nplt.figure(figsize=(10, 7))\nsns.heatmap(val_conf_matrix, annot=True, fmt='d', cmap='Blues')\nplt.ylabel('Actual')\nplt.xlabel('Predicted')\nplt.title('Validation Confusion Matrix')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.320709Z","iopub.status.idle":"2025-04-10T19:19:12.321045Z","shell.execute_reply.started":"2025-04-10T19:19:12.320844Z","shell.execute_reply":"2025-04-10T19:19:12.320859Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Saving the SVM model and scaler","metadata":{}},{"cell_type":"code","source":"import pickle\nwith open('/kaggle/working/svm_model.pkl', 'wb') as f:\n    pickle.dump(svm, f)\nwith open('/kaggle/working/scaler.pkl', 'wb') as f:\n    pickle.dump(scaler, f)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.322572Z","iopub.status.idle":"2025-04-10T19:19:12.322840Z","shell.execute_reply.started":"2025-04-10T19:19:12.322712Z","shell.execute_reply":"2025-04-10T19:19:12.322723Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Loading and preprocessing the test data","metadata":{}},{"cell_type":"code","source":"test_images, test_labels = load_images(test_dir)  \ntest_images = test_images / 255.0","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.323943Z","iopub.status.idle":"2025-04-10T19:19:12.324243Z","shell.execute_reply.started":"2025-04-10T19:19:12.324118Z","shell.execute_reply":"2025-04-10T19:19:12.324130Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Flattening the test images for the SVM input","metadata":{}},{"cell_type":"code","source":"n_test_samples, test_img_height, test_img_width, test_img_channels = test_images.shape\ntest_images_flat = test_images.reshape(n_test_samples, -1)\ntest_images_flat_scaled = scaler.transform(test_images_flat)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.325546Z","iopub.status.idle":"2025-04-10T19:19:12.325896Z","shell.execute_reply.started":"2025-04-10T19:19:12.325721Z","shell.execute_reply":"2025-04-10T19:19:12.325736Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Standardizing the test features","metadata":{}},{"cell_type":"code","source":"test_images_flat_scaled = scaler.transform(test_images_flat)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.326874Z","iopub.status.idle":"2025-04-10T19:19:12.327155Z","shell.execute_reply.started":"2025-04-10T19:19:12.327015Z","shell.execute_reply":"2025-04-10T19:19:12.327030Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Making predictions on the test set","metadata":{}},{"cell_type":"code","source":"y_test_pred = svm.predict(test_images_flat_scaled)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.329824Z","iopub.status.idle":"2025-04-10T19:19:12.330154Z","shell.execute_reply.started":"2025-04-10T19:19:12.330011Z","shell.execute_reply":"2025-04-10T19:19:12.330028Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test_pred","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.331367Z","iopub.status.idle":"2025-04-10T19:19:12.331603Z","shell.execute_reply.started":"2025-04-10T19:19:12.331490Z","shell.execute_reply":"2025-04-10T19:19:12.331500Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Saving the test predictions to a CSV file","metadata":{}},{"cell_type":"code","source":"test_predictions = pd.DataFrame({'Id': np.arange(1, n_test_samples + 1), 'Label': y_test_pred})\ntest_predictions['Label'] = test_predictions['Label'].map({0: 'Cat', 1: 'Dog'})\ntest_predictions.to_csv('/kaggle/working/test_predictions.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2025-04-10T19:19:12.337591Z","iopub.status.idle":"2025-04-10T19:19:12.337846Z","shell.execute_reply.started":"2025-04-10T19:19:12.337728Z","shell.execute_reply":"2025-04-10T19:19:12.337739Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Thank You!","metadata":{}}]}