Computational Pathology and Whole Slide Image Analysis
Module: Healthcare AI | Difficulty: Advanced
Multiple Instance Learning (MIL)
where is the attention weight and is the bag-level representation.
Attention-Based MIL
WSI Processing Metrics
| Approach | AUC (Cancer) | AUC (Grade) | Speed | Memory |
|---|---|---|---|---|
| CNN + Global Average | 0.94 | 0.82 | Fast | Low |
| Attention MIL | 0.96 | 0.87 | Medium | Medium |
| TransMIL | 0.97 | 0.89 | Slow | High |
| CLAM | 0.96 | 0.88 | Medium | Medium |
import torch
import torch.nn as nn
import torch.nn.functional as F
class AttentionMIL(nn.Module):
def __init__(self, input_dim=512, hidden_dim=256, num_classes=2):
super().__init__()
self.feature_encoder = nn.Sequential(
nn.Linear(input_dim, hidden_dim), nn.ReLU(), nn.Dropout(0.25))
self.attention = nn.Sequential(
nn.Linear(hidden_dim, 128), nn.Tanh(), nn.Linear(128, 1))
self.classifier = nn.Linear(hidden_dim, num_classes)
def forward(self, bag):
features = self.feature_encoder(bag)
attn_scores = self.attention(features)
attn_weights = F.softmax(attn_scores, dim=0)
bag_representation = (attn_weights * features).sum(dim=0)
logits = self.classifier(bag_representation)
return logits, attn_weights.squeeze()
class SlideProcessor:
def __init__(self, model, patch_size=256, overlap=0):
self.model = model
self.patch_size = patch_size
self.overlap = overlap
def extract_patches(self, slide_array, tissue_mask=None):
h, w = slide_array.shape[:2]
patches = []
coords = []
for y in range(0, h - self.patch_size + 1,
self.patch_size - self.overlap):
for x in range(0, w - self.patch_size + 1,
self.patch_size - self.overlap):
patch = slide_array[y:y+self.patch_size, x:x+self.patch_size]
if tissue_mask is not None:
mask_patch = tissue_mask[y:y+self.patch_size,
x:x+self.patch_size]
if mask_patch.mean() < 0.1:
continue
patches.append(patch)
coords.append((x, y))
return patches, coords
model = AttentionMIL(input_dim=512, num_classes=2)
bag = torch.randn(100, 512)
logits, weights = model(bag)
print(f'Bag prediction: {torch.softmax(logits, dim=-1)}')
print(f'Attention weights shape: {weights.shape}')
Research Insight: Whole slide images are among the largest medical images (up to 100,000 x 100,000 pixels), requiring specialized processing pipelines. Multi-scale attention mechanisms that process patches at multiple magnifications achieve the best performance for cancer grading and subtyping.