Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Ease iou requirements of faces

laurenspriemcd1d2eb2b30c
diff --git a/infra/ml/test/comparator/compare.py b/infra/ml/test/comparator/compare.py
index 021321e723..5ccab8023f 100644
--- a/infra/ml/test/comparator/compare.py
+++ b/infra/ml/test/comparator/compare.py
@@ -91,7 +91,8 @@ class ThresholdConfig:
clip_cosine_distance: float = 0.015
cross_platform_clip_cosine_distance: float = 0.015
face_embedding_cosine_distance: float = 0.015
- box_iou_threshold: float = 0.98
+ box_iou_threshold: float = 0.80
+ face_match_iou_floor: float = 0.05
landmark_error_threshold: float = 0.03
score_delta_threshold: float = 0.10
@@ -101,6 +102,7 @@ class ThresholdConfig:
"cross_platform_clip_cosine_distance": self.cross_platform_clip_cosine_distance,
"face_embedding_cosine_distance": self.face_embedding_cosine_distance,
"box_iou_threshold": self.box_iou_threshold,
+ "face_match_iou_floor": self.face_match_iou_floor,
"landmark_error_threshold": self.landmark_error_threshold,
"score_delta_threshold": self.score_delta_threshold,
}
@@ -284,6 +286,8 @@ def _index_results(
def _match_faces(
reference_faces: Sequence[FaceResult],
candidate_faces: Sequence[FaceResult],
+ *,
+ min_iou_for_match: float = 0.0,
) -> list[tuple[int, int, float]]:
pairs: list[tuple[float, int, int]] = []
for reference_index, reference_face in enumerate(reference_faces):
@@ -296,6 +300,8 @@ def _match_faces(
used_candidate: set[int] = set()
matches: list[tuple[int, int, float]] = []
for iou, reference_index, candidate_index in pairs:
+ if iou < min_iou_for_match:
+ break
if reference_index in used_reference or candidate_index in used_candidate:
continue
used_reference.add(reference_index)
@@ -460,18 +466,86 @@ def compare_result_sets(
),
)
- matches = _match_faces(reference.faces, candidate.faces)
+ matches = _match_faces(
+ reference.faces,
+ candidate.faces,
+ min_iou_for_match=thresholds.face_match_iou_floor,
+ )
+ maximum_possible_matches = min(reference_face_count, candidate_face_count)
add_file_metric(
file_id=file_id,
metric="matched_face_count",
value=float(len(matches)),
+ threshold=float(maximum_possible_matches),
+ passed=len(matches) == maximum_possible_matches,
+ direction="==",
+ count=len(matches),
+ message="Greedy IoU-gated face matches",
+ )
+
+ unmatched_reference_face_count = reference_face_count - len(
+ {reference_index for reference_index, _, _ in matches}
+ )
+ unmatched_candidate_face_count = candidate_face_count - len(
+ {candidate_index for _, candidate_index, _ in matches}
+ )
+ add_file_metric(
+ file_id=file_id,
+ metric="unmatched_reference_face_count",
+ value=float(unmatched_reference_face_count),
+ threshold=0.0,
+ passed=unmatched_reference_face_count == 0,
+ direction="<=",
+ message=(
+ "Reference faces without a valid IoU-gated match "
+ f"(IoU floor: {thresholds.face_match_iou_floor:.2f})"
+ ),
+ )
+ add_file_metric(
+ file_id=file_id,
+ metric="unmatched_candidate_face_count",
+ value=float(unmatched_candidate_face_count),
+ threshold=0.0,
+ passed=unmatched_candidate_face_count == 0,
+ direction="<=",
+ message=(
+ "Candidate faces without a valid IoU-gated match "
+ f"(IoU floor: {thresholds.face_match_iou_floor:.2f})"
+ ),
+ )
+
+ if unmatched_reference_face_count > 0:
+ add_finding(
+ ComparisonFinding(
+ file_id=file_id,
+ metric="unmatched_reference_face_count",
+ message="Unmatched reference faces after IoU-gated matching",
+ value=float(unmatched_reference_face_count),
+ threshold=0.0,
+ ),
+ )
+ if unmatched_candidate_face_count > 0:
+ add_finding(
+ ComparisonFinding(
+ file_id=file_id,
+ metric="unmatched_candidate_face_count",
+ message="Unmatched candidate faces after IoU-gated matching",
+ value=float(unmatched_candidate_face_count),
+ threshold=0.0,
+ ),
+ )
+
+ add_file_metric(
+ file_id=file_id,
+ metric="face_match_iou_floor",
+ value=thresholds.face_match_iou_floor,
threshold=None,
passed=True,
- count=len(matches),
- message="Greedy IoU face matches",
+ message="Minimum IoU required to keep a match",
)
file_ious: list[float] = []
+ file_iou_shortfalls: list[float] = []
file_landmark_errors: list[float] = []
file_score_deltas: list[float] = []
file_embedding_distances: list[float] = []
@@ -479,17 +553,27 @@ def compare_result_sets(
reference_face = reference.faces[reference_index]
candidate_face = candidate.faces[candidate_index]
+ embedding_distance = cosine_distance(
+ reference_face.embedding,
+ candidate_face.embedding,
+ )
+ file_embedding_distances.append(embedding_distance)
+ face_embedding_distances.append(embedding_distance)
+
+ iou_threshold = thresholds.box_iou_threshold
+
file_ious.append(iou)
- iou_error = 1.0 - iou
- iou_errors.append(iou_error)
- if iou < thresholds.box_iou_threshold:
+ iou_shortfall = max(0.0, iou_threshold - iou)
+ file_iou_shortfalls.append(iou_shortfall)
+ iou_errors.append(iou_shortfall)
+ if iou_shortfall > 0:
add_finding(
ComparisonFinding(
file_id=file_id,
metric="face_box_iou",
message="Face box IoU below threshold",
value=iou,
- threshold=thresholds.box_iou_threshold,
+ threshold=iou_threshold,
),
)
@@ -534,12 +618,6 @@ def compare_result_sets(
),
)
- embedding_distance = cosine_distance(
- reference_face.embedding,
- candidate_face.embedding,
- )
- file_embedding_distances.append(embedding_distance)
- face_embedding_distances.append(embedding_distance)
if embedding_distance > thresholds.face_embedding_cosine_distance:
add_finding(
ComparisonFinding(
@@ -557,20 +635,40 @@ def compare_result_sets(
file_id=file_id,
metric="face_box_iou_min",
value=min_iou,
- threshold=thresholds.box_iou_threshold,
- passed=min_iou >= thresholds.box_iou_threshold,
- direction=">=",
+ threshold=None,
+ passed=True,
count=len(file_ious),
message="Minimum IoU across matched faces",
)
+ max_iou_shortfall = max(file_iou_shortfalls)
+ add_file_metric(
+ file_id=file_id,
+ metric="face_box_iou_shortfall_max",
+ value=max_iou_shortfall,
+ threshold=0.0,
+ passed=max_iou_shortfall <= 0.0,
+ direction="<=",
+ count=len(file_iou_shortfalls),
+ message="Maximum IoU shortfall against size-aware thresholds",
+ )
else:
add_file_metric(
file_id=file_id,
metric="face_box_iou_min",
value=None,
- threshold=thresholds.box_iou_threshold,
+ threshold=None,
passed=reference_face_count == 0 and candidate_face_count == 0,
- direction=">=",
+ count=0,
+ applicable=False,
+ message="No matched faces",
+ )
+ add_file_metric(
+ file_id=file_id,
+ metric="face_box_iou_shortfall_max",
+ value=None,
+ threshold=0.0,
+ passed=reference_face_count == 0 and candidate_face_count == 0,
+ direction="<=",
count=0,
applicable=False,
message="No matched faces",
@@ -655,7 +753,7 @@ def compare_result_sets(
"clip_cosine_distance": _make_aggregate(clip_distances, clip_threshold),
"face_box_iou_error": _make_aggregate(
iou_errors,
- 1.0 - thresholds.box_iou_threshold,
+ 0.0,
),
"landmark_error": _make_aggregate(
landmark_errors,
diff --git a/infra/ml/test/tests/test_comparator.py b/infra/ml/test/tests/test_comparator.py
index 6e05b9b81c..07018acc51 100644
--- a/infra/ml/test/tests/test_comparator.py
+++ b/infra/ml/test/tests/test_comparator.py
@@ -53,6 +53,29 @@ def _face(seed: int, *, x: float, y: float, score: float) -> FaceResult:
)
+def _face_with_box(
+ seed: int,
+ *,
+ x: float,
+ y: float,
+ width: float,
+ height: float,
+ score: float,
+) -> FaceResult:
+ return FaceResult(
+ box=(x, y, width, height),
+ landmarks=(
+ (x + width * 0.30, y + height * 0.35),
+ (x + width * 0.70, y + height * 0.35),
+ (x + width * 0.50, y + height * 0.55),
+ (x + width * 0.35, y + height * 0.78),
+ (x + width * 0.65, y + height * 0.78),
+ ),
+ score=score,
+ embedding=_unit_vector(192, seed=seed),
+ )
+
+
def _result(
*,
file_id: str,
@@ -148,6 +171,108 @@ def test_compare_result_sets_fails_on_clip_drift() -> None:
assert any(finding.metric == "clip_cosine_distance" for finding in report.findings)
+def test_compare_result_sets_passes_when_iou_above_single_loose_threshold() -> None:
+ reference_face = _face_with_box(
+ 310,
+ x=0.20,
+ y=0.20,
+ width=0.20,
+ height=0.20,
+ score=0.92,
+ )
+ reference = _result(
+ file_id="loose-iou-face.webp",
+ platform="python",
+ clip_seed=11,
+ faces=(reference_face,),
+ )
+ candidate_face = _face_with_box(
+ 310,
+ x=0.21,
+ y=0.21,
+ width=0.20,
+ height=0.20,
+ score=0.92,
+ )
+ candidate = ParityResult(
+ file_id=reference.file_id,
+ clip=reference.clip,
+ faces=(candidate_face,),
+ runner_metadata=_metadata("android"),
+ )
+
+ report = compare_result_sets(
+ reference_platform="python",
+ candidate_platform="android",
+ reference_results={reference.file_id: reference},
+ candidate_results={candidate.file_id: candidate},
+ )
+
+ assert report.passed is True
+ assert not any(finding.metric == "face_box_iou" for finding in report.findings)
+ file_status = report.file_statuses[0]
+ iou_shortfall_metric = next(
+ metric
+ for metric in file_status.metrics
+ if metric.metric == "face_box_iou_shortfall_max"
+ )
+ assert iou_shortfall_metric.passed is True
+ assert iou_shortfall_metric.value == pytest.approx(0.0)
+
+
+def test_compare_result_sets_fails_when_iou_below_single_loose_threshold() -> None:
+ reference_face = _face_with_box(
+ 701,
+ x=0.20,
+ y=0.20,
+ width=0.20,
+ height=0.20,
+ score=0.92,
+ )
+ reference = _result(
+ file_id="loose-iou-fail-face.webp",
+ platform="python",
+ clip_seed=21,
+ faces=(reference_face,),
+ )
+ candidate_face = _face_with_box(
+ 999,
+ x=0.23,
+ y=0.23,
+ width=0.20,
+ height=0.20,
+ score=0.92,
+ )
+ candidate = ParityResult(
+ file_id=reference.file_id,
+ clip=reference.clip,
+ faces=(candidate_face,),
+ runner_metadata=_metadata("android"),
+ )
+
+ report = compare_result_sets(
+ reference_platform="python",
+ candidate_platform="android",
+ reference_results={reference.file_id: reference},
+ candidate_results={candidate.file_id: candidate},
+ )
+
+ assert report.passed is False
+ iou_failures = [finding for finding in report.findings if finding.metric == "face_box_iou"]
+ assert iou_failures
+ assert iou_failures[0].threshold == pytest.approx(0.80)
+
+ file_status = report.file_statuses[0]
+ iou_shortfall_metric = next(
+ metric
+ for metric in file_status.metrics
+ if metric.metric == "face_box_iou_shortfall_max"
+ )
+ assert iou_shortfall_metric.passed is False
+ assert iou_shortfall_metric.value is not None
+ assert iou_shortfall_metric.value > 0.0
+
+
def test_compare_result_sets_fails_on_face_count_mismatch() -> None:
reference = _result(
file_id="people.jpeg",
@@ -189,6 +314,77 @@ def test_compare_result_sets_fails_on_face_count_mismatch() -> None:
assert face_count_metric.passed is False
+def test_compare_result_sets_reports_unmatched_faces_after_iou_gating() -> None:
+ reference = _result(
+ file_id="offset-face.jpeg",
+ platform="python",
+ clip_seed=19,
+ faces=(
+ _face_with_box(
+ 901,
+ x=0.10,
+ y=0.10,
+ width=0.20,
+ height=0.20,
+ score=0.88,
+ ),
+ ),
+ )
+ candidate = _result(
+ file_id="offset-face.jpeg",
+ platform="desktop",
+ clip_seed=19,
+ faces=(
+ _face_with_box(
+ 902,
+ x=0.70,
+ y=0.70,
+ width=0.20,
+ height=0.20,
+ score=0.88,
+ ),
+ ),
+ )
+
+ report = compare_result_sets(
+ reference_platform="python",
+ candidate_platform="desktop",
+ reference_results={reference.file_id: reference},
+ candidate_results={candidate.file_id: candidate},
+ )
+
+ assert report.passed is False
+ assert any(
+ finding.metric == "unmatched_reference_face_count" for finding in report.findings
+ )
+ assert any(
+ finding.metric == "unmatched_candidate_face_count" for finding in report.findings
+ )
+ assert not any(finding.metric == "face_box_iou" for finding in report.findings)
+
+ file_status = report.file_statuses[0]
+ matched_metric = next(
+ metric for metric in file_status.metrics if metric.metric == "matched_face_count"
+ )
+ assert matched_metric.value == pytest.approx(0.0)
+ assert matched_metric.passed is False
+
+ unmatched_reference_metric = next(
+ metric
+ for metric in file_status.metrics
+ if metric.metric == "unmatched_reference_face_count"
+ )
+ unmatched_candidate_metric = next(
+ metric
+ for metric in file_status.metrics
+ if metric.metric == "unmatched_candidate_face_count"
+ )
+ assert unmatched_reference_metric.value == pytest.approx(1.0)
+ assert unmatched_candidate_metric.value == pytest.approx(1.0)
+ assert unmatched_reference_metric.passed is False
+ assert unmatched_candidate_metric.passed is False
+
+
def test_aggregate_gate_failure_is_reported() -> None:
reference_results = {}
candidate_results = {}