pub struct Sam2 { /* private fields */ }Expand description
Full SAM 2 model — owns the compiled image encoder + every host-side weight bundle. The encoder result is recomputed per call (no encoder-caching here; layer above can wrap if needed).
Implementations§
Source§impl Sam2
impl Sam2
Sourcepub fn from_safetensors(
weights_path: &str,
cfg: Sam2Config,
) -> Result<Sam2, Error>
pub fn from_safetensors( weights_path: &str, cfg: Sam2Config, ) -> Result<Sam2, Error>
Load every SAM 2 component from a safetensors checkpoint and
compile the image encoder for the CPU backend. For GPU/Metal,
see Sam2::from_safetensors_on.
Sourcepub fn from_safetensors_on(
weights_path: &str,
cfg: Sam2Config,
device: Device,
) -> Result<Sam2, Error>
pub fn from_safetensors_on( weights_path: &str, cfg: Sam2Config, device: Device, ) -> Result<Sam2, Error>
Same as Sam2::from_safetensors but compiles the image
encoder for the given backend. The cross-backend feature flags
match SAM v1’s rlx_sam::Sam::from_safetensors_on.
Sourcepub fn compile_profile(&self) -> &CompileProfile
pub fn compile_profile(&self) -> &CompileProfile
Tier-1 compile profile (sam.rlx.toml next to weights when present).
pub fn config(&self) -> &Sam2Config
Sourcepub fn predict_image(
&mut self,
image_u8: &[u8],
h_in: usize,
w_in: usize,
points: Option<(&[f32], &[f32])>,
boxes: Option<&[f32]>,
mask_input: Option<&[f32]>,
multimask_output: bool,
) -> Result<Sam2ImagePrediction, Error>
pub fn predict_image( &mut self, image_u8: &[u8], h_in: usize, w_in: usize, points: Option<(&[f32], &[f32])>, boxes: Option<&[f32]>, mask_input: Option<&[f32]>, multimask_output: bool, ) -> Result<Sam2ImagePrediction, Error>
Image-segmentation API.
image_u8: row-major RGB h_in × w_in × 3 u8.
points: optional (coords [N,2], labels [N]) — coords in
input-image pixels (0..max(h_in, w_in)), labels per
prompt_encoder_forward.
boxes: optional [M, 4] boxes (x0, y0, x1, y1) in input
pixels.
mask_input: optional [1, 256, 256] low-res mask logits.
multimask_output: true → 3 masks; false → 1 (with optional
dynamic-stability fallback).
Returns (mask_logits, iou_pred, num_masks, h_out, w_out)
where (h_out, w_out) = (4·SAM2_PROMPT_GRID, 4·SAM2_PROMPT_GRID)
= 256×256 — caller resizes to the original image resolution.
Sourcepub fn predict_video_frame(
&mut self,
state: &mut Sam2VideoState,
image_u8: &[u8],
h_in: usize,
w_in: usize,
points: Option<(&[f32], &[f32])>,
boxes: Option<&[f32]>,
mask_input: Option<&[f32]>,
multimask_output: bool,
) -> Result<Sam2ImagePrediction, Error>
pub fn predict_video_frame( &mut self, state: &mut Sam2VideoState, image_u8: &[u8], h_in: usize, w_in: usize, points: Option<(&[f32], &[f32])>, boxes: Option<&[f32]>, mask_input: Option<&[f32]>, multimask_output: bool, ) -> Result<Sam2ImagePrediction, Error>
Per-frame video API. Wraps Sam2::predict_image with the
memory-attention path (cross-attend the current frame’s stride-32
features to the bank) and the memory-encoder path (encode the
chosen mask + features into the bank).
Mirrors SAM2VideoPredictor.add_new_points_or_box +
propagate_in_video semantics: when state is empty, this acts
as image-predict; otherwise it conditions on stored frames.
Auto Trait Implementations§
impl Freeze for Sam2
impl !RefUnwindSafe for Sam2
impl Send for Sam2
impl !Sync for Sam2
impl Unpin for Sam2
impl UnsafeUnpin for Sam2
impl !UnwindSafe for Sam2
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
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