Train and predict automation model pipeline

Introduction

Welcome to the Model Automation Training and Prediction tutorial!

In this guide, you'll learn how to automatically train a computer vision model and use it to make predictions on local images directly from your Python code.

This tutorial provides you with the necessary steps to achieve the following:

  • Automatically run training with given or default parameters.

  • Download pre-trained model weights from Team files where all generated artifacts will be saved.

  • Perform inference with a pre-trained model on local images to obtain object detection predictions.

  • Upload annotated images to Supervisely

Automated pipeline schema

πŸ’» We wll use a 196 lines of Python code in main.py to demonstrate the entire process.

In this demo script we will use to automate the process of training the YOLOv8 model, but this workflow is applicable to other models as well.

Before we dive into the tutorial, let's learn how to debug it.

How to debug this tutorial

Step 1. Prepare ~/supervisely.env file with credentials. Learn more here.

Step 2. Clone repository with source code and demo data and create Virtual Environment.

Step 3. Open repository directory in Visual Studio Code.

Step 4. change βœ… workspace ID, team ID, and project ID βœ… in local.env file by copying the ID from the context menu. A new project with annotated images will be created in the workspace you define. Learn more here.

Copy team, workspace and project IDs from context menu

Step 5. Start debugging src/main.py

Go to Run and Debug section (Ctrl+Shift+D). Press green triangle or F5 to start debugging.

Debug tutorial in Visual Studio Code

Prepare labeled data

You can use on of our demo projects.

Get demo project

If you already have the labeled data β€” just upload it into Supervisely platform using one of the 70+ import Supervisely Apps from our Ecosystem. You will find there the imports for all popular data formats in computer vision.

Python code

Import libraries

Load environment variables

Load environment variables with credentials, team ID, project ID, and workspace ID. Init api for communicating with Supervisely Instance.

Define variables

Set the path to the image you want to predict on

Train model

πŸ“— By changing data field you can customize training parameters such as: project id and dataset ids to train on, train mode (finetune or scratch), number of epochs, patience, batch size, input image size, optimizer, number of workers, learning rate, momentum, weight decay, warmup epochs, warmup momentum, warmup bias lr, augmentation parameters, and many others.

Explore training artefacts in Team files

Training process generates artifacts including model weights (checkpoints), logs, charts, additional visualizations of training batches, predictions on validation, precision-recall curves, confusion matrix and so on. At the last step of the training dashboard you will see the location and direct link where the resulting directory with training artifacts is saved.

It is automatically uploaded from the computer used for training back to the platform to Team Files. You can find it there at any time.

Training artifacts are saved to your account to Team Files

Download model weights from Team files

Get model predictions and visualize

tomato

Upload prediction in Supervisely format

Explore result project with model predictions in Supervisely.

results

In this tutorial we learned how to train a model using automatically train and perform inference on local image for object detection task. You can also use this code for other tasks: instance segmentation and pose estimation. Just change the task_type parameter in the data field of the request and update label creation code in the last part of the tutorial.

Last updated

Was this helpful?