How to train and deploy ML/computer vision models
You can use Viam’s built-in tools to train a machine learning (ML) model on your images and then deploy computer vision on your machines.
You can use ML models to help your machines adapt their behavior to the world around them.
For example, you can train a model to recognize your dog and detect whether they are sitting or standing. You could then use that knowledge to give your dog treats or capture images only when your dog is in the camera frame so you don’t capture hundreds of photos of an empty room.
In this page
Prerequisites
Create a dataset and label data
Start by assembling the dataset to train your machine learning model on.
Tip
For best results when training machine learning models:
- Provide at least 10 images of the same object, taken from different angles, and repeat this approach for each object you want your machine to be able to identify. Generally, the more different perspectives of a given object you tag, the more likely it is that your model will be able to identify it, even under differing conditions.
- Include some images that do not contain any of the objects you wish to identify, but do not add labels to these images. Unlabelled images must not comprise more than 20% of your dataset, so if you have 25 images in your dataset, at least 20 of those must be labelled.
- If you want your machine to operate successfully in various lighting conditions, such as changing sunlight, include images of each object from varying lighting conditions.
1. Collect images
Start by collecting images from your cameras and syncing it to the Viam app. See Collect image data and sync it to the cloud for instructions.
When training machine learning models, it is important to supply a variety of different data about the subject in different situations, such as from different angles or in different lighting situations. The more varied the provided data set, the more accurate the resulting model becomes.
2. Label your images
Once you have enough images of the objects you’d like to identify captured and synced to the Viam app, use the interface on the DATA tab to label your data.
You can label your images to create:
- Detection models: Draw bounding boxes around distinct objects within captured images. The trained model will enable your machine to be able to detect those objects on its own.
- Classification models: Add tags to each of your images with class labels that describe it. The trained model will enable your machine to classify similar images on its own.
3. Create a dataset
A dataset allows you to conveniently view, work with, and train an ML model on a collection of images.
Use the interface on the DATA tab (or the viam data dataset add
command) to add all images you want to train the model on to a dataset.
Click on an image you want to train your ML model. In the Actions pane on the right-hand side, enter a dataset name under Datasets, then press return. Repeat this with all images you want to add to your dataset.
To remove an image from a dataset click on the x button next to the dataset name.
Train a machine learning (ML) model
1. Train an ML model
In the Viam app, navigate to your list of DATASETS and select the one you want to train on.
Click Train model and follow the prompts.
Select to train a new model or update an existing model. You can train or update using Built-in training or using a training script from the Viam Registry.
Click Next steps.
2. Select the details for your ML model
- Enter a name or use the suggested name for your new model.
- For built-in training scripts, select a Model Type.
Depending on the training script you’ve chose, you may have a number of these options:
- Single Label Classification: The resulting model predicts one of the selected labels or
UNKNOWN
per image. Select this if you only have one label on each image. Ensure that the dataset you are training on also contains unlabeled images. - Multi Label Classification: The resulting model predicts one or more of the selected labels per image.
- Object Detection: The resulting model predicts either no detected objects or any number of object labels alongside their locations per image.
- Single Label Classification: The resulting model predicts one of the selected labels or
- For built-in classification training, select the tags you want to train your model on from the Labels section. Unselected tags will be ignored, and will not be part of the resulting model.
- Click Train model.
3. Wait for your model to train
The model now starts training and you can follow its process in the Training section of the Models page.
Once the model has finished training, it becomes visible in the Models section of the page. You will receive an email when your model finishes training.
4. Debug your training job
If your training job failed you can check your job’s logs with the CLI.
You can obtain the job’s id by listing the jobs:
viam train list --org-id=<INSERT ORG ID> --job-status=unspecified
Then use the job id to get your training job’s logs:
viam train logs --job-id=<JOB ID>
Test your ML model
Once your model has finished training, you can test it with images in the Viam app:
- Navigate to the DATA tab and click on the Images subtab.
- Click on an image to open the side menu, and select the Actions tab.
- In the Run model section, select your model and specify a confidence threshold.
- Click Run model
If the results exceed the confidence threshold, the Run model section shows a label and the responding confidence threshold.
When satisfied that your ML model is working well, continue to deploy an ML model. If the vision service is not detecting or classifying reliably, you will need to adjust your ML model by consider adding and labelling more images in your dataset.
Ideally, you want your ML model to be able to identify objects with a high level of confidence, which is dependent on a robust training dataset.
Deploy an ML model
To use an ML model on your machine, you need to deploy it to your machine using a compatible ML model service. The ML model service will run the model and allow a vision service to use it:
1. Deploy your ML model
Navigate to the CONFIGURE tab of one of your machines in the Viam app.
Here, add an ML model service that supports the ML model you just trained and add the model as the Model.
For example use the TFLite CPU
ML model service for TFlite ML models.
This service will deploy and run the model.
2. Configure an mlmodel
vision service
The vision service takes the ML model and applies it to the stream of images from your camera.
Add the vision / ML model
service to your machine.
Then, from the Select model dropdown, select the name of the ML model service you configured in the last step (for example, mlmodel-1
).
Click Save to save your changes.
3. Use your detector or classifier
You can test your detector by clicking on the Test area of the vision service’s configuration panel or from the CONTROL tab.
The camera stream will show classification or detections when it identifies something, depending on your model.
Try placing an object your ML model can recognize in front of the camera.
If you are using a Viam rover, use the viam_base
panel to move your rover, then click on the vision panel to check for classifications or detections.
You can also test your detector or classifier with code.
Versioning for deployed models
If you upload or train a new version of a model, Viam automatically deploys the latest
version of the model to the machine.
If you do not want Viam to automatically deploy the latest
version of the model, you can edit the "packages"
array in the JSON configuration of your machine.
This array is automatically created when you deploy the model and is not embedded in your service configuration.
You can get the version number from a specific model version by navigating to the models page finding the model’s row, clicking on the right-side menu marked with … and selecting Copy package JSON. For example: 2024-02-28T13-36-51
.
The model package config looks like this:
"packages": [
{
"package": "<model_id>/<model_name>",
"version": "YYYY-MM-DDThh-mm-ss",
"name": "<model_name>",
"type": "ml_model"
}
]
Next steps
To work with datasets programmatically, see the data API which includes several methods to work with datasets:
See the following tutorials for examples of using machine learning models to make your machine interact intelligently based on what it detects:
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!