public class MLInferenceEngine extends Object implements Closeable
It allows users to load a trained model from a resource (using MLInferenceEngine#MLInferenceEngine(String)) or from an InputStream (using MLInferenceEngine#MLInferenceEngine(InputStream)).
Users can set input tensors, execute inference, and retrieve output tensor values.
A MLInferenceEngine allocates native resources when it is opened.
The MLInferenceEngine should be closed with the close() method in order to free the native allocation.
| Constructor and Description |
|---|
MLInferenceEngine(InputStream is,
int inferenceMemoryPoolSize)
Initializes the model from a given
InputStream. |
MLInferenceEngine(String modelPath,
int inferenceMemoryPoolSize)
Initializes the model from a pre-trained model resource.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this model and its associated resources.
|
InputTensor |
getInputTensor(int index)
Gets the input tensor specified by the index.
|
int |
getInputTensorCount()
Gets the number of input Tensors.
|
OutputTensor |
getOutputTensor(int index)
Gets the output tensor specified by the index.
|
int |
getOutputTensorCount()
Gets the number of output Tensors.
|
boolean |
isClosed()
Returns whether this model has been closed.
|
int |
reset()
Resets the state of the model interpreter.
|
int |
run()
Runs an inference on the model.
|
public MLInferenceEngine(InputStream is, int inferenceMemoryPoolSize) throws IOException
InputStream.
This method will block until the model is completely retrieved in the native side from the input stream or an IOException occurs.
Map the model into a usable data structure, build an interpreter to run the model with and allocate memory for the model's tensors.
is - the input stream to read model from.inferenceMemoryPoolSize - Memory allocated to load the tensors and perform inferences.IOException - if an IOException is encountered by the input stream.public MLInferenceEngine(String modelPath, int inferenceMemoryPoolSize)
Map the model into a usable data structure, build an interpreter to run the model with and allocate memory for the model's tensors.
modelPath - the path of the pre-trained model resource.inferenceMemoryPoolSize - Memory allocated to load the tensors and perform inferences.public void close()
This method releases the native resources allocated when opening this model. Calling this method on a model that has already been closed has no effect.
close in interface Closeableclose in interface AutoCloseablepublic InputTensor getInputTensor(int index)
index - the index of the tensor, usually index = 0 except models who have more than one tensor as
input.IllegalArgumentException - if index is invalid.public int getInputTensorCount()
public OutputTensor getOutputTensor(int index)
index - the index of the tensor, usually index = 0 except models who have more than one tensor as
output.IllegalArgumentException - if index is invalid.public int getOutputTensorCount()
public boolean isClosed()
public int reset()
public int run()