Dynamic Input in KServe API#
OpenVINO Model Server can serve models with native OpenVINO dynamic shapes. Requests can have different batch sizes or other dimensions, provided their shapes satisfy the model’s fixed dimensions and any configured bounds. This avoids reshaping and reloading the model for each request.
Configure Dynamic Shapes#
OVMS inherits dynamic dimensions from a model exported with them. Alternatively, set --shape when starting the server, if the model supports reshaping to the requested dimensions. For example, for an NCHW input:
--shape "(1,3,-1,-1)"allows dynamic height and width, with batch size 1 and three channels.--shape "(1,3,200:500,200:500)"limits both height and width to the range 200 through 500.--shape "(-1,3,224,224)"allows a dynamic batch dimension with fixed image dimensions.
Dynamic dimensions can be used in other positions as supported by the model. Not all models and operations support dynamic shapes; see the OpenVINO shape inference documentation and batch, shape, and layout configuration.
This feature is distinct from encoded image input: OVMS can decode and, under certain shape and layout conditions, resize images to a model’s input size. That does not make the model input shape dynamic.
Example: Dynamic Image Dimensions#
This example serves the face-detection-retail-0004 model with dynamic height and width. The client resizes images to the requested dimensions, sends numeric tensors, and saves images with predicted bounding boxes.
Clone the repository and download the model:
git clone https://github.com/openvinotoolkit/model_server.git
cd model_server
mkdir -p models/face_detection/1
curl -L https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/3/face-detection-retail-0004/FP32/face-detection-retail-0004.bin -o models/face_detection/1/face-detection-retail-0004.bin
curl -L https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/3/face-detection-retail-0004/FP32/face-detection-retail-0004.xml -o models/face_detection/1/face-detection-retail-0004.xml
Start the server with dynamic height and width:
docker pull openvino/model_server:latest
docker run --rm -d -v "$(pwd)/models:/models" -p 9000:9000 \
openvino/model_server:latest --model_name face-detection \
--model_path /models/face_detection --shape "(1,3,-1,-1)" --port 9000
Run the face detection client with two different input sizes:
cd demos/face_detection/python
pip install -r ../../common/python/requirements.txt
mkdir results_500x500 results_600x400
python face_detection.py --grpc_port 9000 --width 500 --height 500 --input_images_dir ../../common/static/images/people --output_dir results_500x500
python face_detection.py --grpc_port 9000 --width 600 --height 400 --input_images_dir ../../common/static/images/people --output_dir results_600x400
The client saves the results in the specified output directories.
MediaPipe Graphs#
For a MediaPipe graph, the graph input tag determines how OVMS converts the KServe request into a packet. Whether different shapes can be processed also depends on the calculators and models downstream. Examples include:
OVTENSORor an untagged graph input produces anov::Tensor, which can be passed to anOpenVINOInferenceCalculatorrunning a model that supports the requested shape.IMAGEproduces a MediaPipeImageFramefrom raw HWC pixel data, subject to supported datatypes and channel counts. It does not automatically decode an encoded image file.REQUESTpasses the KServe request to the graph; the receiving calculator must interpret its contents and handle varying inputs.OVMS_PY_TENSORconverts request inputs to Python tensors for aPythonExecutorCalculator. The Python execute function determines how to handle their shapes.
See MediaPipe graph input types and Python graph inputs for details.