Spaces:
Sleeping
Sleeping
Pavanupadhyay27 commited on
Commit ·
c40c998
1
Parent(s): 7c11686
Apply memory optimizations to ONNX Runtime to enable real biometric face recognition on Render Free Tier
Browse files
backend/app/api/v1/kiosk.py
CHANGED
|
@@ -199,11 +199,6 @@ def scan_face(
|
|
| 199 |
# Similarity = 1 - Distance
|
| 200 |
similarity = 1.0 - float(distance)
|
| 201 |
|
| 202 |
-
if face_engine.mock_mode:
|
| 203 |
-
# Override match similarity in mock mode to allow functional kiosk scans during demos
|
| 204 |
-
import random
|
| 205 |
-
similarity = random.uniform(0.81, 0.94)
|
| 206 |
-
|
| 207 |
if similarity < face_threshold:
|
| 208 |
# Low confidence match -> Unknown
|
| 209 |
log_entry = crud.create_attendance_log(
|
|
|
|
| 199 |
# Similarity = 1 - Distance
|
| 200 |
similarity = 1.0 - float(distance)
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
if similarity < face_threshold:
|
| 203 |
# Low confidence match -> Unknown
|
| 204 |
log_entry = crud.create_attendance_log(
|
backend/app/services/face_engine.py
CHANGED
|
@@ -66,25 +66,37 @@ class FaceEngine:
|
|
| 66 |
|
| 67 |
def _init_sessions(self):
|
| 68 |
try:
|
| 69 |
-
|
| 70 |
-
#
|
|
|
|
| 71 |
opts = ort.SessionOptions()
|
| 72 |
-
opts.intra_op_num_threads =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
providers = ['CPUExecutionProvider']
|
| 75 |
# If GPU is available (optional setup)
|
| 76 |
if 'CUDAExecutionProvider' in ort.get_available_providers():
|
| 77 |
providers = ['CUDAExecutionProvider'] + providers
|
| 78 |
|
| 79 |
-
logger.info(f"Initializing ONNX sessions with providers: {providers}")
|
| 80 |
|
| 81 |
self.det_session = ort.InferenceSession(self.det_model_path, opts, providers=providers)
|
|
|
|
|
|
|
| 82 |
self.rec_session = ort.InferenceSession(self.rec_model_path, opts, providers=providers)
|
|
|
|
|
|
|
| 83 |
self.live_session_27 = ort.InferenceSession(self.liveness_model_27, opts, providers=providers)
|
|
|
|
| 84 |
|
| 85 |
# Optional 1.8 liveness model
|
| 86 |
if os.path.exists(self.liveness_model_18):
|
| 87 |
self.live_session_18 = ort.InferenceSession(self.liveness_model_18, opts, providers=providers)
|
|
|
|
| 88 |
else:
|
| 89 |
self.live_session_18 = None
|
| 90 |
|
|
|
|
| 66 |
|
| 67 |
def _init_sessions(self):
|
| 68 |
try:
|
| 69 |
+
import gc
|
| 70 |
+
# Initialize ONNX Runtime Inference Sessions with memory-optimized settings
|
| 71 |
+
# to prevent OOM crashes on low-resource servers (like Render's 512MB Free Tier)
|
| 72 |
opts = ort.SessionOptions()
|
| 73 |
+
opts.intra_op_num_threads = 1
|
| 74 |
+
opts.inter_op_num_threads = 1
|
| 75 |
+
opts.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL
|
| 76 |
+
opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_DISABLE_ALL
|
| 77 |
+
opts.enable_cpu_mem_arena = False
|
| 78 |
+
opts.add_session_config_entry("memory.enable_memory_arena_shrinkage", "cpu:0")
|
| 79 |
|
| 80 |
providers = ['CPUExecutionProvider']
|
| 81 |
# If GPU is available (optional setup)
|
| 82 |
if 'CUDAExecutionProvider' in ort.get_available_providers():
|
| 83 |
providers = ['CUDAExecutionProvider'] + providers
|
| 84 |
|
| 85 |
+
logger.info(f"Initializing ONNX sessions with memory optimization and providers: {providers}")
|
| 86 |
|
| 87 |
self.det_session = ort.InferenceSession(self.det_model_path, opts, providers=providers)
|
| 88 |
+
gc.collect()
|
| 89 |
+
|
| 90 |
self.rec_session = ort.InferenceSession(self.rec_model_path, opts, providers=providers)
|
| 91 |
+
gc.collect()
|
| 92 |
+
|
| 93 |
self.live_session_27 = ort.InferenceSession(self.liveness_model_27, opts, providers=providers)
|
| 94 |
+
gc.collect()
|
| 95 |
|
| 96 |
# Optional 1.8 liveness model
|
| 97 |
if os.path.exists(self.liveness_model_18):
|
| 98 |
self.live_session_18 = ort.InferenceSession(self.liveness_model_18, opts, providers=providers)
|
| 99 |
+
gc.collect()
|
| 100 |
else:
|
| 101 |
self.live_session_18 = None
|
| 102 |
|