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
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
All results on the test set (968 images, 242 per class), completely untouched during training and validation.
| 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 |
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