public final class Interpreter extends Object implements AutoCloseable
A Interpreter encapsulates a pre-trained TensorFlow Lite model, in which operations
are executed for model inference.
For example, if a model takes only one input and returns only one output:
try (Interpreter interpreter = new Interpreter(file_of_a_tensorflowlite_model)) {
interpreter.run(input, output);
}
If a model takes multiple inputs or outputs:
Object[] inputs = {input0, input1, ...};
Map<Integer, Object> map_of_indices_to_outputs = new HashMap<>();
float[][][] ith_output = new float[3][2][4];
map_of_indices_to_outputs.put(i, ith_output);
try (Interpreter interpreter = new Interpreter(file_of_a_tensorflowlite_model)) {
interpreter.runForMultipleInputsOutputs(inputs, map_of_indices_to_outputs);
}
Orders of inputs and outputs are determined when converting TensorFlow model to TensorFlowLite model with Toco.
WARNING:Instances of a Interpreter is not thread-safe. A Interpreter owns resources that must be explicitly freed by invoking close()
| Modifier and Type | Class and Description |
|---|---|
static class |
Interpreter.Options
An options class for controlling runtime interpreter behavior.
|
| Constructor and Description |
|---|
Interpreter(@NonNull ByteBuffer byteBuffer)
Initializes a
Interpreter with a ByteBuffer of a model file. |
Interpreter(@NonNull ByteBuffer byteBuffer,
int numThreads)
Deprecated.
Prefer using the
Interpreter(ByteBuffer,Options) constructor. This method
will be removed in a future release. |
Interpreter(@NonNull ByteBuffer byteBuffer,
Interpreter.Options options)
Initializes a
Interpreter with a ByteBuffer of a model file and a set of custom
#Options. |
Interpreter(@NonNull File modelFile)
Initializes a
Interpreter |
Interpreter(@NonNull File modelFile,
int numThreads)
Deprecated.
Prefer using the
Interpreter(File,Options) constructor. This method will
be removed in a future release. |
Interpreter(@NonNull File modelFile,
Interpreter.Options options)
Initializes a
Interpreter and specifies the number of threads used for inference. |
Interpreter(@NonNull MappedByteBuffer mappedByteBuffer)
Deprecated.
Prefer using the
Interpreter(ByteBuffer,Options) constructor. This method
will be removed in a future release. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Release resources associated with the
Interpreter. |
protected void |
finalize() |
int |
getInputIndex(String opName)
Gets index of an input given the op name of the input.
|
Tensor |
getInputTensor(int inputIndex)
Gets the Tensor associated with the provdied input index.
|
int |
getInputTensorCount()
Gets the number of input tensors.
|
Long |
getLastNativeInferenceDurationNanoseconds()
Returns native inference timing.
|
int |
getOutputIndex(String opName)
Gets index of an output given the op name of the output.
|
Tensor |
getOutputTensor(int outputIndex)
Gets the Tensor associated with the provdied output index.
|
int |
getOutputTensorCount()
Gets the number of output Tensors.
|
void |
resizeInput(int idx,
@NonNull int[] dims)
Resizes idx-th input of the native model to the given dims.
|
void |
run(@NonNull Object input,
@NonNull Object output)
Runs model inference if the model takes only one input, and provides only one output.
|
void |
runForMultipleInputsOutputs(@NonNull Object[] inputs,
@NonNull Map<Integer,Object> outputs)
Runs model inference if the model takes multiple inputs, or returns multiple outputs.
|
void |
setNumThreads(int numThreads)
Deprecated.
Prefer using
Interpreter.Options.setNumThreads(int) directly for controlling thread
multi-threading. This method will be removed in a future release. |
void |
setUseNNAPI(boolean useNNAPI)
Deprecated.
Prefer using
Interpreter.Options.setUseNNAPI(boolean) directly for enabling NN API.
This method will be removed in a future release. |
public Interpreter(@NonNull File modelFile)
InterpretermodelFile: - a File of a pre-trained TF Lite model.@Deprecated public Interpreter(@NonNull File modelFile, int numThreads)
Interpreter(File,Options) constructor. This method will
be removed in a future release.Interpreter and specifies the number of threads used for inference.modelFile: - a file of a pre-trained TF Lite modelnumThreads: - number of threads to use for inferencepublic Interpreter(@NonNull File modelFile, Interpreter.Options options)
Interpreter and specifies the number of threads used for inference.modelFile: - a file of a pre-trained TF Lite modeloptions: - a set of options for customizing interpreter behaviorpublic Interpreter(@NonNull ByteBuffer byteBuffer)
Interpreter with a ByteBuffer of a model file.
The ByteBuffer should not be modified after the construction of a Interpreter. The
ByteBuffer can be either a MappedByteBuffer that memory-maps a model file, or a
direct ByteBuffer of nativeOrder() that contains the bytes content of a model.
@Deprecated public Interpreter(@NonNull ByteBuffer byteBuffer, int numThreads)
Interpreter(ByteBuffer,Options) constructor. This method
will be removed in a future release.Interpreter with a ByteBuffer of a model file and specifies the
number of threads used for inference.
The ByteBuffer should not be modified after the construction of a Interpreter. The
ByteBuffer can be either a MappedByteBuffer that memory-maps a model file, or a
direct ByteBuffer of nativeOrder() that contains the bytes content of a model.
@Deprecated public Interpreter(@NonNull MappedByteBuffer mappedByteBuffer)
Interpreter(ByteBuffer,Options) constructor. This method
will be removed in a future release.Interpreter with a MappedByteBuffer to the model file.
The MappedByteBuffer should remain unchanged after the construction of a Interpreter.
public Interpreter(@NonNull ByteBuffer byteBuffer, Interpreter.Options options)
Interpreter with a ByteBuffer of a model file and a set of custom
#Options.
The ByteBuffer should not be modified after the construction of a Interpreter. The
ByteBuffer can be either a MappedByteBuffer that memory-maps a model file, or a
direct ByteBuffer of nativeOrder() that contains the bytes content of a model.
public void run(@NonNull Object input, @NonNull Object output)
Warning: The API runs much faster if ByteBuffer is used as input data type. Please
consider using ByteBuffer to feed input data for better performance.
input - an array or multidimensional array, or a ByteBuffer of primitive types
including int, float, long, and byte. ByteBuffer is the preferred way to pass large
input data. When ByteBuffer is used, its content should remain unchanged until
model inference is done.output - a multidimensional array of output data, or a ByteBuffer of primitive
types including int, float, long, and byte.public void runForMultipleInputsOutputs(@NonNull Object[] inputs, @NonNull Map<Integer,Object> outputs)
Warning: The API runs much faster if ByteBuffer is used as input data type. Please
consider using ByteBuffer to feed input data for better performance.
inputs - an array of input data. The inputs should be in the same order as inputs of the
model. Each input can be an array or multidimensional array, or a ByteBuffer of
primitive types including int, float, long, and byte. ByteBuffer is the preferred
way to pass large input data. When ByteBuffer is used, its content should remain
unchanged until model inference is done.outputs - a map mapping output indices to multidimensional arrays of output data or ByteBuffers of primitive types including int, float, long, and byte. It only needs to keep
entries for the outputs to be used.public void resizeInput(int idx,
@NonNull int[] dims)
IllegalArgumentException will be thrown if it fails to resize.
public int getInputTensorCount()
public int getInputIndex(String opName)
IllegalArgumentException will be thrown if the op name does not exist in the model file used
to initialize the Interpreter.
public Tensor getInputTensor(int inputIndex)
IllegalArgumentException will be thrown if the provided index is invalid.
public int getOutputTensorCount()
public int getOutputIndex(String opName)
IllegalArgumentException will be thrown if the op name does not exist in the model file used
to initialize the Interpreter.
public Tensor getOutputTensor(int outputIndex)
IllegalArgumentException will be thrown if the provided index is invalid.
public Long getLastNativeInferenceDurationNanoseconds()
IllegalArgumentException will be thrown if the model is not initialized by the Interpreter.
@Deprecated public void setUseNNAPI(boolean useNNAPI)
Interpreter.Options.setUseNNAPI(boolean) directly for enabling NN API.
This method will be removed in a future release.@Deprecated public void setNumThreads(int numThreads)
Interpreter.Options.setNumThreads(int) directly for controlling thread
multi-threading. This method will be removed in a future release.public void close()
Interpreter.close in interface AutoCloseableCopyright © 2019. All rights reserved.