飞机预测结果:
小狗预测结果:
文章插图
突然发现这么低的正确率,这个网络还是能识别处理的 。
(2)以视频流的形式测试()
首先要了解各种不同加载图片的通道顺序
所以对摄像头传过来的视频流主要是作通道顺序转换和图片缩放,以及图片转换为(float) 。
# 将opencv读取的图片进行预处理后,送网络中# opencv 读取的图像s缩放为32X32的大小resized_img = cv2.resize(image_raw, (32, 32), i# bgr-->rgbimage_rgb = cv2.cvtColor(resized_img, cv2.COLOR# 转torch.Tensorimage = torch.from_numpy(image_rgb)# 输入的图片是整数,要和网络参数(浮点数)保持一致image = image.float()# print(image.shape)# [3,32,32] --> [1,3,32,32]提升一张照片的维度image = torch.reshape(image, [1, 3, 32, 32])
汽车预测:
六、源码 (1)训练
网络模型:
from torch import nnimport torch# CIFAR10网络模型class Cifarnet(nn.Module):def __init__(self):super(Cifarnet, self).__init__()self.model = nn.Sequential(nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1,padding=2),# stride=1,padding=2卷积后尺寸不变nn.MaxPool2d(2),nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1,padding=2),# stride=1,padding=2卷积后尺寸不变nn.MaxPool2d(2),nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1,padding=2),# stride=1,padding=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 xif __name__ == '__main__':net = Cifarnet()input = torch.ones((64, 3, 32, 32))output = net(input)# torch.Size([64, 10])每一张图片的预测概率为一个10大小的数组print(output.shape)
网络训练和保存:
import torchimport torchvisionfrom torch.utils.data import DataLoaderfrom torch.utils.tensorboard import SummaryWriterfrom model import *# 准备数据集train_data = http://www.kingceram.com/post/torchvision.datasets.CIFAR10(root='./dataset', train=True, transform=torchvision.transforms.ToTensor(),download=True)test_data = torchvision.datasets.CIFAR10(root='./dataset', train=False, transform=torchvision.transforms.ToTensor(),download=True)# 数据集的大小,后面算测试的准确率train_data_size = len(train_data)test_data_size = len(test_data)print("训练集的长度为:{}".format(train_data_size))print("测试集的长度为:{}".format(test_data_size))# 利用DataLoader加载数据集train_dataloader = DataLoader(train_data, batch_size=64)test_dataloader = DataLoader(test_data, batch_size=64)# 创建网络模型net = Cifarnet()# print(net)# 损失函数loss_fn = nn.CrossEntropyLoss()# 优化器learning_rate = 0.01optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate)# 记录训练网络的一些参数# 记录训练次数total_train_step = 0# 记录测试的次数total_test_step = 0# 记录训练的轮数epoch = 5# 使用tensorboard可视化数据writer = SummaryWriter(log_dir='./logs')for i in range(epoch):print("-----第{}轮训练开始-----".format(i+1))# 开始训练net.train()# 仅对特殊的网络层右作用for data in train_dataloader:imgs, targets = dataoutputs = net(imgs)loss = loss_fn(outputs, targets)# 优化器优化模型optimizer.zero_grad()loss.backward()optimizer.step()total_train_step += 1if total_train_step % 100 == 0:print("训练次数:{},Loss:{}".format(total_train_step, loss.item()))writer.add_scalar("train_loss", loss.item(), total_train_step)# 开始测试net.eval()total_test_loss = 0total_accuracy = 0with torch.no_grad():for data in test_dataloader:imgs, targets = dataoutputs = net(imgs)loss = loss_fn(outputs, targets)total_test_loss += loss.item()# argmax找出每一张预测概率最大的下标accuracy = (outputs.argmax(1) == targets).sum()total_accuracy += accuracyprint("测试集的Loss:{}".format(total_test_loss))print("测试集的正确率:{}".format(total_accuracy/test_data_size))writer.add_scalar("test_loss", total_test_loss,total_test_step)writer.add_scalar("test_accurary", total_accuracy/test_data_size, total_test_step)total_test_step += 1# 保存模型torch.save(net, "cifdarnet.pth")print("模型已保存")writer.close()
- OpenCV之YOLOv2-tiny目标检测
- Pytorch之ResNet图像分类
- 乌合之众:大众心理研究
- 移动端vr技术探索之VrPanoramaView
- 二 Pytorch —— 激活函数、损失函数及其梯度
- Web自动化之页面元素定位---Xpath
- 现代简约四口之家,设计上兼顾不同年龄段的需求
- WordPress站点迁移及阿里云空间备案
- Android开发 之 共享元素
- 2023佛山敏捷之旅暨DevOps Meetup精彩回顾