MedSAM<T>

Models & Types · AiDotNet.ComputerVision.Segmentation.Medical

MedSAM: Segment Anything in Medical Images.

Inheritance: NeuralNetworkBase<T> → SegmentationModelBase<T> → MedicalSegmentationBase<T> → MedSAM<T>

For Beginners

Universal medical image segmentation. CT, MRI, ultrasound, X-ray, endoscopy segmentation.

Common use cases:

  • Universal medical image segmentation
  • CT, MRI, ultrasound, X-ray, endoscopy segmentation
  • Box-prompted medical segmentation
  • Clinical decision support

How It Works

Technical Details:

  • SAM architecture fine-tuned on 1.5M medical image-mask pairs
  • ViT-Base image encoder + prompt encoder + mask decoder
  • Supports bounding box prompts for medical ROI
  • Covers 10+ medical imaging modalities

Reference: Ma et al., “Segment Anything in Medical Images”, Nature Communications 2024.

Example

// Create a MedSAM model for universal medical image segmentation
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.ThreeDimensional,
    taskType: NeuralNetworkTaskType.BinaryClassification,
    inputHeight: 1024, inputWidth: 1024, inputDepth: 3, outputSize: 1);
var model = new MedSAM<double>(architecture);
// Every tunable is now set through the options object; these are the defaults:
var custom = new MedSAM<double>(architecture,
    options: new MedSAMOptions { NumClasses = 1, DropRate = 0, ModelSize = MedSAMModelSize.ViTBase });
            
// Or load a pre-trained ONNX model for box-prompted medical segmentation
var onnxModel = new MedSAM<double>(architecture, "medsam.onnx");

Constructors

Constructor Summary
MedSAM(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, MedSAMOptions options) Initializes MedSAM in native (trainable) mode.
MedSAM(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, MedSAMOptions options) Initializes MedSAM in ONNX (inference-only) mode.

Properties

Property Summary
bool Supports3D { get; } MedSAM is a 2D slice model; it does not process volumes natively.

Methods

Method Summary
MedicalSegmentationResult<T> SegmentFewShot(Tensor<T> queryImage, Tensor<T> supportImages, Tensor<T> supportMasks) Segments a query image, ignoring the support set.
MedicalSegmentationResult<T> SegmentSlice(Tensor<T> slice)
MedicalSegmentationResult<T> SegmentVolume(Tensor<T> volume)
ModelMetadata<T> GetModelMetadata() Collects metadata describing this model’s configuration.