Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Visual prompter (#2356)

Edgar Riba75b4963d853f
diff --git a/docs/source/applications/visual_prompting.rst b/docs/source/applications/visual_prompting.rst
new file mode 100644
index 000000000..5864d6a71
--- /dev/null
+++ b/docs/source/applications/visual_prompting.rst
@@ -0,0 +1,62 @@
+Visual Prompting
+================
+
+.. image:: https://kornia-tutorials.readthedocs.io/en/latest/_images/c5ec618b63c6118f00e6f29377cc2b50a1e1df2d247657eec531dfc5454272c7.png
+ :width: 20%
+
+Visual Prompting is the task of streamlining computer vision processes by harnessing the power of prompts,
+inspired by the breakthroughs of text prompting in NLP. This innovative approach involves using a few visual
+prompts to swiftly convert an unlabeled dataset into a deployed model, significantly reducing development time
+for both individual projects and enterprise solutions.
+
+By leveraging large pre-trained vision transformers, Visual Prompting not only eliminates the need for extensive
+data labeling but also facilitates the "teaching" of smaller AI systems.
+
+
+How Kornia leverages Visual Prompting ?
+---------------------------------------
+
+Kornia leverages the Visual Prompting task through the :code:`VisualPrompter`` API, which integrates powerful models like
+the Segment Anything Model (SAM) into its computer vision toolkit. By incorporating SAM and the VisualPrompter API,
+developers can harness the efficiency of Visual Prompting for faster segmentation tasks and improved computer vision workflows. This seamless integration allows users to utilize pre-trained vision transformers, significantly reducing manual data labeling efforts and enabling the "teaching" of smaller AI systems. As a result, Kornia users can take advantage of the versatility and adaptability offered by Visual Prompting, unlocking new possibilities for various computer vision applications.
+
+
+Kornia provides a couple of backbones based on `transformers <https://paperswithcode.com/methods/category/vision-transformer>`_
+to perform image classification. Checkout the following apis :py:class:`~kornia.contrib.VisionTransformer`,
+:py:class:`~kornia.contrib.ClassificationHead` and combine as follows to customize your own classifier:
+
+
+How to use with Kornia
+----------------------
+
+.. code-block:: python
+
+ from kornia.io import load_image, ImageLoadType
+ from kornia.contrib.visual_prompter import VisualPrompter
+
+ # load an image
+ image = load_image('./example.jpg', ImageLoadType.RGB32, device)
+
+ # Load the prompter
+ prompter = VisualPrompter()
+
+ # set the image: This will preprocess the image and already generate the embeddings of it
+ prompter.set_image(image)
+
+ # Generate the prompts
+ keypoints = Keypoints(torch.tensor([[[500, 375]]], device=device, dtype=torch.float32)) # BxNx2
+
+ # For the keypoints label: 1 indicates a foreground point; 0 indicates a background point
+ keypoints_labels = torch.tensor([[1]], device=device) # BxN
+
+ # Runs the prediction with the kypoints prompts
+ prediction = prompter.predict(
+ keypoints=keypoints,
+ keypoints_labels=keypoints_labels,
+ multimask_output=True,
+ )
+
+You also can go through or full tutorial using Colab found `here <https://kornia-tutorials.readthedocs.io/en/latest/_nbs/image_prompter.html>`_.
+
+
+Integration with other libraries, fineturning and more examples soon.
diff --git a/docs/source/contrib.rst b/docs/source/contrib.rst
index 3ac959fd9..12ed712ff 100644
--- a/docs/source/contrib.rst
+++ b/docs/source/contrib.rst
@@ -23,10 +23,10 @@ Structures
:members:
:undoc-members:
-ImagePrompter
--------------
+VisualPrompter
+--------------
-.. autoclass:: kornia.contrib.image_prompter.ImagePrompter
+.. autoclass:: kornia.contrib.visual_prompter.VisualPrompter
:members: set_image, reset_image, compile, predict, preprocess_image, preprocess_prompts
Edge Detection
diff --git a/docs/source/index.rst b/docs/source/index.rst
index 570fbd16a..380f4a701 100644
--- a/docs/source/index.rst
+++ b/docs/source/index.rst
@@ -75,6 +75,7 @@ Join the community
:hidden:
applications/intro
+ applications/visual_prompting
applications/face_detection
applications/image_augmentations
applications/image_classification
@@ -88,6 +89,7 @@ Join the community
:caption: KORNIA MODELS
:hidden:
+ models/segment_anything
models/yunet
models/vit
models/vit_mobile
@@ -97,7 +99,6 @@ Join the community
models/affnet
models/sold2
models/dexined
- models/segment_anything
.. toctree::
:caption: SUPPORT
diff --git a/docs/source/models/segment_anything.rst b/docs/source/models/segment_anything.rst
index 282b459af..fa4748174 100644
--- a/docs/source/models/segment_anything.rst
+++ b/docs/source/models/segment_anything.rst
@@ -9,7 +9,7 @@ can be used to generate masks for all objects in an image.
**Segment Anything**
^^^
- **Abstract:** We introduce the Segment Anything (SA) project: a new task, model, and dataset for image
+ **Abstract:** We introduce the Segment Anything (SAM) project: a new task, model, and dataset for image
segmentation. Using our efficient model in a data collection loop, we built the largest segmentation
dataset to date (by far), with over 1 billion masks on 11M licensed and privacy respecting images. The
model is designed and trained to be promptable, so it can transfer zero-shot to new image distributions
@@ -33,26 +33,26 @@ can be used to generate masks for all objects in an image.
How to use SAM from Kornia
--------------------------
The Kornia API for SAM try to provide a simple API to access initialize the model and load/download the weights. Also,
-providing it to a high-level API called :code:`ImagePrompter`, which allow the users to set an image and run multiple
+providing it to a high-level API called :code:`VisualPrompter`, which allow the users to set an image and run multiple
queries multiple times.
-The :code:`ImagePrompter` works querying on a single image, if you want to explore and query into a batch of images,
+The :code:`VisualPrompter` works querying on a single image, if you want to explore and query into a batch of images,
you can use the :code:`Sam` directly. But, for it you will need to write the boilerplate to preprocess and postprocess to
-use it. This boilerplate, is already handle on the high-level API :code:`ImagePrompter`.
+use it. This boilerplate, is already handle on the high-level API :code:`VisualPrompter`.
Image Prompter
^^^^^^^^^^^^^^
.. _anchor Prompter:
-The High level API :code:`ImagePrompter` handle with the image and prompt transformation, preprocessing and prediction for
+The High level API :code:`VisualPrompter` handle with the image and prompt transformation, preprocessing and prediction for
a given SAM model.
-About the :code:`ImagePrompter`:
+About the :code:`VisualPrompter`:
#. From a `ModelConfig` loads the desired model with the desired checkpoint to be used as the model to receive the query
prompts. For know we just support Segment Anything model, where the *SAM-h* is the default option.
-#. Based on the model, the :code:`ImagePrompter` will handle with the necessary transformations to be done into the image
+#. Based on the model, the :code:`VisualPrompter` will handle with the necessary transformations to be done into the image
and prompts before apply it to the model. These transformations are done using PyTorch backed, by our API of
augmentations. Where we use the :class:`kornia.geometry.augmentation.AugmentationSequential` to handle with the different
data formats (keypoints, boxes, masks, image).
@@ -80,9 +80,9 @@ About the :code:`ImagePrompter`:
--------------
-Example of using the :code:`ImagePrompter`:
+Example of using the :code:`VisualPrompter`:
-Exploring how to simple initialize the :code:`ImagePrompter`, automatically load the weights from a URL,
+Exploring how to simple initialize the :code:`VisualPrompter`, automatically load the weights from a URL,
read the image and set it to be query, how to write the prompts, and the multiple ways we can use these prompts
to query the image masks from the SAM model.
@@ -92,7 +92,7 @@ to query the image masks from the SAM model.
import torch
from kornia.contrib.models.sam import SamConfig
- from kornia.contrib.image_prompter import ImagePrompter
+ from kornia.contrib.visual_prompter import VisualPrompter
from kornia.io import load_image, ImageLoadType
from kornia.geometry.keypoints import Keypoints
from kornia.geometry.boxes import Boxes
@@ -109,7 +109,7 @@ to query the image masks from the SAM model.
config = SamConfig(model_type, checkpoint)
# Load the prompter
- prompter = ImagePrompter(config, device=device)
+ prompter = VisualPrompter(config, device=device)
# You can use torch dynamo/compile API with:
# prompter.compile()
diff --git a/kornia/contrib/image_prompter.py b/kornia/contrib/image_prompter.py
index 487287367..bfd61780c 100644
--- a/kornia/contrib/image_prompter.py
+++ b/kornia/contrib/image_prompter.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import warnings
from typing import Any
import torch
@@ -59,6 +60,10 @@ class ImagePrompter:
dtype: torch.dtype | None = None,
) -> None:
super().__init__()
+ warnings.warn(
+ "ImagePrompter is deprecated in favour of VisualPrompter and will be removed after v0.6.12.",
+ category=DeprecationWarning,
+ )
if isinstance(config, SamConfig):
self.model = Sam.from_config(config)
transforms = (LongestMaxSize(self.model.image_encoder.img_size, p=1.0),)
diff --git a/kornia/contrib/visual_prompter.py b/kornia/contrib/visual_prompter.py
new file mode 100644
index 000000000..caa83adcf
--- /dev/null
+++ b/kornia/contrib/visual_prompter.py
@@ -0,0 +1,376 @@
+from __future__ import annotations
+
+from typing import Any
+
+import torch
+
+from kornia.augmentation import LongestMaxSize
+from kornia.augmentation.container.augment import AugmentationSequential
+from kornia.contrib.models import Prompts, SegmentationResults
+from kornia.contrib.models.sam import Sam, SamConfig
+from kornia.core import Tensor, pad, tensor
+from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPE
+from kornia.enhance import normalize
+from kornia.geometry.boxes import Boxes
+from kornia.geometry.keypoints import Keypoints
+
+
+class VisualPrompter:
+ """This class allow the user to run multiple query with multiple prompts for a model.
+
+ At the moment, we just support the SAM model. The model is loaded based on the given config.
+
+ For default the images are transformed to have their long side with size of the `image_encoder.img_size`. This
+ Prompter class ensure to transform the images and the prompts before prediction. Also, the image is passed
+ automatically for the method `preprocess_image`, which is responsible for normalize the image and pad it to have
+ the right size for the SAM model :math:`(\text{image_encoder.img_size}, \text{image_encoder.img_size})`. For
+ default the image is normalized by the mean and standard deviation of the SAM dataset values.
+
+ Args:
+ config: A model config to generate the model. Now just the SAM model is supported.
+ device: The desired device to use the model.
+ dtype: The desired dtype to use the model.
+
+ Example:
+ >>> # prompter = VisualPrompter() # Will load the vit h for default
+ >>> # You can load a custom SAM type for modifying the config
+ >>> prompter = VisualPrompter(SamConfig('vit_b'))
+ >>> image = torch.rand(3, 25, 30)
+ >>> prompter.set_image(image)
+ >>> boxes = Boxes(
+ ... torch.tensor(
+ ... [[[[0, 0], [0, 10], [10, 0], [10, 10]]]],
+ ... device=prompter.device,
+ ... dtype=torch.float32
+ ... ),
+ ... mode='xyxy'
+ ... )
+ >>> prediction = prompter.predict(boxes=boxes)
+ >>> prediction.logits.shape
+ torch.Size([1, 3, 256, 256])
+ """
+
+ def __init__(
+ self,
+ config: SamConfig = SamConfig(
+ model_type='vit_h', checkpoint='https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth'
+ ),
+ device: torch.device | None = None,
+ dtype: torch.dtype | None = None,
+ ) -> None:
+ super().__init__()
+ if isinstance(config, SamConfig):
+ self.model = Sam.from_config(config)
+ transforms = (LongestMaxSize(self.model.image_encoder.img_size, p=1.0),)
+ self.pixel_mean: Tensor | None = tensor([123.675, 116.28, 103.53], device=device, dtype=dtype) / 255
+ self.pixel_std: Tensor | None = tensor([58.395, 57.12, 57.375], device=device, dtype=dtype) / 255
+ else:
+ raise NotImplementedError
+
+ self.model = self.model.to(device=device, dtype=dtype)
+ self.transforms = AugmentationSequential(*transforms, same_on_batch=True)
+
+ self.device = device
+ self.dtype = dtype
+ self._original_image_size: None | tuple[int, int] = None
+ self._input_image_size: None | tuple[int, int] = None
+ self._input_encoder_size: None | tuple[int, int] = None
+ self.reset_image()
+
+ def preprocess_image(self, x: Tensor, mean: Tensor | None = None, std: Tensor | None = None) -> Tensor:
+ """Normalize and pad a tensor.
+
+ For normalize the tensor: will priorize the `mean` and `std` passed as argument, if None will use the default
+ Sam Dataset values.
+
+ For pad the tensor: Will pad the tensor into the right and bottom to match with the size of
+ `self.model.image_encoder.img_size`
+
+ Args:
+ x: The image to be preprocessed
+ mean: Mean for each channel.
+ std: Standard deviations for each channel.
+
+ Returns:
+ The image preprocessed (normalized if has mean and str available and padded to encoder size)
+ """
+
+ if isinstance(mean, Tensor) and isinstance(std, Tensor):
+ x = normalize(x, mean, std)
+ elif isinstance(self.pixel_mean, Tensor) and isinstance(self.pixel_std, Tensor):
+ x = normalize(x, self.pixel_mean, self.pixel_std)
+
+ encoder_im_size = self.model.image_encoder.img_size
+ pad_h = encoder_im_size - x.shape[-2]
+ pad_w = encoder_im_size - x.shape[-1]
+ x = pad(x, (0, pad_w, 0, pad_h))
+
+ return x
+
+ @torch.no_grad()
+ def set_image(self, image: Tensor, mean: Tensor | None = None, std: Tensor | None = None) -> None:
+ """Set the embeddings from the given image with `image_decoder` of the model.
+
+ Prepare the given image with the selected transforms and the preprocess method.
+
+ Args:
+ image: RGB image. Normally images with range of [0-1], the model preprocess normalize the
+ pixel values with the mean and std defined in its initialization. Expected to be into a float32
+ dtype. Shape :math:`(3, H, W)`.
+ """
+ KORNIA_CHECK_SHAPE(image, ['3', 'H', 'W'])
+
+ self.reset_image()
+
+ self._original_image_size = (image.shape[-2], image.shape[-1])
+
+ image = self.transforms(image, data_keys=['input'])
+ self._tfs_params = self.transforms._params
+ self._input_image_size = (image.shape[-2], image.shape[-1])
+
+ image = self.preprocess_image(image, mean, std)
+
+ self._input_encoder_size = (image.shape[-2], image.shape[-1])
+
+ self.image_embeddings = self.model.image_encoder(image)
+ self.is_image_set = True
+
+ def _valid_keypoints(self, keypoints: Keypoints | Tensor, labels: Tensor) -> Keypoints:
+ """Validate the keypoints shape and ensure to be a Keypoints."""
+ KORNIA_CHECK_SHAPE(keypoints.data, ['K', 'N', '2'])
+ KORNIA_CHECK_SHAPE(labels.data, ['K', 'N'])
+ KORNIA_CHECK(keypoints.shape[0] == labels.shape[0], 'The keypoints and labels should have the same batch size')
+
+ if isinstance(keypoints, Tensor):
+ keypoints = Keypoints.from_tensor(keypoints)
+
+ return keypoints
+
+ def _valid_boxes(self, boxes: Boxes | Tensor) -> Boxes:
+ """Validate the boxes shape and ensure to be a Boxes into xyxy mode."""
+ if isinstance(boxes, Tensor):
+ KORNIA_CHECK_SHAPE(boxes.data, ['K', '4'])
+ boxes = Boxes(boxes, mode='xyxy')
+
+ if boxes.mode == 'xyxy':
+ boxes_xyxy = boxes
+ else:
+ boxes_xyxy = Boxes(boxes.to_tensor(mode='xyxy'), mode='xyxy')
+
+ return boxes_xyxy
+
+ def _valid_masks(self, masks: Tensor) -> Tensor:
+ """Validate the input masks shape."""
+ KORNIA_CHECK_SHAPE(masks, ['K', '1', '256', '256'])
+ return masks
+
+ def _transform_prompts(
+ self, *prompts: Tensor | Boxes | Keypoints, data_keys: list[str] = []
+ ) -> dict[str, Tensor | Boxes | Keypoints]:
+ transformed_prompts = self.transforms(*prompts, data_keys=data_keys, params=self._tfs_params)
+ return {key: transformed_prompts[idx] for idx, key in enumerate(data_keys)}
+
+ def preprocess_prompts(
+ self,
+ keypoints: Keypoints | Tensor | None = None,
+ keypoints_labels: Tensor | None = None,
+ boxes: Boxes | Tensor | None = None,
+ masks: Tensor | None = None,
+ ) -> Prompts:
+ """Validate and preprocess the given prompts to be aligned with the input image."""
+ data_keys = []
+ to_transform: list[Keypoints | Boxes | Tensor] = []
+
+ if isinstance(keypoints, (Keypoints, Tensor)) and isinstance(keypoints_labels, Tensor):
+ keypoints = self._valid_keypoints(keypoints, keypoints_labels)
+ data_keys.append('keypoints')
+ to_transform.append(keypoints)
+
+ if isinstance(boxes, (Boxes, Tensor)):
+ self._valid_boxes(boxes)
+ data_keys.append('bbox_xyxy')
+ to_transform.append(boxes)
+
+ if isinstance(masks, Tensor):
+ self._valid_masks(masks)
+
+ data = self._transform_prompts(*to_transform, data_keys=data_keys)
+
+ if 'keypoints' in data and isinstance(data['keypoints'], Keypoints):
+ kpts_tensor = data['keypoints'].to_tensor()
+ if KORNIA_CHECK_IS_TENSOR(kpts_tensor) and KORNIA_CHECK_IS_TENSOR(keypoints_labels):
+ points = (kpts_tensor[None, ...], keypoints_labels)
+ else:
+ points = None
+
+ if 'bbox_xyxy' in data and isinstance(data['bbox_xyxy'], Boxes):
+ _bbox = data['bbox_xyxy'].to_tensor(mode='xyxy')
+ if KORNIA_CHECK_IS_TENSOR(_bbox):
+ bbox = _bbox
+ else:
+ bbox = None
+
+ return Prompts(points=points, boxes=bbox, masks=masks)
+
+ @torch.no_grad()
+ def predict(
+ self,
+ keypoints: Keypoints | Tensor | None = None,
+ keypoints_labels: Tensor | None = None,
+ boxes: Boxes | Tensor | None = None,
+ masks: Tensor | None = None,
+ multimask_output: bool = True,
+ output_original_size: bool = True,
+ ) -> SegmentationResults:
+ """Predict masks for the given image based on the input prompts.
+
+ Args:
+ keypoints: Point prompts to the model. Each point is in (X,Y) in pixels. Shape :math:`(K, N, 2)`. Where
+ `N` is the number of points and `K` the number of prompts.
+ keypoint_labels: Labels for the point prompts. 1 indicates a foreground point and 0 indicates a background
+ point. Shape :math:`(K, N)`. Where `N` is the number of points, and `K` the number of
+ prompts.
+ boxes: A box prompt to the model. If a tensor, should be in a xyxy mode. Shape :math:`(K, 4)`
+ masks: A low resolution mask input to the model, typically coming from a previous prediction
+ iteration. Has shape :math:`(K, 1, H, W)`, where for SAM, H=W=256.
+ multimask_output: If true, the model will return three masks. For ambiguous input prompts (such as a
+ single click), this will often produce better masks than a single prediction. If only
+ a single mask is needed, the model's predicted quality score can be used to select the
+ best mask. For non-ambiguous prompts, such as multiple input prompts,
+ multimask_output=False can give better results.
+ output_original_size: If true, the logits of `SegmentationResults` will be post-process to match the
+ original input image size.
+ Returns:
+ A prediction with the logits and scores (IoU of each predicted mask)
+ """
+
+ KORNIA_CHECK(self.is_image_set, 'An image must be set with `self.set_image(...)` before `predict` be called!')
+
+ prompts = self.preprocess_prompts(keypoints, keypoints_labels, boxes, masks)
+
+ # Embed prompts
+ sparse_embeddings, dense_embeddings = self.model.prompt_encoder(
+ points=prompts.points, boxes=prompts.boxes, masks=prompts.masks
+ )
+ del prompts
+
+ # Predict masks
+ logits, scores = self.model.mask_decoder(
+ image_embeddings=self.image_embeddings,
+ image_pe=self.model.prompt_encoder.get_dense_pe(),
+ sparse_prompt_embeddings=sparse_embeddings,
+ dense_prompt_embeddings=dense_embeddings,
+ multimask_output=multimask_output,
+ )
+ results = SegmentationResults(logits, scores)
+ if (
+ output_original_size
+ and isinstance(self._input_image_size, tuple)
+ and isinstance(self._original_image_size, tuple)
+ ):
+ results.original_res_logits(self._input_image_size, self._original_image_size, self._input_encoder_size)
+
+ # results = results.squeeze(0)
+
+ return results
+
+ def reset_image(self) -> None:
+ self._tfs_params = None
+ self._original_image_size = None
+ self._input_image_size = None
+ self._input_encoder_size = None
+
+ if hasattr(self, 'image_embeddings'):
+ del self.image_embeddings
+
+ self.image_embeddings = None
+ self.is_image_set = False
+
+ def compile(
+ self,
+ *,
+ fullgraph: bool = False,
+ dynamic: bool = False,
+ backend: str = 'inductor',
+ mode: str | None = None,
+ options: dict[Any, Any] = {},
+ disable: bool = False,
+ ) -> None:
+ """Applies `torch.compile(...)`/dynamo API into the VisualPrompter API.
+
+ .. note:: For more information about the dynamo API check the official docs
+ https://pytorch.org/docs/stable/generated/torch.compile.html
+
+ Args:
+ fullgraph: Whether it is ok to break model into several subgraphs
+ dynamic: Use dynamic shape tracing
+ backend: backend to be used
+ mode: Can be either “default”, “reduce-overhead” or “max-autotune”
+ options: A dictionary of options to pass to the backend.
+ disable: Turn torch.compile() into a no-op for testing
+
+ Example:
+ >>> # prompter = VisualPrompter()
+ >>> # prompter.compile() # You should have torch >= 2.0.0 installed
+ >>> # Use the prompter methods ...
+ """
+
+ # self.set_image = torch.compile( # type: ignore[method-assign]
+ # self.set_image,
+ # fullgraph=fullgraph,
+ # dynamic=dynamic,
+ # backend=backend,
+ # mode=mode,
+ # options=options,
+ # disable=disable,
+ # )
+ # FIXME: compile set image will try to compile AugmentationSequential which fails
+ self.model.image_encoder = torch.compile( # type: ignore
+ self.model.image_encoder,
+ fullgraph=fullgraph,
+ dynamic=dynamic,
+ backend=backend,
+ mode=mode,
+ options=options,
+ disable=disable,
+ )
+
+ # self.preprocess_image = torch.compile( # type: ignore[method-assign]
+ # self.preprocess_image,
+ # fullgraph=fullgraph,
+ # dynamic=dynamic,
+ # backend=backend,
+ # mode=mode,
+ # options=options,
+ # disable=disable,
+ # )
+
+ # FIXME: compile predict will try to compile Preproc prompts, which need to compileAugmentationSequential
+ # which fails
+ # self.predict = torch.compile( # type: ignore[method-assign]
+ # self.predict,
+ # fullgraph=fullgraph,
+ # dynamic=dynamic,
+ # backend=backend,
+ # mode=mode,
+ # options=options,
+ # disable=disable,
+ # )
+ self.model.mask_decoder = torch.compile( # type: ignore
+ self.model.mask_decoder,
+ fullgraph=fullgraph,
+ dynamic=dynamic,
+ backend=backend,
+ mode=mode,
+ options=options,
+ disable=disable,
+ )
+ self.model.prompt_encoder = torch.compile( # type: ignore
+ self.model.prompt_encoder,
+ fullgraph=fullgraph,
+ dynamic=dynamic,
+ backend=backend,
+ mode=mode,
+ options=options,
+ disable=disable,
+ )