Skip to content

armgit / ente

Public Imported from GitHub
0

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

Better face matching between platform

laurenspriem951c8f2d8f07
diff --git a/infra/ml/test/comparator/compare.py b/infra/ml/test/comparator/compare.py
index 04b56d0f59..10fa9b1204 100644
--- a/infra/ml/test/comparator/compare.py
+++ b/infra/ml/test/comparator/compare.py
@@ -107,9 +107,10 @@ class ThresholdConfig:
face_embedding_cosine_distance: float = 0.015
face_embedding_warning_cosine_distance: float = 0.035
box_iou_threshold: float = 0.80
+ face_duplicate_iou_threshold: float = 0.80
face_match_iou_floor: float = 0.05
landmark_error_threshold: float = 0.03
- score_delta_threshold: float = 0.10
+ score_delta_threshold: float = 0.05
min_face_score_for_comparison: float = 0.80
def __post_init__(self) -> None:
@@ -141,6 +142,7 @@ class ThresholdConfig:
"face_embedding_cosine_distance": self.face_embedding_cosine_distance,
"face_embedding_warning_cosine_distance": self.face_embedding_warning_cosine_distance,
"box_iou_threshold": self.box_iou_threshold,
+ "face_duplicate_iou_threshold": self.face_duplicate_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,
@@ -396,6 +398,27 @@ def _match_faces(
return matches
+def _dedupe_faces(
+ faces: Sequence[FaceResult],
+ *,
+ min_iou_for_duplicate: float,
+) -> tuple[FaceResult, ...]:
+ kept: list[tuple[int, FaceResult]] = []
+ for face_index, face in sorted(
+ enumerate(faces),
+ key=lambda entry: (-entry[1].score, entry[0]),
+ ):
+ if any(
+ _box_iou(face.box, kept_face.box) >= min_iou_for_duplicate
+ for _, kept_face in kept
+ ):
+ continue
+ kept.append((face_index, face))
+
+ kept.sort(key=lambda entry: entry[0])
+ return tuple(face for _, face in kept)
+
+
def compare_result_sets(
*,
reference_platform: str,
@@ -563,15 +586,13 @@ def compare_result_sets(
reference = reference_results[file_id]
candidate = candidate_results[file_id]
- reference_faces = tuple(
- face
- for face in reference.faces
- if face.score >= thresholds.min_face_score_for_comparison
+ reference_faces = _dedupe_faces(
+ reference.faces,
+ min_iou_for_duplicate=thresholds.face_duplicate_iou_threshold,
)
- candidate_faces = tuple(
- face
- for face in candidate.faces
- if face.score >= thresholds.min_face_score_for_comparison
+ candidate_faces = _dedupe_faces(
+ candidate.faces,
+ min_iou_for_duplicate=thresholds.face_duplicate_iou_threshold,
)
clip_distance = cosine_distance(reference.clip.embedding, candidate.clip.embedding)
@@ -613,8 +634,52 @@ def compare_result_sets(
),
)
- reference_face_count = len(reference_faces)
- candidate_face_count = len(candidate_faces)
+ all_matches = _match_faces(
+ reference_faces,
+ candidate_faces,
+ min_iou_for_match=thresholds.face_match_iou_floor,
+ )
+ matched_reference_indices = {
+ reference_index for reference_index, _, _ in all_matches
+ }
+ matched_candidate_indices = {
+ candidate_index for _, candidate_index, _ in all_matches
+ }
+ relevant_matches = tuple(
+ (reference_index, candidate_index, iou)
+ for reference_index, candidate_index, iou in all_matches
+ if (
+ reference_faces[reference_index].score
+ >= thresholds.min_face_score_for_comparison
+ or candidate_faces[candidate_index].score
+ >= thresholds.min_face_score_for_comparison
+ )
+ )
+ unmatched_reference_indices = tuple(
+ reference_index
+ for reference_index, face in enumerate(reference_faces)
+ if (
+ face.score >= thresholds.min_face_score_for_comparison
+ and reference_index not in matched_reference_indices
+ )
+ )
+ unmatched_candidate_indices = tuple(
+ candidate_index
+ for candidate_index, face in enumerate(candidate_faces)
+ if (
+ face.score >= thresholds.min_face_score_for_comparison
+ and candidate_index not in matched_candidate_indices
+ )
+ )
+ comparable_reference_indices = {
+ reference_index for reference_index, _, _ in relevant_matches
+ } | set(unmatched_reference_indices)
+ comparable_candidate_indices = {
+ candidate_index for _, candidate_index, _ in relevant_matches
+ } | set(unmatched_candidate_indices)
+
+ reference_face_count = len(comparable_reference_indices)
+ candidate_face_count = len(comparable_candidate_indices)
no_faces = reference_face_count == 0 and candidate_face_count == 0
add_file_metric(
file_id=file_id,
@@ -624,8 +689,8 @@ def compare_result_sets(
status=STATUS_PASS,
passed=True,
message=(
- f"{reference_platform} face count (score >= "
- f"{thresholds.min_face_score_for_comparison:.2f})"
+ f"{reference_platform} comparable face count "
+ f"(matched if either score >= {thresholds.min_face_score_for_comparison:.2f})"
),
)
add_file_metric(
@@ -636,8 +701,8 @@ def compare_result_sets(
status=STATUS_PASS,
passed=True,
message=(
- f"{candidate_platform} face count (score >= "
- f"{thresholds.min_face_score_for_comparison:.2f})"
+ f"{candidate_platform} comparable face count "
+ f"(matched if either score >= {thresholds.min_face_score_for_comparison:.2f})"
),
)
face_count_delta = abs(reference_face_count - candidate_face_count)
@@ -663,30 +728,25 @@ def compare_result_sets(
),
)
- 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)),
+ value=float(len(relevant_matches)),
threshold=float(maximum_possible_matches),
- status=STATUS_PASS if len(matches) == maximum_possible_matches else STATUS_FAIL,
- passed=len(matches) == maximum_possible_matches,
+ status=(
+ STATUS_PASS
+ if len(relevant_matches) == maximum_possible_matches
+ else STATUS_FAIL
+ ),
+ passed=len(relevant_matches) == maximum_possible_matches,
direction="==",
- count=len(matches),
+ count=len(relevant_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}
- )
+ unmatched_reference_face_count = len(unmatched_reference_indices)
+ unmatched_candidate_face_count = len(unmatched_candidate_indices)
add_file_metric(
file_id=file_id,
metric="unmatched_reference_face_count",
@@ -752,7 +812,7 @@ def compare_result_sets(
file_landmark_errors: list[float] = []
file_score_deltas: list[float] = []
file_embedding_distances: list[float] = []
- for reference_index, candidate_index, iou in matches:
+ for reference_index, candidate_index, iou in relevant_matches:
reference_face = reference_faces[reference_index]
candidate_face = candidate_faces[candidate_index]
diff --git a/infra/ml/test/tests/test_comparator.py b/infra/ml/test/tests/test_comparator.py
index 961d69e24f..f9bd8fc6b3 100644
--- a/infra/ml/test/tests/test_comparator.py
+++ b/infra/ml/test/tests/test_comparator.py
@@ -426,6 +426,143 @@ def test_compare_result_sets_ignores_faces_below_min_score_threshold() -> None:
assert not any(finding.metric == "face_count" for finding in report.findings)
+def test_compare_result_sets_includes_matched_face_when_either_score_crosses_threshold() -> None:
+ reference_face = _face(520, x=0.34, y=0.18, score=0.79)
+ candidate_face = _face(520, x=0.3405, y=0.1805, score=0.83)
+ reference = _result(
+ file_id="near-threshold-face.jpeg",
+ platform="python",
+ clip_seed=42,
+ faces=(reference_face,),
+ )
+ candidate = _result(
+ file_id="near-threshold-face.jpeg",
+ platform="ios",
+ clip_seed=42,
+ faces=(candidate_face,),
+ )
+
+ report = compare_result_sets(
+ reference_platform="python",
+ candidate_platform="ios",
+ reference_results={reference.file_id: reference},
+ candidate_results={candidate.file_id: candidate},
+ )
+
+ assert report.passed is True
+ assert report.findings == ()
+ file_status = _status_for_file(report, "near-threshold-face.jpeg")
+ reference_count = next(
+ metric for metric in file_status.metrics if metric.metric == "reference_face_count"
+ )
+ candidate_count = next(
+ metric for metric in file_status.metrics if metric.metric == "candidate_face_count"
+ )
+ score_delta = next(
+ metric for metric in file_status.metrics if metric.metric == "score_delta_max"
+ )
+ assert reference_count.value == pytest.approx(1.0)
+ assert candidate_count.value == pytest.approx(1.0)
+ assert score_delta.value == pytest.approx(0.04)
+ assert score_delta.passed is True
+
+
+def test_compare_result_sets_dedupes_platform_faces_before_matching() -> None:
+ reference_face = _face_with_box(
+ 540,
+ x=0.20,
+ y=0.20,
+ width=0.20,
+ height=0.20,
+ score=0.92,
+ )
+ low_score_duplicate = _face_with_box(
+ 999,
+ x=0.20,
+ y=0.20,
+ width=0.20,
+ height=0.20,
+ score=0.20,
+ )
+ valid_candidate_face = _face_with_box(
+ 540,
+ x=0.202,
+ y=0.202,
+ width=0.20,
+ height=0.20,
+ score=0.91,
+ )
+ reference = _result(
+ file_id="duplicate-face.jpeg",
+ platform="python",
+ clip_seed=44,
+ faces=(reference_face,),
+ )
+ candidate = _result(
+ file_id="duplicate-face.jpeg",
+ platform="ios",
+ clip_seed=44,
+ faces=(low_score_duplicate, valid_candidate_face),
+ )
+
+ report = compare_result_sets(
+ reference_platform="python",
+ candidate_platform="ios",
+ reference_results={reference.file_id: reference},
+ candidate_results={candidate.file_id: candidate},
+ )
+
+ assert report.passed is True
+ assert report.findings == ()
+ file_status = _status_for_file(report, "duplicate-face.jpeg")
+ candidate_count = next(
+ metric for metric in file_status.metrics if metric.metric == "candidate_face_count"
+ )
+ matched_count = next(
+ metric for metric in file_status.metrics if metric.metric == "matched_face_count"
+ )
+ score_delta = next(
+ metric for metric in file_status.metrics if metric.metric == "score_delta_max"
+ )
+ assert candidate_count.value == pytest.approx(1.0)
+ assert matched_count.value == pytest.approx(1.0)
+ assert score_delta.value == pytest.approx(0.01)
+
+
+def test_compare_result_sets_fails_large_score_delta_for_matched_threshold_face() -> None:
+ reference_face = _face(530, x=0.34, y=0.18, score=0.79)
+ candidate_face = _face(530, x=0.3405, y=0.1805, score=0.86)
+ reference = _result(
+ file_id="score-delta-face.jpeg",
+ platform="python",
+ clip_seed=43,
+ faces=(reference_face,),
+ )
+ candidate = _result(
+ file_id="score-delta-face.jpeg",
+ platform="ios",
+ clip_seed=43,
+ faces=(candidate_face,),
+ )
+
+ report = compare_result_sets(
+ reference_platform="python",
+ candidate_platform="ios",
+ reference_results={reference.file_id: reference},
+ candidate_results={candidate.file_id: candidate},
+ )
+
+ assert report.passed is False
+ assert any(finding.metric == "score_delta" for finding in report.findings)
+ file_status = _status_for_file(report, "score-delta-face.jpeg")
+ score_delta = next(
+ metric for metric in file_status.metrics if metric.metric == "score_delta_max"
+ )
+ assert score_delta.value == pytest.approx(0.07)
+ assert score_delta.threshold == pytest.approx(0.05)
+ assert score_delta.passed is False
+
+
def test_compare_result_sets_marks_face_embedding_warning_band() -> None:
reference_face = _face(620, x=0.24, y=0.20, score=0.93)
candidate_face = FaceResult(