Microscopic-level AI analysis of OCT B-scans — real feature maps, neural journey visualization, Mamba state dynamics, and clinical-grade explainability.
Khamir Desai, Mayuri A. Mehta, Sree Saicharan Vadapalli · Sarvajanik College of Engineering and Technology, Surat, India
"The retina is the only part of the central nervous system directly visible to an ophthalmologist. It is also where preventable blindness begins — and where AI can intervene before the window closes."
ViT-B/16 captures global long-range dependencies. Modified MedMamba refines with local Conv + selective State-Space modeling.
Fig 1 — RetViM Architecture: OCT B-scan → ViT-B/16 (6 frozen + 6 trainable blocks) → 2 Modified MedMamba blocks (Conv ∥ SSM → MLP fusion) → Multi-scale pooling → 4-class softmax
Generated architecture visualization · 101.2M params · 12 ViT blocks (6 frozen) + 2 SS-Conv-SSM blocks · Multi-scale pooling fusion
One of the largest publicly available retinal OCT datasets. Curated by Daniel S. Kermany et al. at UC San Diego, validated by board-certified retinal specialists.
A sequential hybrid architecture that first extracts global context via a frozen + fine-tuned ViT, then refines with selective state-space modeling via MedMamba blocks.
OCT B-scan resized to 224×224, converted to RGB (3-channel), normalized with ImageNet statistics (μ=[0.485,0.456,0.406], σ=[0.229,0.224,0.225]) so pretrained weights are properly calibrated.
Image split into 196 non-overlapping 16×16 patches. Each patch linearly projected to 768-dim embedding. A learnable [CLS] token prepended. Positional embeddings added.
First 6 transformer encoder blocks kept frozen from ImageNet pretraining. These extract generalizable low-level features (edges, textures, spatial structure) without overfitting to the small medical dataset.
Last 6 transformer blocks fine-tuned end-to-end. Multi-head self-attention (12 heads, 64-dim/head) captures global inter-patch relationships — critical for detecting diffuse pathologies like DME.
Two sequential Modified MedMamba blocks refine the token sequence. Each block runs a parallel Conv branch (depthwise conv for local texture) and SSM branch (selective state-space scan for sequential dependencies), fused via MLP.
CLS token + mean-pooled patch tokens → dual-stream feature fusion → LayerNorm → 4-class linear head with softmax. Final prediction: argmax over [CNV, DME, DRUSEN, NORMAL].
| Model | Acc % | Prec % | Recall % | Spec % | F1 % | AUC-ROC | Kappa | Errors |
|---|---|---|---|---|---|---|---|---|
| ViT-Base (Baseline) | 99.38 | 99.38 | 99.38 | 99.79 | 99.39 | 1.0000 | 0.9917 | 6 |
| Hybrid ViT-MedMamba v1 | 99.07 | 99.10 | 99.07 | 99.69 | 99.07 | 0.9999 | 0.9876 | 9 |
| RetViM (Proposed) ★ | 99.90 | 99.90 | 99.90 | 99.97 | 99.90 | 1.0000 | 0.9986 | 1 |
| Class | Precision | Recall | Specificity | F1 | AUC | Support |
|---|---|---|---|---|---|---|
| CNV | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 242 |
| DME | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 242 |
| DRUSEN | 0.9959 | 1.0000 | 0.9986 | 0.9979 | 1.0000 | 242 |
| NORMAL | 1.0000 | 0.9959 | 1.0000 | 0.9979 | 1.0000 | 242 |
| Macro Avg | 0.9990 | 0.9990 | 0.9997 | 0.9990 | 1.0000 | 968 |
| Model | Accuracy % | F1 % | AUC-ROC | Epochs | Key Finding |
|---|---|---|---|---|---|
| ViT-Small | 98.97 | 98.97 | 0.9999 | 50 | Self-attention highly effective for OCT |
| Hybrid (4 blk) | 98.55 | 98.56 | 0.9998 | 80 | Over-regularization from 4 blocks |
| MedMamba | 97.11 | 97.11 | 0.9995 | 150 | Slow convergence, needs 3× epochs |
A black-box 99.9% accurate model is not enough for clinical use. RetViM is paired with six complementary explainability methods — each answering a different question about the model's decision.
A 99.9% accurate model that a clinician cannot interrogate is not a clinical tool — it is a liability. RetViM ships six independent explanations with every prediction.
Watch how a retinal OCT scan transforms through every layer of the hybrid ViT-Mamba pipeline. Row 1: smoothed feature heatmaps · Row 2: raw single-channel activations · Row 3: attention overlays (where the network looks). Compare Demo vs Real model weights side-by-side.
Every figure generated from the actual trained RetViM model on real Kermany OCT data.
RetViMNet · 384-dim · ViT-Small transfer + OCT fine-tune · 12 ViT + 4 Mamba blocks
ImprovedMedMamba · 768-dim · ViT-Base/16 + 2 MedMamba blocks · val_acc = 96.68%
Input → Patch Embed → ViT Blk 3/7/11 → Mamba Blk 0/1/2/3 · Each row = one disease class · ViT stages: cool tones · Mamba stages: warm inferno
Alternative feature evolution view · cleaner layout showing progression from raw pixel features to diagnostic representations
DME classification · attention focuses on INL/OPL cystoid spaces · Mamba blocks refine the central macular signal · attention progressively concentrates
PCA across all pipeline stages (Patch Embed → ViT Blk 3/11 → Mamba Blk 0/1/2/3) + t-SNE final layer + average final-layer activations per class · Classes become perfectly separable by Mamba stage
Row = disease class · Columns = ViT Blk 3, ViT Blk 11, Mamba 0, Mamba 2, Mamba 3 · Each uses class-specific color: CNV=red, DME=blue, Drusen=gold, Normal=green
Visual signatures of each disease — Original OCT → ViT early/late features → Mamba features → final GradCAM overlay · Each class has distinct network fingerprint
Top: ViT self-attention architecture (Q·Kᵀ/√d) + SS-Conv-SSM dual branch (h'=Ah+Bx, y=Ch+Dx) · Bottom rows: actual feature maps at Early ViT/Middle ViT/Late ViT/Early Mamba/Middle Mamba/Final Mamba
8-panel PCA: Patch Embed → ViT Blk 3/7/11 → Mamba Blk 0/1/2/3 · Patch Embed: overlapping butterfly shape → ViT Blk 11: classes begin separating → Mamba: tight well-separated clusters · Var explained increases Blk-11 67.7% → Mamba 70.1%
What the network sees: Original → Patch Embed → ViT Blk 7 → Mamba Blk 0 → Mamba Blk 3 · Color-coded by disease: CNV=red, DME=cyan, Drusen=gold, Normal=teal · Brighter = higher activation
8×8 grid · Rows = Patch Embed, ViT Blk 0/3/7/11 (blue), Mamba Blk 0/2/3 (red) · Columns = 8 channels · Each cell = synthetic input that maximally activates that channel · Early: color grids → Late: curved retinal structures
Feature lineage across patch-embed → vit-3 → vit-7 → vit-11 → mamba-1 → mamba-3 · Lines show which features are preserved/transformed · Blue = ViT self-attention · Orange = Mamba state-space
Enhancing features the network detects in real OCT images · Original → ViT Block 5 (frozen, mid-level) → ViT Block 11 (trainable, semantic) → Combined · Network amplifies retinal layer boundaries and pathological textures
If you use RetViM in your research, please cite the following. The paper is accepted/published in IEEE conference proceedings.