Я новичок и учусь кодировать классификатор изображений. Моя цель - создать функцию прогнозирования.
Любое предложение, чтобы исправить это?
В этом проекте я хочу использовать функцию прогнозирования для распознавания разных видов цветов. Чтобы я мог проверить их этикетки позже.
Попытка исправить: К сожалению, ошибка по-прежнему сохраняется. Я уже пробовал эти коды:
img = process_image(Image.open(image))
img = torch.from_numpy(img).type(torch.FloatTensor)
Это ошибка, которую мне нужно исправить сейчас.
AttributeError: у объекта 'JpegImageFile' нет атрибута 'read'
Код:
# 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)
Ошибка:
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'
Все, чего я хочу, это получить такой же результат. Спасибо!
tensor([[ 0.5607, 0.3446, 0.0552, 0.0227, 0.0054]], device='cuda:0')
tensor([[ 8, 1, 31, 24, 7]], device='cuda:0')