Torchvision output issue

maciejbalawejder: <p>The output image of torchvision.transforms return a batch of same images turned grey, even though it suppose to turn it into tensor. I attach the image of the generated output(left) and the reference of the image taken from dataset(right). I was trying to look on the forums but I havent found anything also I tested it on google colab and jupyter notebook with the same result. Is that the way how every image in Pytorch is transformed?<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/c/9/c9c909e3f4888a91bce310c89ab034e0a8610744.png" alt="bug" data-base62-sha1="sN4H4ZOIOkLpZSW79iQKNwQovVW" width="423" height="286"></p>
<pre><code class="lang-auto">class Cityscapes(Dataset):
    def __init__(self, path, transform = None):
        self.root_dir = path
        self.images = self.root_dir + r"train/"
        self.labels = self.root_dir + r"val/"
        print(self.labels, self.images)
        self.transform = transform

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

    def __getitem__(self, index):
        image = np.array(Image.open(self.images + str(index+1) + '.jpg').convert('RGB'))
        label = np.array(Image.open(self.labels + str(index+1) + '.jpg')
        temp = image
        if self.transform is not None:
            image = self.transform(image)

        return image, temp
</code></pre>
<pre><code class="lang-auto">
t = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor()
</code></pre>

ptrblck: <p>The left image is interleaved, which is usually caused by a wrong usage of <code>view</code> or <code>reshape</code> to swap the dimensions of an array or tensor instead of using <code>permute</code>.<br>
Could you check your code, if this could also be the case here?</p>

maciejbalawejder: <p>Unfortunetly the image is not reshaped, or permuted, it becomes interleaved after applying torchvision.transforms.toTensor.</p>

ptrblck: <p>Could you upload the input image here and post the transformation so that we could try to debug it, please?</p>

maciejbalawejder: <p>Of course. <img src="https://discuss.pytorch.org/uploads/default/original/3X/d/a/da6ce81b4b85c057673d2601f15a72c5345ed45a.jpeg" alt="1" data-base62-sha1="vahq2xgOuqW9YDEH408FcCm9mOe" width="256" height="256"></p>
<p>Before I tried also different transformation like rotation with the same result.</p>
<pre><code class="lang-auto">t = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor()
])
</code></pre>

ptrblck: <p>Thanks! The transformation works as intended and this code properly visualizes the normalized image:</p>
<pre data-code-wrap="python"><code class="lang-python">img = PIL.Image.open('tmp02.jpeg')

transform = torchvision.transforms.Compose([
        torchvision.transforms.ToTensor()
])

out = transform(img)
plt.imshow(out.permute(1, 2, 0).numpy())
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/f/5f8bb0215e1a63d463205c4792201a2d4ecf556e.png" alt="image" data-base62-sha1="dDeDFFpCcpXS0cWdwonKa2yl9LM" width="262" height="252"></p>
<p>Feel free to post an executable code snippet, which would reproduce this issue.</p>

maciejbalawejder: <p>Cheers, it works I put it in a return of dataset function, but still is there an explaination why the image turns into interleaved?</p>

ptrblck: <p>The image won’t be interleaved automatically, and since I’m not able to reproduce the issue you would have to check for <code>view</code> or <code>reshape</code> operations in your code, which would interleave the input.</p>