TorchViewer

class ibb.widgets.TorchViewer(**kwargs: Any)[source]

This widget can visualize a PyTorch dataset as bounding boxes drawn on top of the images.
Its arguments work a lot like ibb.BramboxViewer.

Parameters
  • data (torch.utils.data.Dataset) – PyTorch dataset that should return images and bounding boxes (see Note).

  • extract_data (calable) – Extract image and dataframe from dataset output; Default (First Tensor and DataFrame in Sequence/Mapping)

  • label (pandas.Series or callable) – Label to write above the boxes; Default class_label (confidence)

  • color (pandas.Series or callable) – Color to use for drawing; Default every class_label will get its own color, up to 10 labels

  • size (pandas.Series or callable) – Thickness of the border of the bounding boxes; Default 3

  • alpha (pandas.Series or callable) – Alpha fill value of the bounding boxes; Default 00

  • **kwargs (dict) – Extra keyword arguments that will be passed to Viewer

Note

The label, color, size and alpha arguments can also be tacked on to the boxes dataframe as columns.

They can also be a single value, which will then be used for each bounding box.
Basically, as long as you can assign the value as a new column to the dataframe, it will work.

Finally, if these values are callable, they get called with the boxes dataframe and should return a valid pandas series.

Examples

Warning

Note that the widgets are not responding as we do not have a python backend running!

[1]:
import brambox as bb
import torch
import torchvision
from PIL import Image
import ibb
[2]:
class Dataset:
    """ Example dataset for demo. """
    def __init__(self, box_file):
        self.boxes = bb.io.load('pandas', box_file)

    def __len__(self):
        return len(self.boxes['image'].cat.categories)

    def __getitem__(self, index):
        image_name = self.boxes['image'].cat.categories[index]
        image_path = f'images/{image_name}.jpg'

        image = torchvision.transforms.ToTensor()(Image.open(image_path).convert('RGB'))
        boxes = bb.util.select_images(self.boxes, [image_name])

        return image, boxes
[3]:
v = ibb.TorchViewer(Dataset('annotations.h5'))
v
[3]:
[4]:
v = ibb.TorchViewer(Dataset('detections.h5'))
v
[4]: