Main Content

predict

Compute deep learning network output for inference by using a TensorFlow Lite model

Description

example

Y = predict(net,X) returns the network output Y during inference given the input data X and the network net with a single input and a single output.

[Y1,...,YN] = predict(net,X) returns the N outputs Y1, …, YN during inference for networks that have N outputs.

Tip

For prediction with SeriesNetwork and DAGNetwork objects, see predict.

Examples

collapse all

Suppose that your current working directory contains a TensorFlow™ Lite Model named mobilenet_v1_0.5_224.tflite.

Load the model by using the loadTFLite function. Inspect the object this function creates.

net = loadTFLiteModel('mobilenet_v1_0.5_224.tflite');
disp(net)
  TFLiteModel with properties:

            ModelName: 'mobilenet_v1_0.5_224.tflite'
            NumInputs: 1
           NumOutputs: 1
            InputSize: {[224 224 3]}
           OutputSize: {[1001 1]}
           NumThreads: 8
                 Mean: 127.5000
    StandardDeviation: 127.5000

Create a MATLAB® function that can perform inference using the object net. This function loads the Mobilenet-V1 model into a persistent network object. Then the function performs prediction by passing the network object to the predict function. Subsequent calls to this function reuse this the persistent object.

function out = tflite_predict(in)
persistent net;
if isempty(net)
    net = loadTFLiteModel('mobilenet_v1_0.5_224.tflite');
end
out = predict(net,in);
end

For an example that shows how to generate code for this function and deploy on Raspberry Pi™ hardware, see Generate Code for TensorFlow Lite Model and Deploy on Raspberry Pi.

Input Arguments

collapse all

TFLiteModel object that represents the TensorFlow Lite model file.

Image or sequence input to the network, specified as a numeric array.

  • For image classification networks, the input must be of shape (H,W,C,N), where H is height, W is width, C is channel, and N is batch size.

  • For recurrent neural networks, the input must be of shape (D, N, S), where D is channel or feature dimension, N is batch size, and S is timestamp or sequence length.

Output Arguments

collapse all

Output data, specified as a numeric array.

When performing inference with quantized TensorFlow Lite models, the output data is normalized in one of these ways:

  • Signed 8-bit integer type outputs are normalized as output[i] = (prediction[i] + 128) / 256.0.

  • Unsigned 8-bit integer type outputs are normalized as output[i] = prediction[i] / 255.0.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2022a