Skip to main content

Overview

CVAT’s serverless architecture allows you to integrate custom AI models by packaging them as Nuclio functions. This guide covers creating functions for detectors, interactors, trackers, and ReID models.

Function Structure

Each serverless function consists of:

Creating a Detector Function

Step 1: Define Function Metadata

Create function.yaml with function configuration:

Step 2: Implement Model Handler

Create model_handler.py to load and run your model:

Step 3: Create Request Handler

Create main.py to handle HTTP requests:

Creating an Interactor Function

Interactors receive user input (points, boxes) for guided segmentation:

Function Metadata

Handler Implementation

Creating a Tracker Function

Trackers maintain object state across frames:

Function Metadata

Handler Implementation

Output Formats

Rectangle

Polygon

Mask

Skeleton (with Elements)

Advanced Features

Attributes

Add attributes to detections:
Define attributes in function.yaml:

Group Annotations

Group related objects:

Deployment

Build and Deploy

Test Function

Best Practices

Performance Optimization

  1. Lazy Loading: Load models in init_context, not in handler
  2. Batch Processing: Process multiple requests efficiently
  3. GPU Utilization: Use GPU when available for faster inference
  4. Model Optimization: Use ONNX, TensorRT, or quantization
  5. Caching: Cache preprocessed data when possible

Error Handling

Logging

Resource Management

Testing

Unit Tests

Integration Tests

Troubleshooting

Common Issues

Model Not Loading:
  • Check model file path in container
  • Verify dependencies in build directives
  • Increase memory limits
Slow Inference:
  • Use GPU-optimized function variant
  • Optimize model (ONNX, quantization)
  • Adjust numWorkers in function.yaml
Invalid Output Format:
  • Validate against CVAT expected format
  • Check coordinate scaling
  • Test with small dataset first
Memory Errors:
  • Increase container memory limits
  • Reduce batch size
  • Optimize image preprocessing

Examples

Explore existing functions in the CVAT repository:
  • SAM Interactor: serverless/pytorch/facebookresearch/sam/
  • YOLO Detector: serverless/onnx/WongKinYiu/yolov7/
  • TransT Tracker: serverless/pytorch/dschoerk/transt/
  • Mask R-CNN: serverless/openvino/omz/public/mask_rcnn_inception_resnet_v2_atrous_coco/

Next Steps

Deployment Guide

Learn how to deploy serverless functions

Overview

Understand serverless function types