2023-11-02 13:55:24 +01:00
|
|
|
import logging
|
2023-11-17 02:08:41 +01:00
|
|
|
import os.path
|
2024-05-22 00:50:03 +02:00
|
|
|
import re
|
|
|
|
import urllib.request
|
2023-11-02 13:55:24 +01:00
|
|
|
from typing import Literal
|
|
|
|
|
|
|
|
from pydantic import Field
|
|
|
|
|
|
|
|
from frigate.detectors.detection_api import DetectionApi
|
2024-05-22 00:50:03 +02:00
|
|
|
from frigate.detectors.detector_config import BaseDetectorConfig, ModelTypeEnum
|
2023-11-02 13:55:24 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DETECTOR_KEY = "rknn"
|
|
|
|
|
2024-05-22 00:50:03 +02:00
|
|
|
supported_socs = ["rk3562", "rk3566", "rk3568", "rk3576", "rk3588"]
|
|
|
|
|
|
|
|
supported_models = {ModelTypeEnum.yolonas: "^deci-fp16-yolonas_[sml]$"}
|
|
|
|
|
|
|
|
model_chache_dir = "/config/model_cache/rknn_cache/"
|
2023-11-18 14:53:49 +01:00
|
|
|
|
2023-11-02 13:55:24 +01:00
|
|
|
|
|
|
|
class RknnDetectorConfig(BaseDetectorConfig):
|
|
|
|
type: Literal[DETECTOR_KEY]
|
2024-05-22 00:50:03 +02:00
|
|
|
num_cores: int = Field(default=0, ge=0, le=3, title="Number of NPU cores to use.")
|
|
|
|
purge_model_cache: bool = Field(default=True)
|
2023-11-02 13:55:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Rknn(DetectionApi):
|
|
|
|
type_key = DETECTOR_KEY
|
|
|
|
|
|
|
|
def __init__(self, config: RknnDetectorConfig):
|
2024-05-22 00:50:03 +02:00
|
|
|
super().__init__(config)
|
|
|
|
self.height = config.model.height
|
|
|
|
self.width = config.model.width
|
|
|
|
core_mask = 2**config.num_cores - 1
|
|
|
|
soc = self.get_soc()
|
|
|
|
|
|
|
|
model_props = self.parse_model_input(config.model.path, soc)
|
|
|
|
|
|
|
|
if model_props["preset"]:
|
|
|
|
config.model.model_type = model_props["model_type"]
|
|
|
|
|
|
|
|
if model_props["model_type"] == ModelTypeEnum.yolonas:
|
|
|
|
logger.info("""
|
|
|
|
You are using yolo-nas with weights from DeciAI.
|
|
|
|
These weights are subject to their license and can't be used commercially.
|
|
|
|
For more information, see: https://docs.deci.ai/super-gradients/latest/LICENSE.YOLONAS.html
|
|
|
|
""")
|
|
|
|
|
|
|
|
from rknnlite.api import RKNNLite
|
|
|
|
|
|
|
|
self.rknn = RKNNLite(verbose=False)
|
|
|
|
if self.rknn.load_rknn(model_props["path"]) != 0:
|
|
|
|
logger.error("Error initializing rknn model.")
|
|
|
|
if self.rknn.init_runtime(core_mask=core_mask) != 0:
|
|
|
|
logger.error(
|
|
|
|
"Error initializing rknn runtime. Do you run docker in privileged mode?"
|
|
|
|
)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.rknn.release()
|
|
|
|
|
|
|
|
def get_soc(self):
|
2023-11-18 14:53:49 +01:00
|
|
|
try:
|
|
|
|
with open("/proc/device-tree/compatible") as file:
|
|
|
|
soc = file.read().split(",")[-1].strip("\x00")
|
|
|
|
except FileNotFoundError:
|
|
|
|
raise Exception("Make sure to run docker in privileged mode.")
|
|
|
|
|
|
|
|
if soc not in supported_socs:
|
|
|
|
raise Exception(
|
2024-05-22 00:50:03 +02:00
|
|
|
f"Your SoC is not supported. Your SoC is: {soc}. Currently these SoCs are supported: {supported_socs}."
|
2023-11-18 14:53:49 +01:00
|
|
|
)
|
|
|
|
|
2024-05-22 00:50:03 +02:00
|
|
|
return soc
|
|
|
|
|
|
|
|
def parse_model_input(self, model_path, soc):
|
|
|
|
model_props = {}
|
|
|
|
|
|
|
|
# find out if user provides his own model
|
|
|
|
# user provided models should be a path and contain a "/"
|
|
|
|
if "/" in model_path:
|
|
|
|
model_props["preset"] = False
|
|
|
|
model_props["path"] = model_path
|
|
|
|
else:
|
|
|
|
model_props["preset"] = True
|
|
|
|
|
|
|
|
"""
|
|
|
|
Filenames follow this pattern:
|
|
|
|
origin-quant-basename-soc-tk_version-rev.rknn
|
|
|
|
origin: From where comes the model? default: upstream repo; rknn: modifications from airockchip
|
|
|
|
quant: i8 or fp16
|
|
|
|
basename: e.g. yolonas_s
|
|
|
|
soc: e.g. rk3588
|
|
|
|
tk_version: e.g. v2.0.0
|
|
|
|
rev: e.g. 1
|
|
|
|
|
|
|
|
Full name could be: default-fp16-yolonas_s-rk3588-v2.0.0-1.rknn
|
|
|
|
"""
|
|
|
|
|
|
|
|
model_matched = False
|
|
|
|
|
|
|
|
for model_type, pattern in supported_models.items():
|
|
|
|
if re.match(pattern, model_path):
|
|
|
|
model_matched = True
|
|
|
|
model_props["model_type"] = model_type
|
|
|
|
|
|
|
|
if model_matched:
|
|
|
|
model_props["filename"] = model_path + f"-{soc}-v2.0.0-1.rknn"
|
|
|
|
|
|
|
|
model_props["path"] = model_chache_dir + model_props["filename"]
|
|
|
|
|
|
|
|
if not os.path.isfile(model_props["path"]):
|
|
|
|
self.download_model(model_props["filename"])
|
|
|
|
else:
|
|
|
|
supported_models_str = ", ".join(
|
|
|
|
model[1:-1] for model in supported_models
|
2023-11-17 02:08:41 +01:00
|
|
|
)
|
|
|
|
raise Exception(
|
2024-05-22 00:50:03 +02:00
|
|
|
f"Model {model_path} is unsupported. Provide your own model or choose one of the following: {supported_models_str}"
|
2023-11-17 02:08:41 +01:00
|
|
|
)
|
|
|
|
|
2024-05-22 00:50:03 +02:00
|
|
|
return model_props
|
2023-11-17 02:08:41 +01:00
|
|
|
|
2024-05-22 00:50:03 +02:00
|
|
|
def download_model(self, filename):
|
|
|
|
if not os.path.isdir(model_chache_dir):
|
|
|
|
os.mkdir(model_chache_dir)
|
2023-11-02 13:55:24 +01:00
|
|
|
|
2024-05-22 00:50:03 +02:00
|
|
|
urllib.request.urlretrieve(
|
|
|
|
f"https://github.com/MarcA711/rknn-models/releases/download/v2.0.0/{filename}",
|
|
|
|
model_chache_dir + filename,
|
|
|
|
)
|
2023-11-03 01:12:54 +01:00
|
|
|
|
2024-05-22 00:50:03 +02:00
|
|
|
def check_config(self, config):
|
|
|
|
if (config.model.width != 320) or (config.model.height != 320):
|
|
|
|
raise Exception(
|
|
|
|
"Make sure to set the model width and height to 320 in your config.yml."
|
2023-11-17 02:08:41 +01:00
|
|
|
)
|
2023-11-02 13:55:24 +01:00
|
|
|
|
2024-05-22 00:50:03 +02:00
|
|
|
if config.model.input_pixel_format != "bgr":
|
|
|
|
raise Exception(
|
|
|
|
'Make sure to set the model input_pixel_format to "bgr" in your config.yml.'
|
|
|
|
)
|
2023-11-02 13:55:24 +01:00
|
|
|
|
2024-05-22 00:50:03 +02:00
|
|
|
if config.model.input_tensor != "nhwc":
|
|
|
|
raise Exception(
|
|
|
|
'Make sure to set the model input_tensor to "nhwc" in your config.yml.'
|
|
|
|
)
|
2023-11-02 13:55:24 +01:00
|
|
|
|
|
|
|
def detect_raw(self, tensor_input):
|
2024-05-22 00:50:03 +02:00
|
|
|
output = self.rknn.inference(
|
2023-11-02 13:55:24 +01:00
|
|
|
[
|
|
|
|
tensor_input,
|
|
|
|
]
|
|
|
|
)
|
2024-05-22 00:50:03 +02:00
|
|
|
return self.post_process(output)
|