Back to Feed
Computer Vision / Training & Fine-Tuning

Optimizing MedSAM Fine-Tuning for Robustness

Original: When Adaptation Hurts: Connecting Representational Drift to OOD Failures in MedSAM Fine-Tuning

Listen to the summary

Uses a voice available on your device

Audio options
On this page 5 sections
Related concepts 4 concepts

Key Takeaways

  • Full fine-tuning provides the highest performance and robust outcomes for data outside the training domain.
  • Encoder only LoRA functions as the most effective parameter efficient strategy for balancing performance and compute requirements.
  • Applying random 0 to 100 pixel jitter to bounding box prompts during training significantly improves model robustness.
  • Performance degradation on far out of distribution tasks is directly linked to drift in the decoder and output representations rather than the encoder.

Summary & Methodology Analysis

The researchers evaluated six fine-tuning strategies for MedSAM, a foundation model for medical image segmentation, to understand how architectural choices impact robustness under domain shifts. They measured internal representation drift by comparing adapted models against the original zero shot MedSAM using Centered Kernel Alignment (CKA), a technique that calculates the similarity between hidden layer representations. This analysis revealed that degradation in far out of distribution performance is strongly tied to drift occurring specifically within the decoder and output representations, while encoder similarity alone is insufficient to guarantee robustness.

Interactive System Flowchart

Click diagram to expand and zoom

Illustrative Implementation

A short sketch of the paper's core idea, not the authors' own code.

# Illustrative sketch (not from the paper)
import torch
from torch import nn
from torch.utils.data import DataLoader

# Placeholder MedSAM components
class MedSAMEncoder(nn.Module):
    def __init__(self): super().__init__()
    def forward(self, x): return x  # identity stub

class MedSAMDecoder(nn.Module):
    def __init__(self): super().__init__()
    def forward(self, feats, prompt): return torch.sigmoid(feats)  # stub mask

# Simple LoRA wrapper (parameter‑efficient adaptation)
class LoRA(nn.Module):
    def __init__(self, module, r=4):
        super().__init__()
        self.module = module
        self.lora_A = nn.Linear(module.in_features, r, bias=False)
        self.lora_B = nn.Linear(r, module.out_features, bias=False)
    def forward(self, x):
        return self.module(x) + self.lora_B(self.lora_A(x))

# Linear CKA (centered kernel alignment) for two activation tensors
def linear_cka(X, Y):
    X = X - X.mean(0, keepdim=True)
    Y = Y - Y.mean(0, keepdim=True)
    dot_XY = (X.t() @ Y).norm() ** 2
    dot_XX = (X.t() @ X).norm() ** 2
    dot_YY = (Y.t() @ Y).norm() ** 2
    return dot_XY / (dot_XX.sqrt() * dot_YY.sqrt() + 1e-8)

# Mock training step with bounding‑box jitter
def jitter_box(box, max_pix):
    jitter = torch.randint(-max_pix, max_pix+1, box.shape)
    return box + jitter

# Main workflow (illustrative only)
encoder = MedSAMEncoder()
decoder = MedSAMDecoder()
# Example: encoder‑only LoRA adaptation
encoder = LoRA(encoder)
optimizer = torch.optim.Adam(list(encoder.parameters()) + list(decoder.parameters()), lr=1e-4)

for epoch in range(2):  # short demo
    for imgs, boxes, masks in DataLoader([]):  # placeholder loader
        noisy_boxes = jitter_box(boxes, max_pix=100)  # random 0‑100 pixel jitter
        feats = encoder(imgs)
        preds = decoder(feats, noisy_boxes)
        loss = nn.BCELoss()(preds, masks)
        optimizer.zero_grad(); loss.backward(); optimizer.step()

# Evaluation across perturbations & CKA drift measurement
perturb_levels = [0, 20, 50, 100, 200]
for p in perturb_levels:
    # compute representation similarity to zero‑shot model (stub tensors)
    rep_adapt = encoder(torch.randn(1,3,224,224))
    rep_zero = MedSAMEncoder()(torch.randn(1,3,224,224))
    similarity = linear_cka(rep_adapt.view(1,-1), rep_zero.view(1,-1))
    print(f"Perturb {p} px – CKA similarity: {similarity.item():.3f}")

Cross-Examination & FAQs

A deeper dive clarifying mechanics, constraints, and baseline evaluations.

Q1. What is the primary goal of this research?

The research investigates how different fine-tuning strategies for the MedSAM model affect its performance and robustness when exposed to noisy inputs and new medical imaging domains.

Q2. Does fine-tuning always improve model performance?

While fine-tuning is necessary for adaptation, the paper highlights that certain approaches cause representational drift, which can hurt performance on out of distribution data.

Q3. What is the main finding regarding model robustness?

The authors discovered that random 0 to 100 pixel jitter on prompts during training leads to more robust and higher performing models.

Q4. Which fine-tuning strategy is recommended for efficiency?

Encoder only LoRA is identified as the strongest parameter efficient strategy, outperforming other methods like standard LoRA or visual prompt tuning under far out of distribution shifts.

Q5. What role does CKA play in this study?

Centered Kernel Alignment is used to quantify representational drift across model layers, showing that decoder drift is the primary culprit for performance drops in far out of distribution settings.

Q6. What datasets were used to test domain shift?

The researchers used ISIC 2018 for training, PH2 as a close out of distribution dataset, and BUSI breast ultrasound and CBIS-DDSM mammography as far out of distribution datasets.

Q7. Why is it difficult to compare performance across different datasets?

A fixed jitter value does not represent the same level of challenge across different datasets, making out of distribution evaluation substantially more demanding than in domain evaluation.

Q8. What are the limitations of the current study?

Training was limited to dermoscopy, meaning results regarding combined modality and target structure shifts should be interpreted with caution.

Q9. Are there specific performance numbers or latency figures mentioned?

The paper does not provide specific latency or execution time figures, focusing instead on method rankings and robustness performance metrics.

Flag an issue

What is wrong with this summary?

What is wrong?