{ "cells": [ { "cell_type": "markdown", "id": "76b39fb1", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "## Jupyter notebook example - Classification task\n", "### Example using [MESSIDOR2](https://www.adcis.net/en/third-party/messidor2/) dataset\n", "**Application**: Using RETFound for five-category diabetic retinopathy classification\n", "\n", "**Author**: Yukun Zhou\n", "\n", "**Date**: 30 Nov 2025\n", "\n", "**Performance**:\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", "
AccuracyRecallF1 ScoreROC AUCPR AUC
0.70910.56160.60780.90370.6863
\n" ] }, { "cell_type": "markdown", "id": "7ec435a7", "metadata": {}, "source": [ "## 1. Install environment\n", "1. Follow [RETFound README](https://github.com/rmaphoh/RETFound) to install environment\n", "2. Restart this Jupyter Notebook\n", "3. Select Kernel retfound" ] }, { "cell_type": "code", "execution_count": null, "id": "7cbf5e93-6ca0-4401-88e6-64e39968e7cd", "metadata": {}, "outputs": [], "source": [ "import sys, torch\n", "from pathlib import Path\n", "import os\n", "\n", "PROJECT_ROOT = Path.cwd().resolve()\n", "\n", "if PROJECT_ROOT.name == 'examples': PROJECT_ROOT = PROJECT_ROOT.parent\n", "os.chdir(PROJECT_ROOT)\n", "\n", "print('Project root:', PROJECT_ROOT)\n", "print(\"sys.executable:\", sys.executable)\n", "print(\"torch version:\", torch.__version__)" ] }, { "cell_type": "markdown", "id": "ed67953f", "metadata": {}, "source": [ "## 2. Prepare MESSIDOR2 dataset\n", "1. Download from the [shared data pool](https://github.com/rmaphoh/RETFound/blob/main/BENCHMARK.md).\n", "2. Put the data folder under the project directory, e.g. \"RETFound/MESSIDOR2\"\n" ] }, { "cell_type": "markdown", "id": "357be2fa-a914-4d1f-8759-76b2b1c3f20f", "metadata": {}, "source": [ "## 3. Hyperparameter and path settings\n", "1. Can choose finetune or lp (linear probe)\n", "2. Model selection [info](https://github.com/rmaphoh/RETFound#:~:text=In%20train.sh%2C%20the%20model%20can%20be%20selected%20by%20changing%20the%20hyperparameters%20MODEL%2C%20MODEL_ARCH%2C%20FINETUNE%3A)" ] }, { "cell_type": "code", "execution_count": null, "id": "5f675843", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "ADAPTATION='finetune'\n", "MODEL='RETFound_dinov2'\n", "MODEL_ARCH='retfound_dinov2'\n", "FINETUNE='RETFound_dinov2_meh'\n", "DATASET='MESSIDOR2'\n", "NUM_CLASS=5\n", "DATA_PATH=PROJECT_ROOT/DATASET\n", "BATCH_SIZE=24\n", "EPOCHS=50\n", "INPUT_SIZE=224\n", "WORLD_SIZE=1\n", "TASK=f\"{MODEL_ARCH}_{DATASET}_{ADAPTATION}\"\n", "OUTPUT_DIR=PROJECT_ROOT/'output_dir'/TASK\n", "print('DATA_PATH:',DATA_PATH)\n", "print('TASK:',TASK)\n", "print('OUTPUT_DIR:',OUTPUT_DIR)" ] }, { "cell_type": "markdown", "id": "6ac04845", "metadata": {}, "source": [ "## 4. Fine-tuning and testing RETFound on MESSIDOR2" ] }, { "cell_type": "code", "execution_count": null, "id": "d23ff751", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import sys\n", "\n", "!{sys.executable} main_finetune.py \\\n", " --model {MODEL} \\\n", " --model_arch {MODEL_ARCH} \\\n", " --finetune {FINETUNE} \\\n", " --savemodel \\\n", " --global_pool \\\n", " --batch_size {BATCH_SIZE} \\\n", " --epochs {EPOCHS} \\\n", " --nb_classes {NUM_CLASS} \\\n", " --data_path {DATA_PATH} \\\n", " --input_size {INPUT_SIZE} \\\n", " --task {TASK} \\\n", " --adaptation {ADAPTATION}" ] }, { "cell_type": "markdown", "id": "84ce93ac", "metadata": {}, "source": [ "## 5. Evaluation-only" ] }, { "cell_type": "code", "execution_count": null, "id": "0af0f8a7", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import sys\n", "\n", "CKPT = OUTPUT_DIR / \"checkpoint-best.pth\"\n", "\n", "!{sys.executable} main_finetune.py \\\n", " --model {MODEL} \\\n", " --model_arch {MODEL_ARCH} \\\n", " --finetune {FINETUNE} \\\n", " --savemodel \\\n", " --global_pool \\\n", " --batch_size 128 \\\n", " --nb_classes {NUM_CLASS} \\\n", " --data_path {DATA_PATH} \\\n", " --input_size {INPUT_SIZE} \\\n", " --task {TASK} \\\n", " --adaptation {ADAPTATION} \\\n", " --eval \\\n", " --resume {CKPT}\n" ] }, { "cell_type": "code", "execution_count": null, "id": "02d2dce7-31c2-48e2-87ce-9223b74cf94e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "environment": { "kernel": "retfound", "name": "workbench-notebooks.m128", "type": "gcloud", "uri": "us-docker.pkg.dev/deeplearning-platform-release/gcr.io/workbench-notebooks:m128" }, "kernelspec": { "display_name": "retfound_jupyter (Local)", "language": "python", "name": "retfound" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 5 }