From 93ed52724a956520bb9f5987f23a69f44147564a Mon Sep 17 00:00:00 2001 From: Veli Eroglu Date: Mon, 14 Oct 2024 14:59:43 +0300 Subject: [PATCH 1/3] add __init__.py to models --- lang_sam/models/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lang_sam/models/__init__.py diff --git a/lang_sam/models/__init__.py b/lang_sam/models/__init__.py new file mode 100644 index 0000000..e69de29 From 25ce1c273a975501d63d394907238d426505acb7 Mon Sep 17 00:00:00 2001 From: Veli Eroglu Date: Mon, 14 Oct 2024 15:00:15 +0300 Subject: [PATCH 2/3] add mps support to torch device & refactor device_type detection. --- lang_sam/models/gdino.py | 9 +++++++-- lang_sam/models/utils.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 lang_sam/models/utils.py diff --git a/lang_sam/models/gdino.py b/lang_sam/models/gdino.py index e9b6afe..4522a90 100644 --- a/lang_sam/models/gdino.py +++ b/lang_sam/models/gdino.py @@ -2,8 +2,11 @@ import torch from PIL import Image from transformers import AutoModelForZeroShotObjectDetection, AutoProcessor +from lang_sam.models.utils import get_device_type + +device_type = get_device_type() +DEVICE = torch.device(device_type) -DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") if torch.cuda.is_available(): torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__() if torch.cuda.get_device_properties(0).major >= 8: @@ -18,7 +21,9 @@ def __init__(self): def build_model(self, ckpt_path: str | None = None): model_id = "IDEA-Research/grounding-dino-base" self.processor = AutoProcessor.from_pretrained(model_id) - self.model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(DEVICE) + self.model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to( + DEVICE + ) def predict( self, diff --git a/lang_sam/models/utils.py b/lang_sam/models/utils.py new file mode 100644 index 0000000..bb4f003 --- /dev/null +++ b/lang_sam/models/utils.py @@ -0,0 +1,12 @@ +import logging +import torch + + +def get_device_type() -> str: + if torch.backends.mps.is_available(): + return "mps" + elif torch.cuda.is_available(): + return "cuda" + else: + logging.warning("No GPU found, using CPU instead") + return "cpu" From 3b5d4531b232572dc46f0fbe2c5f449f869b780d Mon Sep 17 00:00:00 2001 From: Veli Eroglu Date: Mon, 14 Oct 2024 15:47:36 +0300 Subject: [PATCH 3/3] add missing get_device_type call into sam.py --- lang_sam/models/sam.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lang_sam/models/sam.py b/lang_sam/models/sam.py index e013b4e..220352e 100644 --- a/lang_sam/models/sam.py +++ b/lang_sam/models/sam.py @@ -5,8 +5,10 @@ from omegaconf import OmegaConf from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator from sam2.sam2_image_predictor import SAM2ImagePredictor +from lang_sam.models.utils import get_device_type + +DEVICE = torch.device(get_device_type()) -DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") if torch.cuda.is_available(): torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__() if torch.cuda.get_device_properties(0).major >= 8: