feat: face clustering pipeline improvements and Rust/Swift face processing
- Add Rust face processor module (detection, embedding, tracking, qdrant) - Add Facenet CoreML/ONNX conversion scripts and tests - Refactor swift_face into Sources/swift_face with ArgumentParser support - Update store_traced_faces to handle face.json correctly - Fix ASRX parsing and TKG bbox reading from Qdrant - Improve admin_config and trace_agent APIs - Add M4 workspace docs for face clustering, dedup, and Facenet validation - Fix worker heartbeat TTL (poll_interval x 3, min 60s) for offline detection
This commit is contained in:
Executable
+77
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Validate ONNX model accuracy against PyTorch baseline
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import torch
|
||||
import numpy as np
|
||||
import onnxruntime as ort
|
||||
from facenet_pytorch import InceptionResnetV1
|
||||
from scipy.spatial.distance import cosine
|
||||
|
||||
def validate_accuracy(onnx_path: str, num_tests: int = 10):
|
||||
"""Compare PyTorch vs ONNX embeddings"""
|
||||
|
||||
print(f"[VALIDATE] Loading models...")
|
||||
|
||||
# Load PyTorch model
|
||||
pytorch_model = InceptionResnetV1(pretrained='vggface2').eval()
|
||||
|
||||
# Load ONNX model
|
||||
onnx_session = ort.InferenceSession(onnx_path)
|
||||
|
||||
print(f"[VALIDATE] Running {num_tests} accuracy tests...")
|
||||
|
||||
similarities = []
|
||||
all_passed = True
|
||||
|
||||
for i in range(num_tests):
|
||||
# Generate random test input
|
||||
test_input = np.random.randn(1, 3, 160, 160).astype(np.float32)
|
||||
|
||||
# PyTorch inference
|
||||
with torch.no_grad():
|
||||
pytorch_emb = pytorch_model(torch.from_numpy(test_input))
|
||||
pytorch_emb = pytorch_emb.numpy().flatten()
|
||||
|
||||
# ONNX inference
|
||||
onnx_emb = onnx_session.run(None, {'input': test_input})[0].flatten()
|
||||
|
||||
# Calculate cosine similarity
|
||||
similarity = 1 - cosine(pytorch_emb, onnx_emb)
|
||||
similarities.append(similarity)
|
||||
|
||||
status = "✅" if similarity > 0.9999 else "❌"
|
||||
print(f" Test {i+1}: cosine similarity = {similarity:.6f} {status}")
|
||||
|
||||
if similarity < 0.9999:
|
||||
all_passed = False
|
||||
|
||||
# Summary
|
||||
avg_similarity = np.mean(similarities)
|
||||
min_similarity = np.min(similarities)
|
||||
max_similarity = np.max(similarities)
|
||||
|
||||
print(f"\n[VALIDATE] Summary:")
|
||||
print(f" Average: {avg_similarity:.6f}")
|
||||
print(f" Min: {min_similarity:.6f}")
|
||||
print(f" Max: {max_similarity:.6f}")
|
||||
|
||||
if all_passed:
|
||||
print(f"\n✅ 所有測試通過!準確度符合要求(> 99.99%)")
|
||||
return True
|
||||
else:
|
||||
print(f"\n❌ 部分測試未通過,需要檢查模型轉換")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
onnx_path = sys.argv[1] if len(sys.argv) > 1 else "models/facenet_vggface2.onnx"
|
||||
num_tests = int(sys.argv[2]) if len(sys.argv) > 2 else 10
|
||||
|
||||
if not os.path.exists(onnx_path):
|
||||
print(f"❌ ONNX model not found: {onnx_path}")
|
||||
sys.exit(1)
|
||||
|
||||
success = validate_accuracy(onnx_path, num_tests)
|
||||
sys.exit(0 if success else 1)
|
||||
Reference in New Issue
Block a user