Not able to plot an image from a Pytorch Dataset

arkh: <p>Hi!<br>
I am trying to extract an image and plot it using <code>matplotlib.pyplot.imshow()</code> but I am not able to do so. Here is my Dataset class:</p>
<pre><code class="lang-auto">class MelanomaDataset(Dataset):
    def __init__(self, df, meta_features=None, transforms=None):
        self.df = df.reset_index(drop=True)
        self.meta_features = meta_features
        self.transforms = transforms

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

    def __getitem__(self, index):
        img_name = self.df.iloc[index].image_name
        img_path = os.path.join('/kaggle/input/siim-isic-melanoma-classification/jpeg/train', img_name + '.jpg')

        image = cv2.imread(img_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        if self.transforms is not None:
            res = self.transforms(image=image)
            image = res['image'].astype(np.float32)
        else:
            image = image.astype(np.float32)

        image = image.transpose(2, 0, 1)

        if self.meta_features is not None:
            img = torch.tensor(image).float()
            meta_feats = torch.tensor(
                self.df.iloc[index][self.meta_features]
            ).float()
        else:
            img = torch.tensor(image).float()
            meta_feats = None
        return {
            'image': img,
            'meta_features': meta_feats,
            'target': torch.tensor(self.df.iloc[index].target).long()
        }
</code></pre>
<p>And this is how I am trying to plot a sample from the dataset:</p>
<pre><code class="lang-auto">plt.imshow(dataset[1]["image"].permute(1, 2, 0))
</code></pre>
<p>It would be great if someone could help out with this problem.<br>
Thanks!</p>

ptrblck: <p>What kind of error are you seeing?</p>

arkh: <p>I’m not getting an error. Just the figure that is being printed is white, so the img is not being printed. Here is a screenshot:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/5/e5b7a26c4b8171c7f3b20f1df3505adfe65af40b.png" data-download-href="https://discuss.pytorch.org/uploads/default/e5b7a26c4b8171c7f3b20f1df3505adfe65af40b" title="Screenshot 2023-01-15 at 1.56.26 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5b7a26c4b8171c7f3b20f1df3505adfe65af40b_2_690x464.png" alt="Screenshot 2023-01-15 at 1.56.26 AM" data-base62-sha1="wMaM61cNVlXCqANW3E5QjmwX9In" width="690" height="464" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5b7a26c4b8171c7f3b20f1df3505adfe65af40b_2_690x464.png, https://discuss.pytorch.org/uploads/default/original/3X/e/5/e5b7a26c4b8171c7f3b20f1df3505adfe65af40b.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/5/e5b7a26c4b8171c7f3b20f1df3505adfe65af40b.png 2x" data-dominant-color="FBFBFB"><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">Screenshot 2023-01-15 at 1.56.26 AM</span><span class="informations">788×530 20.1 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>

ptrblck: <p>I guess you are ignoring a warning explaining you might be clipping the image?</p>
<pre><code class="lang-python">Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
</code></pre>

arkh: <p>I am not getting that warning I just double checked.</p>

ptrblck: <p>In that case I wouldn’t know what’s causing the issue as your code works fine after removing undefined parts and casing the output back to <code>uint8</code>:</p>
<pre><code class="lang-python">class MelanomaDataset(Dataset):
    def __init__(self):
        self.paths = ["image.jpeg"]

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

    def __getitem__(self, index):
        img_path = self.paths[index]
        
        image = cv2.imread(img_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        image = image.transpose(2, 0, 1)
        image = torch.from_numpy(image).float()

        return {
            'image': image,
        }
    
dataset = MelanomaDataset()
data = dataset[0]
img = data["image"]
plt.imshow(img.permute(1, 2, 0).numpy().astype(np.uint8))
</code></pre>

arkh: <p>That’s strange. Thanks a lot tho <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>Could you double check the <code>dtype</code> and value range of your <code>data["image"]</code> output and check the warning I’ve posted?<br>
Make sure that floating point numbers are in <code>[0, 1]</code> and ints in <code>[0, 255]</code>.</p>