From 98a10ca0e76a085c21788a94945136e75b140f5c Mon Sep 17 00:00:00 2001 From: ASHWIN UNNIKRISHNAN Date: Sun, 25 Feb 2024 22:31:30 -0500 Subject: [PATCH 1/3] Update inference.py Adding additional function transform_image.. so that users can use a loaded RGB image and transform it to the expected format of grounding dino prediction. --- groundingdino/util/inference.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/groundingdino/util/inference.py b/groundingdino/util/inference.py index 58528ed..88f9ec0 100644 --- a/groundingdino/util/inference.py +++ b/groundingdino/util/inference.py @@ -37,6 +37,17 @@ def load_model(model_config_path: str, model_checkpoint_path: str, device: str = def load_image(image_path: str) -> Tuple[np.array, torch.Tensor]: + """ + Load an image and apply transformations. + This function takes the path to an image file, loads the image, and applies a series of transformations to it. + The transformations include resizing the image, converting it to a tensor, and normalizing its pixel values. + + Parameters: + image_path (str): The path to the image file. + + Returns: + Tuple[np.array, torch.Tensor]: A tuple containing the original image as a NumPy array and the transformed image as a PyTorch tensor. + """ transform = T.Compose( [ T.RandomResize([800], max_size=1333), @@ -49,6 +60,29 @@ def load_image(image_path: str) -> Tuple[np.array, torch.Tensor]: image_transformed, _ = transform(image_source, None) return image, image_transformed +def transform_image(t: PIL.Image.Image) -> Tuple[np.array, torch.Tensor]: + """ + Transform an RGB image and convert it to a tensor. + + This function takes a PIL Image, applies a series of transformations to it, and returns the original and transformed images. + The transformations include resizing the image, converting it to a tensor, and normalizing its pixel values. + + Parameters: + img (PIL.Image.Image): The input image. + + Returns: + Tuple[np.array, torch.Tensor]: A tuple containing the original image as a NumPy array and the transformed image as a PyTorch tensor. + """ + transform = T.Compose( + [ + T.RandomResize([800], max_size=1333), + T.ToTensor(), + T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), + ] + ) + image = np.asarray(t) + image_transformed, _ = transform(t, None) + return image, image_transformed def predict( model, From da66253d1baf38109018c928bf0ba19571274d42 Mon Sep 17 00:00:00 2001 From: ASHWIN UNNIKRISHNAN Date: Sun, 25 Feb 2024 23:11:49 -0500 Subject: [PATCH 2/3] Update inference.py --- groundingdino/util/inference.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/groundingdino/util/inference.py b/groundingdino/util/inference.py index 88f9ec0..7dd5fb1 100644 --- a/groundingdino/util/inference.py +++ b/groundingdino/util/inference.py @@ -60,7 +60,7 @@ def load_image(image_path: str) -> Tuple[np.array, torch.Tensor]: image_transformed, _ = transform(image_source, None) return image, image_transformed -def transform_image(t: PIL.Image.Image) -> Tuple[np.array, torch.Tensor]: +def transform_image(PIL_image: PIL.Image.Image) -> Tuple[np.array, torch.Tensor]: """ Transform an RGB image and convert it to a tensor. @@ -68,7 +68,7 @@ def transform_image(t: PIL.Image.Image) -> Tuple[np.array, torch.Tensor]: The transformations include resizing the image, converting it to a tensor, and normalizing its pixel values. Parameters: - img (PIL.Image.Image): The input image. + PIL_image (PIL.Image.Image): The input image. Returns: Tuple[np.array, torch.Tensor]: A tuple containing the original image as a NumPy array and the transformed image as a PyTorch tensor. @@ -80,7 +80,7 @@ def transform_image(t: PIL.Image.Image) -> Tuple[np.array, torch.Tensor]: T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ] ) - image = np.asarray(t) + image = np.asarray(PIL_image) image_transformed, _ = transform(t, None) return image, image_transformed From c176fd543bc28f0c7d8875867c29901c2f9133a7 Mon Sep 17 00:00:00 2001 From: ASHWIN UNNIKRISHNAN Date: Sun, 25 Feb 2024 23:14:54 -0500 Subject: [PATCH 3/3] Update inference.py --- groundingdino/util/inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/groundingdino/util/inference.py b/groundingdino/util/inference.py index 7dd5fb1..20353db 100644 --- a/groundingdino/util/inference.py +++ b/groundingdino/util/inference.py @@ -81,7 +81,7 @@ def transform_image(PIL_image: PIL.Image.Image) -> Tuple[np.array, torch.Tensor] ] ) image = np.asarray(PIL_image) - image_transformed, _ = transform(t, None) + image_transformed, _ = transform(PIL_image, None) return image, image_transformed def predict(