From 74d5516867231e9f4aa3b724f5ea9127d8c933de Mon Sep 17 00:00:00 2001 From: maciejmajek Date: Sat, 22 Nov 2025 16:32:16 +0100 Subject: [PATCH] fix: unify use_cuda handling in GDBoxer and GDSegmenter --- .../rai_perception/vision_markup/boxer.py | 15 ++++++++++++--- .../rai_perception/vision_markup/segmenter.py | 8 +++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/rai_extensions/rai_perception/rai_perception/vision_markup/boxer.py b/src/rai_extensions/rai_perception/rai_perception/vision_markup/boxer.py index 949d18a2c..1c35bbc6b 100644 --- a/src/rai_extensions/rai_perception/rai_perception/vision_markup/boxer.py +++ b/src/rai_extensions/rai_perception/rai_perception/vision_markup/boxer.py @@ -13,10 +13,12 @@ # limitations under the License. +import logging from os import PathLike from typing import Dict import cv2 +import torch from cv_bridge import CvBridge from groundingdino.util.inference import Model from rclpy.time import Time @@ -64,14 +66,21 @@ def __init__( weight_path: str | PathLike, use_cuda: bool = True, ): + self.logger = logging.getLogger(__name__) self.cfg_path = __file__.replace( "vision_markup/boxer.py", "configs/gdino_config.py" ) self.weight_path = str(weight_path) - if not use_cuda: - self.model = Model(self.cfg_path, self.weight_path, device="cpu") + if use_cuda: + if torch.cuda.is_available(): + self.device = "cuda" + else: + self.logger.warning("CUDA is not available but requested, using CPU") + self.device = "cpu" else: - self.model = Model(self.cfg_path, self.weight_path) + self.device = "cpu" + + self.model = Model(self.cfg_path, self.weight_path, device=self.device) self.bridge = CvBridge() def get_boxes( diff --git a/src/rai_extensions/rai_perception/rai_perception/vision_markup/segmenter.py b/src/rai_extensions/rai_perception/rai_perception/vision_markup/segmenter.py index 1d51acb5c..8bd0b515b 100644 --- a/src/rai_extensions/rai_perception/rai_perception/vision_markup/segmenter.py +++ b/src/rai_extensions/rai_perception/rai_perception/vision_markup/segmenter.py @@ -13,6 +13,7 @@ # limitations under the License. +import logging from os import PathLike from typing import List @@ -33,13 +34,18 @@ def __init__( weight_path: str | PathLike, use_cuda: bool = True, ): + self.logger = logging.getLogger(__name__) self.cfg_path = "seg_config.yml" hydra.core.global_hydra.GlobalHydra.instance().clear() hydra.initialize_config_module("rai_perception.configs") self.weight_path = str(weight_path) if use_cuda: - self.device = "cuda" if torch.cuda.is_available() else "cpu" + if torch.cuda.is_available(): + self.device = "cuda" + else: + self.logger.warning("CUDA is not available but requested, using CPU") + self.device = "cpu" else: self.device = "cpu" self.sam2_model = build_sam2(