企业网站包含哪些页面,网站问答平台推广方案,网站佣金怎么做分录,asp.net 2.0网站开发全程解析 下载目录 LeNet-5 
LeNet-5 结构 
CIFAR-10 
pytorch实现 
lenet模型 
训练模型 
1.导入数据 
2.训练模型 
3.测试模型 
测试单张图片 
代码 
运行结果 LeNet-5 
LeNet-5 是由 Yann LeCun 等人在 1998 年提出的一种经典卷积神经网络#xff08;CNN#xff09;模型#xff0c;主要…目录 LeNet-5 
LeNet-5 结构 
CIFAR-10 
pytorch实现 
lenet模型 
训练模型 
1.导入数据 
2.训练模型 
3.测试模型 
测试单张图片 
代码 
运行结果 LeNet-5 
LeNet-5 是由 Yann LeCun 等人在 1998 年提出的一种经典卷积神经网络CNN模型主要用于手写数字识别任务。它在 MNIST 数据集上表现出色并且是深度学习历史上的一个重要里程碑。 
LeNet-5 结构 
LeNet-5 的结构包括以下几个层次 
输入层: 32x32 的灰度图像。卷积层 C1: 包含 6 个 5x5 的滤波器输出尺寸为 28x28x6。池化层 S2: 平均池化层输出尺寸为 14x14x6。卷积层 C3: 包含 16 个 5x5 的滤波器输出尺寸为 10x10x16。池化层 S4: 平均池化层输出尺寸为 5x5x16。卷积层 C5: 包含 120 个 5x5 的滤波器输出尺寸为 1x1x120。全连接层 F6: 包含 84 个神经元。输出层: 包含 10 个神经元对应于 10 个类别。 CIFAR-10 
CIFAR-10 是一个常用的图像分类数据集包含 10 个类别的 60,000 张 32x32 彩色图像。每个类别有 6,000 张图像其中 50,000 张用于训练10,000 张用于测试。 
1. 标注数据量训练集50000张图像测试集10000张图像 
2. 标注类别数据集共有10个类别。具体分类见图1。 
3. 可视化 pytorch实现 
lenet模型 
平均池化Average Pooling对池化窗口内所有像素的值取平均适合保留图像的背景信息。最大池化Max Pooling对池化窗口内的最大值进行选择适合提取显著特征并具有降噪效果。 
在实际应用中最大池化更常用因为它通常能更好地保留重要特征并提高模型的性能。 
import torch.nn as nn
import torch.nn.functional as funcclass LeNet(nn.Module):def __init__(self):super(LeNet, self).__init__()self.conv1  nn.Conv2d(3, 6, kernel_size5)self.conv2  nn.Conv2d(6, 16, kernel_size5)self.fc1  nn.Linear(16*5*5, 120)self.fc2  nn.Linear(120, 84)self.fc3  nn.Linear(84, 10)def forward(self, x):x  func.relu(self.conv1(x))x  func.max_pool2d(x, 2)x  func.relu(self.conv2(x))x  func.max_pool2d(x, 2)x  x.view(x.size(0), -1)x  func.relu(self.fc1(x))x  func.relu(self.fc2(x))x  self.fc3(x)return x 
训练模型 
1.导入数据 
导入训练数据和测试数据 def load_data(self):#transforms.RandomHorizontalFlip() 是 pytorch 中用来进行随机水平翻转的函数。它将以一定概率默认为0.5对输入的图像进行水平翻转并返回翻转后的图像。这可以用于数据增强使模型能够更好地泛化。train_transform  transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()])test_transform  transforms.Compose([transforms.ToTensor()])train_set  torchvision.datasets.CIFAR10(root./data, trainTrue, downloadTrue, transformtrain_transform)self.train_loader  torch.utils.data.DataLoader(datasettrain_set, batch_sizeself.train_batch_size, shuffleTrue)# shuffleTrue 表示在每次迭代时数据集都会被重新打乱。这可以防止模型在训练过程中过度拟合训练数据并提高模型的泛化能力。test_set  torchvision.datasets.CIFAR10(root./data, trainFalse, downloadTrue, transformtest_transform)self.test_loader  torch.utils.data.DataLoader(datasettest_set, batch_sizeself.test_batch_size, shuffleFalse) 
2.训练模型 def train(self):print(train:)self.model.train()train_loss  0train_correct  0total  0for batch_num, (data, target) in enumerate(self.train_loader):data, target  data.to(self.device), target.to(self.device)self.optimizer.zero_grad()output  self.model(data)loss  self.criterion(output, target)loss.backward()self.optimizer.step()train_loss  loss.item()prediction  torch.max(output, 1)  # second param 1 represents the dimension to be reducedtotal  target.size(0)# train_correct incremented by one if predicted righttrain_correct  np.sum(prediction[1].cpu().numpy()  target.cpu().numpy())progress_bar(batch_num, len(self.train_loader), Loss: %.4f | Acc: %.3f%% (%d/%d)% (train_loss / (batch_num  1), 100. * train_correct / total, train_correct, total))return train_loss, train_correct / total 
3.测试模型 def test(self):print(test:)self.model.eval()test_loss  0test_correct  0total  0with torch.no_grad():for batch_num, (data, target) in enumerate(self.test_loader):data, target  data.to(self.device), target.to(self.device)output  self.model(data)loss  self.criterion(output, target)test_loss  loss.item()prediction  torch.max(output, 1)total  target.size(0)test_correct  np.sum(prediction[1].cpu().numpy()  target.cpu().numpy())progress_bar(batch_num, len(self.test_loader), Loss: %.4f | Acc: %.3f%% (%d/%d)% (test_loss / (batch_num  1), 100. * test_correct / total, test_correct, total))return test_loss, test_correct / total 测试单张图片 
网上随便下载一个图片 然后使用图片编辑工具把图片设置为32x32大小 通过导入模型然后测试一下 
代码 
import torch
import cv2
import torch.nn.functional as F
#from model import Net  ##重要虽然显示灰色(即在次代码中没用到)但若没有引入这个模型代码加载模型时会找不到模型
from torch.autograd import Variable
from torchvision import datasets, transforms
import numpy as npclasses  (plane, car, bird, cat,deer, dog, frog, horse, ship, truck)
if __name__  __main__:device  torch.device(cuda if torch.cuda.is_available() else cpu)model  torch.load(lenet.pth)  # 加载模型model  model.to(device)model.eval()  # 把模型转为test模式img  cv2.imread(bird1.png)  # 读取要预测的图片trans  transforms.Compose([transforms.ToTensor()])img  trans(img)img  img.to(device)img  img.unsqueeze(0)  # 图片扩展多一维,因为输入到保存的模型中是4维的[batch_size,通道,长宽]而普通图片只有三维[通道,长宽]# 扩展后为[112828]output  model(img)prob  F.softmax(output,dim1) #prob是10个分类的概率print(prob)value, predicted  torch.max(output.data, 1)print(predicted.item())print(value)pred_class  classes[predicted.item()]print(pred_class) 
运行结果 
tensor([[1.8428e-01, 1.3935e-06, 7.8295e-01, 8.5042e-04, 3.0219e-06, 1.6916e-04,5.8798e-06, 3.1647e-02, 1.7037e-08, 8.9128e-05]], devicecuda:0,grad_fnSoftmaxBackward0)
2
tensor([4.0915], devicecuda:0)
bird 
从结果看效果还不错。记录一下  文章转载自: http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.wptdg.cn.gov.cn.wptdg.cn http://www.morning.crkmm.cn.gov.cn.crkmm.cn http://www.morning.cpqqf.cn.gov.cn.cpqqf.cn http://www.morning.phechi.com.gov.cn.phechi.com http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.zkqjz.cn.gov.cn.zkqjz.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.crkmm.cn.gov.cn.crkmm.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.spqbp.cn.gov.cn.spqbp.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.kycwt.cn.gov.cn.kycwt.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.qcfgd.cn.gov.cn.qcfgd.cn http://www.morning.wwjft.cn.gov.cn.wwjft.cn http://www.morning.crkhd.cn.gov.cn.crkhd.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.jhrlk.cn.gov.cn.jhrlk.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.rsdm.cn.gov.cn.rsdm.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.rtkz.cn.gov.cn.rtkz.cn http://www.morning.qmbpy.cn.gov.cn.qmbpy.cn http://www.morning.wptdg.cn.gov.cn.wptdg.cn http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.zcrjq.cn.gov.cn.zcrjq.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn http://www.morning.zgdnz.cn.gov.cn.zgdnz.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.wwsgl.com.gov.cn.wwsgl.com http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.dwmmf.cn.gov.cn.dwmmf.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.mmtbn.cn.gov.cn.mmtbn.cn http://www.morning.gbfck.cn.gov.cn.gbfck.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.rrwgh.cn.gov.cn.rrwgh.cn http://www.morning.hhxwr.cn.gov.cn.hhxwr.cn http://www.morning.swimstaracademy.cn.gov.cn.swimstaracademy.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.elmtw.cn.gov.cn.elmtw.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.zcnfm.cn.gov.cn.zcnfm.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn