Skip to main content

Sam2

Struct Sam2 

Source
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

Source

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.

Source

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.

Source

pub fn compile_profile(&self) -> &CompileProfile

Tier-1 compile profile (sam.rlx.toml next to weights when present).

Source

pub fn config(&self) -> &Sam2Config

Source

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.

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V