Design API for on-demand inference
Define the endpoints needed to implement the on-demand inference functionality.
Requirements:
- the endpoint should work with both images and video frames
- the endpoint should support batch inference, to infer on multiple items in parallel
- for videos, the endpoint should support 'range' and 'stride' parameters to conveniently select the frames to run inference on.
Proposal
Endpoint: POST /api/projects/{project_id}/dataset/media:predict
Body:
{
"model_id": "1edd66ec-ce73-4cbc-80eb-f243a7b1d0f8",
"media": [
{"id": "93c4732c-6a06-4833-8269-f6c224a25482"},
{"id": "d97af71b-d931-4768-ab94-e3aeaa259079", "start_frame": 12, "end_frame": 16, "stride": "2"}
],
"save_predictions": false,
}
model_iddetermines the model to use to generate the predictionsmediais a list of media identifiers, to support batch inference as well as inference on a single item. Each item of the list represents either a single image/frame, or a video range (with start/end frame indices and stride).save_predictionsdetermines whether the predictions should be persisted by the server (in thedataset_itemstable) or if the results are ephemeral (i.e. simply returned but not persisted). Ifsave_predictions=True, the server should also extract the corresponding frames. If the media is already annotated (user_reviewed=true), the prediction should NOT override the annotation.
Response:
{
"predictions": [
{
"media": {"id": "93c4732c-6a06-4833-8269-f6c224a25482"},
"prediction": [
{
"labels": [{"id": "d476573e-d43c-42a6-9327-199a9aa75c33"}],
"confidences": [0.85],
"shape": {
"type": "rectangle",
"x": 10,
"y": 20,
"width": 100,
"height": 200
}
},
...
]
},
{
"media": {"id": "d97af71b-d931-4768-ab94-e3aeaa259079", "frame_index": 12},
"prediction": [...]
},
{
"media": {"id": "d97af71b-d931-4768-ab94-e3aeaa259079", "frame_index": 14},
"prediction": [...]
},
{
"media": {"id": "d97af71b-d931-4768-ab94-e3aeaa259079", "frame_index": 16},
"prediction": [...]
},
]
}
The schema of each prediction is the same as DatasetItemAnnotation.
If any media does not exist, return 404 Not Found. If any request value is invalid (e.g. frame index out of bounds), return 400 Bad Request.
We need to introduce new property configuring max number of images and frames (including frame ranges) which will cap the batch size. It should be possible to increase this value via environment variable.
I agree, we can call it MAX_INFERENCE_BATCH_SIZE
To clarify the semantics of save_predictions: if true, it should persist the prediction if and only if the media is not already annotated; a prediction should never override the annotation.