Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- generator.py +186 -51
- models.py +173 -50
generator.py
CHANGED
|
@@ -48,7 +48,7 @@ class RetroArtConverter:
|
|
| 48 |
self.mediapipe_face, mediapipe_success = load_mediapipe_face_detector()
|
| 49 |
self.models_loaded['mediapipe_face'] = mediapipe_success
|
| 50 |
|
| 51 |
-
# Load Depth detector with fallback hierarchy (Leres
|
| 52 |
self.depth_detector, self.depth_type, depth_success = load_depth_detector()
|
| 53 |
self.models_loaded['depth_detector'] = depth_success
|
| 54 |
self.models_loaded['depth_type'] = self.depth_type
|
|
@@ -116,11 +116,29 @@ class RetroArtConverter:
|
|
| 116 |
self.models_loaded['lora'] = lora_success
|
| 117 |
|
| 118 |
# Setup IP-Adapter
|
| 119 |
-
if self.instantid_active and self.image_encoder is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
self.image_proj_model, ip_adapter_success = setup_ip_adapter(self.pipe, self.image_encoder)
|
| 121 |
self.models_loaded['ip_adapter'] = ip_adapter_success
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
else:
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
self.models_loaded['ip_adapter'] = False
|
| 125 |
self.image_proj_model = None
|
| 126 |
|
|
@@ -166,6 +184,25 @@ class RetroArtConverter:
|
|
| 166 |
print(f"{model}: {status}")
|
| 167 |
print("===================\n")
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
print("=== UPGRADE VERIFICATION ===")
|
| 170 |
try:
|
| 171 |
from resampler_enhanced import EnhancedResampler
|
|
@@ -191,7 +228,7 @@ class RetroArtConverter:
|
|
| 191 |
def get_depth_map(self, image):
|
| 192 |
"""
|
| 193 |
Generate depth map using available depth detector.
|
| 194 |
-
Supports: LeresDetector or MidasDetector.
|
| 195 |
"""
|
| 196 |
if self.depth_detector is not None:
|
| 197 |
try:
|
|
@@ -253,6 +290,11 @@ class RetroArtConverter:
|
|
| 253 |
+1-2% improvement in face preservation.
|
| 254 |
"""
|
| 255 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
multi_scale_embeds = []
|
| 257 |
|
| 258 |
for scale in MULTI_SCALE_FACTORS:
|
|
@@ -268,8 +310,9 @@ class RetroArtConverter:
|
|
| 268 |
scaled_array = cv2.cvtColor(np.array(scaled_crop), cv2.COLOR_RGB2BGR)
|
| 269 |
scaled_faces = self.face_app.get(scaled_array)
|
| 270 |
|
| 271 |
-
if len(scaled_faces) > 0:
|
| 272 |
-
|
|
|
|
| 273 |
|
| 274 |
# Average embeddings
|
| 275 |
if len(multi_scale_embeds) > 0:
|
|
@@ -279,7 +322,13 @@ class RetroArtConverter:
|
|
| 279 |
print(f"[MULTI-SCALE] Combined {len(multi_scale_embeds)} scales")
|
| 280 |
return averaged
|
| 281 |
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
except Exception as e:
|
| 285 |
print(f"[MULTI-SCALE] Failed: {e}, using single scale")
|
|
@@ -539,7 +588,7 @@ class RetroArtConverter:
|
|
| 539 |
# Generate depth map
|
| 540 |
depth_image = None
|
| 541 |
if self.depth_active:
|
| 542 |
-
print("Generating depth map...")
|
| 543 |
depth_image = self.get_depth_map(resized_image)
|
| 544 |
if depth_image.size != (target_width, target_height):
|
| 545 |
depth_image = depth_image.resize((int(target_width), int(target_height)), Image.LANCZOS)
|
|
@@ -594,32 +643,82 @@ class RetroArtConverter:
|
|
| 594 |
guidance_scale = adaptive_params['guidance_scale']
|
| 595 |
lora_scale = adaptive_params['lora_scale']
|
| 596 |
|
| 597 |
-
# Extract face embeddings
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
|
| 605 |
-
#
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
|
| 618 |
-
# MULTI-SCALE PROCESSING
|
| 619 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
|
| 621 |
-
# Enhance face crop
|
| 622 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
|
| 624 |
# Draw keypoints
|
| 625 |
face_kps = face.kps
|
|
@@ -691,6 +790,26 @@ class RetroArtConverter:
|
|
| 691 |
print(" - MediapipeFace: tried, found nothing")
|
| 692 |
else:
|
| 693 |
print(" - MediapipeFace: not available")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 694 |
print()
|
| 695 |
|
| 696 |
# Set LORA scale
|
|
@@ -761,31 +880,47 @@ class RetroArtConverter:
|
|
| 761 |
# Add face embeddings for IP-Adapter if available
|
| 762 |
if face_embeddings is not None and self.models_loaded.get('ip_adapter', False) and face_crop_enhanced is not None:
|
| 763 |
print(f"Processing InstantID face embeddings with Resampler...")
|
|
|
|
|
|
|
|
|
|
| 764 |
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
boosted_scale = identity_preservation * IDENTITY_BOOST_MULTIPLIER
|
| 771 |
-
face_proj_embeds = face_proj_embeds * boosted_scale
|
| 772 |
-
|
| 773 |
-
print(f" - Face embedding: {face_emb_tensor.shape} -> {face_proj_embeds.shape}, Scale: {boosted_scale:.2f}")
|
| 774 |
-
|
| 775 |
-
if 'prompt_embeds' in pipe_kwargs:
|
| 776 |
-
original_embeds = pipe_kwargs['prompt_embeds']
|
| 777 |
-
|
| 778 |
-
if original_embeds.shape[0] > 1: # Handle CFG
|
| 779 |
-
face_proj_embeds = torch.cat([torch.zeros_like(face_proj_embeds), face_proj_embeds], dim=0)
|
| 780 |
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 786 |
|
| 787 |
elif has_detected_faces:
|
| 788 |
print(" Face detected but IP-Adapter/embeddings unavailable, using keypoints only")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
|
| 790 |
else:
|
| 791 |
# No face detected - blank map needed to maintain ControlNet list order
|
|
|
|
| 48 |
self.mediapipe_face, mediapipe_success = load_mediapipe_face_detector()
|
| 49 |
self.models_loaded['mediapipe_face'] = mediapipe_success
|
| 50 |
|
| 51 |
+
# Load Depth detector with fallback hierarchy (Leres → Zoe → Midas)
|
| 52 |
self.depth_detector, self.depth_type, depth_success = load_depth_detector()
|
| 53 |
self.models_loaded['depth_detector'] = depth_success
|
| 54 |
self.models_loaded['depth_type'] = self.depth_type
|
|
|
|
| 116 |
self.models_loaded['lora'] = lora_success
|
| 117 |
|
| 118 |
# Setup IP-Adapter
|
| 119 |
+
if self.instantid_active and self.image_encoder is not None:
|
| 120 |
+
print("[IP-ADAPTER] Attempting IP-Adapter setup...")
|
| 121 |
+
print(f" - InstantID active: {self.instantid_active}")
|
| 122 |
+
print(f" - Image encoder available: {self.image_encoder is not None}")
|
| 123 |
+
print(f" - Device: {device}, dtype: {dtype}")
|
| 124 |
+
|
| 125 |
self.image_proj_model, ip_adapter_success = setup_ip_adapter(self.pipe, self.image_encoder)
|
| 126 |
self.models_loaded['ip_adapter'] = ip_adapter_success
|
| 127 |
+
|
| 128 |
+
if ip_adapter_success:
|
| 129 |
+
print("[IP-ADAPTER] ✓ Successfully loaded!")
|
| 130 |
+
else:
|
| 131 |
+
print("[IP-ADAPTER] ✗ Setup failed - face embeddings will not be used")
|
| 132 |
+
print("[IP-ADAPTER] System will fallback to keypoints-only mode (reduced quality)")
|
| 133 |
else:
|
| 134 |
+
reasons = []
|
| 135 |
+
if not self.instantid_active:
|
| 136 |
+
reasons.append("InstantID ControlNet not loaded")
|
| 137 |
+
if self.image_encoder is None:
|
| 138 |
+
reasons.append("Image encoder not loaded")
|
| 139 |
+
|
| 140 |
+
print(f"[INFO] Face preservation: IP-Adapter disabled ({', '.join(reasons)})")
|
| 141 |
+
print("[INFO] System will use keypoints-only mode (reduced quality)")
|
| 142 |
self.models_loaded['ip_adapter'] = False
|
| 143 |
self.image_proj_model = None
|
| 144 |
|
|
|
|
| 184 |
print(f"{model}: {status}")
|
| 185 |
print("===================\n")
|
| 186 |
|
| 187 |
+
# Additional IP-Adapter diagnostic
|
| 188 |
+
print("=== IP-ADAPTER DIAGNOSTIC ===")
|
| 189 |
+
print(f"InstantID ControlNet loaded: {self.models_loaded.get('instantid', False)}")
|
| 190 |
+
print(f"Image encoder available: {self.image_encoder is not None}")
|
| 191 |
+
print(f"Image projection model available: {self.image_proj_model is not None}")
|
| 192 |
+
print(f"IP-Adapter marked as loaded: {self.models_loaded.get('ip_adapter', False)}")
|
| 193 |
+
|
| 194 |
+
if self.models_loaded.get('ip_adapter', False):
|
| 195 |
+
print("✓ IP-Adapter FULLY FUNCTIONAL - face embeddings will be used")
|
| 196 |
+
else:
|
| 197 |
+
print("✗ IP-Adapter NOT AVAILABLE - will use keypoints only (reduced quality)")
|
| 198 |
+
if not self.models_loaded.get('instantid', False):
|
| 199 |
+
print(" Issue: InstantID ControlNet failed to load")
|
| 200 |
+
if self.image_encoder is None:
|
| 201 |
+
print(" Issue: Image encoder (CLIP) failed to load")
|
| 202 |
+
if self.image_proj_model is None:
|
| 203 |
+
print(" Issue: Image projection model (Resampler) failed to load")
|
| 204 |
+
print("=============================\n")
|
| 205 |
+
|
| 206 |
print("=== UPGRADE VERIFICATION ===")
|
| 207 |
try:
|
| 208 |
from resampler_enhanced import EnhancedResampler
|
|
|
|
| 228 |
def get_depth_map(self, image):
|
| 229 |
"""
|
| 230 |
Generate depth map using available depth detector.
|
| 231 |
+
Supports: LeresDetector, ZoeDetector, or MidasDetector.
|
| 232 |
"""
|
| 233 |
if self.depth_detector is not None:
|
| 234 |
try:
|
|
|
|
| 290 |
+1-2% improvement in face preservation.
|
| 291 |
"""
|
| 292 |
try:
|
| 293 |
+
# Check if face has valid embedding first
|
| 294 |
+
if not hasattr(face, 'normed_embedding') or face.normed_embedding is None:
|
| 295 |
+
print("[MULTI-SCALE] Face has no normed_embedding, cannot extract features")
|
| 296 |
+
return None
|
| 297 |
+
|
| 298 |
multi_scale_embeds = []
|
| 299 |
|
| 300 |
for scale in MULTI_SCALE_FACTORS:
|
|
|
|
| 310 |
scaled_array = cv2.cvtColor(np.array(scaled_crop), cv2.COLOR_RGB2BGR)
|
| 311 |
scaled_faces = self.face_app.get(scaled_array)
|
| 312 |
|
| 313 |
+
if len(scaled_faces) > 0 and hasattr(scaled_faces[0], 'normed_embedding'):
|
| 314 |
+
if scaled_faces[0].normed_embedding is not None:
|
| 315 |
+
multi_scale_embeds.append(scaled_faces[0].normed_embedding)
|
| 316 |
|
| 317 |
# Average embeddings
|
| 318 |
if len(multi_scale_embeds) > 0:
|
|
|
|
| 322 |
print(f"[MULTI-SCALE] Combined {len(multi_scale_embeds)} scales")
|
| 323 |
return averaged
|
| 324 |
|
| 325 |
+
# Return original if multi-scale failed but original exists
|
| 326 |
+
if hasattr(face, 'normed_embedding') and face.normed_embedding is not None:
|
| 327 |
+
print("[MULTI-SCALE] Multi-scale failed, using original embedding")
|
| 328 |
+
return face.normed_embedding
|
| 329 |
+
|
| 330 |
+
print("[MULTI-SCALE] No embeddings available at any scale")
|
| 331 |
+
return None
|
| 332 |
|
| 333 |
except Exception as e:
|
| 334 |
print(f"[MULTI-SCALE] Failed: {e}, using single scale")
|
|
|
|
| 588 |
# Generate depth map
|
| 589 |
depth_image = None
|
| 590 |
if self.depth_active:
|
| 591 |
+
print("Generating Zoe depth map...")
|
| 592 |
depth_image = self.get_depth_map(resized_image)
|
| 593 |
if depth_image.size != (target_width, target_height):
|
| 594 |
depth_image = depth_image.resize((int(target_width), int(target_height)), Image.LANCZOS)
|
|
|
|
| 643 |
guidance_scale = adaptive_params['guidance_scale']
|
| 644 |
lora_scale = adaptive_params['lora_scale']
|
| 645 |
|
| 646 |
+
# Extract face embeddings with validation
|
| 647 |
+
try:
|
| 648 |
+
if not hasattr(face, 'normed_embedding') or face.normed_embedding is None:
|
| 649 |
+
print(" [ERROR] Face object has no normed_embedding attribute")
|
| 650 |
+
face_embeddings_base = None
|
| 651 |
+
else:
|
| 652 |
+
face_embeddings_base = face.normed_embedding
|
| 653 |
+
print(f" [OK] Base embeddings extracted: shape {face_embeddings_base.shape}")
|
| 654 |
+
except Exception as e:
|
| 655 |
+
print(f" [ERROR] Failed to extract base embeddings: {e}")
|
| 656 |
+
face_embeddings_base = None
|
| 657 |
|
| 658 |
+
# Extract face crop with validation
|
| 659 |
+
try:
|
| 660 |
+
bbox = face.bbox.astype(int)
|
| 661 |
+
x1, y1, x2, y2 = bbox[0], bbox[1], bbox[2], bbox[3]
|
| 662 |
+
face_bbox_original = [x1, y1, x2, y2]
|
| 663 |
+
|
| 664 |
+
# Validate bbox
|
| 665 |
+
face_width = x2 - x1
|
| 666 |
+
face_height = y2 - y1
|
| 667 |
+
|
| 668 |
+
print(f" [INFO] Face bbox: ({x1}, {y1}, {x2}, {y2}), size: {face_width}x{face_height}")
|
| 669 |
+
|
| 670 |
+
if face_width <= 0 or face_height <= 0:
|
| 671 |
+
print(f" [ERROR] Invalid face dimensions: {face_width}x{face_height}")
|
| 672 |
+
raise ValueError("Invalid face bbox")
|
| 673 |
+
|
| 674 |
+
if face_width < 20 or face_height < 20:
|
| 675 |
+
print(f" [WARNING] Face very small: {face_width}x{face_height} (may affect quality)")
|
| 676 |
+
|
| 677 |
+
# Add padding
|
| 678 |
+
padding_x = int(face_width * 0.3)
|
| 679 |
+
padding_y = int(face_height * 0.3)
|
| 680 |
+
x1 = max(0, x1 - padding_x)
|
| 681 |
+
y1 = max(0, y1 - padding_y)
|
| 682 |
+
x2 = min(resized_image.width, x2 + padding_x)
|
| 683 |
+
y2 = min(resized_image.height, y2 + padding_y)
|
| 684 |
+
|
| 685 |
+
# Validate padded bbox
|
| 686 |
+
if x2 <= x1 or y2 <= y1:
|
| 687 |
+
print(f" [ERROR] Invalid padded bbox: ({x1}, {y1}, {x2}, {y2})")
|
| 688 |
+
raise ValueError("Invalid padded bbox")
|
| 689 |
+
|
| 690 |
+
# Crop face region
|
| 691 |
+
face_crop = resized_image.crop((x1, y1, x2, y2))
|
| 692 |
+
print(f" [OK] Face cropped: {face_crop.size}")
|
| 693 |
+
|
| 694 |
+
except Exception as e:
|
| 695 |
+
print(f" [ERROR] Face cropping failed: {e}")
|
| 696 |
+
face_crop = None
|
| 697 |
+
face_bbox_original = None
|
| 698 |
|
| 699 |
+
# MULTI-SCALE PROCESSING (only if we have valid crop and base embeddings)
|
| 700 |
+
if face_crop is not None and face_embeddings_base is not None:
|
| 701 |
+
try:
|
| 702 |
+
face_embeddings = self.extract_multi_scale_face(face_crop, face)
|
| 703 |
+
print(f" [OK] Multi-scale embeddings extracted")
|
| 704 |
+
except Exception as e:
|
| 705 |
+
print(f" [WARNING] Multi-scale extraction failed: {e}, using base embeddings")
|
| 706 |
+
face_embeddings = face_embeddings_base
|
| 707 |
+
else:
|
| 708 |
+
print(f" [ERROR] Cannot extract embeddings - crop or base embeddings unavailable")
|
| 709 |
+
face_embeddings = None
|
| 710 |
|
| 711 |
+
# Enhance face crop (only if crop succeeded)
|
| 712 |
+
if face_crop is not None:
|
| 713 |
+
try:
|
| 714 |
+
face_crop_enhanced = enhance_face_crop(face_crop)
|
| 715 |
+
print(f" [OK] Face crop enhanced: {face_crop_enhanced.size}")
|
| 716 |
+
except Exception as e:
|
| 717 |
+
print(f" [WARNING] Face enhancement failed: {e}, using original crop")
|
| 718 |
+
face_crop_enhanced = face_crop
|
| 719 |
+
else:
|
| 720 |
+
print(f" [ERROR] Cannot enhance - no face crop available")
|
| 721 |
+
face_crop_enhanced = None
|
| 722 |
|
| 723 |
# Draw keypoints
|
| 724 |
face_kps = face.kps
|
|
|
|
| 790 |
print(" - MediapipeFace: tried, found nothing")
|
| 791 |
else:
|
| 792 |
print(" - MediapipeFace: not available")
|
| 793 |
+
|
| 794 |
+
print("\n[RECOMMENDATION] To improve face detection:")
|
| 795 |
+
print(" 1. Ensure face is clearly visible and front-facing")
|
| 796 |
+
print(" 2. Face should be at least 30% of the image area")
|
| 797 |
+
print(" 3. Use good lighting and avoid extreme angles")
|
| 798 |
+
print(" 4. Minimum recommended face size: 100x100 pixels")
|
| 799 |
+
print()
|
| 800 |
+
elif face_embeddings is None and has_detected_faces:
|
| 801 |
+
print("\n[SUMMARY] Face detected but embeddings extraction failed")
|
| 802 |
+
print("[REASON] This can happen when:")
|
| 803 |
+
print(" 1. Face is detected but too small for embedding extraction (<50x50px)")
|
| 804 |
+
print(" 2. Face angle is too extreme (profile view >45°)")
|
| 805 |
+
print(" 3. Face is partially occluded or cut off at image edge")
|
| 806 |
+
print(" 4. Detection confidence is low (<0.5)")
|
| 807 |
+
print("\n[RECOMMENDATION] To fix:")
|
| 808 |
+
print(" 1. Use a larger, clearer image")
|
| 809 |
+
print(" 2. Ensure face is centered and front-facing")
|
| 810 |
+
print(" 3. Crop image to focus on the face")
|
| 811 |
+
print(" 4. Avoid faces near image borders")
|
| 812 |
+
print("\n[IMPACT] Generation will use keypoints only (85-90% similarity vs 96-99% with embeddings)")
|
| 813 |
print()
|
| 814 |
|
| 815 |
# Set LORA scale
|
|
|
|
| 880 |
# Add face embeddings for IP-Adapter if available
|
| 881 |
if face_embeddings is not None and self.models_loaded.get('ip_adapter', False) and face_crop_enhanced is not None:
|
| 882 |
print(f"Processing InstantID face embeddings with Resampler...")
|
| 883 |
+
print(f" [DEBUG] face_embeddings shape: {face_embeddings.shape if hasattr(face_embeddings, 'shape') else 'numpy array'}")
|
| 884 |
+
print(f" [DEBUG] image_proj_model available: {self.image_proj_model is not None}")
|
| 885 |
+
print(f" [DEBUG] IP-Adapter loaded: {self.models_loaded.get('ip_adapter', False)}")
|
| 886 |
|
| 887 |
+
try:
|
| 888 |
+
with torch.no_grad():
|
| 889 |
+
face_emb_tensor = torch.from_numpy(face_embeddings).to(device=self.device, dtype=self.dtype)
|
| 890 |
+
face_emb_tensor = face_emb_tensor.reshape(1, -1, 512)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 891 |
|
| 892 |
+
if self.image_proj_model is None:
|
| 893 |
+
print(" [ERROR] image_proj_model is None! Cannot process embeddings.")
|
| 894 |
+
else:
|
| 895 |
+
face_proj_embeds = self.image_proj_model(face_emb_tensor)
|
| 896 |
+
|
| 897 |
+
boosted_scale = identity_preservation * IDENTITY_BOOST_MULTIPLIER
|
| 898 |
+
face_proj_embeds = face_proj_embeds * boosted_scale
|
| 899 |
+
|
| 900 |
+
print(f" - Face embedding: {face_emb_tensor.shape} -> {face_proj_embeds.shape}, Scale: {boosted_scale:.2f}")
|
| 901 |
+
|
| 902 |
+
if 'prompt_embeds' in pipe_kwargs:
|
| 903 |
+
original_embeds = pipe_kwargs['prompt_embeds']
|
| 904 |
+
|
| 905 |
+
if original_embeds.shape[0] > 1: # Handle CFG
|
| 906 |
+
face_proj_embeds = torch.cat([torch.zeros_like(face_proj_embeds), face_proj_embeds], dim=0)
|
| 907 |
+
|
| 908 |
+
combined_embeds = torch.cat([original_embeds, face_proj_embeds], dim=1)
|
| 909 |
+
pipe_kwargs['prompt_embeds'] = combined_embeds
|
| 910 |
+
print(f" [OK] Face embeddings concatenated successfully! New shape: {combined_embeds.shape}")
|
| 911 |
+
else:
|
| 912 |
+
print(f" [WARNING] Can't concatenate - no prompt_embeds (use Compel)")
|
| 913 |
+
except Exception as e:
|
| 914 |
+
print(f" [ERROR] Failed to process face embeddings: {e}")
|
| 915 |
+
import traceback
|
| 916 |
+
traceback.print_exc()
|
| 917 |
|
| 918 |
elif has_detected_faces:
|
| 919 |
print(" Face detected but IP-Adapter/embeddings unavailable, using keypoints only")
|
| 920 |
+
print(f" - face_embeddings available: {face_embeddings is not None}")
|
| 921 |
+
print(f" - IP-Adapter loaded: {self.models_loaded.get('ip_adapter', False)}")
|
| 922 |
+
print(f" - face_crop_enhanced available: {face_crop_enhanced is not None}")
|
| 923 |
+
print(f" - image_proj_model available: {self.image_proj_model is not None}")
|
| 924 |
|
| 925 |
else:
|
| 926 |
# No face detected - blank map needed to maintain ControlNet list order
|
models.py
CHANGED
|
@@ -13,7 +13,7 @@ from diffusers import (
|
|
| 13 |
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 14 |
from transformers import CLIPVisionModelWithProjection
|
| 15 |
from insightface.app import FaceAnalysis
|
| 16 |
-
from controlnet_aux import OpenposeDetector, LeresDetector, MidasDetector, MediapipeFaceDetector
|
| 17 |
from huggingface_hub import hf_hub_download
|
| 18 |
from compel import Compel, ReturnedEmbeddingsType
|
| 19 |
|
|
@@ -62,7 +62,7 @@ def download_model_with_retry(repo_id, filename, max_retries=None):
|
|
| 62 |
|
| 63 |
|
| 64 |
def load_face_analysis():
|
| 65 |
-
"""Load face analysis model with proper error handling."""
|
| 66 |
print("Loading face analysis model...")
|
| 67 |
try:
|
| 68 |
face_app = FaceAnalysis(
|
|
@@ -70,20 +70,39 @@ def load_face_analysis():
|
|
| 70 |
root='./models/insightface',
|
| 71 |
providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
|
| 72 |
)
|
|
|
|
|
|
|
|
|
|
| 73 |
face_app.prepare(
|
| 74 |
ctx_id=FACE_DETECTION_CONFIG['ctx_id'],
|
| 75 |
det_size=FACE_DETECTION_CONFIG['det_size']
|
| 76 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
print(" [OK] Face analysis model loaded successfully")
|
| 78 |
return face_app, True
|
| 79 |
except Exception as e:
|
| 80 |
print(f" [WARNING] Face detection not available: {e}")
|
|
|
|
|
|
|
| 81 |
return None, False
|
| 82 |
|
| 83 |
|
| 84 |
def load_depth_detector():
|
| 85 |
"""
|
| 86 |
-
Load depth detector with fallback hierarchy: Leres
|
| 87 |
Returns (detector, detector_type, success).
|
| 88 |
"""
|
| 89 |
print("Loading depth detector with fallback hierarchy...")
|
|
@@ -98,9 +117,19 @@ def load_depth_detector():
|
|
| 98 |
except Exception as e:
|
| 99 |
print(f" [INFO] LeresDetector not available: {e}")
|
| 100 |
|
| 101 |
-
# Fallback to
|
| 102 |
try:
|
| 103 |
-
print(" Attempting
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
midas_depth = MidasDetector.from_pretrained("lllyasviel/Annotators")
|
| 105 |
midas_depth.to(device)
|
| 106 |
print(" [OK] MidasDetector loaded successfully")
|
|
@@ -140,24 +169,40 @@ def load_mediapipe_face_detector():
|
|
| 140 |
|
| 141 |
def load_controlnets():
|
| 142 |
"""Load ControlNet models."""
|
| 143 |
-
print("Loading ControlNet Depth model...")
|
| 144 |
controlnet_depth = ControlNetModel.from_pretrained(
|
| 145 |
-
"diffusers/controlnet-zoe-depth-sdxl-1.0",
|
| 146 |
torch_dtype=dtype
|
| 147 |
).to(device)
|
| 148 |
print(" [OK] ControlNet Depth loaded")
|
| 149 |
|
| 150 |
# --- NEW: Load OpenPose ControlNet ---
|
| 151 |
print("Loading ControlNet OpenPose model...")
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# --- END NEW ---
|
| 162 |
|
| 163 |
print("Loading InstantID ControlNet...")
|
|
@@ -237,18 +282,37 @@ def setup_ip_adapter(pipe, image_encoder):
|
|
| 237 |
Based on the reference InstantID pipeline.
|
| 238 |
"""
|
| 239 |
if image_encoder is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
return None, False
|
| 241 |
|
| 242 |
print("Setting up IP-Adapter for InstantID face embeddings (proper implementation)...")
|
|
|
|
|
|
|
| 243 |
try:
|
| 244 |
-
|
| 245 |
ip_adapter_path = download_model_with_retry(
|
| 246 |
"InstantX/InstantID",
|
| 247 |
"ip-adapter.bin"
|
| 248 |
)
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
state_dict = torch.load(ip_adapter_path, map_location="cpu")
|
|
|
|
| 252 |
|
| 253 |
# Extract image_proj and ip_adapter weights
|
| 254 |
image_proj_state_dict = {}
|
|
@@ -260,38 +324,81 @@ def setup_ip_adapter(pipe, image_encoder):
|
|
| 260 |
elif key.startswith("ip_adapter."):
|
| 261 |
ip_adapter_state_dict[key.replace("ip_adapter.", "")] = value
|
| 262 |
|
| 263 |
-
|
| 264 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
image_proj_model = Resampler(
|
| 266 |
-
dim=1280,
|
| 267 |
-
depth=4,
|
| 268 |
-
dim_head=64,
|
| 269 |
-
heads=20,
|
| 270 |
-
num_queries=16,
|
| 271 |
-
embedding_dim=512,
|
| 272 |
-
output_dim=
|
| 273 |
-
ff_mult=4
|
| 274 |
)
|
| 275 |
|
| 276 |
image_proj_model.eval()
|
| 277 |
image_proj_model = image_proj_model.to(device, dtype=dtype)
|
|
|
|
| 278 |
|
| 279 |
-
# Load
|
| 280 |
if image_proj_state_dict:
|
| 281 |
try:
|
| 282 |
-
image_proj_model.load_state_dict(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
print(" [OK] Resampler loaded with pretrained weights")
|
| 284 |
except Exception as e:
|
| 285 |
print(f" [WARNING] Could not load Resampler weights: {e}")
|
| 286 |
-
print(" Using randomly initialized Resampler")
|
| 287 |
else:
|
| 288 |
-
print(" [WARNING] No image_proj weights
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
attn_procs = {}
|
| 293 |
-
num_tokens = 16
|
| 294 |
|
|
|
|
| 295 |
for name in pipe.unet.attn_processors.keys():
|
| 296 |
cross_attention_dim = None if name.endswith("attn1.processor") else pipe.unet.config.cross_attention_dim
|
| 297 |
|
|
@@ -315,32 +422,48 @@ def setup_ip_adapter(pipe, image_encoder):
|
|
| 315 |
scale=1.0,
|
| 316 |
num_tokens=num_tokens
|
| 317 |
).to(device, dtype=dtype)
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
# Set attention processors
|
| 320 |
pipe.unet.set_attn_processor(attn_procs)
|
|
|
|
| 321 |
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
if ip_adapter_state_dict:
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
print(f" [WARNING]
|
|
|
|
|
|
|
|
|
|
| 330 |
else:
|
| 331 |
-
print(" [WARNING] No ip_adapter weights
|
| 332 |
|
| 333 |
-
# Store image encoder
|
| 334 |
pipe.image_encoder = image_encoder
|
| 335 |
|
| 336 |
-
print(" [
|
| 337 |
-
print(f"
|
| 338 |
-
print(f"
|
|
|
|
| 339 |
|
| 340 |
return image_proj_model, True
|
| 341 |
|
| 342 |
except Exception as e:
|
| 343 |
-
print(f" [ERROR]
|
| 344 |
import traceback
|
| 345 |
traceback.print_exc()
|
| 346 |
return None, False
|
|
|
|
| 13 |
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 14 |
from transformers import CLIPVisionModelWithProjection
|
| 15 |
from insightface.app import FaceAnalysis
|
| 16 |
+
from controlnet_aux import ZoeDetector, OpenposeDetector, LeresDetector, MidasDetector, MediapipeFaceDetector
|
| 17 |
from huggingface_hub import hf_hub_download
|
| 18 |
from compel import Compel, ReturnedEmbeddingsType
|
| 19 |
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
def load_face_analysis():
|
| 65 |
+
"""Load face analysis model with proper error handling and recognition enabled."""
|
| 66 |
print("Loading face analysis model...")
|
| 67 |
try:
|
| 68 |
face_app = FaceAnalysis(
|
|
|
|
| 70 |
root='./models/insightface',
|
| 71 |
providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
|
| 72 |
)
|
| 73 |
+
|
| 74 |
+
# Prepare with explicit recognition model enabled
|
| 75 |
+
print(" Preparing face analysis with recognition...")
|
| 76 |
face_app.prepare(
|
| 77 |
ctx_id=FACE_DETECTION_CONFIG['ctx_id'],
|
| 78 |
det_size=FACE_DETECTION_CONFIG['det_size']
|
| 79 |
)
|
| 80 |
+
|
| 81 |
+
# Verify recognition model is available
|
| 82 |
+
has_rec = False
|
| 83 |
+
for task in face_app.models.keys():
|
| 84 |
+
if 'recognition' in task or 'rec' in task:
|
| 85 |
+
has_rec = True
|
| 86 |
+
print(f" [OK] Recognition model found: {task}")
|
| 87 |
+
break
|
| 88 |
+
|
| 89 |
+
if not has_rec:
|
| 90 |
+
print(" [WARNING] No recognition model found in face_app")
|
| 91 |
+
print(f" [INFO] Available models: {list(face_app.models.keys())}")
|
| 92 |
+
print(" [INFO] Face embeddings may not be available")
|
| 93 |
+
|
| 94 |
print(" [OK] Face analysis model loaded successfully")
|
| 95 |
return face_app, True
|
| 96 |
except Exception as e:
|
| 97 |
print(f" [WARNING] Face detection not available: {e}")
|
| 98 |
+
import traceback
|
| 99 |
+
traceback.print_exc()
|
| 100 |
return None, False
|
| 101 |
|
| 102 |
|
| 103 |
def load_depth_detector():
|
| 104 |
"""
|
| 105 |
+
Load depth detector with fallback hierarchy: Leres → Zoe → Midas.
|
| 106 |
Returns (detector, detector_type, success).
|
| 107 |
"""
|
| 108 |
print("Loading depth detector with fallback hierarchy...")
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
print(f" [INFO] LeresDetector not available: {e}")
|
| 119 |
|
| 120 |
+
# Fallback to ZoeDetector
|
| 121 |
try:
|
| 122 |
+
print(" Attempting ZoeDetector (fallback #1)...")
|
| 123 |
+
zoe_depth = ZoeDetector.from_pretrained("lllyasviel/Annotators")
|
| 124 |
+
zoe_depth.to(device)
|
| 125 |
+
print(" [OK] ZoeDetector loaded successfully")
|
| 126 |
+
return zoe_depth, 'zoe', True
|
| 127 |
+
except Exception as e:
|
| 128 |
+
print(f" [INFO] ZoeDetector not available: {e}")
|
| 129 |
+
|
| 130 |
+
# Final fallback to MidasDetector
|
| 131 |
+
try:
|
| 132 |
+
print(" Attempting MidasDetector (fallback #2)...")
|
| 133 |
midas_depth = MidasDetector.from_pretrained("lllyasviel/Annotators")
|
| 134 |
midas_depth.to(device)
|
| 135 |
print(" [OK] MidasDetector loaded successfully")
|
|
|
|
| 169 |
|
| 170 |
def load_controlnets():
|
| 171 |
"""Load ControlNet models."""
|
| 172 |
+
print("Loading ControlNet Zoe Depth model...")
|
| 173 |
controlnet_depth = ControlNetModel.from_pretrained(
|
| 174 |
+
"diffusers/controlnet-zoe-depth-sdxl-1.0",
|
| 175 |
torch_dtype=dtype
|
| 176 |
).to(device)
|
| 177 |
print(" [OK] ControlNet Depth loaded")
|
| 178 |
|
| 179 |
# --- NEW: Load OpenPose ControlNet ---
|
| 180 |
print("Loading ControlNet OpenPose model...")
|
| 181 |
+
controlnet_openpose = None # Initialize as None
|
| 182 |
+
|
| 183 |
+
# Try multiple known OpenPose ControlNet models for SDXL
|
| 184 |
+
openpose_models = [
|
| 185 |
+
("lllyasviel/control_v11p_sd15_openpose", "SDXL-compatible OpenPose from lllyasviel"),
|
| 186 |
+
("CrucibleAI/ControlNetMediaPipeFace", "MediaPipe Face alternative"),
|
| 187 |
+
]
|
| 188 |
+
|
| 189 |
+
for model_id, description in openpose_models:
|
| 190 |
+
try:
|
| 191 |
+
print(f" Trying {description}: {model_id}")
|
| 192 |
+
controlnet_openpose = ControlNetModel.from_pretrained(
|
| 193 |
+
model_id,
|
| 194 |
+
torch_dtype=dtype
|
| 195 |
+
).to(device)
|
| 196 |
+
print(f" [OK] ControlNet OpenPose loaded from {model_id}")
|
| 197 |
+
break
|
| 198 |
+
except Exception as e:
|
| 199 |
+
print(f" [INFO] {model_id} not compatible: {str(e)[:100]}")
|
| 200 |
+
continue
|
| 201 |
+
|
| 202 |
+
if controlnet_openpose is None:
|
| 203 |
+
print(" [WARNING] No OpenPose ControlNet available for SDXL")
|
| 204 |
+
print(" [INFO] Expression control will be disabled (not critical)")
|
| 205 |
+
print(" [INFO] System will work with Identity + Depth ControlNets only")
|
| 206 |
# --- END NEW ---
|
| 207 |
|
| 208 |
print("Loading InstantID ControlNet...")
|
|
|
|
| 282 |
Based on the reference InstantID pipeline.
|
| 283 |
"""
|
| 284 |
if image_encoder is None:
|
| 285 |
+
print("[ERROR] setup_ip_adapter: image_encoder is None")
|
| 286 |
+
return None, False
|
| 287 |
+
|
| 288 |
+
if pipe is None:
|
| 289 |
+
print("[ERROR] setup_ip_adapter: pipe is None")
|
| 290 |
return None, False
|
| 291 |
|
| 292 |
print("Setting up IP-Adapter for InstantID face embeddings (proper implementation)...")
|
| 293 |
+
|
| 294 |
+
# Step 1: Download weights
|
| 295 |
try:
|
| 296 |
+
print(" [1/5] Downloading IP-Adapter weights...")
|
| 297 |
ip_adapter_path = download_model_with_retry(
|
| 298 |
"InstantX/InstantID",
|
| 299 |
"ip-adapter.bin"
|
| 300 |
)
|
| 301 |
+
if ip_adapter_path is None:
|
| 302 |
+
print(" [ERROR] Failed to download ip-adapter.bin")
|
| 303 |
+
return None, False
|
| 304 |
+
print(f" [OK] IP-Adapter weights downloaded to: {ip_adapter_path}")
|
| 305 |
+
except Exception as e:
|
| 306 |
+
print(f" [ERROR] Download failed: {e}")
|
| 307 |
+
import traceback
|
| 308 |
+
traceback.print_exc()
|
| 309 |
+
return None, False
|
| 310 |
+
|
| 311 |
+
# Step 2: Load state dict
|
| 312 |
+
try:
|
| 313 |
+
print(" [2/5] Loading state dict...")
|
| 314 |
state_dict = torch.load(ip_adapter_path, map_location="cpu")
|
| 315 |
+
print(f" [OK] State dict loaded with {len(state_dict)} keys")
|
| 316 |
|
| 317 |
# Extract image_proj and ip_adapter weights
|
| 318 |
image_proj_state_dict = {}
|
|
|
|
| 324 |
elif key.startswith("ip_adapter."):
|
| 325 |
ip_adapter_state_dict[key.replace("ip_adapter.", "")] = value
|
| 326 |
|
| 327 |
+
print(f" [OK] Extracted {len(image_proj_state_dict)} image_proj keys")
|
| 328 |
+
print(f" [OK] Extracted {len(ip_adapter_state_dict)} ip_adapter keys")
|
| 329 |
+
|
| 330 |
+
if len(image_proj_state_dict) == 0:
|
| 331 |
+
print(" [WARNING] No image_proj weights found in state dict!")
|
| 332 |
+
if len(ip_adapter_state_dict) == 0:
|
| 333 |
+
print(" [WARNING] No ip_adapter weights found in state dict!")
|
| 334 |
+
|
| 335 |
+
except Exception as e:
|
| 336 |
+
print(f" [ERROR] Failed to load state dict: {e}")
|
| 337 |
+
import traceback
|
| 338 |
+
traceback.print_exc()
|
| 339 |
+
return None, False
|
| 340 |
+
|
| 341 |
+
# Step 3: Create Resampler
|
| 342 |
+
try:
|
| 343 |
+
print(" [3/5] Creating Resampler (Perceiver architecture)...")
|
| 344 |
+
|
| 345 |
+
# Verify pipe config
|
| 346 |
+
if not hasattr(pipe.unet, 'config'):
|
| 347 |
+
print(" [ERROR] pipe.unet has no config attribute")
|
| 348 |
+
return None, False
|
| 349 |
+
|
| 350 |
+
if not hasattr(pipe.unet.config, 'cross_attention_dim'):
|
| 351 |
+
print(" [ERROR] pipe.unet.config has no cross_attention_dim")
|
| 352 |
+
return None, False
|
| 353 |
+
|
| 354 |
+
output_dim = pipe.unet.config.cross_attention_dim
|
| 355 |
+
print(f" [INFO] Using cross_attention_dim: {output_dim}")
|
| 356 |
+
|
| 357 |
image_proj_model = Resampler(
|
| 358 |
+
dim=1280,
|
| 359 |
+
depth=4,
|
| 360 |
+
dim_head=64,
|
| 361 |
+
heads=20,
|
| 362 |
+
num_queries=16,
|
| 363 |
+
embedding_dim=512,
|
| 364 |
+
output_dim=output_dim,
|
| 365 |
+
ff_mult=4
|
| 366 |
)
|
| 367 |
|
| 368 |
image_proj_model.eval()
|
| 369 |
image_proj_model = image_proj_model.to(device, dtype=dtype)
|
| 370 |
+
print(f" [OK] Resampler created and moved to {device}")
|
| 371 |
|
| 372 |
+
# Load weights
|
| 373 |
if image_proj_state_dict:
|
| 374 |
try:
|
| 375 |
+
missing_keys, unexpected_keys = image_proj_model.load_state_dict(
|
| 376 |
+
image_proj_state_dict, strict=False
|
| 377 |
+
)
|
| 378 |
+
if len(missing_keys) > 0:
|
| 379 |
+
print(f" [WARNING] Missing keys in Resampler: {len(missing_keys)}")
|
| 380 |
+
if len(unexpected_keys) > 0:
|
| 381 |
+
print(f" [WARNING] Unexpected keys in Resampler: {len(unexpected_keys)}")
|
| 382 |
print(" [OK] Resampler loaded with pretrained weights")
|
| 383 |
except Exception as e:
|
| 384 |
print(f" [WARNING] Could not load Resampler weights: {e}")
|
| 385 |
+
print(" [INFO] Using randomly initialized Resampler (reduced quality)")
|
| 386 |
else:
|
| 387 |
+
print(" [WARNING] No image_proj weights available (reduced quality)")
|
| 388 |
+
|
| 389 |
+
except Exception as e:
|
| 390 |
+
print(f" [ERROR] Failed to create Resampler: {e}")
|
| 391 |
+
import traceback
|
| 392 |
+
traceback.print_exc()
|
| 393 |
+
return None, False
|
| 394 |
+
|
| 395 |
+
# Step 4: Setup attention processors
|
| 396 |
+
try:
|
| 397 |
+
print(" [4/5] Setting up IP-Adapter attention processors...")
|
| 398 |
attn_procs = {}
|
| 399 |
+
num_tokens = 16
|
| 400 |
|
| 401 |
+
processor_count = 0
|
| 402 |
for name in pipe.unet.attn_processors.keys():
|
| 403 |
cross_attention_dim = None if name.endswith("attn1.processor") else pipe.unet.config.cross_attention_dim
|
| 404 |
|
|
|
|
| 422 |
scale=1.0,
|
| 423 |
num_tokens=num_tokens
|
| 424 |
).to(device, dtype=dtype)
|
| 425 |
+
processor_count += 1
|
| 426 |
+
|
| 427 |
+
print(f" [OK] Created {processor_count} IP-Adapter attention processors")
|
| 428 |
|
| 429 |
# Set attention processors
|
| 430 |
pipe.unet.set_attn_processor(attn_procs)
|
| 431 |
+
print(" [OK] Attention processors set on UNet")
|
| 432 |
|
| 433 |
+
except Exception as e:
|
| 434 |
+
print(f" [ERROR] Failed to setup attention processors: {e}")
|
| 435 |
+
import traceback
|
| 436 |
+
traceback.print_exc()
|
| 437 |
+
return None, False
|
| 438 |
+
|
| 439 |
+
# Step 5: Load IP-Adapter weights
|
| 440 |
+
try:
|
| 441 |
+
print(" [5/5] Loading IP-Adapter weights into attention processors...")
|
| 442 |
if ip_adapter_state_dict:
|
| 443 |
+
ip_layers = torch.nn.ModuleList(pipe.unet.attn_processors.values())
|
| 444 |
+
missing_keys, unexpected_keys = ip_layers.load_state_dict(
|
| 445 |
+
ip_adapter_state_dict, strict=False
|
| 446 |
+
)
|
| 447 |
+
if len(missing_keys) > 0:
|
| 448 |
+
print(f" [WARNING] Missing keys in IP-Adapter: {len(missing_keys)}")
|
| 449 |
+
if len(unexpected_keys) > 0:
|
| 450 |
+
print(f" [WARNING] Unexpected keys in IP-Adapter: {len(unexpected_keys)}")
|
| 451 |
+
print(" [OK] IP-Adapter attention weights loaded")
|
| 452 |
else:
|
| 453 |
+
print(" [WARNING] No ip_adapter weights available (reduced quality)")
|
| 454 |
|
| 455 |
+
# Store image encoder
|
| 456 |
pipe.image_encoder = image_encoder
|
| 457 |
|
| 458 |
+
print("\n [SUCCESS] IP-Adapter fully loaded with InstantID architecture")
|
| 459 |
+
print(f" - Resampler: 4 layers, 20 heads, 16 output tokens")
|
| 460 |
+
print(f" - Face embeddings: 512D -> 16x2048D")
|
| 461 |
+
print(f" - Device: {device}, dtype: {dtype}\n")
|
| 462 |
|
| 463 |
return image_proj_model, True
|
| 464 |
|
| 465 |
except Exception as e:
|
| 466 |
+
print(f" [ERROR] Failed to load IP-Adapter weights: {e}")
|
| 467 |
import traceback
|
| 468 |
traceback.print_exc()
|
| 469 |
return None, False
|