当前位置: 首页 > news >正文

PyTorch深度学习快速入门学习总结(四)

完整的模型验证(测试)套路

CIFAR10测试集分类

单个图片测试代码:

import torch
import torchvision.datasets
from torch import nn
from torch.nn import MaxPool2d, Sequential, Conv2d, Flatten, Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import time
from PIL import Imageimage_path = './image/jinx.jpg'
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 Chenxi(nn.Module):def __init__(self, *args, **kwargs) -> None:super().__init__(*args, **kwargs)self.model1 = Sequential(Conv2d(3, 32, 5, padding=2),MaxPool2d(2),Conv2d(32, 32, 5, padding=2),MaxPool2d(2),Conv2d(32, 64, 5, padding=2),MaxPool2d(2),Flatten(),Linear(1024, 64),Linear(64, 10),)def forward(self, x):x = self.model1(x)return xmodel = torch.load('chenxi_0.pth') # 调用自己训练好的模型
# model = torch.load('chenxi_0.pth', map_location=torch.device('cpu'))
print(model)image = torch.reshape(image, (1, 3, 32, 32)) # 关于batch_sizeimage = image.cuda()model.eval()
with torch.no_grad():output = model(image)
print(output)
# tensor([[-0.7223,  0.4807, -0.1583,  0.2034, -0.0316,  0.4585,  0.3231,  0.1076,# -0.4046,  0.2497]], device='cuda:0')print(output.argmax(1)) # 转换输出类型
# tensor([1], device='cuda:0')
http://www.dtcms.com/a/309824.html

相关文章:

  • JS-第十九天-事件(一)
  • safari添加主屏及PWA启动方式
  • 玩转ChatGPT:寻找仪器用户手册
  • TEM在量子点发光材料领域的应用-测试GO
  • 汇川ITS7100E触摸屏交互界面开发(一)调试事项说明
  • BIFU币富探索合规新路径 助力用户玩转RWA
  • 两步走解决家庭路由器无法上网
  • Java 根据多个 MM-dd 日期计算总时长(包含当日和次日)
  • python PIL图片转base64字符串
  • [极客大挑战 2019]RCE ME
  • RockAI 的破壁之战:Yan 架构如何啃下“端侧炼丹”硬骨头?
  • Vue3 setup、ref和reactive函数
  • 元策联盈:深耕金融领域,赋能行业发展​
  • 【Flutter3.8x】flutter从入门到实战基础教程(四):自定义实现一个自增的StatefulWidget组件
  • 磁力搜索+磁力下载!无需解释!直接上车!
  • 工作好用小工具积累
  • EGE 流量开关 LG 518 GSP P11237
  • 异步函数被调用多次,多次处理同一个文件导致占用,如何让异步函数按顺序执行?
  • 书生浦语第五期L0G2
  • 【音视频】WebRTC 中的RTP、RTCP、SDP、Candidate
  • Ubuntu-Server-24.04-LTS版本操作系统如何关闭自动更新,并移除不必要的内核
  • C++在类的构造函数中调用另一个构造函数
  • STM32CubeIDE新建项目过程记录备忘(四)EXTI外部中断
  • 网络编程-加密算法
  • Spring Boot 异步执行方式全解析:@Async、CompletableFuture 与 TaskExecutor 对比
  • 面试题:怎么理解 OSI 参考模型(开放式系统互联参考模型) 和 TCP/IP 模型(传输控制协议 / 网际协议模型 )
  • Node.js中Buffer的用法
  • Linux中Docker Swarm介绍和使用
  • 数据结构学习基础和从包装类缓存到泛型擦除的避坑指南
  • QT初入门-界面设置