Struct Interpreter

Source
pub struct Interpreter<'a> { /* private fields */ }
Expand description

A TensorFlow Lite interpreter that performs inference from a given model.

  • Note: Interpreter instances are not thread-safe.

Implementations§

Source§

impl<'a> Interpreter<'a>

Source

pub fn new( model: &'a Model<'a>, options: Option<Options>, ) -> Result<Interpreter<'a>>

Creates new Interpreter

§Arguments
§Examples
use tflitec::model::Model;
use tflitec::interpreter::Interpreter;
let model = Model::new("tests/add.bin")?;
let interpreter = Interpreter::new(&model, None)?;
§Errors

Returns error if TensorFlow Lite C fails internally.

Source

pub fn input_tensor_count(&self) -> usize

Returns the total number of input Tensors associated with the model.

Source

pub fn output_tensor_count(&self) -> usize

Returns the total number of output Tensors associated with the model.

Source

pub fn invoke(&self) -> Result<()>

Invokes the interpreter to perform inference from the loaded graph.

§Errors

Returns error if TensorFlow Lite C fails to invoke.

Source

pub fn input(&self, index: usize) -> Result<Tensor<'_>>

Returns the input Tensor at the given index.

§Arguments
  • index: The index for the input Tensor.
§Errors

Returns error if Interpreter::allocate_tensors() was not called before calling this or given index is not a valid input tensor index in [0, Interpreter::input_tensor_count()).

Source

pub fn output(&self, index: usize) -> Result<Tensor<'_>>

Returns the output Tensor at the given index.

§Arguments
  • index: The index for the output Tensor.
§Errors

Returns error if given index is not a valid output tensor index in [0, Interpreter::output_tensor_count()). And, it may return error unless the output tensor has been both sized and allocated. In general, best practice is to call this after calling Interpreter::invoke().

Source

pub fn resize_input(&self, index: usize, shape: Shape) -> Result<()>

Resizes the input Tensor at the given index to the specified Shape.

  • Note: After resizing an input tensor, the client must explicitly call Interpreter::allocate_tensors() before attempting to access the resized tensor data or invoking the interpreter to perform inference.
§Arguments
  • index: The index for the input Tensor.
  • shape: The shape to resize the input Tensor to.
§Errors

Returns error if given index is not a valid input tensor index in [0, Interpreter::input_tensor_count()) or TensorFlow Lite C fails internally.

Source

pub fn allocate_tensors(&self) -> Result<()>

Allocates memory for all input Tensors and dependent tensors based on their Shapes.

  • Note: This is a relatively expensive operation and should only be called after creating the interpreter and resizing any input tensors.
§Error

Returns error if TensorFlow Lite C fails to allocate memory for the input tensors.

Source

pub fn copy<T>(&self, data: &[T], index: usize) -> Result<()>

Copies the given data to the input Tensor at the given index.

§Arguments
  • data: The data to be copied to the input Tensor’s data buffer.
  • index: The index for the input Tensor.
§Errors

Returns error if byte count of the data does not match the buffer size of the input tensor or the given index is not a valid input tensor index in [0, Interpreter::input_tensor_count()) or TensorFlow Lite C fails internally.

Source

pub fn options(&self) -> Option<&Options>

Returns optional reference of Options.

Trait Implementations§

Source§

impl Debug for Interpreter<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Interpreter<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Interpreter<'_>

Auto Trait Implementations§

§

impl<'a> Freeze for Interpreter<'a>

§

impl<'a> RefUnwindSafe for Interpreter<'a>

§

impl<'a> !Sync for Interpreter<'a>

§

impl<'a> Unpin for Interpreter<'a>

§

impl<'a> UnwindSafe for Interpreter<'a>

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