使用pytorch测试单张图片(test single image with pytorch)

以下代码实现使用pytorch测试一张图片

引用文章:

https://www.learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/

from __future__ import print_function, division

from PIL import Image
import torch
from torchvision import transforms
import matplotlib.pyplot as plt


plt.ion()   # interactive mode

# 图片路径
save_path = '/home/guomin/.cache/torch/checkpoints/resnet18-customs-angle.pth'

# ------------------------ 加载数据 --------------------------- #
# Data augmentation and normalization for training
# Just normalization for validation
# 定义预训练变换
preprocess_transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])

class_names = ['0', '180', '270', '90']

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# ------------------------ 载入模型并且训练 --------------------------- #
model = torch.load(save_path)
model.eval()
# print(model)

image_PIL = Image.open('image.jpg')
# 
image_tensor = preprocess_transform(image_PIL)
# 以下语句等效于 image_tensor = torch.unsqueeze(image_tensor, 0)
image_tensor.unsqueeze_(0)
# 没有这句话会报错
image_tensor = image_tensor.to(device)

out = model(image_tensor)
# 得到预测结果,并且从大到小排序
_, indices = torch.sort(out, descending=True)
# 返回每个预测值的百分数
percentage = torch.nn.functional.softmax(out, dim=1)[0] * 100

print([(class_names[idx], percentage[idx].item()) for idx in indices[0][:5]])

 结果返回:

[('270', 99.9299545288086), ('90', 0.06985548883676529), ('0', 0.0001458235055906698), ('180', 4.714601891464554e-05)]

猜你喜欢

转载自www.cnblogs.com/ttweixiao-IT-program/p/11977884.html