{"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":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, accuracy_score, roc_curve, auc\nimport matplotlib.pyplot as plt\n# from skimage.feature import greycomatrix, greycoprops\n\n# Adjust the base path according to your dataset location\nbase_path = \"/kaggle/input/aptos2019-blindness-detection\"\ntrain_csv_path = f\"{base_path}/train.csv\"\ntrain_images_path = f\"{base_path}/train_images\"\n\n# Read the dataset\ndf = pd.read_csv(train_csv_path)\n\n# Preprocess and feature extraction functions\ndef preprocess_image(image_path, output_size=(224, 224)):\n    image = cv2.imread(image_path)\n    image_resized = cv2.resize(image, output_size)\n    image_gray = cv2.cvtColor(image_resized, cv2.COLOR_BGR2GRAY)\n    image_enhanced = cv2.equalizeHist(image_gray)\n    return image_enhanced\n\ndef extract_features(image):\n    # This function integrates color and texture feature extraction\n    # Dummy feature extraction for demonstration\n    # Replace with actual feature extraction logic\n    return np.histogram(image, bins=8)[0]\n\n# Prepare dataset\nX = []\ny = []\n\nfor index, row in df.iterrows():\n    image_path = f\"{train_images_path}/{row['id_code']}.png\"\n    image = preprocess_image(image_path)\n    features = extract_features(image)\n    X.append(features)\n    y.append(row['diagnosis'])\n\nX = np.array(X)\ny = np.array(y)\n\n# Split dataset\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Train SVM classifier\nsvm_model = SVC(kernel='rbf', C=1.0, gamma='auto', probability=True)\nsvm_model.fit(X_train, y_train)\n\n# Evaluate the model\ny_pred = svm_model.predict(X_test)\nprint(\"Accuracy:\", accuracy_score(y_test, y_pred))\nprint(classification_report(y_test, y_pred))\n\n# Assuming binary classification for demonstration\n# For multi-class, consider One-vs-Rest (OvR) for ROC calculation\ny_prob = svm_model.predict_proba(X_test)[:, 1]\nfpr, tpr, thresholds = roc_curve(y_test, y_prob, pos_label=1)\nroc_auc = auc(fpr, tpr)\n\nplt.figure()\nplt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)\nplt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('Receiver Operating Characteristic')\nplt.legend(loc=\"lower right\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:35:21.209749Z","iopub.execute_input":"2024-04-08T00:35:21.210432Z","iopub.status.idle":"2024-04-08T00:42:50.464122Z","shell.execute_reply.started":"2024-04-08T00:35:21.210397Z","shell.execute_reply":"2024-04-08T00:42:50.462731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, accuracy_score\nimport os\n\n# Path configuration\nbase_path = \"/kaggle/input/aptos2019-blindness-detection\"\ntrain_csv_path = os.path.join(base_path, \"train.csv\")\ntrain_images_path = os.path.join(base_path, \"train_images\")\n\n# Load dataset\ndf = pd.read_csv(train_csv_path)\n\n# Image preprocessing and feature extraction function\ndef preprocess_and_extract_features(image_path):\n    # Read and preprocess the image\n    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n    image_resized = cv2.resize(image, (128, 128))  # Resize to 128x128\n\n    # Simple feature extraction - histogram of pixel intensities\n    hist = cv2.calcHist([image_resized], [0], None, [256], [0, 256]).flatten()\n    return hist\n\n# Prepare the dataset for training\nX = []\ny = []\n\nfor _, row in df.iterrows():\n    image_path = os.path.join(train_images_path, f\"{row['id_code']}.png\")\n    features = preprocess_and_extract_features(image_path)\n    X.append(features)\n    y.append(row['diagnosis'])\n\nX = np.array(X)\ny = np.array(y)\n\n# Split the dataset into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Initialize and train the SVM classifier\nsvm_model = SVC(kernel='linear', C=1.0, gamma='auto')\nsvm_model.fit(X_train, y_train)\n\n# Predict and evaluate the model\ny_pred = svm_model.predict(X_test)\nprint(\"Accuracy:\", accuracy_score(y_test, y_pred))\nprint(\"Classification Report:\")\nprint(classification_report(y_test, y_pred))\n","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:44:34.705205Z","iopub.execute_input":"2024-04-08T00:44:34.706434Z"},"trusted":true},"execution_count":null,"outputs":[]}]}