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
Createfunction.yaml with function configuration:
Step 2: Implement Model Handler
Createmodel_handler.py to load and run your model:
Step 3: Create Request Handler
Createmain.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:Group Annotations
Group related objects:Deployment
Build and Deploy
Test Function
Best Practices
Performance Optimization
- Lazy Loading: Load models in
init_context, not in handler - Batch Processing: Process multiple requests efficiently
- GPU Utilization: Use GPU when available for faster inference
- Model Optimization: Use ONNX, TensorRT, or quantization
- 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
- Use GPU-optimized function variant
- Optimize model (ONNX, quantization)
- Adjust
numWorkersin function.yaml
- Validate against CVAT expected format
- Check coordinate scaling
- Test with small dataset first
- 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