Skip to main content

Sam

Struct Sam 

Source
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

Source

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.

Source

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:

featurebackend
metalDevice::Metal
mlxDevice::Mlx
gpuDevice::Gpu
cudaDevice::Cuda
rocmDevice::Rocm
tpuDevice::Tpu
Source

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).

Source

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.

Source

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.

Source

pub fn config(&self) -> &SamConfig

Source

pub fn mask_side(&self) -> usize

Spatial side length of the predicted mask logits (= 4 · hw = 256 for ViT-B at 1024 input).

Source

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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