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

username: engr.zaheer91
cooked: <p>i am a beginner in Pytorch using to detect image class and facing this error i don’t know how to resolve this error</p>
<p>here is code</p>
<p>sm = nn.Softmax(dim = 1)</p>
<p>data=test_transforms(image)</p>
<p>data.unsqueeze_(0)<br>
data=Variable(data)<br>
data = data.type(torch.FloatTensor)</p>
<p>classifier.eval()<br>
output = classifier(data)</p>
<p>print(labels[output.cpu().data.numpy().argmax()])</p>
<p>facing this error</p>
<hr>
<p>TypeError                                 Traceback (most recent call last)<br>
 in <br>
1 sm = nn.Softmax(dim = 1)<br>
2<br>
----&gt; 3 data=test_transforms(image)<br>
4<br>
5 data.unsqueeze_(0)</p>
<p>~.conda\envs\tensorflow\lib\site-packages\torchvision\transforms\transforms.py in <strong>call</strong>(self, img)<br>
58     def <strong>call</strong>(self, img):<br>
59         for t in self.transforms:<br>
—&gt; 60             img = t(img)<br>
61         return img<br>
62</p>
<p>~.conda\envs\tensorflow\lib\site-packages\torchvision\transforms\transforms.py in <strong>call</strong>(self, img)<br>
193             PIL Image: Rescaled image.<br>
194         “”"<br>
 → 195         return F.resize(img, self.size, self.interpolation)<br>
196<br>
197     def <strong>repr</strong>(self):</p>
<p>~.conda\envs\tensorflow\lib\site-packages\torchvision\transforms\functional.py in resize(img, size, interpolation)<br>
227     “”"<br>
228     if not _is_pil_image(img):<br>
 → 229         raise TypeError(‘img should be PIL Image. Got {}’.format(type(img)))<br>
230     if not (isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)):<br>
231         raise TypeError(‘Got inappropriate size arg: {}’.format(size))</p>
<p>TypeError: img should be PIL Image. Got &lt;class ‘str’&gt;<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/8/c8c8c72ad5a9d1baac2449048d4f4c6fc9b3728b.png" data-download-href="https://discuss.pytorch.org/uploads/default/c8c8c72ad5a9d1baac2449048d4f4c6fc9b3728b" title="error"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/8/c8c8c72ad5a9d1baac2449048d4f4c6fc9b3728b.png" alt="error" data-base62-sha1="sEdER3RQDcyhAuWFW6GudhX7KKn" width="690" height="456" data-dominant-color="F0F1F1"><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">error</span><span class="informations">908×601 29.6 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>

username: ptrblck
cooked: <p>Based on the error message it seems you might be passing the path or image name as <code>image</code>, while an already loaded <code>PIL.Image</code> is expected.<br>
If that’s the case, try to load the image before passing it to the transformation via <code>im = PIL.Image.open(image_path)</code>.</p>

username: engr.zaheer91
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/d/3d3fa15cbc6217b97a20577acd17f724e88e3efa.png" data-download-href="https://discuss.pytorch.org/uploads/default/3d3fa15cbc6217b97a20577acd17f724e88e3efa" title="error"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/d/3d3fa15cbc6217b97a20577acd17f724e88e3efa.png" alt="error" data-base62-sha1="8JPs5sdgP4eerzKkXuBSdf4ji2K" width="690" height="418" data-dominant-color="F4F4F4"><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">error</span><span class="informations">1073×651 28.9 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>
<p>got this error after changing code</p>

username: engr.zaheer91
cooked: <p>sm = nn.Softmax(dim = 1)</p>
<p>data=test_transforms(PIL.Image.open(image_path))        <span class="hashtag">#im</span> = PIL.Image.open(image_path)</p>
<p>data.unsqueeze_(0)<br>
data=Variable(data)<br>
data = data.type(torch.FloatTensor)</p>
<p>classifier.eval()<br>
output = classifier(data)</p>
<p>print(labels[output.cpu().data.numpy().argmax()])</p>
<h2>got this error</h2>
<p>NameError                                 Traceback (most recent call last)<br>
 in <br>
1 sm = nn.Softmax(dim = 1)<br>
2<br>
----&gt; 3 data=test_transforms(PIL.Image.open(image_path)) <span class="hashtag">#im</span> = PIL.Image.open(image_path)<br>
4<br>
5 data.unsqueeze_(0)</p>
<p>NameError: name ‘PIL’ is not defined</p>

username: ptrblck
cooked: <p>You would have to either <code>import PIL</code> directly or <code>from PIL import Image</code> and call <code>Image.open</code>.</p>