PyTorch深度学习笔记(二十)(模型验证测试)

前言

到这一章节为止,依据小土堆课程的PyTorch深度学习笔记基础部分已经完结了,接下来将依据李沐动手学深度学习课程进行PyTorch深度学习笔记的进阶部分

预测图片

完整的模型验证(测试,demo)套路,利用已经训练好的模型,然后给它提供输入。

输入狗的图片,并打开

python 复制代码
image_path = "imgs/dog.png"
image = Image.open(image_path)

4通道的RGBA转为3通道的RGB图片

python 复制代码
image = image.convert("RGB")

转换图像格式并设定网络

python 复制代码
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.model1 = 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.model1(x)
        return x

GPU上训练的东西映射到CPU上

python 复制代码
model = torch.load("model/tudui_29.pth",map_location=torch.device('cpu'))

转为四维,符合网络输入需求

python 复制代码
image = torch.reshape(image,(1,3,32,32))

将模型转为测试类型

python 复制代码
model.eval()

不进行梯度计算,减少内存计算

python 复制代码
with torch.no_grad():
    output = model(image)

概率最大类别的输出

python 复制代码
print(output.argmax(1))

完整代码

python 复制代码
import torchvision
from PIL import Image
from torch import nn
import torch

image_path = "imgs/dog.png"
image = Image.open(image_path)
image = image.convert("RGB") 
print(image)

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.model1 = 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.model1(x)
        return x

model = torch.load("model/tudui_29.pth",map_location=torch.device('cpu'))
print(model)
image = torch.reshape(image,(1,3,32,32)) 
model.eval()
with torch.no_grad():
    output = model(image)
output = model(image)
print(output)
print(output.argmax(1)) 
相关推荐
雪隐5 分钟前
个人电脑玩AI-09让5060 Ti给你打工——让 AI 读懂你的资料
人工智能·后端
大模型真好玩21 分钟前
LangChain DeepAgents 速通指南(十)—— DeepAgents Code 智能体服务核心源码解读
人工智能·langchain·agent
网易云信38 分钟前
「帝王蟹」企业AI落地实战营西安站落幕:共探“人工智能+”落地深水区
人工智能·agent·产品
阿虎儿42 分钟前
本地构建的自定义sandbox-extra镜像推送到沙盒daytona的snapshot列表中
人工智能
网易云信1 小时前
Agent在客服和营销领域走到哪一步了?深度解析3个挑战和5大趋势
人工智能·agent
网易云信1 小时前
AI 融入协作场景,Hermes 接入云信 IM
人工智能·agent
vivo互联网技术2 小时前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
饼干哥哥2 小时前
ChatGPT会员掉了,代充黑幕藏不住了
人工智能·操作系统·产品
ZzT2 小时前
Claude Sonnet 5 来了:Opus 级的能力,Sonnet 的价
人工智能·ai编程·claude
用户5191495848452 小时前
CVE-2025-14440 漏洞利用工具 - WordPress 插件认证绕过检测
人工智能·aigc