- Any idea how to fix
TypeError: img should be PIL Image. Got <class ‘str’> - Let me know what I did wrong.
# 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)
<ipython-input-92-b49fdcab5791> in <module>()
----> 1 probs, classes = predict(image, model)
2 print(probs)
3 print(classes)
<ipython-input-91-05809355bfe0> in predict(image, model, topk)
2 ‘’' Predict the class (or classes) of an image using a trained deep learning model.
3 ‘’'
----> 4 img = process_image(image)
5 img = img.unsqueeze(0)
6
<ipython-input-20-02663a696e34> in process_image(image)
11 std=[0.229, 0.224, 0.225])
12 ])
---> 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:
---> 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 “”"
--> 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):
--> 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 <class ‘str’>