Visualize image from pixels

username: smu226
cooked: <p>Hello! I have a pytorch tensor, call it a, which should contain the pixel values of an image. Here it is how it looks like:</p>
<pre><code class="lang-auto">tensor([[[154., 143., 137.,  ..., 136., 136., 136.],
         [140., 155., 143.,  ..., 136., 136., 136.],
         [131., 153., 138.,  ..., 136., 136., 136.],
         ...,
         [163., 170., 181.,  ..., 189., 172., 152.],
         [172., 176., 184.,  ..., 204., 199., 190.],
         [178., 179., 184.,  ..., 213., 218., 224.]],

        [[207., 196., 194.,  ..., 204., 204., 204.],
         [196., 213., 202.,  ..., 204., 204., 204.],
         [193., 216., 204.,  ..., 204., 204., 204.],
         ...,
         [194., 197., 201.,  ..., 213., 207., 192.],
         [200., 197., 194.,  ..., 211., 203., 193.],
         [205., 196., 190.,  ..., 208., 203., 203.]],

        [[223., 212., 211.,  ..., 223., 223., 223.],
         [211., 227., 218.,  ..., 223., 223., 223.],
         [208., 231., 218.,  ..., 223., 223., 223.],
         ...,
         [214., 180., 132.,  ..., 177., 201., 204.],
         [178., 156., 121.,  ..., 169., 176., 172.],
         [154., 141., 118.,  ..., 166., 160., 158.]]])
</code></pre>
<p>How can I turn this numbers back into the image? Thank you!</p>

username: Balamurali_M
cooked: <p>x is a tensor<br>
y = x.numpy()</p>
<pre><code class="lang-auto">from matplotlib import pyplot as plt 
plt.imshow(y) % y should be 2D.
</code></pre>

username: Pranavan_Theivendira
cooked: <p>Hi,</p>
<p>Your image dimension is 3xMxN. To use imshow, the image dimension should be (M,N,3) if the image data is 3 dimensional. In addition the values should be in range [0,1] or [0,255]. In your case, it looks like that the values fall in the latter range. Hence, no issues with the pixel values. But, you still have to transpose. Following would be a solution.</p>
<pre><code class="lang-auto">import matplotlib.pyplot as plt
import numpy as np

npimg = img.numpy() # img is your tensor
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
</code></pre>
<p>Thanks</p>

username: smu226
cooked: <p>Thank you for your reply. I don’t really get the right image back. This is what I get:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/2/2a6a43e3cec8ce4218855789b62a4611c58257e4.png" alt="img" data-base62-sha1="63dJ0jC4C2tozbeHbFKOLLOHswc" width="545" height="500"><br>
which seems to be mostly white.</p>

username: Pranavan_Theivendira
cooked: <aside class="quote no-group" data-username="smu226" data-post="4" data-topic="47811">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smu226/48/9063_2.png" class="avatar"> smu226:</div>
<blockquote>
<p>Thank you for your reply. I don’t really get the right image back. This is what I get:</p>
</blockquote>
</aside>
<p>Hi,</p>
<p>This image contains pixels where most of them are white. It means that pixel value is (255,255,255).  As from the tensor values that you have shared, there are lots of other values as well. Check whether you are loading the right image. And check whether you are changing the values.</p>
<p>Thanks</p>

username: smu226
cooked: <p>All I did was to use the lines of code that you provided, replacing img with the tensor that I show above. I am not sure where I could have gone wrong.</p>

username: Pranavan_Theivendira
cooked: <p>Hi,</p>
<p>Could you please share where are you loading the tensor, then I can have a look.</p>
<p>Thanks</p>

username: smu226
cooked: <p>This is how (part of) my code looks like:</p>
<pre><code class="lang-auto">class ImageDataset(Dataset):
    def __init__(self, csv_file, root_dir):
        self.file_source = pd.read_csv(csv_file)
        self.root_dir = root_dir

    def __len__(self):
        return len(self.file_source)

    def __getitem__(self, idx):
        img_name = os.path.join(self.root_dir,
                                self.file_source.iloc[idx, 1])
        image = io.imread(img_name)
        image = image.transpose(2, 0, 1)
        image = torch.from_numpy(image)
        image = image.float()
        
        return image

rocket_dataset = ImageDataset(csv_file='./rocket_images_reduced/rockets.csv',
                              root_dir='./rocket_images_reduced/train/')

img_test = rocket_dataset[0]

npimg = img_test.numpy() # img is your tensor
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
</code></pre>

username: Pranavan_Theivendira
cooked: <aside class="quote no-group" data-username="smu226" data-post="8" data-topic="47811">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smu226/48/9063_2.png" class="avatar"> smu226:</div>
<blockquote>
<p>1</p>
</blockquote>
</aside>
<p>What is the size of img_test tensor?</p>
<pre><code class="lang-auto">print(img_test.size())
</code></pre>

username: smu226
cooked: <aside class="quote no-group" data-username="Pranavan_Theivendira" data-post="9" data-topic="47811">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pranavan_theivendira/48/12366_2.png" class="avatar"> Pranavan_Theivendira:</div>
<blockquote>
<p>print(img_test.size())</p>
</blockquote>
</aside>
<p>torch.Size([3, 64, 64])</p>

username: Pranavan_Theivendira
cooked: <p>Hi,</p>
<p>try this and check.</p>
<pre><code class="lang-auto">import matplotlib.pyplot as plt
import numpy as np

npimg = img.numpy() # img is your tensor
npimg = npimg / 255 # add this line
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
</code></pre>
<p>Thanks</p>

username: smu226
cooked: <aside class="quote no-group" data-username="Pranavan_Theivendira" data-post="11" data-topic="47811">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pranavan_theivendira/48/12366_2.png" class="avatar"> Pranavan_Theivendira:</div>
<blockquote>
<p>npimg = npimg / 255</p>
</blockquote>
</aside>
<p>It’s working now! Thank you so much!</p>