Spaces:
Runtime error
Runtime error
Update generator.py
Browse files- generator.py +22 -14
generator.py
CHANGED
|
@@ -101,7 +101,8 @@ class RetroArtConverter:
|
|
| 101 |
Generate depth map using available depth detector.
|
| 102 |
Supports: LeresDetector, ZoeDetector, or MidasDetector.
|
| 103 |
"""
|
| 104 |
-
|
|
|
|
| 105 |
try:
|
| 106 |
if image.mode != 'RGB':
|
| 107 |
image = image.convert('RGB')
|
|
@@ -120,14 +121,15 @@ class RetroArtConverter:
|
|
| 120 |
image_for_depth = image.resize(size_for_depth, Image.LANCZOS)
|
| 121 |
|
| 122 |
if target_width != orig_width or target_height != orig_height:
|
| 123 |
-
|
|
|
|
| 124 |
|
| 125 |
# Use torch.no_grad() and clear cache
|
| 126 |
with torch.no_grad():
|
| 127 |
-
# --- FIX:
|
| 128 |
-
self.
|
| 129 |
-
depth_image = self.
|
| 130 |
-
self.
|
| 131 |
|
| 132 |
# ADDED: Clear GPU cache after depth detection
|
| 133 |
if torch.cuda.is_available():
|
|
@@ -137,11 +139,13 @@ class RetroArtConverter:
|
|
| 137 |
if depth_width != orig_width or depth_height != orig_height:
|
| 138 |
depth_image = depth_image.resize((int(orig_width), int(orig_height)), Image.LANCZOS)
|
| 139 |
|
| 140 |
-
|
|
|
|
| 141 |
return depth_image
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
-
|
|
|
|
| 145 |
# ADDED: Clear cache on error
|
| 146 |
if torch.cuda.is_available():
|
| 147 |
torch.cuda.empty_cache()
|
|
@@ -237,7 +241,8 @@ class RetroArtConverter:
|
|
| 237 |
|
| 238 |
# Generate depth map
|
| 239 |
print("Generating depth map...")
|
| 240 |
-
|
|
|
|
| 241 |
|
| 242 |
if depth_image is None:
|
| 243 |
raise RuntimeError("Failed to generate depth map")
|
|
@@ -267,7 +272,8 @@ class RetroArtConverter:
|
|
| 267 |
face_bbox_original = face.bbox
|
| 268 |
|
| 269 |
print(f" [OK] Face detected")
|
| 270 |
-
|
|
|
|
| 271 |
print(f" - Keypoints: {face.kps.shape}")
|
| 272 |
print(f" - Bbox: {face_bbox_original}")
|
| 273 |
|
|
@@ -285,7 +291,7 @@ class RetroArtConverter:
|
|
| 285 |
print(" [ADAPTIVE] Low confidence - increasing identity weight")
|
| 286 |
identity_preservation = max(identity_preservation, ADAPTIVE_PARAMS['low_confidence']['identity_preservation'])
|
| 287 |
identity_control_scale = max(identity_control_scale, ADAPTIVE_PARAMS['low_confidence']['identity_control_scale'])
|
| 288 |
-
|
| 289 |
else:
|
| 290 |
print(" No faces detected in image")
|
| 291 |
|
|
@@ -370,14 +376,16 @@ class RetroArtConverter:
|
|
| 370 |
if face_embeddings is not None:
|
| 371 |
print(f"Adding face embeddings for IP-Adapter...")
|
| 372 |
|
| 373 |
-
#
|
| 374 |
-
|
|
|
|
| 375 |
|
| 376 |
# Control IP-Adapter strength
|
| 377 |
boosted_scale = identity_preservation * IDENTITY_BOOST_MULTIPLIER
|
| 378 |
pipe_kwargs["ip_adapter_scale"] = boosted_scale
|
| 379 |
|
| 380 |
-
|
|
|
|
| 381 |
print(f" - IP-Adapter scale: {boosted_scale:.2f}")
|
| 382 |
print(f" [OK] Face embeddings configured")
|
| 383 |
else:
|
|
|
|
| 101 |
Generate depth map using available depth detector.
|
| 102 |
Supports: LeresDetector, ZoeDetector, or MidasDetector.
|
| 103 |
"""
|
| 104 |
+
# --- FIX 1: Check for self.zoe_depth, not self.depth_detector ---
|
| 105 |
+
if self.zoe_depth is not None:
|
| 106 |
try:
|
| 107 |
if image.mode != 'RGB':
|
| 108 |
image = image.convert('RGB')
|
|
|
|
| 121 |
image_for_depth = image.resize(size_for_depth, Image.LANCZOS)
|
| 122 |
|
| 123 |
if target_width != orig_width or target_height != orig_height:
|
| 124 |
+
# --- FIX 2: Use "ZOE" instead of undefined self.depth_type ---
|
| 125 |
+
print(f"[DEPTH] Resized for ZOEDetector: {orig_width}x{orig_height} -> {target_width}x{target_height}")
|
| 126 |
|
| 127 |
# Use torch.no_grad() and clear cache
|
| 128 |
with torch.no_grad():
|
| 129 |
+
# --- FIX 1: Use self.zoe_depth ---
|
| 130 |
+
self.zoe_depth.to(self.device)
|
| 131 |
+
depth_image = self.zoe_depth(image_for_depth)
|
| 132 |
+
self.zoe_depth.to("cpu")
|
| 133 |
|
| 134 |
# ADDED: Clear GPU cache after depth detection
|
| 135 |
if torch.cuda.is_available():
|
|
|
|
| 139 |
if depth_width != orig_width or depth_height != orig_height:
|
| 140 |
depth_image = depth_image.resize((int(orig_width), int(orig_height)), Image.LANCZOS)
|
| 141 |
|
| 142 |
+
# --- FIX 2: Use "ZOE" instead of undefined self.depth_type ---
|
| 143 |
+
print(f"[DEPTH] ZOE depth map generated: {orig_width}x{orig_height}")
|
| 144 |
return depth_image
|
| 145 |
|
| 146 |
except Exception as e:
|
| 147 |
+
# --- FIX 2: Use "ZOE" instead of undefined self.depth_type ---
|
| 148 |
+
print(f"[DEPTH] ZOEDetector failed ({e}), falling back to grayscale depth")
|
| 149 |
# ADDED: Clear cache on error
|
| 150 |
if torch.cuda.is_available():
|
| 151 |
torch.cuda.empty_cache()
|
|
|
|
| 241 |
|
| 242 |
# Generate depth map
|
| 243 |
print("Generating depth map...")
|
| 244 |
+
# --- FIX 3: get_depth_map only returns one value ---
|
| 245 |
+
depth_image = self.get_depth_map(resized_image)
|
| 246 |
|
| 247 |
if depth_image is None:
|
| 248 |
raise RuntimeError("Failed to generate depth map")
|
|
|
|
| 272 |
face_bbox_original = face.bbox
|
| 273 |
|
| 274 |
print(f" [OK] Face detected")
|
| 275 |
+
# --- FIX 4: Clarify this is the numpy shape ---
|
| 276 |
+
print(f" - Embedding shape (numpy): {face_embeddings.shape}")
|
| 277 |
print(f" - Keypoints: {face.kps.shape}")
|
| 278 |
print(f" - Bbox: {face_bbox_original}")
|
| 279 |
|
|
|
|
| 291 |
print(" [ADAPTIVE] Low confidence - increasing identity weight")
|
| 292 |
identity_preservation = max(identity_preservation, ADAPTIVE_PARAMS['low_confidence']['identity_preservation'])
|
| 293 |
identity_control_scale = max(identity_control_scale, ADAPTIVE_PARAMS['low_confidence']['identity_control_scale'])
|
| 294 |
+
|
| 295 |
else:
|
| 296 |
print(" No faces detected in image")
|
| 297 |
|
|
|
|
| 376 |
if face_embeddings is not None:
|
| 377 |
print(f"Adding face embeddings for IP-Adapter...")
|
| 378 |
|
| 379 |
+
# --- FIX 4: Convert numpy array to torch tensor, add batch dim, and move to device ---
|
| 380 |
+
face_embeds_tensor = torch.tensor(face_embeddings, dtype=self.dtype, device=self.device).unsqueeze(0)
|
| 381 |
+
pipe_kwargs["image_embeds"] = face_embeds_tensor
|
| 382 |
|
| 383 |
# Control IP-Adapter strength
|
| 384 |
boosted_scale = identity_preservation * IDENTITY_BOOST_MULTIPLIER
|
| 385 |
pipe_kwargs["ip_adapter_scale"] = boosted_scale
|
| 386 |
|
| 387 |
+
# --- FIX 4: Update log to show tensor shape ---
|
| 388 |
+
print(f" - Face embeddings tensor shape: {face_embeds_tensor.shape}")
|
| 389 |
print(f" - IP-Adapter scale: {boosted_scale:.2f}")
|
| 390 |
print(f" [OK] Face embeddings configured")
|
| 391 |
else:
|