利用已经训练好的模型,拿出训练集中的部分数据进行测试
下面给出完整的示例代码:
# -*- coding: utf-8 -*-
# 作者:小土堆
# 公众号:土堆碎念
import torch
import torchvision
from PIL import Image
from torch import nnimage_path = "imgs/dog.jpg"
image = Image.open(image_path)
print(image)
image = image.convert('RGB')
transform = torchvision.transforms.Compose([torchvision.transforms.Resize((32, 32)),torchvision.transforms.ToTensor()])image = transform(image)
print(image.shape)class Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()self.model = nn.Sequential(nn.Conv2d(3, 32, 5, 1, 2),nn.MaxPool2d(2),nn.Conv2d(32, 32, 5, 1, 2),nn.MaxPool2d(2),nn.Conv2d(32, 64, 5, 1, 2),nn.MaxPool2d(2),nn.Flatten(),nn.Linear(64*4*4, 64),nn.Linear(64, 10))def forward(self, x):x = self.model(x)return xmodel = torch.load("tudui_9.pth", map_location=torch.device('cpu'), weights_only=False)
# model = torch.load("tudui_9.pth")
print(model)
image = torch.reshape(image, (1, 3, 32, 32))
model.eval()
with torch.no_grad():output = model(image)
print(output)print(output.argmax(1))
运行输出结果如下:
/home/decre/miniconda3/envs/pytorch/bin/python /home/decre/work/ybb/base_pytorch/06_verify_model.py
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1024x726 at 0x777290ABD040>
torch.Size([3, 32, 32])
Tudui((model): Sequential((0): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)(2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)(4): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)(6): Flatten(start_dim=1, end_dim=-1)(7): Linear(in_features=1024, out_features=64, bias=True)(8): Linear(in_features=64, out_features=10, bias=True))
)
tensor([[-1.4207, -7.0271, 1.5175, 3.6118, 1.4886, 5.2744, 1.2743, 3.9919,-5.6039, -2.7634]])
tensor([5])Process finished with exit code 0
结果分析:最终的输出结果通过print(output.argmax(1)),找到数组 output 中每行的最大值的索引,最终结果是tensor[5]。CIFAR10数据集对应的结果和下标如下:
也即我们放了狗的图片,识别出来的是狗,模型识别正确。