Configure a tflite_cpu ML model service
The tflite_cpu
ML model service allows you to deploy TensorFlow Lite ML models as long as your models adhere to the model requirements.
It is supported on any CPU and Linux, Raspbian, MacOS and Android machines.
To work with the tflite_cpu
ML model service, an ML model is comprised of a tflite_cpu
ML model service, you can deploy:
- a model from the registry
- a model trained outside the Viam platform that you have uploaded
- a model available on your machine
To configure a tflite_cpu
ML model service:
Navigate to the CONFIGURE tab of your machine’s page in the Viam app.
Click the + icon next to your machine part in the left-hand menu and select Service.
Select the ML model
type, then select the TFLite CPU
model.
Enter a name or use the suggested name for your service and click Create.
You can choose to configure your service with an existing model on the machine or deploy a model onto your machine:
To configure your service and deploy a model onto your machine, select Deploy model on machine for the Deployment field in the resulting ML model service configuration pane.
Click Select model. In the modal that appears, search for models from your organization or the Registry.
You can select a model to see more details about it, and then select the model to deploy it to your machine.
Also, optionally select the Number of threads.
Click Save at the top right of the window to save your changes.
- To configure your service with an existing model on the machine, select Path to existing model on robot for the Deployment field.
- Then specify the absolute Model path and any Optional settings such as the absolute Label path and the Number of threads.
Add the tflite_cpu
ML model object to the services array in your raw JSON configuration:
"services": [
{
"name": "<mlmodel_name>",
"type": "mlmodel",
"model": "tflite_cpu",
"attributes": {
"model_path": "${packages.<model_name>}/<model-name>.tflite",
"label_path": "${packages.<model_name>}/labels.txt",
"num_threads": <number>
}
},
... // Other services
]
{
"packages": [
{
"package": "39c34811-9999-4fff-bd91-26a0e4e90644/my_fruit_model",
"version": "YYYY-MM-DDThh-mm-ss",
"name": "my_fruit_model",
"type": "ml_model"
}
], ... // < Insert "components", "modules" etc. >
"services": [
{
"name": "fruit_classifier",
"type": "mlmodel",
"model": "tflite_cpu",
"attributes": {
"model_path": "${packages.my_fruit_model}/my_fruit_model.tflite",
"label_path": "${packages.my_fruit_model}/labels.txt",
"num_threads": 1
}
}
]
}
The "packages"
array shown above is automatically created when you deploy the model.
You do not need to edit the configuration yourself, except if you wish to specify a specific version for your deployed model.
The following parameters are available for a "tflite_cpu"
model:
Parameter | Required? | Description |
---|---|---|
model_path | Required | The absolute path to the .tflite model file, as a string . |
label_path | Optional | The absolute path to a .txt file that holds class labels for your TFLite model, as a string . This text file should contain an ordered listing of class labels. Without this file, classes will read as “1”, “2”, and so on. |
num_threads | Optional | An integer that defines how many CPU threads to use to run inference. Default: 1 . |
Note
If you Deploy model on machine, model_path
and label_path
will be automatically configured in the format "${packages.<model_name>}/<model-name>.tflite"
and "${packages.<model_name>}/labels.txt"
respectively.
If you take the Path to existing model on machine approach, your model and label paths do not have to be in the same format.
For example, they might resemble home/models/fruit/my_fruit_model.tflite
.
Save the configuration.
Model requirements
Tip
Models trained in the Viam app meet these requirements by design.
We strongly recommend that you package your TensorFlow Lite model with metadata in the standard form.
In the absence of metadata, your tflite_cpu
model must satisfy the following requirements:
- A single input tensor representing the image of type UInt8 (expecting values from 0 to 255) or Float 32 (values from -1 to 1).
- At least 3 output tensors (the rest won’t be read) containing the bounding boxes, class labels, and confidence scores (in that order).
- Bounding box output tensor must be ordered [x x y y], where x is an x-boundary (xmin or xmax) of the bounding box and the same is true for y. Each value should be between 0 and 1, designating the percentage of the image at which the boundary can be found.
These requirements are satisfied by a few publicly available model architectures including EfficientDet, MobileNet, and SSD MobileNet V1. You can use one of these architectures or build your own.
Next steps
The ML model service only runs your model on the machine. To use the inferences from the model, you must use an additional service such as a vision service:
Have questions, or want to meet other people working on robots? Join our Community Discord.
If you notice any issues with the documentation, feel free to file an issue or edit this file.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!