How to show a image in jupyter notebook with pytorch easily?

username: richardhahahaha
cooked: <p>like in itorch, I can use itorch.image to show a image in the notebook.</p>

username: amdegroot
cooked: <p>Yeah this frustrated me a lot too because it’s so easy with itorch… You have a few options with python but there’s not a stand-alone command that I’m aware of for displaying an image from a PyTorch Tensor.  If you convert to a PIL image then you can just execute the Image variable in a cell and it will display the image.  To load to PIL:</p>
<pre><code class="lang-auto">img =  Image.open('path-to-image-file').convert('RGB')
</code></pre>
<p>Or to convert straight from a PyTorch Tensor:</p>
<pre><code class="lang-auto">to_pil = torchvision.transforms.ToPILImage()
img = to_pil(your-tensor)

</code></pre>
<p>Other than that, there’s the usual matplotlib <code>plt.show()</code> using numpy, and if you’ve used cv2 there are cv2 equivalents. In python it’s kind of whatever you get comfortable with, although apparently opencv might be faster performance-wise.</p>

username: chenyuntc
cooked: <p>this is how I display an image:</p>
<pre><code class="lang-python">import torch as t
from torchvision.transforms import ToPILImage
from IPython.display import Image
to_img = ToPILImage()

# display tensor
a = t.Tensor(3, 64, 64).normal_()
to_img(a)

# display imagefile
Image('/path/to/my.png')
</code></pre>

username: smth
cooked: <p>you can look at my notebook here, but yea seems like no standard solution.</p>
<p>Basically, I do:</p>
<pre><code class="lang-python">%matplotlib inline
def show(img):
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1,2,0)), interpolation='nearest')
</code></pre>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/vision/blob/master/test/sanity_checks.ipynb" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/master/test/sanity_checks.ipynb" target="_blank" rel="nofollow noopener">pytorch/vision/blob/master/test/sanity_checks.ipynb</a></h4>
<pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torchvision.transforms as transforms\n",
    "import torchvision.datasets as datasets\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
</code></pre>

  This file has been truncated. <a href="https://github.com/pytorch/vision/blob/master/test/sanity_checks.ipynb" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: oya163
cooked: <p>Hi Soumith,</p>
<p>I would like to know how I can use your solution, if I am using iterator like this</p>
<pre><code>dataiter = iter(trainloader)
images, labels = dataiter.next()
</code></pre>
<p>and the trainloader is loading data using <code>torchvision.datasets.ImageFolder</code></p>
<p>I am stuck on this. I have created my own dataset and I am following CIFAR10 example from pytorch tutorial.<br>
I would like to train a AlexNet using my own dataset.</p>
<p>Thank you in advance.</p>

username: farrokhi
cooked: <p>I have same question…</p>

username: Vimos
cooked: <p>This seems not working in an iteration. I have to wrap it in <code>display</code>.</p>
<pre><code class="lang-auto">    display(Image(image_path))
</code></pre>

username: talha3111997
cooked: <p>This is what I did, hope can be helpful for someone.</p>
<pre><code class="lang-auto">from PIL import Image
`image = Image.open('img_path.jpg').convert('RGB')
 plt.imshow(np.transpose(np.asarray(content.squeeze()),(1,2,0)))`

</code></pre>

username: xiaoyanzhuo
cooked: <p>What I did: transfer the tensor to numpy array, reshape(if needed), and use plt.imshow():</p>
<pre><code class="lang-python">import matplotlib.pyplot as plt
img_np_arr = img_tensor.numpy()   # transfer the pytorch tensor(img_tensor) to numpy array
img_np_arr.shape    # check shape before reshape if needed
img_np_arr_reshaped = img_np_arr.reshape(img_w, img_h)  # reshape to 2-dims image
plt.imshow(img_np_arr_reshaped, cmap='gray')   # when display the grayscale image
</code></pre>

username: ramapinnimty
cooked: <p>This worked flawlessly for displaying an image from CIFAR-100 dataset. Thank you!</p>