{"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":[{"sourceType":"competition","sourceId":14774,"databundleVersionId":875431}],"dockerImageVersionId":31286,"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":{"iopub.status.busy":"2026-03-22T14:23:58.030506Z","iopub.execute_input":"2026-03-22T14:23:58.032329Z","iopub.status.idle":"2026-03-22T14:24:03.007303Z","shell.execute_reply.started":"2026-03-22T14:23:58.032270Z","shell.execute_reply":"2026-03-22T14:24:03.006088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.listdir('/kaggle/input')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:24:03.009227Z","iopub.execute_input":"2026-03-22T14:24:03.009545Z","iopub.status.idle":"2026-03-22T14:24:03.017415Z","shell.execute_reply.started":"2026-03-22T14:24:03.009505Z","shell.execute_reply":"2026-03-22T14:24:03.016274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.listdir('/kaggle/input/competitions')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:24:03.018674Z","iopub.execute_input":"2026-03-22T14:24:03.019062Z","iopub.status.idle":"2026-03-22T14:24:03.040343Z","shell.execute_reply.started":"2026-03-22T14:24:03.019020Z","shell.execute_reply":"2026-03-22T14:24:03.039266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir('/kaggle/input/competitions/aptos2019-blindness-detection')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:24:03.041635Z","iopub.execute_input":"2026-03-22T14:24:03.042504Z","iopub.status.idle":"2026-03-22T14:24:03.062207Z","shell.execute_reply.started":"2026-03-22T14:24:03.042471Z","shell.execute_reply":"2026-03-22T14:24:03.061172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv('/kaggle/input/competitions/aptos2019-blindness-detection/train.csv')\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:24:03.065082Z","iopub.execute_input":"2026-03-22T14:24:03.065403Z","iopub.status.idle":"2026-03-22T14:24:03.102727Z","shell.execute_reply.started":"2026-03-22T14:24:03.065370Z","shell.execute_reply":"2026-03-22T14:24:03.101666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\nimg_path = '/kaggle/input/competitions/aptos2019-blindness-detection/train_images/000c1434d8d7.png'\nimg = cv2.imread(img_path)\n\nplt.imshow(img)\nplt.axis('off')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:24:03.104288Z","iopub.execute_input":"2026-03-22T14:24:03.104626Z","iopub.status.idle":"2026-03-22T14:24:04.255337Z","shell.execute_reply.started":"2026-03-22T14:24:03.104597Z","shell.execute_reply":"2026-03-22T14:24:04.254401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_SIZE = 224\n\nimport cv2\n\ndef preprocess_image(path):\n    img = cv2.imread(path)\n    img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n    img = img / 255.0\n    return img","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:24:04.257198Z","iopub.execute_input":"2026-03-22T14:24:04.258177Z","iopub.status.idle":"2026-03-22T14:24:04.263505Z","shell.execute_reply.started":"2026-03-22T14:24:04.258139Z","shell.execute_reply":"2026-03-22T14:24:04.262280Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv('/kaggle/input/competitions/aptos2019-blindness-detection/train.csv')\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:24:04.264628Z","iopub.execute_input":"2026-03-22T14:24:04.265079Z","iopub.status.idle":"2026-03-22T14:24:04.295126Z","shell.execute_reply.started":"2026-03-22T14:24:04.264946Z","shell.execute_reply":"2026-03-22T14:24:04.294174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images = []\nlabels = []\nfor index, row in df.iterrows():\n    \n    img_path = img_path = '/kaggle/input/competitions/aptos2019-blindness-detection/train_images/' + row['id_code'] + '.png'\n    \n    img = preprocess_image(img_path)\n    \n    images.append(img)\n    labels.append(row['diagnosis'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:24:04.296244Z","iopub.execute_input":"2026-03-22T14:24:04.296911Z","iopub.status.idle":"2026-03-22T14:32:16.547388Z","shell.execute_reply.started":"2026-03-22T14:24:04.296880Z","shell.execute_reply":"2026-03-22T14:32:16.545289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\nX = np.array(images)\ny = np.array(labels)\n\nprint(X.shape)\nprint(y.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.548831Z","iopub.status.idle":"2026-03-22T14:32:16.549290Z","shell.execute_reply.started":"2026-03-22T14:32:16.549086Z","shell.execute_reply":"2026-03-22T14:32:16.549115Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images = []\nlabels = []\n\nfor index, row in df.head(500).iterrows():\n\n    img_path = '/kaggle/input/competitions/aptos2019-blindness-detection/train_images/' + row['id_code'] + '.png'\n\n    img = preprocess_image(img_path)\n\n    images.append(img)\n    labels.append(row['diagnosis'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.550734Z","iopub.status.idle":"2026-03-22T14:32:16.551067Z","shell.execute_reply.started":"2026-03-22T14:32:16.550921Z","shell.execute_reply":"2026-03-22T14:32:16.550938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv('/kaggle/input/competitions/aptos2019-blindness-detection/train.csv')\n\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.552407Z","iopub.status.idle":"2026-03-22T14:32:16.552877Z","shell.execute_reply.started":"2026-03-22T14:32:16.552631Z","shell.execute_reply":"2026-03-22T14:32:16.552658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images = []\nlabels = []\n\nfor index, row in df.head(500).iterrows():\n\n    img_path = '/kaggle/input/competitions/aptos2019-blindness-detection/train_images/' + row['id_code'] + '.png'\n\n    img = preprocess_image(img_path)\n\n    images.append(img)\n    labels.append(row['diagnosis'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.554331Z","iopub.status.idle":"2026-03-22T14:32:16.554631Z","shell.execute_reply.started":"2026-03-22T14:32:16.554489Z","shell.execute_reply":"2026-03-22T14:32:16.554506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\n\nIMG_SIZE = 224\n\ndef preprocess_image(path):\n    img = cv2.imread(path)\n    img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n    img = img / 255.0\n    return img","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.556518Z","iopub.status.idle":"2026-03-22T14:32:16.556951Z","shell.execute_reply.started":"2026-03-22T14:32:16.556791Z","shell.execute_reply":"2026-03-22T14:32:16.556812Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv('/kaggle/input/competitions/aptos2019-blindness-detection/train.csv')\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.557994Z","iopub.status.idle":"2026-03-22T14:32:16.558325Z","shell.execute_reply.started":"2026-03-22T14:32:16.558165Z","shell.execute_reply":"2026-03-22T14:32:16.558182Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images = []\nlabels = []\n\nfor index, row in df.head(500).iterrows():\n\n    img_path = '/kaggle/input/competitions/aptos2019-blindness-detection/train_images/' + row['id_code'] + '.png'\n\n    img = preprocess_image(img_path)\n\n    images.append(img)\n    labels.append(row['diagnosis'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.559797Z","iopub.status.idle":"2026-03-22T14:32:16.560093Z","shell.execute_reply.started":"2026-03-22T14:32:16.559955Z","shell.execute_reply":"2026-03-22T14:32:16.559972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\nX = np.array(images)\ny = np.array(labels)\n\nprint(X.shape)\nprint(y.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.561573Z","iopub.status.idle":"2026-03-22T14:32:16.562320Z","shell.execute_reply.started":"2026-03-22T14:32:16.562114Z","shell.execute_reply":"2026-03-22T14:32:16.562142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\ny = to_categorical(y, num_classes=5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.563580Z","iopub.status.idle":"2026-03-22T14:32:16.563894Z","shell.execute_reply.started":"2026-03-22T14:32:16.563757Z","shell.execute_reply":"2026-03-22T14:32:16.563774Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(y.shape)\nprint(y[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.565567Z","iopub.status.idle":"2026-03-22T14:32:16.566068Z","shell.execute_reply.started":"2026-03-22T14:32:16.565822Z","shell.execute_reply":"2026-03-22T14:32:16.565850Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.2, random_state=42\n)\n\nprint(X_train.shape)\nprint(X_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.568039Z","iopub.status.idle":"2026-03-22T14:32:16.568385Z","shell.execute_reply.started":"2026-03-22T14:32:16.568244Z","shell.execute_reply":"2026-03-22T14:32:16.568261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.569589Z","iopub.status.idle":"2026-03-22T14:32:16.570206Z","shell.execute_reply.started":"2026-03-22T14:32:16.570017Z","shell.execute_reply":"2026-03-22T14:32:16.570036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\n\n# 1st Convolution Layer\nmodel.add(Conv2D(32, (3,3), activation='relu', input_shape=(224,224,3)))\nmodel.add(MaxPooling2D(2,2))\n\n# 2nd Convolution Layer\nmodel.add(Conv2D(64, (3,3), activation='relu'))\nmodel.add(MaxPooling2D(2,2))\n\n# Flatten Layer\nmodel.add(Flatten())\n\n# Dense Layer\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dropout(0.5))\n\n# Output Layer\nmodel.add(Dense(5, activation='softmax'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.572124Z","iopub.status.idle":"2026-03-22T14:32:16.572592Z","shell.execute_reply.started":"2026-03-22T14:32:16.572364Z","shell.execute_reply":"2026-03-22T14:32:16.572391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.574666Z","iopub.status.idle":"2026-03-22T14:32:16.575187Z","shell.execute_reply.started":"2026-03-22T14:32:16.574952Z","shell.execute_reply":"2026-03-22T14:32:16.574980Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train,\n    y_train,\n    epochs=10,\n    batch_size=32,\n    validation_data=(X_test, y_test)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.576678Z","iopub.status.idle":"2026-03-22T14:32:16.577139Z","shell.execute_reply.started":"2026-03-22T14:32:16.576921Z","shell.execute_reply":"2026-03-22T14:32:16.576947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.2, random_state=42\n)\n\nprint(X_train.shape)\nprint(X_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:16.578659Z","iopub.status.idle":"2026-03-22T14:32:16.579121Z","shell.execute_reply.started":"2026-03-22T14:32:16.578893Z","shell.execute_reply":"2026-03-22T14:32:16.578920Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport cv2\n\n# Take one sample image path\nsample_path = '/kaggle/input/competitions/aptos2019-blindness-detection/train_images/' + df.iloc[0]['id_code'] + '.png'\n\n# Original image\noriginal = cv2.imread(sample_path)\noriginal = cv2.cvtColor(original, cv2.COLOR_BGR2RGB)\n\n# Processed image\nprocessed = preprocess_image(sample_path)\n\n# Show both\nplt.figure(figsize=(10,5))\n\nplt.subplot(1,2,1)\nplt.imshow(original)\nplt.title(\"Original Image\")\nplt.axis('off')\n\nplt.subplot(1,2,2)\nplt.imshow(processed)\nplt.title(\"Processed Image (224x224 + Normalized)\")\nplt.axis('off')\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T14:32:23.748051Z","iopub.execute_input":"2026-03-22T14:32:23.748457Z","iopub.status.idle":"2026-03-22T14:32:25.000096Z","shell.execute_reply.started":"2026-03-22T14:32:23.748417Z","shell.execute_reply":"2026-03-22T14:32:24.998955Z"}},"outputs":[],"execution_count":null}]}