TypeError: img should be PIL Image. Got <class 'torch.Tensor'>

Hyeongu_Oh: <pre><code class="lang-auto">def make_loader():
    trans = transforms.Compose([transforms.ToTensor(),
                            transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)),
                            transforms.RandomHorizontalFlip(0.5)])
    trainset = torchvision.datasets.ImageFolder(root='/content/drive/My Drive/All Data', transform=trans)
    loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)

    return loader
loader = make_loader()
</code></pre>
<pre><code class="lang-auto">----&gt; 5     for data in loader:
--&gt; 414         raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
TypeError: img should be PIL Image. Got &lt;class 'torch.Tensor'&gt;
</code></pre>
<p>So, I modified the code.</p>
<p>But,</p>
<pre><code class="lang-auto">def make_loader():
    trans = transforms.Compose([transforms.ToPILImage(),
                            transforms.ToTensor(),
                            transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)),
                            transforms.RandomHorizontalFlip(0.5)])
    trainset = torchvision.datasets.ImageFolder(root='/content/drive/My Drive/All Data', transform=trans)
    loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)

    return loader
loader = make_loader()
</code></pre>
<pre><code class="lang-auto">----&gt; 5     for data in loader:
--&gt; 100         raise TypeError('pic should be Tensor or ndarray. Got {}.'.format(type(pic)))
TypeError: pic should be Tensor or ndarray. Got &lt;class 'PIL.Image.Image'&gt;.
</code></pre>
<p>how to solve this problem?<br>
or Let me know if you know a simple way to create a dataloader that randomly reverses images when you call.</p>

Nikronic: <p>Hi,<br>
All classes in <code>transforms</code> except <code>Normalize</code> work on PIL images so you have put below line or similar to it before converting it to tensor using <code>ToTensor</code>. After these transforms call <code>ToTensor</code> and then <code>Normalize</code>.</p>
<aside class="quote no-group" data-username="Hyeongu_Oh" data-post="1" data-topic="85834">
<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/hyeongu_oh/48/14475_2.png" class="avatar"> Hyeongu_Oh:</div>
<blockquote>
<p><code>transforms.RandomHorizontalFlip(0.5)</code></p>
</blockquote>
</aside>
<p>Solution:</p>
<pre><code class="lang-auto">trans = transforms.Compose([transforms.ToPILImage(),
                            transforms.RandomHorizontalFlip(0.5),
                            ...
                            ...
                            transforms.ToTensor(),
                            transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
</code></pre>
<p>Bests</p>

Hyeongu_Oh: <p>Thank you for the answer.<br>
But, There is still an error.</p>
<pre><code class="lang-auto">/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py in __call__(self, pic)
    125 
    126         """
--&gt; 127         return F.to_pil_image(pic, self.mode)
    128 
    129     def __repr__(self):

--&gt; 100         raise TypeError('pic should be Tensor or ndarray. Got {}.'.format(type(pic)))
TypeError: pic should be Tensor or ndarray. Got &lt;class 'PIL.Image.Image'&gt;.
</code></pre>
<p>Original image is PNG file. That’s matter?<br>
I think ImageFolder is the problem.</p>

Nikronic: <p>No, that was my mistake, remove <code>ToPILImage</code> from transfroms list and it will be ok.</p>
<aside class="quote no-group quote-modified" data-username="Nikronic" data-post="2" data-topic="85834">
<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/nikronic/48/71873_2.png" class="avatar"> Nikronic:</div>
<blockquote>
<pre><code class="lang-auto">trans = transforms.Compose([transforms.ToPILImage(),  # remove this
                            transforms.RandomHorizontalFlip(0.5),
                            ...
                            ...
                            transforms.ToTensor(),
                            transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
</code></pre>
</blockquote>
</aside>
<p>The explanation would be that <code>ImageFolder</code> converts to <code>PIL</code> by itself and <code>ToPIL</code> was converting a PIL image to PIL which is wrong as `ToPIL only needs tensor.</p>

Hyeongu_Oh: <p>Oh my god, thanks!! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>