SlimSAM<T>

Models & Types · AiDotNet.ComputerVision.Segmentation.Efficient

SlimSAM: Pruned and distilled SAM for efficient segmentation.

Inheritance: NeuralNetworkBase<T> → SegmentationModelBase<T> → PromptableSegmentationBase<T> → SlimSAM<T>

For Beginners

Efficient segment anything. Pruned SAM for resource-constrained deployment.

Common use cases:

  • Efficient segment anything
  • Pruned SAM for resource-constrained deployment
  • Fast interactive segmentation
  • Data-efficient SAM compression

How It Works

Technical Details:

  • Alternate slimming: prune + distill iteratively
  • Uses only 0.1% of SA-1B data for distillation
  • Embedding-disturbed pruning for ViT layers
  • Maintains SAM quality with fewer parameters

Reference: Chen et al., “SlimSAM: 0.1% Data Frees Slim Segment Anything Model”, arXiv 2023.

Example

// Create a SlimSAM model for efficient promptable segmentation
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.ThreeDimensional,
    taskType: NeuralNetworkTaskType.BinaryClassification,
    inputHeight: 1024, inputWidth: 1024, inputDepth: 3, outputSize: 1);
var model = new SlimSAM<double>(architecture);
// Every tunable is now set through the options object; these are the defaults:
var custom = new SlimSAM<double>(architecture,
    options: new SlimSAMOptions { NumClasses = 1, DropRate = 0 });
            
// Or load a pre-trained ONNX model for pruned SAM inference
var onnxModel = new SlimSAM<double>(architecture, "slimsam.onnx");

Constructors

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

Methods

Method Summary
List<PromptedSegmentationResult<T>> SegmentEverything()
ModelMetadata<T> GetModelMetadata() Collects metadata describing this model’s configuration.
PromptedSegmentationResult<T> SegmentFromBox(Tensor<T> box)
PromptedSegmentationResult<T> SegmentFromMask(Tensor<T> mask)
PromptedSegmentationResult<T> SegmentFromPoints(Tensor<T> points, Tensor<T> labels)
void SetImage(Tensor<T> image)