jebin2 commited on
Commit
3bc1feb
·
1 Parent(s): cb61203

clean up mem

Browse files
comic_panel_extractor/annorator_server.py CHANGED
@@ -63,13 +63,13 @@ def load_yolo_boxes(image_path: str, label_path: str, detect: bool = False):
63
  if detect and not os.path.exists(label_path):
64
  from .yolo_manager import YOLOManager
65
  from .utils import Config
66
- yolo_manager = YOLOManager()
67
- weights_path = f'{current_path}/{Config.YOLO_MODEL_NAME}.pt'
68
 
69
- yolo_manager.load_model(weights_path)
70
 
71
- # Run inference
72
- _, label_path = yolo_manager.annotate_images(image_paths=[image_path], output_dir=IMAGE_LABEL_ROOT, save_image=False, label_path=label_path)
73
 
74
  if os.path.exists(label_path):
75
  with open(label_path, "r") as f:
 
63
  if detect and not os.path.exists(label_path):
64
  from .yolo_manager import YOLOManager
65
  from .utils import Config
66
+ with YOLOManager() as yolo_manager:
67
+ weights_path = f'{current_path}/{Config.YOLO_MODEL_NAME}.pt'
68
 
69
+ yolo_manager.load_model(weights_path)
70
 
71
+ # Run inference
72
+ _, label_path = yolo_manager.annotate_images(image_paths=[image_path], output_dir=IMAGE_LABEL_ROOT, save_image=False, label_path=label_path)
73
 
74
  if os.path.exists(label_path):
75
  with open(label_path, "r") as f:
comic_panel_extractor/yolo_manager.py CHANGED
@@ -228,4 +228,34 @@ class YOLOManager:
228
 
229
  except Exception as e:
230
  print(f"❌ Error processing {image_path}: {str(e)}")
231
- return None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
  except Exception as e:
230
  print(f"❌ Error processing {image_path}: {str(e)}")
231
+ return None, None
232
+
233
+ def __enter__(self):
234
+ # When entering context, just return self
235
+ return self
236
+
237
+ def __del__(self):
238
+ # On exit, unload model and clear cache
239
+ self.unload_model()
240
+
241
+ def __exit__(self, exc_type, exc_value, traceback):
242
+ # On exit, unload model and clear cache
243
+ self.unload_model()
244
+
245
+ def unload_model(self):
246
+ if self.model is not None:
247
+ print("🧹 Unloading YOLO model and clearing CUDA cache...")
248
+ try:
249
+ import torch
250
+ import gc
251
+ del self.model
252
+ self.model = None
253
+ gc.collect()
254
+ if torch.cuda.is_available():
255
+ torch.cuda.empty_cache()
256
+ torch.cuda.ipc_collect()
257
+ print("✅ Model unloaded and GPU cache cleared.")
258
+ except Exception as e:
259
+ print(f"❌ Error unloading model: {e}")
260
+ else:
261
+ print("⚠️ No model loaded to unload.")