training_extensions icon indicating copy to clipboard operation
training_extensions copied to clipboard

Design API for on-demand inference

Open leoll2 opened this issue 9 months ago • 4 comments

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.

leoll2 avatar Feb 11 '26 07:02 leoll2

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_id determines the model to use to generate the predictions
  • media is 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_predictions determines whether the predictions should be persisted by the server (in the dataset_items table) or if the results are ephemeral (i.e. simply returned but not persisted). If save_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.

leoll2 avatar Feb 25 '26 11:02 leoll2

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.

warrkan avatar Feb 26 '26 09:02 warrkan

I agree, we can call it MAX_INFERENCE_BATCH_SIZE

leoll2 avatar Feb 26 '26 09:02 leoll2

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.

leoll2 avatar Mar 05 '26 09:03 leoll2