Adding Boundaries to images shown in tensorboard

username: kinwai_cheuk
cooked: <p>In the Tensorboard offical websites, they can use <code>tf.summary.image("25 training data examples", images, max_outputs=25, step=0)</code> to plot multiple images with boundaries.</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://www.tensorflow.org/tensorboard/image_summaries#visualizing_multiple_images">
  <header class="source">
      <img src="https://www.gstatic.com/devrel-devsite/prod/vb47a36f3a983ed748bf281529457db47955fe57e2b5ea15e7e9641c5e7b5032e/tensorflow/images/favicon.png" class="site-icon" width="32" height="32">

      <a href="https://www.tensorflow.org/tensorboard/image_summaries#visualizing_multiple_images" target="_blank" rel="noopener nofollow ugc">TensorFlow</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/387;"><img src="https://www.tensorflow.org/static/site-assets/images/project-logos/tensorboard-logo-social.png" class="thumbnail" width="690" height="388"></div>

<h3><a href="https://www.tensorflow.org/tensorboard/image_summaries#visualizing_multiple_images" target="_blank" rel="noopener nofollow ugc">Displaying image data in TensorBoard &nbsp;|&nbsp; TensorFlow</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>But I don’t find this function in PyTorch SummaryWriter. Instead, there is a function called <code>writer.add_images()</code> 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.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/1/c180425c3cb32f1565879a865f6370015b2c827e.png" alt="image" data-base62-sha1="rBMWE4mLZZtyNvOU1Q5e98XBeLs" width="337" height="141"></p>
<p>For MNIST, it’s easy to spot the boundaries, since there is only one object per image.<br>
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.</p>
<p>Is it possible to add a boundary between each image?</p>

username: ssgosh
cooked: <p>Hello, I might be a bit late, but you may use <code>torchvision.utils.make_grid()</code> to accomplish this.</p>
<p>Example code:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>Output:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/2/622e8a131e6831a3d91154a3064037ef8e801b79.png" data-download-href="https://discuss.pytorch.org/uploads/default/622e8a131e6831a3d91154a3064037ef8e801b79" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/2/622e8a131e6831a3d91154a3064037ef8e801b79.png" alt="image" data-base62-sha1="e0yv8uQm1omKyBzjFeGkU5dFZeV" width="381" height="500" data-dominant-color="AFAFAE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">403×528 17.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><a href="https://pytorch.org/docs/stable/torchvision/utils.html#torchvision.utils.make_grid" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/torchvision/utils.html#torchvision.utils.make_grid</a></p>