Tensorboard SummaryWriter not writing images correctly

import-antigravity: <p>I know TB is not made by pytorch but I think the issue probably has to do with the Pytorch SummaryWriter side of things.</p>
<p>I’ve been trying to train a CIFAR-10 DCGAN and pulling my hair out for a week because the images always come out like this:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/5/05ddb75ae131c8985270d7cf8d4ef18708ccfeab.png" alt="Screen Shot 2021-03-18 at 3.36.45 PM" data-base62-sha1="PTpyrhlj2OZvuN7stJUqZU3HIf" width="368" height="432"></p>
<p>Observe the extreme clipping/saturation.</p>
<p>Now, today I decided to write some training data to tensorboard as well and I got this:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/8/88edb4f27b75ec6a86ca0ed0f52bcb4d46d75d7b.png" data-download-href="https://discuss.pytorch.org/uploads/default/88edb4f27b75ec6a86ca0ed0f52bcb4d46d75d7b" title="Screen Shot 2021-03-18 at 3.37.26 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/8/88edb4f27b75ec6a86ca0ed0f52bcb4d46d75d7b_2_690x398.png" alt="Screen Shot 2021-03-18 at 3.37.26 PM" data-base62-sha1="jxkdFCt8pzJP2ReencdgNXCdcqD" width="690" height="398" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/8/88edb4f27b75ec6a86ca0ed0f52bcb4d46d75d7b_2_690x398.png, https://discuss.pytorch.org/uploads/default/original/3X/8/8/88edb4f27b75ec6a86ca0ed0f52bcb4d46d75d7b.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/8/88edb4f27b75ec6a86ca0ed0f52bcb4d46d75d7b.png 2x" data-dominant-color="838283"><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">Screen Shot 2021-03-18 at 3.37.26 PM</span><span class="informations">765×442 180 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>Here is the training data observed with <code>plt.imshow</code> in a Jupyter notebook, which confirms that my data is not loaded incorrectly.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/b/0b595e7e148bf29892555a5285f6cef2a1023a4e.png" data-download-href="https://discuss.pytorch.org/uploads/default/0b595e7e148bf29892555a5285f6cef2a1023a4e" title="Screen Shot 2021-03-18 at 3.36.13 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/b/0b595e7e148bf29892555a5285f6cef2a1023a4e_2_516x500.png" alt="Screen Shot 2021-03-18 at 3.36.13 PM" data-base62-sha1="1CoIVpxMLWYthCawvslHSHxAvOC" width="516" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/b/0b595e7e148bf29892555a5285f6cef2a1023a4e_2_516x500.png, https://discuss.pytorch.org/uploads/default/original/3X/0/b/0b595e7e148bf29892555a5285f6cef2a1023a4e.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/b/0b595e7e148bf29892555a5285f6cef2a1023a4e.png 2x" data-dominant-color="969791"><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">Screen Shot 2021-03-18 at 3.36.13 PM</span><span class="informations">575×557 362 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>I’m completely stumped. Grayscale images (MNIST) work fine on TB. Does anyone have any insight? This is my code to write the images to tensorboard (I’m using pytorch lightning but that shouldn’t matter):</p>
<pre><code class="lang-auto">from torchvision.utils import make_grid
img = pl_module.sample_G(self._n * self._n)

train_batch = next(iter(pl_module.train_dataloader()))[:(self._n * self._n)]

# Concatenate images
grid = make_grid(img, self._n)
t_grid = make_grid(train_batch, self._n)

if pl_module.logger is not None:
    pl_module.logger.experiment.add_image('train', t_grid, pl_module.global_step, dataformats='CHW')
    pl_module.logger.experiment.add_image('generated', grid, pl_module.global_step, dataformats='CHW')
</code></pre>

import-antigravity: <p>Update: fixed it!</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/8/b813b8a9c3fb5b2286f3f33225690ec563b8932a.png" alt="image" data-base62-sha1="qgq6EC20CaOzirkpCN6v17IJeA2" width="386" height="408"></p>
<p>The problem is that the images were between -1 and 1, and <code>torch.utils.tensorboard.summary.image()</code> only accepts floating point images in [0, 1] or integer images in [0, 255]. I’m going to go ahead and open an issue on github because the code should be changed or the docs should be changed to make this more obvious.</p>