mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-12-04 19:22:40 +03:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
596ab2be9b | ||
|
|
90c43d6679 |
+1
-3
@@ -117,9 +117,7 @@ class FrigateApp:
|
||||
self.log_queue: Queue = mp.Queue()
|
||||
self.camera_metrics: DictProxy = self.metrics_manager.dict()
|
||||
|
||||
self.embeddings_metrics = DataProcessorMetrics(
|
||||
self.metrics_manager, list(config.classification.custom.keys())
|
||||
)
|
||||
self.embeddings_metrics = DataProcessorMetrics(self.metrics_manager)
|
||||
self.ptz_metrics: dict[str, PTZMetrics] = {}
|
||||
self.processes: dict[str, int] = {}
|
||||
self.embeddings: EmbeddingsContext | None = None
|
||||
|
||||
@@ -16,7 +16,7 @@ from frigate.config import FrigateConfig
|
||||
from frigate.config.classification import CustomClassificationConfig
|
||||
from frigate.const import CLIPS_DIR, MODEL_CACHE_DIR
|
||||
from frigate.log import suppress_stderr_during
|
||||
from frigate.util.builtin import EventsPerSecond, InferenceSpeed, load_labels
|
||||
from frigate.util.builtin import EventsPerSecond, load_labels
|
||||
from frigate.util.classification import resize_classification_image
|
||||
from frigate.util.file import trim_oldest_files
|
||||
from frigate.util.image import calculate_region, write_image
|
||||
@@ -58,16 +58,11 @@ class CustomStateClassificationProcessor(DeferredRealtimeProcessorApi):
|
||||
self.labelmap: dict[int, str] = {}
|
||||
self.classifications_per_second = EventsPerSecond()
|
||||
self.state_history: dict[str, dict[str, Any]] = {}
|
||||
self.inference_speed: float | None = None
|
||||
|
||||
if (
|
||||
self.metrics
|
||||
and self.model_config.name in self.metrics.classification_speeds
|
||||
):
|
||||
self.inference_speed: InferenceSpeed | None = InferenceSpeed(
|
||||
self.metrics.classification_speeds[self.model_config.name]
|
||||
)
|
||||
else:
|
||||
self.inference_speed = None
|
||||
if self.metrics:
|
||||
self.metrics.classification_speeds[self.model_config.name] = 0.0
|
||||
self.metrics.classification_cps[self.model_config.name] = 0.0
|
||||
|
||||
# monotonic so a backward wall clock step can't pause classification
|
||||
self.last_run = time.monotonic()
|
||||
@@ -113,8 +108,16 @@ class CustomStateClassificationProcessor(DeferredRealtimeProcessorApi):
|
||||
|
||||
def __update_metrics(self, duration: float) -> None:
|
||||
self.classifications_per_second.update()
|
||||
if self.inference_speed:
|
||||
self.inference_speed.update(duration)
|
||||
self.inference_speed = (
|
||||
duration
|
||||
if self.inference_speed is None
|
||||
else (self.inference_speed * 9 + duration) / 10
|
||||
)
|
||||
|
||||
if self.metrics and self.model_config.name and not self._stop_event.is_set():
|
||||
self.metrics.classification_speeds[self.model_config.name] = (
|
||||
self.inference_speed
|
||||
)
|
||||
|
||||
def _should_save_image(
|
||||
self, camera: str, detected_state: str, score: float = 1.0
|
||||
@@ -199,10 +202,10 @@ class CustomStateClassificationProcessor(DeferredRealtimeProcessorApi):
|
||||
if not self.model_config.name or not self.model_config.state_config:
|
||||
return
|
||||
|
||||
if self.metrics and self.model_config.name in self.metrics.classification_cps:
|
||||
self.metrics.classification_cps[
|
||||
self.model_config.name
|
||||
].value = self.classifications_per_second.eps()
|
||||
if self.metrics:
|
||||
self.metrics.classification_cps[self.model_config.name] = (
|
||||
self.classifications_per_second.eps()
|
||||
)
|
||||
camera = str(frame_data.get("camera"))
|
||||
|
||||
if camera not in self.model_config.state_config.cameras:
|
||||
@@ -429,16 +432,11 @@ class CustomObjectClassificationProcessor(DeferredRealtimeProcessorApi):
|
||||
self.classification_history: dict[str, list[tuple[str, float, float]]] = {}
|
||||
self.labelmap: dict[int, str] = {}
|
||||
self.classifications_per_second = EventsPerSecond()
|
||||
self.inference_speed: float | None = None
|
||||
|
||||
if (
|
||||
self.metrics
|
||||
and self.model_config.name in self.metrics.classification_speeds
|
||||
):
|
||||
self.inference_speed: InferenceSpeed | None = InferenceSpeed(
|
||||
self.metrics.classification_speeds[self.model_config.name]
|
||||
)
|
||||
else:
|
||||
self.inference_speed = None
|
||||
if self.metrics:
|
||||
self.metrics.classification_speeds[self.model_config.name] = 0.0
|
||||
self.metrics.classification_cps[self.model_config.name] = 0.0
|
||||
|
||||
self.__build_detector()
|
||||
|
||||
@@ -466,8 +464,16 @@ class CustomObjectClassificationProcessor(DeferredRealtimeProcessorApi):
|
||||
|
||||
def __update_metrics(self, duration: float) -> None:
|
||||
self.classifications_per_second.update()
|
||||
if self.inference_speed:
|
||||
self.inference_speed.update(duration)
|
||||
self.inference_speed = (
|
||||
duration
|
||||
if self.inference_speed is None
|
||||
else (self.inference_speed * 9 + duration) / 10
|
||||
)
|
||||
|
||||
if self.metrics and self.model_config.name and not self._stop_event.is_set():
|
||||
self.metrics.classification_speeds[self.model_config.name] = (
|
||||
self.inference_speed
|
||||
)
|
||||
|
||||
def get_weighted_score(
|
||||
self,
|
||||
@@ -543,10 +549,10 @@ class CustomObjectClassificationProcessor(DeferredRealtimeProcessorApi):
|
||||
if not self.model_config.name or not self.model_config.object_config:
|
||||
return
|
||||
|
||||
if self.metrics and self.model_config.name in self.metrics.classification_cps:
|
||||
self.metrics.classification_cps[
|
||||
self.model_config.name
|
||||
].value = self.classifications_per_second.eps()
|
||||
if self.metrics:
|
||||
self.metrics.classification_cps[self.model_config.name] = (
|
||||
self.classifications_per_second.eps()
|
||||
)
|
||||
|
||||
if obj_data["false_positive"]:
|
||||
return
|
||||
|
||||
@@ -26,11 +26,11 @@ class DataProcessorMetrics:
|
||||
review_desc_dps: ValueProxy[float]
|
||||
object_desc_speed: ValueProxy[float]
|
||||
object_desc_dps: ValueProxy[float]
|
||||
classification_speeds: DictProxy[str, ValueProxy[float]]
|
||||
classification_cps: DictProxy[str, ValueProxy[float]]
|
||||
classification_speeds: DictProxy[str, float]
|
||||
classification_cps: DictProxy[str, float]
|
||||
runtime_devices: DictProxy[str, str]
|
||||
|
||||
def __init__(self, manager: SyncManager, custom_classification_models: list[str]):
|
||||
def __init__(self, manager: SyncManager):
|
||||
self.image_embeddings_speed = manager.Value("d", 0.0)
|
||||
self.image_embeddings_eps = manager.Value("d", 0.0)
|
||||
self.text_embeddings_speed = manager.Value("d", 0.0)
|
||||
@@ -49,11 +49,6 @@ class DataProcessorMetrics:
|
||||
self.classification_cps = manager.dict()
|
||||
self.runtime_devices = manager.dict()
|
||||
|
||||
if custom_classification_models:
|
||||
for key in custom_classification_models:
|
||||
self.classification_speeds[key] = manager.Value("d", 0.0)
|
||||
self.classification_cps[key] = manager.Value("d", 0.0)
|
||||
|
||||
|
||||
class DataProcessorModelRunner:
|
||||
def __init__(self, requestor: Any, device: str = "CPU", model_size: str = "large"):
|
||||
|
||||
@@ -412,6 +412,8 @@ class EmbeddingMaintainer(threading.Thread):
|
||||
else:
|
||||
remaining.append(processor)
|
||||
self.realtime_processors = remaining
|
||||
self.metrics.classification_speeds.pop(model_name, None)
|
||||
self.metrics.classification_cps.pop(model_name, None)
|
||||
|
||||
def _handle_custom_classification_update(
|
||||
self, topic: str, model_config: Any
|
||||
|
||||
@@ -216,13 +216,12 @@ def embeddings_stats(
|
||||
embeddings_metrics.object_desc_dps.value, 2
|
||||
)
|
||||
|
||||
for key in embeddings_metrics.classification_speeds.keys():
|
||||
stats[f"{key}_classification_speed"] = round(
|
||||
embeddings_metrics.classification_speeds[key].value * 1000, 2
|
||||
)
|
||||
stats[f"{key}_classification_events_per_second"] = round(
|
||||
embeddings_metrics.classification_cps[key].value, 2
|
||||
)
|
||||
speeds = embeddings_metrics.classification_speeds.copy()
|
||||
cps = embeddings_metrics.classification_cps.copy()
|
||||
|
||||
for key, speed in speeds.items():
|
||||
stats[f"{key}_classification_speed"] = round(speed * 1000, 2)
|
||||
stats[f"{key}_classification_events_per_second"] = round(cps.get(key, 0.0), 2)
|
||||
|
||||
devices = dict(embeddings_metrics.runtime_devices)
|
||||
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
"""Tests for custom classification speed and rate metrics."""
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
from multiprocessing import Manager
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
# Mock TFLite before importing the maintainer / classification modules
|
||||
_MOCK_MODULES = [
|
||||
"tflite_runtime",
|
||||
"tflite_runtime.interpreter",
|
||||
"ai_edge_litert",
|
||||
"ai_edge_litert.interpreter",
|
||||
]
|
||||
for mod in _MOCK_MODULES:
|
||||
if mod not in sys.modules:
|
||||
sys.modules[mod] = MagicMock()
|
||||
|
||||
from frigate.data_processing.real_time.custom_classification import ( # noqa: E402
|
||||
CustomObjectClassificationProcessor,
|
||||
CustomStateClassificationProcessor,
|
||||
)
|
||||
from frigate.data_processing.types import DataProcessorMetrics # noqa: E402
|
||||
from frigate.embeddings.maintainer import EmbeddingMaintainer # noqa: E402
|
||||
from frigate.stats.util import embeddings_stats # noqa: E402
|
||||
|
||||
|
||||
class TestClassificationMetrics(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.manager = Manager()
|
||||
self.metrics = DataProcessorMetrics(self.manager)
|
||||
|
||||
def tearDown(self):
|
||||
self.manager.shutdown()
|
||||
|
||||
def _make_object_processor(self, name: str) -> CustomObjectClassificationProcessor:
|
||||
model_config = MagicMock()
|
||||
model_config.name = name
|
||||
|
||||
with patch.object(
|
||||
CustomObjectClassificationProcessor,
|
||||
"_CustomObjectClassificationProcessor__build_detector",
|
||||
):
|
||||
processor = CustomObjectClassificationProcessor(
|
||||
MagicMock(), model_config, MagicMock(), MagicMock(), self.metrics
|
||||
)
|
||||
|
||||
self.addCleanup(processor.shutdown)
|
||||
return processor
|
||||
|
||||
def _make_state_processor(self, name: str) -> CustomStateClassificationProcessor:
|
||||
model_config = MagicMock()
|
||||
model_config.name = name
|
||||
model_config.state_config.cameras = {}
|
||||
|
||||
with patch.object(
|
||||
CustomStateClassificationProcessor,
|
||||
"_CustomStateClassificationProcessor__build_detector",
|
||||
):
|
||||
processor = CustomStateClassificationProcessor(
|
||||
MagicMock(), model_config, MagicMock(), self.metrics
|
||||
)
|
||||
|
||||
self.addCleanup(processor.shutdown)
|
||||
return processor
|
||||
|
||||
def _stats(self) -> dict:
|
||||
config = MagicMock()
|
||||
config.semantic_search.enabled = False
|
||||
config.face_recognition.enabled = False
|
||||
config.lpr.enabled = False
|
||||
return embeddings_stats(config, self.metrics)
|
||||
|
||||
def test_model_added_after_startup_reports_stats(self):
|
||||
self._make_object_processor("breed")
|
||||
self._make_state_processor("gate")
|
||||
|
||||
stats = self._stats()
|
||||
|
||||
self.assertEqual(stats["breed_classification_speed"], 0.0)
|
||||
self.assertEqual(stats["breed_classification_events_per_second"], 0.0)
|
||||
self.assertEqual(stats["gate_classification_speed"], 0.0)
|
||||
self.assertEqual(stats["gate_classification_events_per_second"], 0.0)
|
||||
|
||||
def test_inference_speed_is_averaged(self):
|
||||
processor = self._make_object_processor("breed")
|
||||
|
||||
processor._CustomObjectClassificationProcessor__update_metrics(0.010)
|
||||
processor._CustomObjectClassificationProcessor__update_metrics(0.020)
|
||||
|
||||
self.assertAlmostEqual(self.metrics.classification_speeds["breed"], 0.011)
|
||||
self.assertEqual(self._stats()["breed_classification_speed"], 11.0)
|
||||
|
||||
def test_process_frame_writes_rate(self):
|
||||
processor = self._make_state_processor("gate")
|
||||
processor.classifications_per_second = MagicMock()
|
||||
processor.classifications_per_second.eps.return_value = 2.5
|
||||
|
||||
processor.process_frame({"camera": "front"}, MagicMock())
|
||||
|
||||
self.assertEqual(self.metrics.classification_cps["gate"], 2.5)
|
||||
|
||||
def test_removed_model_drops_stats(self):
|
||||
maintainer = EmbeddingMaintainer.__new__(EmbeddingMaintainer)
|
||||
maintainer.metrics = self.metrics
|
||||
maintainer.realtime_processors = [
|
||||
self._make_object_processor("breed"),
|
||||
self._make_state_processor("gate"),
|
||||
]
|
||||
|
||||
maintainer._remove_custom_classification_processor("breed")
|
||||
|
||||
stats = self._stats()
|
||||
self.assertNotIn("breed_classification_speed", stats)
|
||||
self.assertNotIn("breed_classification_events_per_second", stats)
|
||||
self.assertIn("gate_classification_speed", stats)
|
||||
|
||||
def test_worker_finishing_after_removal_does_not_restore_stats(self):
|
||||
processor = self._make_object_processor("breed")
|
||||
maintainer = EmbeddingMaintainer.__new__(EmbeddingMaintainer)
|
||||
maintainer.metrics = self.metrics
|
||||
maintainer.realtime_processors = [processor]
|
||||
|
||||
maintainer._remove_custom_classification_processor("breed")
|
||||
processor._CustomObjectClassificationProcessor__update_metrics(0.010)
|
||||
|
||||
self.assertNotIn("breed_classification_speed", self._stats())
|
||||
|
||||
def test_stats_tolerate_missing_rate(self):
|
||||
self.metrics.classification_speeds["breed"] = 0.01
|
||||
|
||||
stats = self._stats()
|
||||
|
||||
self.assertEqual(stats["breed_classification_events_per_second"], 0.0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user