Object tracking
Step-by-step tutorial on how to integrate custom visual object tracking neural network into Supervisely platform on the example of MixFormer model.
Introduction
In this tutorial, you will learn how to integrate your custom object-tracking model into Supervisely by creating two simple serving apps. First, we will construct a straightforward model that only moves the original bound box as an illustration. The SOTA model MixFormer, which already has the majority of the necessary functions implemented, will be used in the second part.
Implementation details
To integrate your model, you need to subclass sly.nn.inference.BBoxTracking
and implement 3 methods:
load_on_device
method for downloading the weights and initializing the model on a specific device. Takes amodel_dir
argument, which is a directory for all model files (like configs, weights, etc). The second argument is thedevice
-torch.device
likecuda:0
,cpu
.initialize
method passes the image and the bound box of the object, which the model should track during the prediction step.predict
. The core implementation of model inference. It takes the frame of typenp.ndarray
, inference settings, previous (or initial) frame and bound box as arguments, applies the model inference to the current frame and returns a prediction (both input bound box and predicted aresly.nn.PredictionBBox
objects).
Currently, integrating models that can track several objects simultaneously is not possible due to the implementation of the sly.nn.inference.BBox
class. However, multiobject tracking is available: objects will be tracked one by one and the model will be re-initialized for each object.
Overall structure
The overall structure of the class we will implement is looking like this:
The superclass has a serve()
method. For running the code on the Supervisely platform, m.serve()
method should be executed:
And here is the beauty comes in. The method serve()
internally handles everything and deploys your model as a REST API service on the Supervisely platform. It means that other applications can communicate with your model and get predictions from it.
So let's implement the class.
Simple model
Getting started
Step 1. Prepare ~/supervisely.env
file with credentials. Learn more here.
Step 2. Create Virtual Environment and install supervisely==6.72.32
in it.
Step-by-step implementation
Defining imports and global variables
1. load_on_device
The following code creates the model according to config model_settings.yaml
. Path to .yaml
config is passed during initialization. These settings can also be given as a Python dictionary. Config in the form of a dictionary becomes available in self.custom_inference_settings_dict
attribute. Also, load_on_device
will keep the model as a self.model
for further use:
Our settings.yaml
file:
2. initialize and predict
The core methods for model inference. Here we will use the defined model and make sure that the predicted bound box is not outside of the bounds.
It must return exactly an sly.nn.PredictionBBox
object for compatibility with Supervisely.
Usage of our class
Once the class is created, here we initialize it and get one test prediction for debugging.
Here are the output predictions of our simple model:
MixFormer tracking model
Let's now implement the class for a pre-trained model. The majority of the code used to load and run the model is taken directly from the original repository. We will also include the option to choose a model before launching the app because the authors provide two pre-trained models.
Getting started
Step 1. Prepare ~/supervisely.env
file with credentials. Learn more here.
Step 2. Clone the repository with source code and create Virtual Environment.
Step 3. Load model weights.
Step 4. Open the repository directory in Visual Studio Code.
Step 5. Run debug for script src/main.py
Python script
The integration script is simple:
Initialize model.
Run inference on demo images.
Collect predictions and save them in chronological order.
Step-by-step implementation
Defining imports and global variables
1. load_on_device
The following code creates the model which will keep as a self.model
. The SupportedModels
class is simply an Enum
class that was built to prevent working with raw strings. The Tracker
class is a collection of functions supplied by the original repository's creator to create and use pre-trained models (check implementation here)
Here we are downloading the model weights from local storage, but it can be also downloaded by path in Supervisely Team Files.
2. initialize and predict
The core methods for model inference. Here we are preparing the initial bound box in initialization and wrapping model predictions into sly.nn.PredictionBBox
.
It must return exactly a list of sly.nn.PredictionBBox
objects for compatibility with Supervisely.
Usage of our class
Once the class is created, here we initialize it and get one test prediction for debugging.
Here are the output predictions of our MixFormer model:
Run and debug
The beauty of this class is that you can easily debug your code locally in your favorite IDE.
For now, we recommend using Visual Studio Code IDE, because our repositories have prepared settings for convenient debugging in VSCode. It is the easiest way to start.
Local debug
You can run the code locally for debugging. For Visual Studio Code we've created a launch.json
config file that can be selected:
Debug in Supervisely platform
Once the code seems working locally, it's time to test the code right in the Supervisely platform as a debugging app. For that:
If you develop in a Docker container, you should run the container with
--cap_add=NET_ADMIN
option.Install
sudo apt-get install wireguard iproute2
.Define your
TEAM_ID
in thelocal.env
file. Other env variables that are needed, are already provided in./vscode/launch.json
for you.Switch the
launch.json
config to theAdvanced debug in Supervisely platform
:
Run the code.
✅ It will deploy the model in the Supervisely platform as a regular serving App that can communicate with all other apps in the platform:
Now you can use apps like Apply NN to Images, Apply NN to videos with your deployed model.
Or get the model inference via Python API with the help of sly.nn.inference.Session
class just in one line of code. See Inference API Tutorial.
Release your code as a Supervisely App.
Once you've tested the code, it's time to release it into the platform. It can be released as an App that is shared with the all Supervisely community, or as your own private App.
Refer to How to Release your App for all releasing details. For a private app check also Private App Tutorial.
Repository structure
The structure of our GitHub repository is the following:
Explanation:
serve/serve/src/main.py
- main inference scriptserve/serve/src/sly_functions.py
- functions to run the MixFormer model based on the original repository codeserve/serve/src/modal.html
- modal window template; a simple way to control ENV variables (e.g. model type)save/models
- directory with model weightsdata/racing
- directory with demo frames for inferenceserve/serve/README.md
- readme of your application, it is the main page of an application in Ecosystem with some images, videos, and how-to-use guidesserve/serve/config.json
- configuration of the Supervisely application, which defines the name and description of the app, its context menu, icon, poster, and running settingsrequirements.txt
- all packages needed for debugginglocal.env
- file with variables used for debuggingserve/serve/docker
- directory with the custom Dockerfile for this application and the script that builds it and publishes it to the docker registry
modal.html
The author of the original repository provides us with different models. We create a basic html file with a selector so that users may choose a model before launching the application. It is now sufficient to correctly specify the configuration, and the model name will be accessible via environment variables.
App configuration
App configuration is stored in config.json
file. A detailed explanation of all possible fields is covered in this Configuration Tutorial. Let's check the config for our current app:
Here is the explanation for the fields:
type
- a type of the module in Supervisely Ecosystemversion
- version of Supervisely App Engine. Just keep it by defaultname
- the name of the applicationdescription
- the description of the applicationcategories
- these tags are used to place the application in the correct category in Ecosystem.session_tags
- these tags will be assigned to every running session of the application. They can be used by other apps to find and filter all running sessionsmodal_template
- path to modal window template (modal.html
file previously described)modal_template_state
- list of default values for all states"need_gpu": true
- should be true if you want to use anycuda
devices."community_agent": false
- this means that this app can not be run on the agents started by Supervisely team, so users have to connect their computers and run the app only on their agents. Only applicable in Community Edition. Enterprise customers use their private instances so they can ignore the current optiondocker_image
- Docker container will be started from the defined Docker image, GitHub repository will be downloaded and mounted inside the container.entrypoint
- the command that starts our application in a containerport
- port inside the container"headless": true
means that the app has no User Interfaceallowed_shapes
- shapes can be tracked with this model
Last updated