TypeError: pic should be PIL Image or ndarray. Got <class 'numpy.ndarray'>

luodahei: <p>when i ues "torchvision.transforms.Totensor()"to conver a numpy.array to tensor.But numpy.array is not ndarray?</p>

ptrblck: <p>Could you print the shape and type or your input?</p>

a53769: <p>maybe your input dimension is not a accept shape, can not deal with like a image</p>

purespring209: <p><a class="mention" href="/u/luodahei">@luodahei</a> Hi, have you fixed the problem?<br>
I have encountered the same problem, but no idea to fix it.<br>
Could you please help me?</p>
<p>Best regards,<br>
purespring</p>

eolicneb: <p>torchvision.transforms.ToTensor() does accept a ndarray. What doesn’t is the torchvision.transforms.Resize() or CenterCrop().<br>
Resize() takes a PIL image, so you need to convert the ndarray like this:</p>
<pre><code class="lang-auto">from PIL import Image
PIL_image = Image.fromarray(ndarray_image)
</code></pre>
<p>Hope this does the trick for you!</p>

skyuuka: <p>I have the same error, and the reason in my case is the array is None, i.e, obtained from <code>np.array(x)</code> where <code>x = None</code></p>

D_B: <p>I have the same issue, being that my data is of size <code>torch.Size([32, 3, 64, 64])</code>, and I’m using the transformations:</p>
<pre><code class="lang-python">transform = transforms.Compose(
    [transforms.RandomHorizontalFlip(p=0.2),
    transforms.ColorJitter(brightness=.5, hue=.1),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
</code></pre>
<p>I am calling as this:</p>
<pre><code class="lang-python">transform(data)
</code></pre>
<p>According to the <a href="https://pytorch.org/vision/stable/transforms.html" rel="noopener nofollow ugc">docs</a>, this composition should accept tensor. My PyTorch version is <code>1.7.1</code></p>
<p>Why is there even such difference between accepting PIL or tensor types? This makes everything so much harder</p>

ptrblck: <p>This code works for me, so you might need to update PyTorch and <code>torchvision</code>:</p>
<pre data-code-wrap="python"><code class="lang-python">transform = transforms.Compose(
    [transforms.RandomHorizontalFlip(p=0.2),
    transforms.ColorJitter(brightness=.5, hue=.1),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

x = torch.randn(32, 3, 64, 64)
out = transform(x)
print(out.shape)
&gt; torch.Size([32, 3, 64, 64])
</code></pre>
<aside class="quote no-group" data-username="D_B" data-post="7" data-topic="20134">
<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/d_b/48/42031_2.png" class="avatar"> D_B:</div>
<blockquote>
<p>Why is there even such difference between accepting PIL or tensor types? This makes everything so much harder</p>
</blockquote>
</aside>
<p>The ability to apply <code>torchvision.transforms</code> on tensors is relatively new and makes it easier now.</p>

nuv: <p>Hi I’m also getting this error with the most updated pytorch and torchvision. My transforms are</p>
<pre><code class="lang-auto">transform = transforms.Compose([torchvision.transforms.RandomCrop(size=(16, 16)), 
                                torchvision.transforms.RandomHorizontalFlip(p=0.5), 
                                torchvision.transforms.ToTensor(),
                                torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
                               ])
</code></pre>
<p>If I comment the ToTensor() transform out I instead get an “Unexpected type &lt;class ‘numpy.ndarray’&gt;” error.</p>
<p>The error shows up when I’m calling this line in my training function.</p>
<pre><code class="lang-auto">for i, (x, y) in enumerate(loader):
</code></pre>
<p>It works though when I just have the ToTensor and Normalise transform, but not the first 2</p>

ptrblck: <p>The transformations would work on <code>PIL.Image</code>s as well as PyTorch tensors (in newer <code>torchvision</code> releases), while it seems that you are trying to use a numpy array based on the error message.<br>
Pass on of the supported types to <code>transform</code> and it should work.</p>