pub struct Sam { /* private fields */ }Expand description
Full SAM model — owns the compiled image encoder + all decoder
weights. Stateless wrt prompts: every call to Sam::forward
runs the cached encoder + a fresh decoder forward.
Implementations§
Source§impl Sam
impl Sam
Sourcepub fn from_safetensors(
weights_path: &str,
cfg: SamConfig,
) -> Result<Sam, Error>
pub fn from_safetensors( weights_path: &str, cfg: SamConfig, ) -> Result<Sam, Error>
Load SAM ViT-B (or L/H — pass the matching config) from a
safetensors checkpoint, compiling the image encoder for the
CPU backend. For GPU/Metal/MLX, use
Sam::from_safetensors_on.
Sourcepub fn from_safetensors_on(
weights_path: &str,
cfg: SamConfig,
device: Device,
) -> Result<Sam, Error>
pub fn from_safetensors_on( weights_path: &str, cfg: SamConfig, device: Device, ) -> Result<Sam, Error>
Same as Sam::from_safetensors but compiles the image
encoder for the given device. Requires the matching backend
feature on rlx-models:
| feature | backend |
|---|---|
metal | Device::Metal |
mlx | Device::Mlx |
gpu | Device::Gpu |
cuda | Device::Cuda |
rocm | Device::Rocm |
tpu | Device::Tpu |
Sourcepub fn encode_image(&mut self, image_nchw: &[f32]) -> Vec<f32>
pub fn encode_image(&mut self, image_nchw: &[f32]) -> Vec<f32>
Encode an image into the [256, 64, 64] image embedding.
image_nchw: pre-padded [3, 1024, 1024] NCHW f32 tensor
(see super::preprocess::preprocess_image).
Sourcepub fn predict_masks(
&mut self,
image_embeddings: &[f32],
points: Option<(&[f32], &[f32])>,
boxes: Option<&[f32]>,
masks: Option<&[f32]>,
multimask_output: bool,
) -> Result<MaskPrediction, Error>
pub fn predict_masks( &mut self, image_embeddings: &[f32], points: Option<(&[f32], &[f32])>, boxes: Option<&[f32]>, masks: Option<&[f32]>, multimask_output: bool, ) -> Result<MaskPrediction, Error>
Run the prompt encoder + mask decoder on a pre-encoded image.
Sourcepub fn forward(
&mut self,
rgb: &[u8],
h_in: usize,
w_in: usize,
points: Option<(&[f32], &[f32])>,
boxes: Option<&[f32]>,
masks: Option<&[f32]>,
multimask_output: bool,
) -> Result<(MaskPrediction, (usize, usize)), Error>
pub fn forward( &mut self, rgb: &[u8], h_in: usize, w_in: usize, points: Option<(&[f32], &[f32])>, boxes: Option<&[f32]>, masks: Option<&[f32]>, multimask_output: bool, ) -> Result<(MaskPrediction, (usize, usize)), Error>
End-to-end forward: image bytes → masks. rgb is HWC u8.
pub fn config(&self) -> &SamConfig
Sourcepub fn mask_side(&self) -> usize
pub fn mask_side(&self) -> usize
Spatial side length of the predicted mask logits (= 4 · hw = 256 for ViT-B at 1024 input).
Sourcepub fn input_image_size(&self) -> usize
pub fn input_image_size(&self) -> usize
Image side that the model operates on internally.
Auto Trait Implementations§
impl !RefUnwindSafe for Sam
impl !Sync for Sam
impl !UnwindSafe for Sam
impl Freeze for Sam
impl Send for Sam
impl Unpin for Sam
impl UnsafeUnpin for Sam
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more