Train a vision model with built-in training
Train an image classification or object detection model from a dataset of labeled images, with no training code. Viam runs the job on cloud GPUs and saves the trained model in your organization’s registry. To train any other kind of model, such as a robot policy or a PyTorch model, use a custom training script; to choose between the two, see Built-in or custom training.
Prerequisites
- A dataset of labeled images: tags for classification, or bounding boxes for object detection. To make one, see Create a dataset and Annotate data. For tips on making it a good one, see Prepare a dataset for training.
Built-in training rejects a job if the dataset doesn’t meet these minimums:
| Requirement | Minimum |
|---|---|
| Labeled images | 80% of the dataset |
| Examples per label | 10 images (classification) or 10 bounding boxes (object detection) |
| Total images | 15 (object detection only; classification has no total-image floor) |
Built-in training uses only the images in a dataset and skips any other files, such as video or audio. It can’t train on a sequence dataset.
Choose a framework
The framework sets which software runs the model on your machine. Built-in training produces either one:
| TFLite | TensorFlow | |
|---|---|---|
| Choose it when | You aren’t sure, or the model runs on a small computer such as a Raspberry Pi | Your machine has an NVIDIA GPU you want the model to use |
| Runs on the machine in | tflite_cpu, on the CPU of almost any machine | tensorflow-cpu, or triton on an NVIDIA GPU |
| Resources | Made for devices with little memory and compute | Needs more memory and compute, and takes longer to train |
For the platforms each service supports, see What runs the model.
1. Start a training job
- In the Viam app, go to DATA > DATASETS and click the dataset you want to train on.
- Click Train model.
- Select New model. To retrain a model you already have, select New version and pick the model instead.
- Select Built-in (TensorFlow Lite) or Built-in (TensorFlow).
- Enter a Name for the model, such as
part-inspector. The name identifies the model in your organization’s registry. - Select the task type: Single label classification, Multi label classification, or Object detection. It must match how you labeled the dataset.
- Select the labels to train on. Multi label classification needs at least two. Leave out labels with too few examples or that you don’t want the model to learn.
- Click Train model.
viam train submit managed \
--dataset-id=<dataset-id> \
--model-org-id=<org-id> \
--model-name=part-inspector \
--model-type=single_label_classification \
--model-framework=tflite \
--model-labels=good-part,defective-part
Set --model-type to single_label_classification, multi_label_classification, or object_detection, and --model-framework to tflite or tensorflow.
--model-version is optional and defaults to the current timestamp.
If --model-name is already a model in your organization, the job adds a new version of it.
The SDKs can’t choose a framework: a job submitted from code always trains a TFLite model.
from viam.proto.app.mltraining import ModelType
# viam_client is a connected ViamClient
ml_training_client = viam_client.ml_training_client
job_id = await ml_training_client.submit_training_job(
org_id="<org-id>",
dataset_id="<dataset-id>",
model_name="part-inspector",
model_version="1",
model_type=ModelType.MODEL_TYPE_SINGLE_LABEL_CLASSIFICATION,
tags=["good-part", "defective-part"],
)
print(f"Training job: {job_id}")
The SDKs can’t choose a framework: a job submitted from code always trains a TFLite model.
// viamClient is a connected *app.ViamClient
mlTrainingClient := viamClient.MLTrainingClient()
jobID, err := mlTrainingClient.SubmitTrainingJob(
ctx,
app.SubmitTrainingJobArgs{
DatasetID: "<dataset-id>",
OrganizationID: "<org-id>",
ModelName: "part-inspector",
ModelVersion: "1",
},
app.ModelTypeSingleLabelClassification,
[]string{"good-part", "defective-part"},
)
if err != nil {
logger.Fatal(err)
}
fmt.Printf("Training job: %s\n", jobID)
The job starts, and you get its job ID. Training is billed by usage; see pricing.
2. Monitor the training job
In the Viam app, a job’s status is Preparing while it’s set up, Waiting for GPU while it’s queued for a cloud GPU, then Training, and it ends as Completed, Failed, or Canceled.
The CLI and SDKs report Preparing as pending, and both Waiting for GPU and Training as in_progress.
You get an email when it finishes.
- In the Viam app, go to DATA > MODELS and expand Active Training. It lists your organization’s training jobs with their status.
- Click a job ID to see its details and logs.
Check one job, and read its logs:
viam train get --job-id=<job-id>
viam train logs --job-id=<job-id>
List your organization’s training jobs:
viam train list --org-id=<org-id> --job-status=unspecified
job = await ml_training_client.get_training_job(id="<job-id>")
print(f"Status: {job.status}")
# List your organization's training jobs
for job in await ml_training_client.list_training_jobs(org_id="<org-id>"):
print(f"{job.id}: {job.status}, model {job.model_name}")
job, err := mlTrainingClient.GetTrainingJob(ctx, "<job-id>")
if err != nil {
logger.Fatal(err)
}
fmt.Printf("Status: %v\n", job.Status)
// List your organization's training jobs
jobs, err := mlTrainingClient.ListTrainingJobs(ctx, "<org-id>", app.TrainingStatusUnspecified)
if err != nil {
logger.Fatal(err)
}
for _, j := range jobs {
fmt.Printf("%s: %v, model %s\n", j.ID, j.Status, j.ModelName)
}
Training logs are kept for 7 days after the job finishes. Copy them before then if you need them longer.
3. Verify the trained model
When the job’s status is Completed, the model is in your organization’s registry. In the Viam app, go to DATA > MODELS and click the model’s name to open its details page, which lists its versions.
Built-in training holds back part of the dataset to validate the model, but it doesn’t report loss curves or test results. To see how the model does on images it hasn’t seen, evaluate it on a labeled test dataset before you deploy it.
Troubleshooting
What’s next
- Evaluate a model: measure the model’s precision and recall on a labeled test dataset.
- Deploy a model to a machine: add the model, an ML model service, and a vision service to your machine.
- Detect objects (2D): use an object detection model to find objects in camera images.
- Classify images: use a classification model to sort what your camera sees.
- Automate annotation: label new images faster with your trained model.
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!