From c80235e05d869876e868128a56f3e38b3efc30c7 Mon Sep 17 00:00:00 2001 From: Hamzawi <98920963+halqadasi@users.noreply.github.com> Date: Fri, 29 Mar 2024 11:10:14 +0100 Subject: [PATCH] Update mmdetection.py The primary issue is the inefficiency of repeatedly loading a large model into memory for each new instance of the `MMDetection` class. This process occurred every time an image required labeling, which led to rapidly exhausting available memory resources. Solution: The solution introduced a class-level attribute within the `MMDetection` class to store and share a single model instance across all instances of the class, ensuring the model loads only once. This approach dramatically reduced memory consumption and prevented out-of-memory errors by eliminating redundant model loading and leveraging model instance reuse. --- label_anything/sam/mmdetection.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/label_anything/sam/mmdetection.py b/label_anything/sam/mmdetection.py index 0c30271..74a13ba 100644 --- a/label_anything/sam/mmdetection.py +++ b/label_anything/sam/mmdetection.py @@ -62,6 +62,7 @@ def load_my_model( class MMDetection(LabelStudioMLBase): """Object detector based on https://github.com/open-mmlab/mmdetection.""" + _predictor = None def __init__(self, model_name="sam_hq", config_file=None, @@ -78,10 +79,13 @@ def __init__(self, **kwargs): super(MMDetection, self).__init__(**kwargs) - - PREDICTOR = load_my_model( - model_name, device, sam_config, sam_checkpoint_file) - self.PREDICTOR = PREDICTOR + + # Only load the model if it hasn't been loaded before + if MMDetection._predictor is None: + MMDetection._predictor = load_my_model( + model_name, device, sam_config, sam_checkpoint_file) + + self.PREDICTOR = MMDetection._predictor self.out_mask = out_mask self.out_bbox = out_bbox