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

Paula_Hwang: <ul>
<li>Any idea how to fix<br>
TypeError: img should be PIL Image. Got &lt;class ‘str’&gt;</li>
<li>Let me know what I did wrong.</li>
</ul>
<pre><code class="lang-auto"># Predict 
def predict(image, model, topk=5):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    img = process_image(image)
    img = img.unsqueeze(0)
    
    output = model.forward(img)
    probs, labels = torch.topk(output, topk)        
    probs = probs.exp()
        
    # Reverse the dict
    idx_to_class = {val: key for key, val in model.class_to_idx.items()}
    # Get the correct indices
    top_classes = [idx_to_class[each] for each in classes]
    
    return labels, probs

#Passing 
probs, classes = predict(image, model)
print(probs)
print(classes)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-92-b49fdcab5791&gt; in &lt;module&gt;()
----&gt; 1 probs, classes = predict(image, model)
     2 print(probs)
     3 print(classes)

&lt;ipython-input-91-05809355bfe0&gt; in predict(image, model, topk)
     2     ‘’' Predict the class (or classes) of an image using a trained deep learning model.
     3     ‘’'
----&gt; 4     img = process_image(image)
     5     img = img.unsqueeze(0)
     6

&lt;ipython-input-20-02663a696e34&gt; in process_image(image)
    11                              std=[0.229, 0.224, 0.225])
    12     ])
---&gt; 13     image = process(image)
    14     return image

/opt/conda/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py in __call__(self, img)
    47     def __call__(self, img):
    48         for t in self.transforms:
---&gt; 49             img = t(img)
    50         return img
    51

/opt/conda/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py in __call__(self, img)
   173             PIL Image: Rescaled image.
   174         “”"
--&gt; 175         return F.resize(img, self.size, self.interpolation)
   176
   177     def __repr__(self):

/opt/conda/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/functional.py in resize(img, size, interpolation)
   187     “”"
   188     if not _is_pil_image(img):
--&gt; 189         raise TypeError(‘img should be PIL Image. Got {}’.format(type(img)))
   190     if not (isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 2)):
   191         raise TypeError(‘Got inappropriate size arg: {}’.format(size))

TypeError: img should be PIL Image. Got &lt;class ‘str’&gt;</code></pre>

ptrblck: <p>It seems you are passing <code>image</code> as a string (maybe the image path?).<br>
If that’s the case, load the image before passing it to <code>predict</code>:</p>
<pre><code class="lang-python">image = Image.open(image_path)
predict(image, model)
</code></pre>

Paula_Hwang: <p>Yes the image is the image path.<br>
I made on purpose not to use image_path because of the other error.</p>
<p>I have load it. Here are two different errors.</p>
<p><code>image = Image.open(image_path)</code><br>
<code>predict(image, model)</code></p>
<hr>
<p>NameError                                 Traceback (most recent call last)<br>
 in ()<br>
----&gt; 1 image = Image.open(image_path)<br>
2 predict(image, model)</p>
<p>NameError: name ‘image_path’ is not defined</p>
<p>Secondly, I did load this one. I got this.</p>
<p><code>image = Image.open(image)</code><br>
<code>predict(image, model)</code></p>
<p>I got another different error.<br>
AttributeError                            Traceback (most recent call last)<br>
 in ()<br>
----&gt; 1 image = Image.open(image)<br>
2 predict(image, model)</p>
<p>/opt/conda/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode)<br>
2587         exclusive_fp = True<br>
2588<br>
-&gt; 2589     prefix = fp.read(16)<br>
2590<br>
2591     preinit()</p>
<p>AttributeError: ‘JpegImageFile’ object has no attribute ‘read’</p>

Paula_Hwang: <p>I finally managed to fix the problem:<br>
Unfortunately, I got another error.</p>
<blockquote>
<p>AttributeError: ‘JpegImageFile’ object has no attribute ‘read’</p>
</blockquote>
<p><strong>Code:</strong></p>
<pre><code class="lang-auto">def predict(image, model, topk=5):
    ''' 
      Predict the class (or classes) of an image using a trained deep learning model.
      Here, image is the path to an image file, but input to process_image should be                                                         
      Image.open(image)
    '''
    img = process_image(Image.open(image))
    img = torch.from_numpy(img).type(torch.FloatTensor) 

    output = model.forward(img)
    probs, labels = torch.topk(output, topk)        
    probs = probs.exp()

    # Reverse the dict
    idx_to_class = {val: key for key, val in model.class_to_idx.items()}
    # Get the correct indices
    top_classes = [idx_to_class[each] for each in classes]

    return labels, probs
</code></pre>
<pre><code class="lang-auto">probs, classes = predict(image, model)
print(probs)
print(classes)
</code></pre>
<p><strong>Error</strong></p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-32-b49fdcab5791&gt; in &lt;module&gt;()
----&gt; 1 probs, classes = predict(image, model)
      2 print(probs)
      3 print(classes)

&lt;ipython-input-31-6f996290ea63&gt; in predict(image, model, topk)
      5       Image.open(image)
      6     '''
----&gt; 7     img = process_image(Image.open(image))
      8     img = torch.from_numpy(img).type(torch.FloatTensor)
      9 

/opt/conda/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode)
   2587         exclusive_fp = True
   2588 
-&gt; 2589     prefix = fp.read(16)
   2590 
   2591     preinit()

AttributeError: 'JpegImageFile' object has no attribute 'read'
</code></pre>

ptrblck: <p>Could you post the following outputs:</p>
<pre><code class="lang-python">print(image)
print(type(image))
image = Image.open(image)
</code></pre>

Paula_Hwang: <aside class="quote no-group" data-username="ptrblck" data-post="5" data-topic="49644">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>print(image) print(type(image)) image = Image.open(image)</p>
</blockquote>
</aside>
<p>I have used your code. This is what I got:</p>
<pre><code class="lang-auto">&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=695x500 at 0x7F3D941C15F8&gt;
&lt;class 'PIL.JpegImagePlugin.JpegImageFile'&gt;

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-27-3d8674b159a4&gt; in &lt;module&gt;()
      1 print(image)
      2 print(type(image))
----&gt; 3 image = Image.open(image)

/opt/conda/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode)
   2587         exclusive_fp = True
   2588 
-&gt; 2589     prefix = fp.read(16)
   2590 
   2591     preinit()

AttributeError: 'JpegImageFile' object has no attribute 'read'

</code></pre>

ptrblck: <p>Thanks for the information.<br>
How did you create <code>image</code> then?<br>
You should pass the path to the image file to <code>Image.open()</code>.</p>

Paula_Hwang: <p>I  created this code ( before predict function).</p>
<pre><code class="lang-auto">#Showing my image 
with Image.open('flowers/test/32/image_05591.jpg') as image:
    plt.imshow(image)
</code></pre>
<p>Do you think I should do this?</p>
<pre><code class="lang-auto">Image.open(image_path).
</code></pre>

ptrblck: <p>Yes, load the image and pass it to <code>process_image</code>:</p>
<pre><code class="lang-python">image = Image.open('flowers/test/32/image_05591.jpg')
img = process_image(image)
</code></pre>

Paula_Hwang: <p>After I have used your code.</p>
<pre><code class="lang-auto">image = Image.open('flowers/test/32/image_05591.jpg')
img = process_image(image)
</code></pre>
<p>This happens.</p>
<pre><code class="lang-auto">tensor([[[ 1.1015,  1.0331,  0.9474,  ..., -0.4739, -0.5253, -0.4397],
         [ 1.1015,  1.0502,  1.0159,  ..., -0.3541, -0.4911, -0.5253],
         [ 1.1187,  1.0673,  1.0331,  ..., -0.0287, -0.2513, -0.4739],
         ...,
         [-1.3644, -1.3644, -1.3815,  ..., -1.5357, -1.5870, -1.6384],
         [-1.3130, -1.3302, -1.3815,  ..., -1.5528, -1.5870, -1.6213],
         [-1.2959, -1.3644, -1.4500,  ..., -1.6213, -1.6213, -1.6213]],

        [[-1.5280, -1.5455, -1.5455,  ..., -0.1099, -0.0749,  0.0476],
         [-1.5105, -1.5455, -1.5455,  ..., -0.2150, -0.1099, -0.0574],
         [-1.5105, -1.5280, -1.5280,  ..., -0.0749,  0.0826,  0.0826],
         ...,
         [-1.1604, -1.1604, -1.1429,  ..., -1.3179, -1.3704, -1.4580],
         [-1.1078, -1.1078, -1.1604,  ..., -1.3704, -1.4055, -1.4405],
         [-1.0553, -1.1078, -1.1779,  ..., -1.3880, -1.3880, -1.3880]],

        [[-1.3687, -1.3861, -1.3861,  ..., -0.7413, -0.8110, -0.7413],
         [-1.3339, -1.3687, -1.3861,  ..., -0.7413, -0.8110, -0.8633],
         [-1.3339, -1.3513, -1.3687,  ..., -0.5670, -0.6193, -0.7761],
         ...,
         [-1.4733, -1.4907, -1.4907,  ..., -1.5430, -1.5953, -1.6650],
         [-1.4384, -1.4907, -1.5430,  ..., -1.5256, -1.5604, -1.5953],
         [-1.3513, -1.4559, -1.5430,  ..., -1.5430, -1.5256, -1.5256]]])
</code></pre>

ptrblck: <p>Great!<br>
Looks like the image was successfully processed, so that you can now execute the remaining code. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Paula_Hwang: <p>I understand… I am assuming I should change the name of the pathways …</p>

Paula_Hwang: <p>I see. I guess I am going to change the name of the pathway…</p>

Paula_Hwang: <p>I have changed the name:</p>
<pre><code class="lang-auto">image = Image.open('flowers/test/32/image_05591.jpg')
img = process_image(image)
</code></pre>
<pre><code class="lang-auto">def predict(image, model, topk=5):
    ''' 
      Predict the class (or classes) of an image using a trained deep learning model.
      Here, image is the path to an image file, but input to process_image should be                                                         
      Image.open(image)
    '''
    img = process_image(Image.open(img))
    img = torch.from_numpy(img).type(torch.FloatTensor) 

    output = model.forward(img)
    probs, labels = torch.topk(output, topk)        
    probs = probs.exp()

    # Reverse the dict
    idx_to_class = {val: key for key, val in model.class_to_idx.items()}
    # Get the correct indices
    top_classes = [idx_to_class[each] for each in classes]

    return labels, probs
</code></pre>
<p>The test it:</p>
<pre><code class="lang-auto">probs, classes = predict(image, model)
print(probs)
print(classes)
</code></pre>
<p>New errors:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
&lt;ipython-input-29-b49fdcab5791&gt; in &lt;module&gt;()
----&gt; 1 probs, classes = predict(image, model)
      2 print(probs)
      3 print(classes)

&lt;ipython-input-28-2ef4f9abd430&gt; in predict(image, model, topk)
      5       Image.open(image)
      6     '''
----&gt; 7     img = torch.from_numpy(img).type(torch.FloatTensor)
      8 
      9     output = model.forward(img)

UnboundLocalError: local variable 'img' referenced before assignment
</code></pre>

Megga_Dismas: <aside class="quote no-group" data-username="Paula_Hwang" data-post="15" data-topic="49644">
<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/paula_hwang/48/13730_2.png" class="avatar"> Paula_Hwang:</div>
<blockquote>
<p><code>img = process_image(Image.open(img))</code></p>
</blockquote>
</aside>
<p>instead of ‘img’, you had to put ‘image’.</p>
<p>did it work anyway? i mean, did you fix the entire problem?</p>