MedSAM2Options

Options & Configuration · AiDotNet.ComputerVision.Segmentation.Medical

Configuration options for MedSAM2, MedSAM2 (Ma et al. 2025): SAM2.1-Tiny at 512 pixels fine-tuned for 3D medical images and videos.

Inheritance: ModelOptions → NeuralNetworkOptions → ModelHyperparameterOptions → SegmentationModelOptions → MedSAM2Options

For Beginners

Leave these at their defaults; they reproduce the published model.

How It Works

The defaults are the paper’s: SAM2.1-Tiny (Hiera blocks 1-2-7-2, global attention at blocks 5, 7 and 9) at 512 pixels, AdamW (0.9, 0.999) with weight decay 0.01, learning rates 3e-5 for the image encoder and 5e-5 for the rest, and the focal and dice losses at 20:1. The official config fills what the paper leaves out: a cosine schedule to a tenth with no warmup, layer-wise decay 0.9, gradient clipping at 0.1, the IoU and occlusion losses at 1, and no weight decay on biases and LayerNorms. The capacity settings default to SAM2.1-Tiny, so the model can load the released checkpoint.

Constructors

Constructor Summary
MedSAM2Options() Initializes a new instance with default values.
MedSAM2Options(MedSAM2Options other) Initializes a new instance by copying from another instance.

Properties

Property Summary
double FocalAlpha { get; set; } Focal loss’s α (0.25, the RetinaNet value SAM uses).
double FocalGamma { get; set; } Focal loss’s γ (2, the RetinaNet value SAM uses).
double ImageEncoderLearningRate { get; set; } The image encoder’s learning rate (MedSAM2 paper: 3e-5).
double IouLossWeight { get; set; } The ℓ1 IoU-loss weight (paper: 1).
double LearningRate { get; set; } The learning rate of everything but the image encoder (MedSAM2 paper: 5e-5).
double MaskDiceWeight { get; set; } The dice-loss weight (paper: 1).
double MaskFocalWeight { get; set; } The focal-loss weight (paper: 20).
double MaxGradientNorm { get; set; } The gradient-norm clip (paper: 0.1).
double OcclusionLossWeight { get; set; } The occlusion cross-entropy weight (paper: 1).
double WeightDecay { get; set; } AdamW’s decoupled weight decay (MedSAM2 paper: 0.01).
double? DropPath { get; set; } The largest stochastic-depth rate of the Hiera blocks (the MedSAM2 config uses none).
double? LayerDecay { get; set; } The per-block layer-wise decay of the Hiera learning rates (the MedSAM2 config’s 0.9).
int CorrectionPoints { get; set; } Corrective clicks per corrected training frame (paper: 7).
int MemoryBankSize { get; set; } The memory bank: the prompted frames and up to MemoryBankSize − 1 recent frames (default: 7).
int TrainingSteps { get; set; } The training steps the cosine schedule spans (MedSAM2 trains 70 epochs: set it to 70 × the steps per epoch of your data).
int WarmupSteps { get; set; } The linear warmup steps (the MedSAM2 config has none).
int? DecoderHeadCount { get; set; } The mask decoder’s attention heads (null: 8).
int? HieraEmbeddingDimension { get; set; } The Hiera stem width (null: SAM2.1-Tiny’s 96); it doubles per stage.
int? HieraInitialHeadCount { get; set; } The first stage’s attention heads (null: SAM2.1-Tiny’s 1); they double per stage.
int? ImageSize { get; set; } The square size frames are resized to for the image encoder (null: the architecture’s larger side; the MedSAM2 uses 512).
int? MaskDecoderMlpDimension { get; set; } The mask decoder’s MLP width (null: 2048).
int? MemoryAttentionFeedForwardDimension { get; set; } The memory attention’s feed-forward width (null: 2048).
int? MemoryAttentionLayerCount { get; set; } The memory attention layers (null: 4).
int? MemoryDimension { get; set; } The memory width (null: 64).
int? MemoryFuserDimension { get; set; } The memory fuser’s hidden width (null: 1024).
int? ModelDimension { get; set; } The FPN, prompt and mask-decoder width (null: 256).
int[] HieraGlobalAttentionBlockIndexes { get; set; } The blocks (0-based over all stages) with global attention (null: the size’s).
int[] HieraStageDepths { get; set; } The Hiera blocks per stage (four values; null: the size’s).
int[] HieraWindowSizes { get; set; } The windowed-attention size per stage (four values; null: the size’s).

Methods

Method Summary
void Validate() Throws if a setting is out of range.