Skip to content

Commit

Permalink
Merge branch 'florence-2-fts-in-workflows' of github.com:roboflow/inf…
Browse files Browse the repository at this point in the history
…erence into florence-2-fts-in-workflows
  • Loading branch information
probicheaux committed Nov 13, 2024
2 parents 917d784 + 5d6ece1 commit 93c8c21
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 7 deletions.
1 change: 1 addition & 0 deletions inference/core/entities/requests/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, **kwargs):
model_config = ConfigDict(protected_namespaces=())
id: str
api_key: Optional[str] = ApiKey
usage_billable: bool = True
start: Optional[float] = None
source: Optional[str] = None
source_info: Optional[str] = None
Expand Down
1 change: 1 addition & 0 deletions inference/core/interfaces/http/http_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,7 @@ async def legacy_infer_from_request(
raise MissingServiceSecretError(
"Service secret is required to disable inference usage tracking"
)
logger.info("Not counting inference for usage")
else:
request_model_id = model_id
logger.debug(
Expand Down
2 changes: 1 addition & 1 deletion inference/core/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.26.0"
__version__ = "0.26.1"


if __name__ == "__main__":
Expand Down
22 changes: 21 additions & 1 deletion inference/core/workflows/core_steps/analytics/line_counter/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

OUTPUT_KEY_COUNT_IN: str = "count_in"
OUTPUT_KEY_COUNT_OUT: str = "count_out"
OUTPUT_KEY_DETECTIONS_IN: str = "detections_in"
OUTPUT_KEY_DETECTIONS_OUT: str = "detections_out"
IN: str = "in"
OUT: str = "out"
DETECTIONS_IN_OUT_PARAM: str = "in_out"
Expand Down Expand Up @@ -86,6 +88,20 @@ def describe_outputs(cls) -> List[OutputDefinition]:
name=OUTPUT_KEY_COUNT_OUT,
kind=[INTEGER_KIND],
),
OutputDefinition(
name=OUTPUT_KEY_DETECTIONS_IN,
kind=[
OBJECT_DETECTION_PREDICTION_KIND,
INSTANCE_SEGMENTATION_PREDICTION_KIND,
],
),
OutputDefinition(
name=OUTPUT_KEY_DETECTIONS_OUT,
kind=[
OBJECT_DETECTION_PREDICTION_KIND,
INSTANCE_SEGMENTATION_PREDICTION_KIND,
],
),
]

@classmethod
Expand Down Expand Up @@ -136,9 +152,13 @@ def run(
)
line_zone = self._batch_of_line_zones[metadata.video_identifier]

line_zone.trigger(detections=detections)
mask_in, mask_out = line_zone.trigger(detections=detections)
detections_in = detections[mask_in]
detections_out = detections[mask_out]

return {
OUTPUT_KEY_COUNT_IN: line_zone.in_count,
OUTPUT_KEY_COUNT_OUT: line_zone.out_count,
OUTPUT_KEY_DETECTIONS_IN: detections_in,
OUTPUT_KEY_DETECTIONS_OUT: detections_out,
}
10 changes: 7 additions & 3 deletions inference/usage_tracking/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import atexit
import json
import mimetypes
import numbers
import socket
import sys
import time
Expand Down Expand Up @@ -315,6 +316,7 @@ def _update_usage_payload(
fps: float = 0,
):
source = str(source) if source else ""
frames = frames if isinstance(frames, numbers.Number) else 0
api_key_hash = self._calculate_api_key_hash(api_key=api_key)
if not resource_id and resource_details:
resource_id = UsageCollector._calculate_resource_hash(resource_details)
Expand All @@ -332,7 +334,9 @@ def _update_usage_payload(
source_usage["timestamp_start"] = time.time_ns()
source_usage["timestamp_stop"] = time.time_ns()
source_usage["processed_frames"] += frames if not inference_test_run else 0
source_usage["fps"] = round(fps, 2)
source_usage["fps"] = (
round(fps, 2) if isinstance(fps, numbers.Number) else 0
)
source_usage["source_duration"] += (
frames / fps if fps and not inference_test_run else 0
)
Expand All @@ -355,7 +359,7 @@ def record_usage(
resource_id: str = "",
inference_test_run: bool = False,
fps: float = 0,
) -> DefaultDict[str, Any]:
):
if not api_key:
return
if self._settings.opt_out and not api_key:
Expand Down Expand Up @@ -388,7 +392,7 @@ async def async_record_usage(
resource_id: str = "",
inference_test_run: bool = False,
fps: float = 0,
) -> DefaultDict[str, Any]:
):
if self._async_lock:
async with self._async_lock:
self.record_usage(
Expand Down
28 changes: 28 additions & 0 deletions tests/inference/unit_tests/usage_tracking/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,3 +855,31 @@ def test_system_info_with_no_dedicated_deployment_id():
}
for k, v in expected_system_info.items():
assert system_info[k] == v


def test_record_malformed_usage():
# given
collector = UsageCollector()

# when
collector.record_usage(
source=None,
category="model",
frames=None,
api_key="fake",
resource_details=None,
resource_id=None,
inference_test_run=None,
fps=None,
)

# then
assert "fake" in collector._usage
assert "model:None" in collector._usage["fake"]
assert collector._usage["fake"]["model:None"]["processed_frames"] == 0
assert collector._usage["fake"]["model:None"]["fps"] == 0
assert collector._usage["fake"]["model:None"]["source_duration"] == 0
assert collector._usage["fake"]["model:None"]["category"] == "model"
assert collector._usage["fake"]["model:None"]["resource_id"] == None
assert collector._usage["fake"]["model:None"]["resource_details"] == "{}"
assert collector._usage["fake"]["model:None"]["api_key_hash"] == "fake"
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def test_line_counter() -> None:
)

# then
assert frame1_result == {"count_in": 0, "count_out": 0}
assert frame2_result == {"count_in": 1, "count_out": 1}
assert frame1_result == {"count_in": 0, "count_out": 0, "detections_in": frame1_detections[[False, False, False, False]], "detections_out": frame1_detections[[False, False, False, False]]}
assert frame2_result == {"count_in": 1, "count_out": 1, "detections_in": frame2_detections[[True, False, False, False]], "detections_out": frame2_detections[[False, True, False, False]]}


def test_line_counter_no_trackers() -> None:
Expand Down

0 comments on commit 93c8c21

Please sign in to comment.