{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":4748798,"sourceType":"datasetVersion","datasetId":2716794}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport cv2\nimport os\nimport matplotlib.pyplot as plt\nfrom sklearn import svm\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\nfrom keras.applications.vgg16 import VGG16, preprocess_input\nfrom keras.preprocessing.image import img_to_array\n\n# Load the VGG16 model\nbase_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\ndef extract_features(folder):\n    features = []\n    for filename in os.listdir(folder):\n        img_path = os.path.join(folder, filename)\n        img = cv2.imread(img_path)\n        img = cv2.resize(img, (224, 224))  # Resize to match VGG16 input size\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        feature = base_model.predict(img_array)\n        features.append(feature.flatten())\n    return np.array(features)\n\n# Paths to your dataset\ntrain_cats_path = '/kaggle/input/cats-and-dogs-image-classification/train/cats'\ntrain_dogs_path = '/kaggle/input/cats-and-dogs-image-classification/train/dogs'\ntest_cats_path = '/kaggle/input/cats-and-dogs-image-classification/test/cats'\ntest_dogs_path = '/kaggle/input/cats-and-dogs-image-classification/test/dogs'\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-08T00:46:46.559131Z","iopub.execute_input":"2024-08-08T00:46:46.559596Z","iopub.status.idle":"2024-08-08T00:47:05.724723Z","shell.execute_reply.started":"2024-08-08T00:46:46.559560Z","shell.execute_reply":"2024-08-08T00:47:05.723192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features\ntrain_cats_features = extract_features(train_cats_path)\ntrain_dogs_features = extract_features(train_dogs_path)\nX_train = np.vstack([train_cats_features, train_dogs_features])\ny_train = np.array([0] * len(train_cats_features) + [1] * len(train_dogs_features))\n\ntest_cats_features = extract_features(test_cats_path)\ntest_dogs_features = extract_features(test_dogs_path)\nX_test = np.vstack([test_cats_features, test_dogs_features])\ny_test = np.array([0] * len(test_cats_features) + [1] * len(test_dogs_features))\n\n# Split the data\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-08-08T00:47:24.785164Z","iopub.execute_input":"2024-08-08T00:47:24.786581Z","iopub.status.idle":"2024-08-08T00:51:45.377947Z","shell.execute_reply.started":"2024-08-08T00:47:24.786540Z","shell.execute_reply":"2024-08-08T00:51:45.376868Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\nfrom sklearn.metrics import classification_report, confusion_matrix\n\n# Train the SVM model\nclf = SVC(kernel='linear', probability=True)  # Use probability=True for plotting curves\nclf.fit(X_train, y_train)\n\n# Validate the model\ny_val_pred = clf.predict(X_val)\nprint(\"Validation Accuracy:\", accuracy_score(y_val, y_val_pred))\n\n# Test the model\ny_test_pred = clf.predict(X_test)\nprint(\"Test Accuracy:\", accuracy_score(y_test, y_test_pred))\n","metadata":{"execution":{"iopub.status.busy":"2024-08-08T00:53:09.282224Z","iopub.execute_input":"2024-08-08T00:53:09.282653Z","iopub.status.idle":"2024-08-08T00:53:17.793322Z","shell.execute_reply.started":"2024-08-08T00:53:09.282621Z","shell.execute_reply":"2024-08-08T00:53:17.787368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion_matrix(cm, classes):\n    plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)\n    plt.title('Confusion Matrix')\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    plt.show()\n\ndef plot_roc_curve(y_true, y_scores):\n    from sklearn.metrics import roc_curve, auc\n    fpr, tpr, _ = roc_curve(y_true, y_scores)\n    roc_auc = auc(fpr, tpr)\n    plt.figure()\n    plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)\n    plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\n    plt.xlim([0.0, 1.0])\n    plt.ylim([0.0, 1.05])\n    plt.xlabel('False Positive Rate')\n    plt.ylabel('True Positive Rate')\n    plt.title('Receiver Operating Characteristic')\n    plt.legend(loc=\"lower right\")\n    plt.show()\n\n# Confusion Matrix\ncm = confusion_matrix(y_test, y_test_pred)\nplot_confusion_matrix(cm, classes=['Cat', 'Dog'])\n\n# ROC Curve\ny_test_prob = clf.decision_function(X_test)\nplot_roc_curve(y_test, y_test_prob)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-08T00:53:36.245986Z","iopub.execute_input":"2024-08-08T00:53:36.246443Z","iopub.status.idle":"2024-08-08T00:53:37.349181Z","shell.execute_reply.started":"2024-08-08T00:53:36.246410Z","shell.execute_reply":"2024-08-08T00:53:37.347658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}