Adding Boundaries to images shown in tensorboard

In the Tensorboard offical websites, they can use tf.summary.image("25 training data examples", images, max_outputs=25, step=0) to plot multiple images with boundaries.

But I don’t find this function in PyTorch SummaryWriter. Instead, there is a function called writer.add_images() that can show a batch of images at a time. However, these images are placed right next to each other. I have attached an example below showing how 16 MNIST images are shown.

image

For MNIST, it’s easy to spot the boundaries, since there is only one object per image.
But I have a dataset with more complicated images, putting them right next to each other causes me a hard time in distinguishing which image is which.

Is it possible to add a boundary between each image?

Hello, I might be a bit late, but you may use torchvision.utils.make_grid() to accomplish this.

Example code:

class TensorBoardHelper:

    def __init__(self, name=None):
        self.writer = SummaryWriter(name)


    def add_image_grid(self, images, tag, global_step):
        images = images.detach()
        img_grid = torchvision.utils.make_grid(images, 3, normalize=True, scale_each=True,
                padding=2, pad_value=1.0, # Pad 2 pixels, with white borders
                )
        self.writer.add_image(tag, img_grid, global_step=global_step)

Output:

https://pytorch.org/docs/stable/torchvision/utils.html#torchvision.utils.make_grid

1 Like