TensorFlow 2.0 Beta is available Learn more

tflite::FlatBufferModel

#include <model.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.


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

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.
VerifyAndBuildFromBuffer(const char *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
string
GetModel() const
const tflite::Model *
allocation() const
const Allocation *
error_reporter() const
initialized() const
bool
operator->() const
const tflite::Model *
operator=(const FlatBufferModel &)=delete

Public static functions

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.

VerifyAndBuildFromBuffer

std::unique_ptr< FlatBufferModel > VerifyAndBuildFromBuffer(
  const char *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

string GetMinimumRuntime() const 

GetModel

const tflite::Model * GetModel() 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()