primerz commited on
Commit
92a1eaf
·
verified ·
1 Parent(s): a072f7a

Update generator.py

Browse files
Files changed (1) hide show
  1. generator.py +54 -46
generator.py CHANGED
@@ -1,6 +1,7 @@
1
  """
2
  Generation logic for Pixagram AI Pixel Art Generator
3
  """
 
4
  import torch
5
  import numpy as np
6
  import cv2
@@ -196,55 +197,62 @@ class RetroArtConverter:
196
  print(f"[INFO] Verification skipped: {e}")
197
  print("============================\n")
198
 
 
199
  def get_depth_map(self, image):
200
- """
201
- Generate depth map using available depth detector.
202
- Supports: LeresDetector, ZoeDetector, or MidasDetector.
203
- """
204
- if self.depth_detector is not None:
205
- try:
206
- if image.mode != 'RGB':
207
- image = image.convert('RGB')
208
-
209
- orig_width, orig_height = image.size
210
- orig_width = int(orig_width)
211
- orig_height = int(orig_height)
212
-
213
- # FIXED: Use multiples of 64 (not 32)
214
- target_width = int((orig_width // 64) * 64)
215
- target_height = int((orig_height // 64) * 64)
216
-
217
- target_width = int(max(64, target_width))
218
- target_height = int(max(64, target_height))
219
-
220
- size_for_depth = (int(target_width), int(target_height))
221
- image_for_depth = image.resize(size_for_depth, Image.LANCZOS)
222
-
223
- if target_width != orig_width or target_height != orig_height:
224
- print(f"[DEPTH] Resized for {self.depth_type.upper()}Detector: {orig_width}x{orig_height} -> {target_width}x{target_height}")
225
-
226
- # FIXED: Add torch.no_grad() wrapper
227
- with torch.no_grad():
228
- depth_image = self.depth_detector(image_for_depth)
229
-
230
- depth_width, depth_height = depth_image.size
231
- if depth_width != orig_width or depth_height != orig_height:
232
- depth_image = depth_image.resize((int(orig_width), int(orig_height)), Image.LANCZOS)
233
-
234
- print(f"[DEPTH] {self.depth_type.upper()} depth map generated: {orig_width}x{orig_height}")
235
- return depth_image
236
-
237
- except Exception as e:
238
- print(f"[DEPTH] {self.depth_type.upper()}Detector failed ({e}), falling back to grayscale depth")
239
- gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
240
- depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
241
- return Image.fromarray(depth_colored)
242
- else:
243
- # No depth detector available, use grayscale fallback
244
- print("[DEPTH] No depth detector available, using grayscale fallback")
 
245
  gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
246
  depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
247
- return Image.fromarray(depth_colored)
 
 
 
 
 
248
 
249
 
250
  def add_trigger_word(self, prompt):
 
1
  """
2
  Generation logic for Pixagram AI Pixel Art Generator
3
  """
4
+ import gc
5
  import torch
6
  import numpy as np
7
  import cv2
 
197
  print(f"[INFO] Verification skipped: {e}")
198
  print("============================\n")
199
 
200
+
201
  def get_depth_map(self, image):
202
+ """
203
+ Generate depth map using available depth detector.
204
+ Supports: LeresDetector, ZoeDetector, or MidasDetector.
205
+ """
206
+ if self.depth_detector is not None:
207
+ try:
208
+ if image.mode != 'RGB':
209
+ image = image.convert('RGB')
210
+
211
+ orig_width, orig_height = image.size
212
+ orig_width = int(orig_width)
213
+ orig_height = int(orig_height)
214
+
215
+ target_width = int((orig_width // 64) * 64)
216
+ target_height = int((orig_height // 64) * 64)
217
+
218
+ target_width = int(max(64, target_width))
219
+ target_height = int(max(64, target_height))
220
+
221
+ size_for_depth = (int(target_width), int(target_height))
222
+ image_for_depth = image.resize(size_for_depth, Image.LANCZOS)
223
+
224
+ if target_width != orig_width or target_height != orig_height:
225
+ print(f"[DEPTH] Resized for {self.depth_type.upper()}Detector: {orig_width}x{orig_height} -> {target_width}x{target_height}")
226
+
227
+ # Use torch.no_grad() and clear cache
228
+ with torch.no_grad():
229
+ depth_image = self.depth_detector(image_for_depth)
230
+
231
+ # ADDED: Clear GPU cache after depth detection
232
+ if torch.cuda.is_available():
233
+ torch.cuda.empty_cache()
234
+
235
+ depth_width, depth_height = depth_image.size
236
+ if depth_width != orig_width or depth_height != orig_height:
237
+ depth_image = depth_image.resize((int(orig_width), int(orig_height)), Image.LANCZOS)
238
+
239
+ print(f"[DEPTH] {self.depth_type.upper()} depth map generated: {orig_width}x{orig_height}")
240
+ return depth_image
241
+
242
+ except Exception as e:
243
+ print(f"[DEPTH] {self.depth_type.upper()}Detector failed ({e}), falling back to grayscale depth")
244
+ # ADDED: Clear cache on error
245
+ if torch.cuda.is_available():
246
+ torch.cuda.empty_cache()
247
+
248
  gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
249
  depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
250
+ return Image.fromarray(depth_colored)
251
+ else:
252
+ print("[DEPTH] No depth detector available, using grayscale fallback")
253
+ gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
254
+ depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
255
+ return Image.fromarray(depth_colored)
256
 
257
 
258
  def add_trigger_word(self, prompt):