Class MobileSAM
Predictor using MobileSAM models. Call SetImage(Texture) to specify the image you want to generate masks for. After setting an image you can call Predict(float[], float[]) to get the masks. The results will be stored in the Result RenderTexture.
Implements
Inherited Members
Namespace: Doji.AI.Segmentation
Assembly: .dll
Syntax
public class MobileSAM : IDisposable
Constructors
MobileSAM(BackendType)
Initializes a new instance of a mask predictor and loads the models without waiting.
Declaration
public MobileSAM(BackendType backend = null)
Parameters
| Type | Name | Description |
|---|---|---|
| BackendType | backend |
Remarks
The models come from HF Hub (tools.doji.hf-hub). Loading without waiting works in the Editor and
for bundled models on Windows, macOS, Linux and iOS. On Android and the Web, or when the models are
downloaded at runtime, use CreateAsync(BackendType, IProgress<DownloadProgress>, CancellationToken).
MobileSAM(Model, Model, BackendType)
Initializes a new instance of a mask predictor with models that you loaded yourself.
Declaration
public MobileSAM(Model encoder, Model decoder, BackendType backend = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Model | encoder | The MobileSAM image encoder, without pixel scaling. |
| Model | decoder | The MobileSAM mask decoder. |
| BackendType | backend | The backend to run the models with. |
Properties
Backend
Which BackendType to run the model with.
Declaration
public BackendType Backend { get; set; }
Property Value
| Type | Description |
|---|---|
| BackendType |
Decoder
The mask decoder.
Declaration
public Model Decoder { get; }
Property Value
| Type | Description |
|---|---|
| Model |
Encoder
The image encoder, with the pixel scaling of SetImage(Texture) added.
Declaration
public Model Encoder { get; }
Property Value
| Type | Description |
|---|---|
| Model |
OutputFormat
Which format the predicted masks are returned as.
Declaration
public RenderTextureFormat OutputFormat { get; set; }
Property Value
| Type | Description |
|---|---|
| RenderTextureFormat |
Result
Declaration
public RenderTexture Result { get; }
Property Value
| Type | Description |
|---|---|
| RenderTexture |
Methods
CreateAsync(BackendType, IProgress<DownloadProgress>, CancellationToken)
Loads the models from HF Hub and creates a mask predictor. Works on every platform, and for bundled and downloaded models. Downloaded models are fetched on first use.
Declaration
public static Task<MobileSAM> CreateAsync(BackendType backend = null, IProgress<DownloadProgress> progress = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| BackendType | backend | The backend to run the models with. |
| IProgress<DownloadProgress> | progress | Reports download progress when the models are downloaded at runtime. |
| CancellationToken | cancellationToken | Cancels the download. |
Returns
| Type | Description |
|---|---|
| Task<MobileSAM> |
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Declaration
public void Dispose()
Predict(float[], float[])
Predict masks for the given input prompts, using the currently set image.
Declaration
public void Predict(float[] pointCoords, float[] pointLabels)
Parameters
| Type | Name | Description |
|---|---|---|
| float[] | pointCoords | A float array of point prompts to the model. Each point is given in pixel coordinates. |
| float[] | pointLabels | A length N array of labels for the point prompts. 1 indicates a foreground point and 0 indicates a background point. |
SetImage(Tensor, (int height, int width))
Declaration
public void SetImage(Tensor transformedImage, (int height, int width) origSize)
Parameters
| Type | Name | Description |
|---|---|---|
| Tensor | transformedImage | |
| (int height, int width) | origSize |
SetImage(Texture)
Specifies the image to be used for mask generation using the Predict(float[], float[]) method.
Declaration
public void SetImage(Texture image)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture | image |
Remarks
This encodes the input image and stores the calculated image embeddings. The image only needs to be encoded once. Afterwards, you can query as many masks as you want.