Tensorboard image quality lower than matplotlib?

username: aksg87
cooked: <p>New to tensorboard and was testing out some basic functionality.</p>
<p>Displaying the image in matplotlib shows the image expected</p>
<pre data-code-wrap="python"><code class="lang-python">print(torch.__version__)
1.2.0
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/2/126db61781bdcdb8b32ebf85de407017b8624ea4.png" data-download-href="https://discuss.pytorch.org/uploads/default/126db61781bdcdb8b32ebf85de407017b8624ea4" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/126db61781bdcdb8b32ebf85de407017b8624ea4_2_505x499.png" alt="image" data-base62-sha1="2D1EhY8vfSgaN0x26WVFwnAMZ1i" width="505" height="499" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/126db61781bdcdb8b32ebf85de407017b8624ea4_2_505x499.png, https://discuss.pytorch.org/uploads/default/original/3X/1/2/126db61781bdcdb8b32ebf85de407017b8624ea4.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/2/126db61781bdcdb8b32ebf85de407017b8624ea4.png 2x" data-dominant-color="A9A8A8"><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">595×588 45.8 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>However, adding the image to tensorboard gives a degraded image:</p>
<pre data-code-wrap="python"><code class="lang-python">tb = SummaryWriter()
tb.add_image('image_indiv',dcm[0].unsqueeze(0))
tb.close()
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/7/478ffd17b7c238e5c6037a02cd98287a6de4423b.png" data-download-href="https://discuss.pytorch.org/uploads/default/478ffd17b7c238e5c6037a02cd98287a6de4423b" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/7/478ffd17b7c238e5c6037a02cd98287a6de4423b.png" alt="image" data-base62-sha1="ad4ot1rT32bxLfyLFmz9tQKztjB" width="233" height="250" data-dominant-color="888787"><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">556×595 20.2 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>

username: tdchaitanya
cooked: <p>This looks like some image normalization issue. Matplotlib normalizes the image before displaying, but I don’t think tensorboard does that (not sure though).</p>

username: alex.veuthey
cooked: <p>It looks more like full preprocessing rather than just normalization, the colors seem inverted. Are you sure that you are calling the two lines (<code>plt.imshow</code> and <code>tb.add_image</code>) without <code>dcm</code> changing in between?</p>

username: shaolinkhoa
cooked: <p>Hi,<br>
I’m also facing with this problem when I’m using <code>torch.utils.tensorboard</code> with <code>tensorboard 2</code></p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6b1df740ae7ee1c67299686d120686f032aca4ef.png" alt="image" data-base62-sha1="fhBi1XWI7EctBlUQzKDEhVO7kPZ" width="409" height="396"><br>
The left picture is the image on tensorboard. It’s bad.<br>
The right picture is when I save the numpy array by using the <code>imageio.imwrite(image)</code></p>
<p>Previous I used this <a href="https://gist.github.com/tm157/e9759e14121a02f69a2da694f123fce7" rel="noopener nofollow ugc">code</a> to plot image in <code>tensorboard 1.13.1</code> and the quality was good just like the right picture above.</p>
<p>Does anyone have the best practice for this situation?</p>

username: shaolinkhoa
cooked: <p>I found the problem.<br>
Because my input array is not <code>np.uint8</code> so <a href="https://github.com/pytorch/pytorch/blob/master/torch/utils/tensorboard/summary.py#L329" rel="nofollow noopener">_calc_scale_factor</a> multiply the input array with <code>scale_factor  = 255</code> one more time.</p>
<p><strong>Solution</strong>:<br>
Change the type before feed it to add_image<br>
<code>writer.add_image(tag, images.astype(np.uint8), step, dataformats=dataformats)</code></p>