Spaces:
Sleeping
Sleeping
Update app.py
Browse files14 unmodified lines151617181920212220 unmodified lines4344454647484939 unmodified lines8990919293949515 unmodified lines11111211311411511611714 unmodified linesimport numpy as np# ββ ONNX RUNTIME PERFORMANCE TUNING (SET BEFORE ANY SESSION LOADS) ββ_cpu_count = os.cpu_count() or 4os.environ["OMP_NUM_THREADS"] = str(_cpu_count)os.environ["OMP_WAIT_POLICY"] = "ACTIVE"os.environ["ONNXRUNTIME_SESSION_THREAD_POOL_SIZE"] = str(_cpu_count)20 unmodified linesactive_session = None # Dereference old sessiongc.collect() # Force garbage collection to free RAMactive_session = new_session(model_name)active_model_name = model_nameprint(f"Model '{model_name}' loaded successfully!")return active_session39 unmodified linesif max_dim > max_limit:scale_factor = max_limit / max_dimnew_size = (int(original_size[0] * scale_factor), int(original_size[1] * scale_factor))processing_image = processing_image.resize(new_size, Image.Resampling.LANCZOS)# ββ 2. BACKGROUND REMOVAL (GET MASK) ββββββββββββββββββββββββββββt_model_start = time.perf_counter()15 unmodified lines# ββ 4. RESIZE MASK TO ORIGINAL RESOLUTION ββββββββββββββββββββββif mask.size != original_size:mask = Image.fromarray(mask_np)mask = mask.resize(original_size, Image.Resampling.LANCZOS)mask_np = np.array(mask, dtype=np.float32)else:mask_np = mask_np.astype(np.float32)14 unmodified lines15161718192021222320 unmodified lines44454647484950515253545556575859606139 unmodified lines10110210310410510610710815 unmodified lines12412512612712812913013114 unmodified linesimport numpy as np# ββ ONNX RUNTIME PERFORMANCE TUNING (OPTIMIZED FOR 2-CORE CONTAINERS E.G. HUGGING FACE) ββ# Explicitly set threads to 2 to avoid overhead/context switching on shared hypervisors._cpu_count = 2os.environ["OMP_NUM_THREADS"] = str(_cpu_count)os.environ["OMP_WAIT_POLICY"] = "ACTIVE"os.environ["ONNXRUNTIME_SESSION_THREAD_POOL_SIZE"] = str(_cpu_count)20 unmodified linesactive_session = None # Dereference old sessiongc.collect() # Force garbage collection to free RAM# Build highly optimized ONNX Runtime SessionOptions to speed up inference on CPUsess_opts = ort.SessionOptions()sess_opts.intra_op_num_threads = 2sess_opts.inter_op_num_threads = 1sess_opts.execution_mode = ort.ExecutionMode.ORT_SEQUENTIALsess_opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALLactive_session = new_session(model_name,providers=['CPUExecutionProvider'],sess_opts=sess_opts)active_model_name = model_nameprint(f"Model '{model_name}' loaded successfully!")return active_session39 unmodified linesif max_dim > max_limit:scale_factor = max_limit / max_dimnew_size = (int(original_size[0] * scale_factor), int(original_size[1] * scale_factor))# BILINEAR is 5x faster than LANCZOS and works identically for generating model inputsprocessing_image = processing_image.resize(new_size, Image.Resampling.BILINEAR)# ββ 2. BACKGROUND REMOVAL (GET MASK) ββββββββββββββββββββββββββββt_model_start = time.perf_counter()15 unmodified lines# ββ 4. RESIZE MASK TO ORIGINAL RESOLUTION ββββββββββββββββββββββif mask.size != original_size:mask = Image.fromarray(mask_np)# BILINEAR is much faster for alpha masks and visually identical after subsequent smoothingmask = mask.resize(original_size, Image.Resampling.BILINEAR)mask_np = np.array(mask, dtype=np.float32)else:mask_np = mask_np.astype(np.float32)
app.py+19-5