eval是做什么的pytorch预训练模型预测⾃⼰的图⽚没有⾦贵的GPU,⽤⽤巨头训练好的模型把⾃⼰的图⽚分分类。
其中的 dog.JPG是⾃⼰需要预测的图⽚。
代码如下:
import torch
from PIL import Image
from torchvision import transforms
dels as models
mobilenetV2 = bilenet_v2(pretrained=True)
#print(mobilenetV2)
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)])
img = Image.open("dog.JPG")
print('Input image shape: ',img.size)
img_t = transform(img)
batch_t = torch.unsqueeze(img_t, 0)
mobilenetV2.eval()
out = mobilenetV2(batch_t)
print(out.shape)
with open('') as f:
classes = [line.strip() for line adlines()]
_, indices = torch.sort(out, descending=True)
percentage = functional.softmax(out, dim=1)[0] * 100
prediction = [[classes[idx], percentage[idx].item()] for idx in indices[0][:5]]
for i in prediction:
print('Prediciton-> {:<25} Accuracy-> ({:.2f}%)'.format(i[0][5:], i[1]))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。