MobileSAM<T>
Models & Types · AiDotNet.ComputerVision.Segmentation.Efficient
MobileSAM: Faster Segment Anything with TinyViT encoder.
Inheritance: NeuralNetworkBase<T> → SegmentationModelBase<T> → PromptableSegmentationBase<T> → MobileSAM<T>
For Beginners
Mobile segment anything. On-device interactive segmentation.
Common use cases:
- Mobile segment anything
- On-device interactive segmentation
- Lightweight promptable segmentation
- Mobile AR applications
How It Works
Technical Details:
- TinyViT encoder distilled from SAM ViT-H
- Decoupled distillation: image encoder only
- Same mask decoder as original SAM
- 60x smaller encoder than SAM-ViT-H
Reference: Zhang et al., “Faster Segment Anything: Towards Lightweight SAM for Mobile Applications”, arXiv 2023.
Example
// Create a MobileSAM model for lightweight mobile segmentation
var architecture = new NeuralNetworkArchitecture<double>(
inputType: InputType.ThreeDimensional,
taskType: NeuralNetworkTaskType.BinaryClassification,
inputHeight: 1024, inputWidth: 1024, inputDepth: 3, outputSize: 1);
var model = new MobileSAM<double>(architecture);
// Every tunable is now set through the options object; these are the defaults:
var custom = new MobileSAM<double>(architecture,
options: new MobileSAMOptions { NumClasses = 1, DropRate = 0 });
Constructors
| Constructor | Summary |
|---|---|
MobileSAM(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, MobileSAMOptions options) |
Initializes MobileSAM in native (trainable) mode. |
MobileSAM(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, MobileSAMOptions options) |
Initializes MobileSAM 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) |