π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Show warnings for low elevated clip embedding differences
laurenspriem2cb3853ed5b9
diff --git a/infra/ml/test/comparator/compare.py b/infra/ml/test/comparator/compare.pyindex ae966e5e56..66271bd01e 100644--- a/infra/ml/test/comparator/compare.py+++ b/infra/ml/test/comparator/compare.py@@ -94,6 +94,8 @@ def _percentile(values: Sequence[float], percentile: float) -> float:class ThresholdConfig:clip_cosine_distance: float = 0.015cross_platform_clip_cosine_distance: float = 0.015+ clip_warning_cosine_distance: float = 0.035+ cross_platform_clip_warning_cosine_distance: float = 0.035face_embedding_cosine_distance: float = 0.015face_embedding_warning_cosine_distance: float = 0.035box_iou_threshold: float = 0.80@@ -103,6 +105,18 @@ class ThresholdConfig:min_face_score_for_comparison: float = 0.80def __post_init__(self) -> None:+ if self.clip_warning_cosine_distance < self.clip_cosine_distance:+ raise ValueError(+ "clip_warning_cosine_distance must be >= clip_cosine_distance"+ )+ if (+ self.cross_platform_clip_warning_cosine_distance+ < self.cross_platform_clip_cosine_distance+ ):+ raise ValueError(+ "cross_platform_clip_warning_cosine_distance must be >= "+ "cross_platform_clip_cosine_distance"+ )if self.face_embedding_warning_cosine_distance < self.face_embedding_cosine_distance:raise ValueError("face_embedding_warning_cosine_distance must be >= face_embedding_cosine_distance"@@ -112,6 +126,10 @@ class ThresholdConfig:return {"clip_cosine_distance": self.clip_cosine_distance,"cross_platform_clip_cosine_distance": self.cross_platform_clip_cosine_distance,+ "clip_warning_cosine_distance": self.clip_warning_cosine_distance,+ "cross_platform_clip_warning_cosine_distance": (+ self.cross_platform_clip_warning_cosine_distance+ ),"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,@@ -378,11 +396,19 @@ def compare_result_sets(candidate_results: Mapping[str, ParityResult],thresholds: ThresholdConfig | None = None,clip_threshold: float | None = None,+ clip_warning_threshold: float | None = None,) -> ComparisonReport:thresholds = thresholds or ThresholdConfig()clip_threshold = (thresholds.clip_cosine_distance if clip_threshold is None else clip_threshold)+ clip_warning_threshold = (+ thresholds.clip_warning_cosine_distance+ if clip_warning_threshold is None+ else clip_warning_threshold+ )+ if clip_warning_threshold < clip_threshold:+ raise ValueError("clip_warning_threshold must be >= clip_threshold")missing_files = tuple(sorted(set(reference_results) - set(candidate_results)))extra_files = tuple(sorted(set(candidate_results) - set(reference_results)))@@ -504,17 +530,37 @@ def compare_result_sets(clip_distance = cosine_distance(reference.clip.embedding, candidate.clip.embedding)clip_distances.append(clip_distance)+ if clip_distance <= clip_threshold:+ clip_status = STATUS_PASS+ elif clip_distance <= clip_warning_threshold:+ clip_status = STATUS_WARNING+ else:+ clip_status = STATUS_FAILadd_file_metric(file_id=file_id,metric="clip_cosine_distance",value=clip_distance,threshold=clip_threshold,- status=STATUS_PASS if clip_distance <= clip_threshold else STATUS_FAIL,- passed=clip_distance <= clip_threshold,+ status=clip_status,+ passed=clip_status != STATUS_FAIL,direction="<=",message="CLIP cosine distance",)- if clip_distance > clip_threshold:+ if clip_status == STATUS_WARNING:+ add_warning(+ ComparisonFinding(+ file_id=file_id,+ metric="clip_cosine_distance",+ message=(+ "CLIP cosine distance in warning band "+ f"({clip_threshold:.3f} - {clip_warning_threshold:.3f})"+ ),+ value=clip_distance,+ threshold=clip_warning_threshold,+ severity=STATUS_WARNING,+ ),+ )+ elif clip_status == STATUS_FAIL:add_failure(ComparisonFinding(file_id=file_id,@@ -930,7 +976,11 @@ def compare_result_sets()aggregates = {- "clip_cosine_distance": _make_aggregate(clip_distances, clip_threshold),+ "clip_cosine_distance": _make_aggregate(+ clip_distances,+ clip_threshold,+ warning_threshold=clip_warning_threshold,+ ),"face_box_iou_error": _make_aggregate(iou_errors,0.0,@@ -1085,6 +1135,7 @@ def compare_platform_matrix(candidate_results=by_platform[platform],thresholds=thresholds,clip_threshold=thresholds.clip_cosine_distance,+ clip_warning_threshold=thresholds.clip_warning_cosine_distance,),)@@ -1100,6 +1151,9 @@ def compare_platform_matrix(candidate_results=by_platform[right_platform],thresholds=thresholds,clip_threshold=thresholds.cross_platform_clip_cosine_distance,+ clip_warning_threshold=(+ thresholds.cross_platform_clip_warning_cosine_distance+ ),),)diff --git a/infra/ml/test/tests/test_comparator.py b/infra/ml/test/tests/test_comparator.pyindex 5d3ebf15d6..961d69e24f 100644--- a/infra/ml/test/tests/test_comparator.py+++ b/infra/ml/test/tests/test_comparator.py@@ -176,6 +176,63 @@ 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_marks_clip_warning_band() -> None:+ reference = _result(+ file_id="clip-warning.jpeg",+ platform="python",+ clip_seed=10,+ faces=(_face(910, x=0.2, y=0.1, score=0.91),),+ )+ candidate = ParityResult(+ file_id=reference.file_id,+ clip=ClipResult(+ embedding=_jittered_vector(reference.clip.embedding, index=0, delta=0.25)+ ),+ faces=reference.faces,+ runner_metadata=_metadata("ios"),+ )++ report = compare_result_sets(+ reference_platform="python",+ candidate_platform="ios",+ reference_results={reference.file_id: reference},+ candidate_results={candidate.file_id: candidate},+ )++ assert report.status == "warning"+ assert report.passed is True+ assert report.findings == ()+ assert report.warning_files == ("*aggregate*", "clip-warning.jpeg")+ assert report.failing_files == ()+ assert any(+ warning.metric == "clip_cosine_distance"+ and warning.file_id == "clip-warning.jpeg"+ for warning in report.warnings+ )+ assert any(+ warning.metric == "clip_cosine_distance" and warning.file_id == "*aggregate*"+ for warning in report.warnings+ )++ file_status = _status_for_file(report, "clip-warning.jpeg")+ assert file_status.status == "warning"+ assert file_status.passed is True+ assert file_status.failures == ()+ assert any(+ warning.metric == "clip_cosine_distance" for warning in file_status.warnings+ )++ clip_metric = next(+ metric+ for metric in file_status.metrics+ if metric.metric == "clip_cosine_distance"+ )+ assert clip_metric.status == "warning"+ assert clip_metric.passed is True+ assert clip_metric.value is not None+ assert 0.015 < clip_metric.value <= 0.035++def test_compare_result_sets_passes_when_iou_above_single_loose_threshold() -> None:reference_face = _face_with_box(310,@@ -698,3 +755,25 @@ def test_threshold_config_rejects_invalid_warning_threshold() -> None:face_embedding_cosine_distance=0.02,face_embedding_warning_cosine_distance=0.01,)+++def test_threshold_config_rejects_invalid_clip_warning_threshold() -> None:+ with pytest.raises(+ ValueError,+ match="clip_warning_cosine_distance must be >=",+ ):+ ThresholdConfig(+ clip_cosine_distance=0.02,+ clip_warning_cosine_distance=0.01,+ )+++def test_threshold_config_rejects_invalid_cross_platform_clip_warning_threshold() -> None:+ with pytest.raises(+ ValueError,+ match="cross_platform_clip_warning_cosine_distance must be >=",+ ):+ ThresholdConfig(+ cross_platform_clip_cosine_distance=0.02,+ cross_platform_clip_warning_cosine_distance=0.01,+ )