I am a beginner and I am learning to code an image classifier. My goal is to create a predict function.
Any suggestion to fix it?
In this project, I want to use the predict function to recognize different flower species. So I could check their labels later.
Attempt to fix: Unfortunately, the error is still persistent. I have already tried these codes:
img = process_image(Image.open(image))
img = torch.from_numpy(img).type(torch.FloatTensor)
This is the error that I need to fix now.
AttributeError: 'JpegImageFile' object has no attribute 'read'
Code:
# Imports here
import pandas as pd
import numpy as np
import torch
from torch import nn
from torchvision import datasets, transforms, models
import torchvision.models as models
import torch.nn.functional as F
import torchvision.transforms.functional as F
from torch import optim
import json
from collections import OrderedDict
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from PIL import Image
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
predict(image,model)
print(probs)
print(classes)
Error:
AttributeError Traceback (most recent call last)
<ipython-input-32-b49fdcab5791> in <module>()
----> 1 probs, classes = predict(image, model)
2 print(probs)
3 print(classes)
<ipython-input-31-6f996290ea63> in predict(image, model, topk)
5 Image.open(image)
6 '''
----> 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
-> 2589 prefix = fp.read(16)
2590
2591 preinit()
AttributeError: 'JpegImageFile' object has no attribute 'read'
All I want is to get these similar result. Thank you!
tensor([[ 0.5607, 0.3446, 0.0552, 0.0227, 0.0054]], device='cuda:0')
tensor([[ 8, 1, 31, 24, 7]], device='cuda:0')
Attributes are functions or properties associated with an object of a class. Everything in Python is an object, and all these objects have a class with some attributes. We can access such properties using the . operator. This tutorial will discuss the object has no attribute python error in Python. This error belongs to the AttributeError type.
AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. For example, if we take a variable x we assined a value 10.
The "AttributeError: 'dict' object has no attribute 'read'" occurs when we try to access the read attribute on a dictionary, e.g. by passing a dict to json.load (). To solve the error, use the json.dumps () method if trying to convert a dictionary to JSON. Here is an example of how the error occurs.
The error attributeerror: ‘str’ object has no attribute ‘read’ occurs when you try to read the string file from the filename instead of the file object. If you have a file that contains JSON response and you use the json.load () method then you will also get the ‘str’ object has no attribute ‘read’ error.
Converting image to RGB after openning solved the issue for me
PIL.Image.open(image_path).convert('RGB')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With