2024-09-27 15:41:48 +02:00
|
|
|
"""Model Utils"""
|
|
|
|
|
2024-11-04 15:07:57 +01:00
|
|
|
import logging
|
2024-09-27 15:41:48 +02:00
|
|
|
import os
|
|
|
|
|
2025-02-10 22:00:12 +01:00
|
|
|
import cv2
|
|
|
|
import numpy as np
|
2024-09-27 15:41:48 +02:00
|
|
|
import onnxruntime as ort
|
|
|
|
|
2024-11-04 15:07:57 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2025-02-10 22:00:12 +01:00
|
|
|
### Post Processing
|
|
|
|
|
|
|
|
|
|
|
|
def post_process_yolov9(predictions: np.ndarray, width, height) -> np.ndarray:
|
|
|
|
predictions = np.squeeze(predictions).T
|
|
|
|
scores = np.max(predictions[:, 4:], axis=1)
|
|
|
|
predictions = predictions[scores > 0.4, :]
|
|
|
|
scores = scores[scores > 0.4]
|
|
|
|
class_ids = np.argmax(predictions[:, 4:], axis=1)
|
|
|
|
|
|
|
|
# Rescale box
|
|
|
|
boxes = predictions[:, :4]
|
|
|
|
|
|
|
|
input_shape = np.array([width, height, width, height])
|
|
|
|
boxes = np.divide(boxes, input_shape, dtype=np.float32)
|
|
|
|
indices = cv2.dnn.NMSBoxes(boxes, scores, score_threshold=0.4, nms_threshold=0.4)
|
|
|
|
detections = np.zeros((20, 6), np.float32)
|
|
|
|
for i, (bbox, confidence, class_id) in enumerate(
|
|
|
|
zip(boxes[indices], scores[indices], class_ids[indices])
|
|
|
|
):
|
|
|
|
if i == 20:
|
|
|
|
break
|
|
|
|
|
|
|
|
detections[i] = [
|
|
|
|
class_id,
|
|
|
|
confidence,
|
|
|
|
bbox[1] - bbox[3] / 2,
|
|
|
|
bbox[0] - bbox[2] / 2,
|
|
|
|
bbox[1] + bbox[3] / 2,
|
|
|
|
bbox[0] + bbox[2] / 2,
|
|
|
|
]
|
|
|
|
|
|
|
|
return detections
|
|
|
|
|
|
|
|
|
|
|
|
### ONNX Utilities
|
|
|
|
|
2024-09-27 15:41:48 +02:00
|
|
|
|
|
|
|
def get_ort_providers(
|
2024-10-29 16:28:05 +01:00
|
|
|
force_cpu: bool = False, device: str = "AUTO", requires_fp16: bool = False
|
2024-09-27 15:41:48 +02:00
|
|
|
) -> tuple[list[str], list[dict[str, any]]]:
|
|
|
|
if force_cpu:
|
2024-10-11 20:03:47 +02:00
|
|
|
return (
|
|
|
|
["CPUExecutionProvider"],
|
|
|
|
[
|
|
|
|
{
|
2024-10-21 17:00:45 +02:00
|
|
|
"enable_cpu_mem_arena": False,
|
2024-10-11 20:03:47 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
2024-09-27 15:41:48 +02:00
|
|
|
|
2024-10-14 04:34:51 +02:00
|
|
|
providers = []
|
2024-09-27 15:41:48 +02:00
|
|
|
options = []
|
|
|
|
|
2024-10-14 04:34:51 +02:00
|
|
|
for provider in ort.get_available_providers():
|
|
|
|
if provider == "CUDAExecutionProvider":
|
2024-11-10 15:43:24 +01:00
|
|
|
device_id = 0 if not device.isdigit() else int(device)
|
2024-10-14 04:34:51 +02:00
|
|
|
providers.append(provider)
|
|
|
|
options.append(
|
|
|
|
{
|
|
|
|
"arena_extend_strategy": "kSameAsRequested",
|
2024-11-10 15:43:24 +01:00
|
|
|
"device_id": device_id,
|
2024-10-14 04:34:51 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
elif provider == "TensorrtExecutionProvider":
|
|
|
|
# TensorrtExecutionProvider uses too much memory without options to control it
|
2024-10-29 16:28:05 +01:00
|
|
|
# so it is not enabled by default
|
|
|
|
if device == "Tensorrt":
|
|
|
|
os.makedirs(
|
|
|
|
"/config/model_cache/tensorrt/ort/trt-engines", exist_ok=True
|
|
|
|
)
|
2024-11-10 15:43:24 +01:00
|
|
|
device_id = 0 if not device.isdigit() else int(device)
|
2024-10-29 16:28:05 +01:00
|
|
|
providers.append(provider)
|
|
|
|
options.append(
|
|
|
|
{
|
2024-11-10 15:43:24 +01:00
|
|
|
"device_id": device_id,
|
2024-10-29 16:28:05 +01:00
|
|
|
"trt_fp16_enable": requires_fp16
|
|
|
|
and os.environ.get("USE_FP_16", "True") != "False",
|
|
|
|
"trt_timing_cache_enable": True,
|
|
|
|
"trt_engine_cache_enable": True,
|
|
|
|
"trt_timing_cache_path": "/config/model_cache/tensorrt/ort",
|
|
|
|
"trt_engine_cache_path": "/config/model_cache/tensorrt/ort/trt-engines",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
continue
|
2024-09-27 15:41:48 +02:00
|
|
|
elif provider == "OpenVINOExecutionProvider":
|
|
|
|
os.makedirs("/config/model_cache/openvino/ort", exist_ok=True)
|
2024-10-14 04:34:51 +02:00
|
|
|
providers.append(provider)
|
2024-09-27 15:41:48 +02:00
|
|
|
options.append(
|
|
|
|
{
|
2024-10-11 20:03:47 +02:00
|
|
|
"arena_extend_strategy": "kSameAsRequested",
|
2024-09-27 15:41:48 +02:00
|
|
|
"cache_dir": "/config/model_cache/openvino/ort",
|
2024-10-29 16:28:05 +01:00
|
|
|
"device_type": device,
|
2024-09-27 15:41:48 +02:00
|
|
|
}
|
|
|
|
)
|
2024-10-11 20:03:47 +02:00
|
|
|
elif provider == "CPUExecutionProvider":
|
2024-10-14 04:34:51 +02:00
|
|
|
providers.append(provider)
|
2024-10-11 20:03:47 +02:00
|
|
|
options.append(
|
|
|
|
{
|
2024-10-21 17:00:45 +02:00
|
|
|
"enable_cpu_mem_arena": False,
|
2024-10-11 20:03:47 +02:00
|
|
|
}
|
|
|
|
)
|
2024-09-27 15:41:48 +02:00
|
|
|
else:
|
2024-10-14 04:34:51 +02:00
|
|
|
providers.append(provider)
|
2024-09-27 15:41:48 +02:00
|
|
|
options.append({})
|
|
|
|
|
|
|
|
return (providers, options)
|