File size: 26,400 Bytes
a00a1ac 462ff09 a00a1ac 31653b7 a00a1ac 31653b7 a00a1ac bcb51b3 a00a1ac 31653b7 462ff09 a00a1ac 462ff09 a00a1ac bcb51b3 a00a1ac 462ff09 a00a1ac 462ff09 bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 a00a1ac e2343a1 a00a1ac bcb51b3 a00a1ac bcb51b3 a00a1ac e2343a1 a00a1ac a54098c a00a1ac bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 462ff09 31653b7 462ff09 bcb51b3 a00a1ac 31653b7 a00a1ac bcb51b3 a00a1ac 31653b7 a00a1ac 31653b7 a00a1ac 31653b7 a00a1ac bcb51b3 31653b7 bcb51b3 31653b7 bcb51b3 a00a1ac 31653b7 a00a1ac 31653b7 bcb51b3 462ff09 bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 462ff09 a00a1ac a54098c a00a1ac a54098c a00a1ac a54098c a00a1ac a54098c 462ff09 a00a1ac 34c8dd9 a00a1ac 462ff09 a00a1ac 31653b7 a00a1ac bcb51b3 462ff09 a00a1ac 462ff09 bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 a00a1ac bcb51b3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
"""
Model Loading Module
Handles loading and validation of SAM2 and MatAnyone AI models
"""
# ============================================================================ #
# IMPORTS AND DEPENDENCIES
# ============================================================================ #
import os
import gc
import sys
import time
import shutil
import logging
import tempfile
import traceback
from typing import Optional, Dict, Any, Tuple, Union
from pathlib import Path
import torch
import gradio as gr
from omegaconf import DictConfig, OmegaConf
# Import modular components
import exceptions
import device_manager
import memory_manager
logger = logging.getLogger(__name__)
# ============================================================================ #
# HARD CACHE CLEANER
# ============================================================================ #
class HardCacheCleaner:
"""
Comprehensive cache cleaning system to resolve SAM2 loading issues
Clears Python module cache, HuggingFace cache, and temp files
"""
@staticmethod
def clean_all_caches(verbose: bool = True):
"""Clean all caches that might interfere with SAM2 loading"""
if verbose:
logger.info("Starting comprehensive cache cleanup...")
# 1. Clean Python module cache
HardCacheCleaner._clean_python_cache(verbose)
# 2. Clean HuggingFace cache
HardCacheCleaner._clean_huggingface_cache(verbose)
# 3. Clean PyTorch cache
HardCacheCleaner._clean_pytorch_cache(verbose)
# 4. Clean temp directories
HardCacheCleaner._clean_temp_directories(verbose)
# 5. Clear import cache
HardCacheCleaner._clear_import_cache(verbose)
# 6. Force garbage collection
HardCacheCleaner._force_gc_cleanup(verbose)
if verbose:
logger.info("Cache cleanup completed")
@staticmethod
def _clean_python_cache(verbose: bool = True):
"""Clean Python bytecode cache"""
try:
# Clear sys.modules cache for SAM2 related modules
sam2_modules = [key for key in sys.modules.keys() if 'sam2' in key.lower()]
for module in sam2_modules:
if verbose:
logger.info(f"Removing cached module: {module}")
del sys.modules[module]
# Clear __pycache__ directories
for root, dirs, files in os.walk("."):
for dir_name in dirs[:]: # Use slice to modify list during iteration
if dir_name == "__pycache__":
cache_path = os.path.join(root, dir_name)
if verbose:
logger.info(f"Removing __pycache__: {cache_path}")
shutil.rmtree(cache_path, ignore_errors=True)
dirs.remove(dir_name)
except Exception as e:
logger.warning(f"Python cache cleanup failed: {e}")
@staticmethod
def _clean_huggingface_cache(verbose: bool = True):
"""Clean HuggingFace model cache"""
try:
cache_paths = [
os.path.expanduser("~/.cache/huggingface/"),
os.path.expanduser("~/.cache/torch/"),
"./checkpoints/",
"./.cache/",
]
for cache_path in cache_paths:
if os.path.exists(cache_path):
if verbose:
logger.info(f"Cleaning cache directory: {cache_path}")
# Remove SAM2 specific files
for root, dirs, files in os.walk(cache_path):
for file in files:
if any(pattern in file.lower() for pattern in ['sam2', 'segment-anything-2']):
file_path = os.path.join(root, file)
try:
os.remove(file_path)
if verbose:
logger.info(f"Removed cached file: {file_path}")
except:
pass
for dir_name in dirs[:]:
if any(pattern in dir_name.lower() for pattern in ['sam2', 'segment-anything-2']):
dir_path = os.path.join(root, dir_name)
try:
shutil.rmtree(dir_path, ignore_errors=True)
if verbose:
logger.info(f"Removed cached directory: {dir_path}")
dirs.remove(dir_name)
except:
pass
except Exception as e:
logger.warning(f"HuggingFace cache cleanup failed: {e}")
@staticmethod
def _clean_pytorch_cache(verbose: bool = True):
"""Clean PyTorch cache"""
try:
import torch
if torch.cuda.is_available():
torch.cuda.empty_cache()
if verbose:
logger.info("Cleared PyTorch CUDA cache")
except Exception as e:
logger.warning(f"PyTorch cache cleanup failed: {e}")
@staticmethod
def _clean_temp_directories(verbose: bool = True):
"""Clean temporary directories"""
try:
temp_dirs = [tempfile.gettempdir(), "/tmp", "./tmp", "./temp"]
for temp_dir in temp_dirs:
if os.path.exists(temp_dir):
for item in os.listdir(temp_dir):
if 'sam2' in item.lower() or 'segment' in item.lower():
item_path = os.path.join(temp_dir, item)
try:
if os.path.isfile(item_path):
os.remove(item_path)
elif os.path.isdir(item_path):
shutil.rmtree(item_path, ignore_errors=True)
if verbose:
logger.info(f"Removed temp item: {item_path}")
except:
pass
except Exception as e:
logger.warning(f"Temp directory cleanup failed: {e}")
@staticmethod
def _clear_import_cache(verbose: bool = True):
"""Clear Python import cache"""
try:
import importlib
# Invalidate import caches
importlib.invalidate_caches()
if verbose:
logger.info("Cleared Python import cache")
except Exception as e:
logger.warning(f"Import cache cleanup failed: {e}")
@staticmethod
def _force_gc_cleanup(verbose: bool = True):
"""Force garbage collection"""
try:
collected = gc.collect()
if verbose:
logger.info(f"Garbage collection freed {collected} objects")
except Exception as e:
logger.warning(f"Garbage collection failed: {e}")
# ============================================================================ #
# MODEL LOADER CLASS - MAIN INTERFACE
# ============================================================================ #
class ModelLoader:
"""
Comprehensive model loading and management for SAM2 and MatAnyone
Handles automatic config detection, multiple fallback strategies, and memory management
"""
def __init__(self, device_mgr: device_manager.DeviceManager, memory_mgr: memory_manager.MemoryManager):
self.device_manager = device_mgr
self.memory_manager = memory_mgr
self.device = self.device_manager.get_optimal_device()
# Model storage
self.sam2_predictor = None
self.matanyone_model = None
self.matanyone_core = None
# Configuration paths
self.checkpoints_dir = "./checkpoints"
os.makedirs(self.checkpoints_dir, exist_ok=True)
# Model loading statistics
self.loading_stats = {
'sam2_load_time': 0.0,
'matanyone_load_time': 0.0,
'total_load_time': 0.0,
'models_loaded': False,
'loading_attempts': 0
}
logger.info(f"ModelLoader initialized for device: {self.device}")
self._apply_gradio_patch()
# ============================================================================ #
# INITIALIZATION AND SETUP
# ============================================================================ #
def _apply_gradio_patch(self):
"""Apply Gradio schema monkey patch to prevent validation errors"""
try:
import gradio.components.base
original_get_config = gradio.components.base.Component.get_config
def patched_get_config(self):
config = original_get_config(self)
# Remove problematic keys that cause validation errors
config.pop("show_progress_bar", None)
config.pop("min_width", None)
config.pop("scale", None)
return config
gradio.components.base.Component.get_config = patched_get_config
logger.debug("Applied Gradio schema monkey patch")
except (ImportError, AttributeError) as e:
logger.warning(f"Could not apply Gradio monkey patch: {e}")
# ============================================================================ #
# MAIN MODEL LOADING ORCHESTRATION
# ============================================================================ #
def load_all_models(self, progress_callback: Optional[callable] = None, cancel_event=None) -> Tuple[Any, Any]:
"""
Load both SAM2 and MatAnyone models with comprehensive error handling
Args:
progress_callback: Progress update callback
cancel_event: Event to check for cancellation
Returns:
Tuple of (sam2_predictor, matanyone_model)
"""
start_time = time.time()
self.loading_stats['loading_attempts'] += 1
try:
logger.info("Starting model loading process...")
if progress_callback:
progress_callback(0.0, "Initializing model loading...")
# Clear any existing models
self._cleanup_models()
# Load SAM2 first (typically faster)
logger.info("Loading SAM2 predictor...")
if progress_callback:
progress_callback(0.1, "Loading SAM2 predictor...")
self.sam2_predictor = self._load_sam2_predictor(progress_callback)
if self.sam2_predictor is None:
raise exceptions.ModelLoadingError("SAM2", "Failed to load SAM2 predictor")
sam2_time = time.time() - start_time
self.loading_stats['sam2_load_time'] = sam2_time
logger.info(f"SAM2 loaded in {sam2_time:.2f}s")
# Load MatAnyone
logger.info("Loading MatAnyone model...")
if progress_callback:
progress_callback(0.6, "Loading MatAnyone model...")
matanyone_start = time.time()
self.matanyone_model, self.matanyone_core = self._load_matanyone_model(progress_callback)
if self.matanyone_model is None:
raise exceptions.ModelLoadingError("MatAnyone", "Failed to load MatAnyone model")
matanyone_time = time.time() - matanyone_start
self.loading_stats['matanyone_load_time'] = matanyone_time
logger.info(f"MatAnyone loaded in {matanyone_time:.1f}s")
# Final setup
total_time = time.time() - start_time
self.loading_stats['total_load_time'] = total_time
self.loading_stats['models_loaded'] = True
if progress_callback:
progress_callback(1.0, "Models loaded successfully!")
logger.info(f"All models loaded successfully in {total_time:.2f}s")
return self.sam2_predictor, self.matanyone_model
except Exception as e:
error_msg = f"Model loading failed: {str(e)}"
logger.error(f"{error_msg}\n{traceback.format_exc()}")
# Cleanup on failure
self._cleanup_models()
self.loading_stats['models_loaded'] = False
if progress_callback:
progress_callback(1.0, f"Error: {error_msg}")
return None, None
# ============================================================================ #
# SAM2 MODEL LOADING - HUGGINGFACE TRANSFORMERS APPROACH
# ============================================================================ #
def _load_sam2_predictor(self, progress_callback: Optional[callable] = None):
"""
Load SAM2 using HuggingFace Transformers integration with cache cleanup
This method works reliably on HuggingFace Spaces without config file issues
Args:
progress_callback: Progress update callback
Returns:
SAM2 model or None
"""
logger.info("=== USING NEW HF TRANSFORMERS SAM2 LOADER ===")
# Step 1: Clean caches before loading
if progress_callback:
progress_callback(0.15, "Cleaning caches...")
HardCacheCleaner.clean_all_caches(verbose=True)
# Step 2: Determine model size based on device memory
model_size = "large" # default
if hasattr(self.device_manager, 'get_device_memory_gb'):
try:
memory_gb = self.device_manager.get_device_memory_gb()
if memory_gb < 4:
model_size = "tiny"
elif memory_gb < 8:
model_size = "base"
logger.info(f"Selected SAM2 {model_size} based on {memory_gb}GB memory")
except Exception as e:
logger.warning(f"Could not determine device memory: {e}")
# Step 3: Try multiple HuggingFace approaches
model_map = {
"tiny": "facebook/sam2.1-hiera-tiny",
"small": "facebook/sam2.1-hiera-small",
"base": "facebook/sam2.1-hiera-base-plus",
"large": "facebook/sam2.1-hiera-large"
}
model_id = model_map.get(model_size, model_map["large"])
if progress_callback:
progress_callback(0.3, f"Loading SAM2 {model_size}...")
# Method 1: HuggingFace Transformers Pipeline (most reliable)
try:
logger.info("Trying Transformers pipeline approach...")
from transformers import pipeline
sam2_pipeline = pipeline(
"mask-generation",
model=model_id,
device=0 if str(self.device) == "cuda" else -1
)
logger.info("SAM2 loaded successfully via Transformers pipeline")
return sam2_pipeline
except Exception as e:
logger.warning(f"Pipeline approach failed: {e}")
# Method 2: Direct Transformers classes
try:
logger.info("Trying direct Transformers classes...")
from transformers import Sam2Processor, Sam2Model
processor = Sam2Processor.from_pretrained(model_id)
model = Sam2Model.from_pretrained(model_id).to(self.device)
logger.info("SAM2 loaded successfully via Transformers classes")
return {"model": model, "processor": processor}
except Exception as e:
logger.warning(f"Direct class approach failed: {e}")
# Method 3: Official SAM2 with from_pretrained
try:
logger.info("Trying official SAM2 from_pretrained...")
from sam2.sam2_image_predictor import SAM2ImagePredictor
predictor = SAM2ImagePredictor.from_pretrained(model_id)
logger.info("SAM2 loaded successfully via official from_pretrained")
return predictor
except Exception as e:
logger.warning(f"Official from_pretrained approach failed: {e}")
# Method 4: Fallback to direct checkpoint download
try:
logger.info("Trying fallback checkpoint approach...")
from huggingface_hub import hf_hub_download
from transformers import Sam2Model
# Download checkpoint directly
checkpoint_path = hf_hub_download(
repo_id=model_id,
filename="model.safetensors" if "sam2.1" in model_id else "pytorch_model.bin"
)
logger.info(f"Downloaded checkpoint to: {checkpoint_path}")
# Load with minimal approach
model = Sam2Model.from_pretrained(model_id)
model = model.to(self.device)
logger.info("SAM2 loaded successfully via fallback approach")
return model
except Exception as e:
logger.warning(f"Fallback approach failed: {e}")
logger.error("All SAM2 loading methods failed")
return None
# ============================================================================ #
# MATANYONE MODEL LOADING - MULTIPLE STRATEGIES
# ============================================================================ #
def _load_matanyone_model(self, progress_callback: Optional[callable] = None):
"""
Load MatAnyone model with multiple import strategies
Args:
progress_callback: Progress update callback
Returns:
Tuple[model, core] or (None, None)
"""
import_strategies = [
self._load_matanyone_strategy_1,
self._load_matanyone_strategy_2,
self._load_matanyone_strategy_3,
self._load_matanyone_strategy_4
]
for i, strategy in enumerate(import_strategies, 1):
try:
logger.info(f"Trying MatAnyone loading strategy {i}...")
if progress_callback:
progress_callback(0.7 + (i * 0.05), f"MatAnyone strategy {i}...")
model, core = strategy()
if model is not None and core is not None:
logger.info(f"MatAnyone loaded successfully with strategy {i}")
return model, core
except Exception as e:
logger.warning(f"MatAnyone strategy {i} failed: {e}")
continue
logger.error("All MatAnyone loading strategies failed")
return None, None
# ============================================================================ #
# MATANYONE LOADING STRATEGIES
# ============================================================================ #
def _load_matanyone_strategy_1(self):
"""MatAnyone loading strategy 1: Official HuggingFace InferenceCore"""
from matanyone import InferenceCore
# Initialize with the official model repo
processor = InferenceCore("PeiqingYang/MatAnyone")
return processor, processor
def _load_matanyone_strategy_2(self):
"""MatAnyone loading strategy 2: Alternative import paths"""
from matanyone import MatAnyOne
from matanyone import InferenceCore
cfg = OmegaConf.create({
'model_name': 'matanyone',
'device': str(self.device)
})
model = MatAnyOne(cfg)
core = InferenceCore(model, cfg)
return model, core
def _load_matanyone_strategy_3(self):
"""MatAnyone loading strategy 3: Repository-specific imports"""
try:
from matanyone.models.matanyone import MatAnyOneModel
from matanyone.core import InferenceEngine
except ImportError:
from matanyone.src.models import MatAnyOneModel
from matanyone.src.core import InferenceEngine
config = {
'model_path': None, # Will use default
'device': self.device,
'precision': 'fp16' if self.device.type == 'cuda' else 'fp32'
}
model = MatAnyOneModel.from_pretrained(config)
engine = InferenceEngine(model)
return model, engine
def _load_matanyone_strategy_4(self):
"""MatAnyone loading strategy 4: Direct model class"""
from matanyone.model.matanyone import MatAnyone
model = MatAnyone.from_pretrained("not-lain/matanyone")
return model, model
# ============================================================================ #
# MODEL MANAGEMENT AND CLEANUP
# ============================================================================ #
def _cleanup_models(self):
"""Clean up loaded models and free memory"""
if self.sam2_predictor is not None:
del self.sam2_predictor
self.sam2_predictor = None
if self.matanyone_model is not None:
del self.matanyone_model
self.matanyone_model = None
if self.matanyone_core is not None:
del self.matanyone_core
self.matanyone_core = None
# Clear GPU cache
self.memory_manager.cleanup_aggressive()
gc.collect()
logger.debug("Model cleanup completed")
def cleanup(self):
"""Clean up all resources"""
self._cleanup_models()
logger.info("ModelLoader cleanup completed")
# ============================================================================ #
# MODEL INFORMATION AND STATUS
# ============================================================================ #
def get_model_info(self) -> Dict[str, Any]:
"""
Get information about loaded models
Returns:
Dict with model information and statistics
"""
info = {
'models_loaded': self.loading_stats['models_loaded'],
'sam2_loaded': self.sam2_predictor is not None,
'matanyone_loaded': self.matanyone_model is not None,
'device': str(self.device),
'loading_stats': self.loading_stats.copy()
}
if self.sam2_predictor is not None:
try:
info['sam2_model_type'] = type(self.sam2_predictor).__name__
except:
info['sam2_model_type'] = "Unknown"
if self.matanyone_model is not None:
try:
info['matanyone_model_type'] = type(self.matanyone_model).__name__
except:
info['matanyone_model_type'] = "Unknown"
return info
def get_status(self) -> Dict[str, Any]:
"""Get model loader status for backward compatibility"""
return self.get_model_info()
def get_load_summary(self) -> str:
"""Get a human-readable summary of model loading"""
if not self.loading_stats['models_loaded']:
return "Models not loaded"
sam2_time = self.loading_stats['sam2_load_time']
matanyone_time = self.loading_stats['matanyone_load_time']
total_time = self.loading_stats['total_load_time']
summary = f"Models loaded successfully in {total_time:.1f}s\n"
summary += f"SAM2: {sam2_time:.1f}s\n"
summary += f"MatAnyone: {matanyone_time:.1f}s\n"
summary += f"Device: {self.device}"
return summary
# ============================================================================ #
# MODEL VALIDATION AND TESTING
# ============================================================================ #
def validate_models(self) -> bool:
"""
Validate that models are properly loaded and functional
Returns:
bool: True if models are valid
"""
try:
# Basic validation
if not self.loading_stats['models_loaded']:
return False
if self.sam2_predictor is None or self.matanyone_model is None:
return False
# Try basic model operations
# This could include running a small test inference
logger.info("Model validation passed")
return True
except Exception as e:
logger.error(f"Model validation failed: {e}")
return False
# ============================================================================ #
# UTILITY METHODS
# ============================================================================ #
def reload_models(self, progress_callback: Optional[callable] = None) -> Tuple[Any, Any]:
"""
Reload all models (useful for error recovery)
Args:
progress_callback: Progress update callback
Returns:
Tuple of (sam2_predictor, matanyone_model)
"""
logger.info("Reloading models...")
self._cleanup_models()
self.loading_stats['models_loaded'] = False
return self.load_all_models(progress_callback)
@property
def models_ready(self) -> bool:
"""Check if all models are loaded and ready"""
return (
self.loading_stats['models_loaded'] and
self.sam2_predictor is not None and
self.matanyone_model is not None
) |