{"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":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":31011,"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","trusted":true,"execution":{"iopub.status.busy":"2025-05-08T05:22:38.143785Z","iopub.execute_input":"2025-05-08T05:22:38.144255Z","iopub.status.idle":"2025-05-08T05:22:38.150340Z","shell.execute_reply.started":"2025-05-08T05:22:38.144233Z","shell.execute_reply":"2025-05-08T05:22:38.149737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nimport os\n\n# Path to the ZIP file\nzip_path = '/kaggle/input/dogs-vs-cats/train.zip'\nextract_dir = '/kaggle/working'\n\n# Extract the zip file\nwith zipfile.ZipFile(zip_path, 'r') as zip_ref:\n    zip_ref.extractall(extract_dir)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T05:22:38.151392Z","iopub.execute_input":"2025-05-08T05:22:38.151654Z","iopub.status.idle":"2025-05-08T05:22:46.829060Z","shell.execute_reply.started":"2025-05-08T05:22:38.151636Z","shell.execute_reply":"2025-05-08T05:22:46.828479Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\n# Check the root extraction path\nroot_dir = '/kaggle/working'\ntrain_dir = os.path.join(root_dir, 'train')\ntest_dir= os.path.join(root_dir, 'test1')\n\n# Count and preview files\nall_files = os.listdir(train_dir)\nprint(f\"Total files in 'train/': {len(all_files)}\")\n\n# Separate by class for info\ncat_files = [f for f in all_files if f.startswith('cat')]\ndog_files = [f for f in all_files if f.startswith('dog')]\n\nprint(f\"Number of cat images: {len(cat_files)}\")\nprint(f\"Number of dog images: {len(dog_files)}\")\nprint(\"\\nSample filenames:\")\nprint(\"Cats:\", cat_files[:5])\nprint(\"Dogs:\", dog_files[:5])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T05:22:46.830076Z","iopub.execute_input":"2025-05-08T05:22:46.830311Z","iopub.status.idle":"2025-05-08T05:22:46.855437Z","shell.execute_reply.started":"2025-05-08T05:22:46.830293Z","shell.execute_reply":"2025-05-08T05:22:46.854907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom PIL import Image\nimport random\n\n\n# Get file lists\ncat_files = [os.path.join(train_dir, f) for f in os.listdir(train_dir) if f.startswith('cat')]\ndog_files = [os.path.join(train_dir, f) for f in os.listdir(train_dir) if f.startswith('dog')]\n\n# Randomly select 5 images from each class\nsample_cats = random.sample(cat_files, 5)\nsample_dogs = random.sample(dog_files, 5)\n\n# Plot cats\nplt.figure(figsize=(15, 3))\nfor i, file in enumerate(sample_cats):\n    img = Image.open(os.path.join(train_dir, file))\n    plt.subplot(1, 5, i + 1)\n    plt.imshow(img)\n    plt.title(\"Cat\")\n    plt.axis('off')\nplt.suptitle(\"Sample Cat Images\", fontsize=16)\nplt.show()\n\n# Plot dogs\nplt.figure(figsize=(15, 3))\nfor i, file in enumerate(sample_dogs):\n    img = Image.open(os.path.join(train_dir, file))\n    plt.subplot(1, 5, i + 1)\n    plt.imshow(img)\n    plt.title(\"Dog\")\n    plt.axis('off')\nplt.suptitle(\"Sample Dog Images\", fontsize=16)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T05:22:46.856201Z","iopub.execute_input":"2025-05-08T05:22:46.856542Z","iopub.status.idle":"2025-05-08T05:22:47.901611Z","shell.execute_reply.started":"2025-05-08T05:22:46.856510Z","shell.execute_reply":"2025-05-08T05:22:47.900920Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nimport numpy as np\nimport os\n\nmodel=VGG16(weights='imagenet', include_top=False,input_shape=(224,224,3))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T05:22:47.903236Z","iopub.execute_input":"2025-05-08T05:22:47.903587Z","iopub.status.idle":"2025-05-08T05:23:04.001828Z","shell.execute_reply.started":"2025-05-08T05:22:47.903534Z","shell.execute_reply":"2025-05-08T05:23:04.001051Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def features_extractor(img_path):\n    img=load_img(img_path,target_size=(224,224))\n    img_array=img_to_array(img)\n    img_array=np.expand_dims(img_array,axis=0)\n    img_array=preprocess_input(img_array)\n    features=model.predict(img_array,verbose=0)\n    features=features.flatten()\n    return features/np.linalg.norm(features)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T05:27:55.161999Z","iopub.execute_input":"2025-05-08T05:27:55.162722Z","iopub.status.idle":"2025-05-08T05:27:55.166966Z","shell.execute_reply.started":"2025-05-08T05:27:55.162699Z","shell.execute_reply":"2025-05-08T05:27:55.166205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features=[]\nlables=[]\nfor img in cat_files:\n    features.append(features_extractor(img))\n    lables.append('cat')#cat\nfor img in dog_files:\n    features.append(features_extractor(img))\n    lables.append('dog')#cat","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T05:27:57.172022Z","iopub.execute_input":"2025-05-08T05:27:57.172793Z","iopub.status.idle":"2025-05-08T05:58:07.167624Z","shell.execute_reply.started":"2025-05-08T05:27:57.172767Z","shell.execute_reply":"2025-05-08T05:58:07.166949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.svm import SVC\nX_train, X_test, y_train, y_test = train_test_split(features, lables, test_size=0.2, random_state=42)\nclf = SVC(kernel='linear')\nclf.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T06:00:02.032927Z","iopub.execute_input":"2025-05-08T06:00:02.033524Z","iopub.status.idle":"2025-05-08T06:06:20.639872Z","shell.execute_reply.started":"2025-05-08T06:00:02.033498Z","shell.execute_reply":"2025-05-08T06:06:20.639086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = clf.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T06:14:39.945678Z","iopub.execute_input":"2025-05-08T06:14:39.946284Z","iopub.status.idle":"2025-05-08T06:16:10.609519Z","shell.execute_reply.started":"2025-05-08T06:14:39.946261Z","shell.execute_reply":"2025-05-08T06:16:10.608903Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report, confusion_matrix,precision_score,recall_score,f1_score\nprecision = precision_score(y_test, y_pred, average='weighted')\nrecall = recall_score(y_test, y_pred, average='weighted')\nf1 = f1_score(y_test, y_pred, average='weighted')\naccuracy = accuracy_score(y_test, y_pred)\nprint(\"Precision:\", precision)\nprint(\"Recall:\", recall)\nprint(\"F1 Score:\", f1)\nprint(\"accuracy: \",accuracy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T06:23:26.801965Z","iopub.execute_input":"2025-05-08T06:23:26.802262Z","iopub.status.idle":"2025-05-08T06:23:26.862031Z","shell.execute_reply.started":"2025-05-08T06:23:26.802239Z","shell.execute_reply":"2025-05-08T06:23:26.861416Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nsns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues')\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-08T06:17:51.345952Z","iopub.execute_input":"2025-05-08T06:17:51.346525Z","iopub.status.idle":"2025-05-08T06:17:51.843448Z","shell.execute_reply.started":"2025-05-08T06:17:51.346488Z","shell.execute_reply":"2025-05-08T06:17:51.842605Z"}},"outputs":[],"execution_count":null}]}