{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","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":31259,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"The SVM classifier achieved approximately 64% accuracy using HOG features. While classical machine learning methods perform reasonably, performance can be further improved using deep learning approaches such as CNNs.\n","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom skimage.feature import hog\nfrom sklearn.svm import LinearSVC\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom tqdm import tqdm\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-02-05T13:41:55.504115Z","iopub.execute_input":"2026-02-05T13:41:55.504361Z","iopub.status.idle":"2026-02-05T13:41:58.436788Z","shell.execute_reply.started":"2026-02-05T13:41:55.504336Z","shell.execute_reply":"2026-02-05T13:41:58.436032Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nbase_path = \"/kaggle/input\"\n\nprint(os.listdir(base_path))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-05T13:44:52.901593Z","iopub.execute_input":"2026-02-05T13:44:52.901837Z","iopub.status.idle":"2026-02-05T13:44:52.905766Z","shell.execute_reply.started":"2026-02-05T13:44:52.901819Z","shell.execute_reply":"2026-02-05T13:44:52.905145Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nimport os\n\nzip_path = \"/kaggle/input/dogs-vs-cats/train.zip\"\nextract_path = \"/kaggle/working/train\"\n\nwith zipfile.ZipFile(zip_path, 'r') as zip_ref:\n    zip_ref.extractall(extract_path)\n\nprint(\"Extraction completed!\")\nprint(os.listdir(extract_path)[:10])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-05T13:50:41.450231Z","iopub.execute_input":"2026-02-05T13:50:41.450549Z","iopub.status.idle":"2026-02-05T13:50:48.748292Z","shell.execute_reply.started":"2026-02-05T13:50:41.450525Z","shell.execute_reply":"2026-02-05T13:50:48.747479Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = \"/kaggle/working/train/train\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-05T13:53:17.328346Z","iopub.execute_input":"2026-02-05T13:53:17.329364Z","iopub.status.idle":"2026-02-05T13:53:17.333779Z","shell.execute_reply.started":"2026-02-05T13:53:17.329312Z","shell.execute_reply":"2026-02-05T13:53:17.332314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nfrom skimage.feature import hog\nfrom sklearn.svm import LinearSVC\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom tqdm import tqdm\n\n# Dataset path\ntrain_dir = \"/kaggle/working/train/train\"\n\nimage_size = (128, 128)\nfeatures = []\nlabels = []\n\nlimit = 4000   # keep small for speed\n\nprint(\"Loading images...\")\n\ncount = 0\nfor file in tqdm(os.listdir(train_dir)):\n    if file.endswith(\".jpg\"):\n        img_path = os.path.join(train_dir, file)\n\n        img = cv2.imread(img_path)\n        img = cv2.resize(img, image_size)\n        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n\n        hog_features = hog(\n            gray,\n            pixels_per_cell=(8,8),\n            cells_per_block=(2,2)\n        )\n\n        features.append(hog_features)\n\n        if file.startswith(\"dog\"):\n            labels.append(1)\n        else:\n            labels.append(0)\n\n        count += 1\n        if count >= limit:\n            break\n\nfeatures = np.array(features)\nlabels = np.array(labels)\n\nprint(\"Total samples:\", len(features))\n\n# Train-test split\nX_train, X_test, y_train, y_test = train_test_split(\n    features, labels, test_size=0.2, random_state=42\n)\n\n# Train SVM\nsvm = LinearSVC()\nsvm.fit(X_train, y_train)\n\nprint(\"Training complete!\")\n\n# Evaluate\ny_pred = svm.predict(X_test)\n\nprint(\"\\nClassification Report:\\n\")\nprint(classification_report(y_test, y_pred, target_names=[\"Cat\",\"Dog\"]))\n\nprint(\"\\nConfusion Matrix:\\n\")\nprint(confusion_matrix(y_test, y_pred))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-05T13:55:17.242994Z","iopub.execute_input":"2026-02-05T13:55:17.243279Z","iopub.status.idle":"2026-02-05T13:56:16.070307Z","shell.execute_reply.started":"2026-02-05T13:55:17.243261Z","shell.execute_reply":"2026-02-05T13:56:16.069782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(features.shape)\nprint(labels.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-05T13:56:20.224930Z","iopub.execute_input":"2026-02-05T13:56:20.225276Z","iopub.status.idle":"2026-02-05T13:56:20.230301Z","shell.execute_reply.started":"2026-02-05T13:56:20.225251Z","shell.execute_reply":"2026-02-05T13:56:20.229493Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.unique(labels, return_counts=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-05T13:56:28.827581Z","iopub.execute_input":"2026-02-05T13:56:28.827944Z","iopub.status.idle":"2026-02-05T13:56:28.834706Z","shell.execute_reply.started":"2026-02-05T13:56:28.827913Z","shell.execute_reply":"2026-02-05T13:56:28.833976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint(\"Accuracy:\", accuracy_score(y_test, y_pred))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-05T13:56:33.623887Z","iopub.execute_input":"2026-02-05T13:56:33.624186Z","iopub.status.idle":"2026-02-05T13:56:33.629726Z","shell.execute_reply.started":"2026-02-05T13:56:33.624147Z","shell.execute_reply":"2026-02-05T13:56:33.628634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_img = os.listdir(train_dir)[100]\n\nimg_path = os.path.join(train_dir, sample_img)\n\nimg = cv2.imread(img_path)\nimg = cv2.resize(img, (128,128))\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n\nhog_features = hog(gray, pixels_per_cell=(8,8), cells_per_block=(2,2))\n\nprediction = svm.predict([hog_features])[0]\n\nprint(\"Image:\", sample_img)\nprint(\"Prediction:\", \"Dog\" if prediction==1 else \"Cat\")\n\nplt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\nplt.axis(\"off\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-05T13:57:16.673777Z","iopub.execute_input":"2026-02-05T13:57:16.674036Z","iopub.status.idle":"2026-02-05T13:57:16.849255Z","shell.execute_reply.started":"2026-02-05T13:57:16.674018Z","shell.execute_reply":"2026-02-05T13:57:16.848570Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}