Stay organized with collections Save and categorize content based on your preferences.

tflite::FlatBufferModel

#include <model_builder.h>

An RAII object that represents a read-only tflite model, copied from disk, or mmapped.

Summary

This uses flatbuffers as the serialization format.

NOTE: The current API requires that a FlatBufferModel instance be kept alive by the client as long as it is in use by any dependent Interpreter instances. As the FlatBufferModel instance is effectively immutable after creation, the client may safely use a single model with multiple dependent Interpreter instances, even across multiple threads (though note that each Interpreter instance is not thread-safe).


using namespace tflite;
StderrReporter error_reporter;
auto model = FlatBufferModel::BuildFromFile("interesting_model.tflite",
                                            &error_reporter);
MyOpResolver resolver;  // You need to subclass OpResolver to provide
                        // implementations.
InterpreterBuilder builder(*model, resolver);
std::unique_ptr interpreter;
if(builder(&interpreter) == kTfLiteOk) {
  .. run model inference with interpreter
}

OpResolver must be defined to provide your kernel implementations to the interpreter. This is environment specific and may consist of just the builtin ops, or some custom operators you defined to extend tflite.

Constructors and Destructors

FlatBufferModel(const FlatBufferModel &)
~FlatBufferModel()

Public static functions

BuildFromAllocation(std::unique_ptr< Allocation > allocation, ErrorReporter *error_reporter)
std::unique_ptr< FlatBufferModel >
Builds a model directly from an allocation.
BuildFromBuffer(const char *caller_owned_buffer, size_t buffer_size, ErrorReporter *error_reporter)
std::unique_ptr< FlatBufferModel >
Builds a model based on a pre-loaded flatbuffer.
BuildFromFile(const char *filename, ErrorReporter *error_reporter)
std::unique_ptr< FlatBufferModel >
Builds a model based on a file.
BuildFromModel(const tflite::Model *caller_owned_model_spec, ErrorReporter *error_reporter)
std::unique_ptr< FlatBufferModel >
Builds a model directly from a flatbuffer pointer Caller retains ownership of the buffer and should keep it alive until the returned object is destroyed.
VerifyAndBuildFromAllocation(std::unique_ptr< Allocation > allocation, TfLiteVerifier *extra_verifier, ErrorReporter *error_reporter)
std::unique_ptr< FlatBufferModel >
Verifies whether the content of the allocation is legit, then builds a model based on the provided allocation.
VerifyAndBuildFromBuffer(const char *caller_owned_buffer, size_t buffer_size, TfLiteVerifier *extra_verifier, ErrorReporter *error_reporter)
std::unique_ptr< FlatBufferModel >
Verifies whether the content of the buffer is legit, then builds a model based on the pre-loaded flatbuffer.
VerifyAndBuildFromFile(const char *filename, TfLiteVerifier *extra_verifier, ErrorReporter *error_reporter)
std::unique_ptr< FlatBufferModel >
Verifies whether the content of the file is legit, then builds a model based on the file.

Public functions

CheckModelIdentifier() const
bool
Returns true if the model identifier is correct (otherwise false and reports an error).
GetMinimumRuntime() const
std::string
GetModel() const
const tflite::Model *
ReadAllMetadata() const
std::map< std::string, std::string >
allocation() const
const Allocation *
error_reporter() const
initialized() const
bool
operator->() const
const tflite::Model *
operator=(const FlatBufferModel &)=delete

Public static functions

BuildFromAllocation

std::unique_ptr< FlatBufferModel > BuildFromAllocation(
  std::unique_ptr< Allocation > allocation,
  ErrorReporter *error_reporter
)

Builds a model directly from an allocation.

Ownership of the allocation is passed to the model, but the caller retains ownership of error_reporter and must ensure its lifetime is longer than the FlatBufferModel instance. Returns a nullptr in case of failure (e.g., the allocation is invalid).

BuildFromBuffer

std::unique_ptr< FlatBufferModel > BuildFromBuffer(
  const char *caller_owned_buffer,
  size_t buffer_size,
  ErrorReporter *error_reporter
)

Builds a model based on a pre-loaded flatbuffer.

Caller retains ownership of the buffer and should keep it alive until the returned object is destroyed. Caller also retains ownership of error_reporter and must ensure its lifetime is longer than the FlatBufferModel instance. Returns a nullptr in case of failure. NOTE: this does NOT validate the buffer so it should NOT be called on invalid/untrusted input. Use VerifyAndBuildFromBuffer in that case

BuildFromFile

std::unique_ptr< FlatBufferModel > BuildFromFile(
  const char *filename,
  ErrorReporter *error_reporter
)

Builds a model based on a file.

Caller retains ownership of error_reporter and must ensure its lifetime is longer than the FlatBufferModel instance. Returns a nullptr in case of failure.

BuildFromModel

std::unique_ptr< FlatBufferModel > BuildFromModel(
  const tflite::Model *caller_owned_model_spec,
  ErrorReporter *error_reporter
)

Builds a model directly from a flatbuffer pointer Caller retains ownership of the buffer and should keep it alive until the returned object is destroyed.

Caller retains ownership of error_reporter and must ensure its lifetime is longer than the FlatBufferModel instance. Returns a nullptr in case of failure.

VerifyAndBuildFromAllocation

std::unique_ptr< FlatBufferModel > VerifyAndBuildFromAllocation(
  std::unique_ptr< Allocation > allocation,
  TfLiteVerifier *extra_verifier,
  ErrorReporter *error_reporter
)

Verifies whether the content of the allocation is legit, then builds a model based on the provided allocation.

The extra_verifier argument is an additional optional verifier for the buffer. By default, we always check with tflite::VerifyModelBuffer. If extra_verifier is supplied, the buffer is checked against the extra_verifier after the check against tflite::VerifyModelBuilder. Ownership of the allocation is passed to the model, but the caller retains ownership of error_reporter and must ensure its lifetime is longer than the FlatBufferModel instance. Returns a nullptr in case of failure.

VerifyAndBuildFromBuffer

std::unique_ptr< FlatBufferModel > VerifyAndBuildFromBuffer(
  const char *caller_owned_buffer,
  size_t buffer_size,
  TfLiteVerifier *extra_verifier,
  ErrorReporter *error_reporter
)

Verifies whether the content of the buffer is legit, then builds a model based on the pre-loaded flatbuffer.

The extra_verifier argument is an additional optional verifier for the buffer. By default, we always check with tflite::VerifyModelBuffer. If extra_verifier is supplied, the buffer is checked against the extra_verifier after the check against tflite::VerifyModelBuilder. The caller retains ownership of the buffer and should keep it alive until the returned object is destroyed. Caller retains ownership of error_reporter and must ensure its lifetime is longer than the FlatBufferModel instance. Returns a nullptr in case of failure.

VerifyAndBuildFromFile

std::unique_ptr< FlatBufferModel > VerifyAndBuildFromFile(
  const char *filename,
  TfLiteVerifier *extra_verifier,
  ErrorReporter *error_reporter
)

Verifies whether the content of the file is legit, then builds a model based on the file.

The extra_verifier argument is an additional optional verifier for the file contents. By default, we always check with tflite::VerifyModelBuffer. If extra_verifier is supplied, the file contents is also checked against the extra_verifier after the check against tflite::VerifyModelBuilder. Caller retains ownership of error_reporter and must ensure its lifetime is longer than the FlatBufferModel instance. Returns a nullptr in case of failure.

Public functions

CheckModelIdentifier

bool CheckModelIdentifier() const 

Returns true if the model identifier is correct (otherwise false and reports an error).

FlatBufferModel

 FlatBufferModel(
  const FlatBufferModel &
)=delete

GetMinimumRuntime

std::string GetMinimumRuntime() const 

GetModel

const tflite::Model * GetModel() const 

ReadAllMetadata

std::map< std::string, std::string > ReadAllMetadata() const 

allocation

const Allocation * allocation() const 

error_reporter

ErrorReporter * error_reporter() const 

initialized

bool initialized() const 

operator->

const tflite::Model * operator->() const 

operator=

FlatBufferModel & operator=(
  const FlatBufferModel &
)=delete

~FlatBufferModel

 ~FlatBufferModel()