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

Fashion-MNIST LeNet训练

前面使用线性神经网络softmax 和  多层感知机进行图像分类,本次我们使用LeNet 卷积神经网络进行

训练,期望能捕捉到图像中的图像结构信息,提高识别精度:

import torch
import torchvision
from torchvision import transforms
from torch.utils import data
import time
from torch import nn
from matplotlib import pyplot as plt
from matplotlib_inline import backend_inline
from IPython import displaysize = lambda x, *args, **kwargs: x.numel(*args, **kwargs)
reduce_sum = lambda x, *args, **kwargs: x.sum(*args, **kwargs)
argmax = lambda x, *args, **kwargs: x.argmax(*args, **kwargs)
astype = lambda x, *args, **kwargs: x.type(*args, **kwargs)class Timer:"""记录多次运行时间"""def __init__(self):"""Defined in :numref:`subsec_linear_model`"""self.times = []self.start()def start(self):"""启动计时器"""self.tik = time.time()def stop(self):"""停止计时器并将时间记录在列表中"""self.times.append(time.time() - self.tik)return self.times[-1]def avg(self):"""返回平均时间"""return sum(self.times) / len(self.times)def sum(self):"""返回时间总和"""return sum(self.times)def cumsum(self):"""返回累计时间"""return np.array(self.times).cumsum().tolist()def accuracy(y_hat, y):"""计算预测正确的数量"""if len(y_hat.shape) > 1 and y_hat.shape[1] > 1:y_hat = argmax(y_hat, axis=1)cmp = astype(y_hat, y.dtype) == yreturn float(reduce_sum(astype(cmp, y.dtype)).cpu())def evaluate_accuracy(net, data_iter, device=None):"""计算在指定数据集上模型的精度"""metric = Accumulator(2)  # 正确预测数、预测总数net.eval()with torch.no_grad():for X, y in data_iter:X, y = X.to(device), y.to(device)metric.add(accuracy(net(X), y), size(y))return metric[0] / metric[1]def evaluate_accuracy_gpu(net, data_iter, device=None):"""计算在指定数据集上模型的精度"""if isinstance(net, nn.Module):net.eval()if not device:device = next(iter(net.parameters())).devicemetric = Accumulator(2)  # 正确预测数、预测总数net.eval()with torch.no_grad():for X, y in data_iter:if isinstance(X, list):X = [x.to(device) for x in X]else:X = X.to(device)y = y.to(device)metric.add(accuracy(net(X), y), y.numel())return metric[0] / metric[1]
def use_svg_display():"""使用svg格式在Jupyter中显示绘图Defined in :numref:`sec_calculus`"""backend_inline.set_matplotlib_formats('svg')def set_figsize(figsize=(3.5, 2.5)):"""设置matplotlib的图表大小Defined in :numref:`sec_calculus`"""use_svg_display()plt.rcParams['figure.figsize'] = figsizedef set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):"""设置matplotlib的轴Defined in :numref:`sec_calculus`"""axes.set_xlabel(xlabel)axes.set_ylabel(ylabel)axes.set_xscale(xscale)axes.set_yscale(yscale)axes.set_xlim(xlim)axes.set_ylim(ylim)if legend:axes.legend(legend)axes.grid()def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,ylim=None, xscale='linear', yscale='linear',fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None):"""绘制数据点Defined in :numref:`sec_calculus`"""if legend is None:legend = []set_figsize(figsize)axes = axes if axes else plt.gca()# 如果X有一个轴,输出Truedef has_one_axis(X):return (hasattr(X, "ndim") and X.ndim == 1 or isinstance(X, list)and not hasattr(X[0], "__len__"))if has_one_axis(X):X = [X]if Y is None:X, Y = [[]] * len(X), Xelif has_one_axis(Y):Y = [Y]if len(X) != len(Y):X = X * len(Y)axes.cla()for x, y, fmt in zip(X, Y, fmts):if len(x):axes.plot(x, y, fmt)else:axes.plot(y, fmt)set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)class Animator:"""在动画中绘制数据"""def __init__(self, xlabel=None, ylabel=None, legend=None, xlim=None,ylim=None, xscale='linear', yscale='linear',fmts=('-', 'm--', 'g-.', 'r:'), nrows=1, ncols=1,figsize=(3.5, 2.5)):"""Defined in :numref:`sec_softmax_scratch`"""# 增量地绘制多条线if legend is None:legend = []use_svg_display()self.fig, self.axes = plt.subplots(nrows, ncols, figsize=figsize)if nrows * ncols == 1:self.axes = [self.axes, ]# 使用lambda函数捕获参数self.config_axes = lambda: set_axes(self.axes[0], xlabel, ylabel, xlim, ylim, xscale, yscale, legend)self.X, self.Y, self.fmts = None, None, fmtsdef add(self, x, y):# 向图表中添加多个数据点if not hasattr(y, "__len__"):y = [y]n = len(y)if not hasattr(x, "__len__"):x = [x] * nif not self.X:self.X = [[] for _ in range(n)]if not self.Y:self.Y = [[] for _ in range(n)]for i, (a, b) in enumerate(zip(x, y)):if a is not None and b is not None:self.X[i].append(a)self.Y[i].append(b)self.axes[0].cla()for x, y, fmt in zip(self.X, self.Y, self.fmts):self.axes[0].plot(x, y, fmt)self.config_axes()display.display(self.fig)display.clear_output(wait=True)class Accumulator:"""在n个变量上累加"""def __init__(self, n):self.data = [0.0] * ndef add(self, *args):self.data = [a + float(b) for a, b in zip(self.data, args)]def reset(self):self.data = [0.0] * len(self.data)def __getitem__(self, idx):return self.data[idx]def get_dataloader_workers():return 4def load_data_fashion_mnist(batch_size, resize=None):"""下载Fashion-MNIST数据集,然后将其加载到内存中"""trans = [transforms.ToTensor()]if resize:trans.insert(0, transforms.Resize(resize))trans = transforms.Compose(trans)mnist_train = torchvision.datasets.FashionMNIST(root="../data", train=True, transform=trans, download=True)mnist_test = torchvision.datasets.FashionMNIST(root="../data", train=False, transform=trans, download=True)return (data.DataLoader(mnist_train, batch_size, shuffle=True,num_workers=get_dataloader_workers()),data.DataLoader(mnist_test, batch_size, shuffle=False,num_workers=get_dataloader_workers()))def train_ch6(net, train_iter, test_iter, num_epochs, lr, device):def init_weight(m):if type(m) == nn.Linear or type(m) == nn.Conv2d:nn.init.xavier_uniform_(m.weight)net.apply(init_weight)print('training on', device)net.to(device)optimizer = torch.optim.SGD(net.parameters(), lr=lr)loss = nn.CrossEntropyLoss()animator = Animator(xlabel='epoch',xlim=[1, num_epochs],legend=['train loss', 'train acc', 'test_acc'])timer, num_batches = Timer(), len(train_iter)for epoch in range(num_epochs):metric = Accumulator(3)net.train()for i, (X, y) in enumerate(train_iter):timer.start()optimizer.zero_grad()X, y = X.to(device), y.to(device)y_hat = net(X)l = loss(y_hat, y)l.backward()optimizer.step()with torch.no_grad():metric.add(l * X.shape[0], accuracy(y_hat, y),X.shape[0])timer.stop()train_l = metric[0] / metric[2]train_acc = metric[1] / metric[2]if (i + 1) % (num_batches //5) == 0 or i == num_batches - 1:animator.add(epoch + (i + 1) / num_batches, (train_l, train_acc, None))test_acc = evaluate_accuracy_gpu(net, test_iter)animator.add(epoch + 1, (None, None, test_acc))print(f'epoch {epoch + 1}, train_l={train_l:.5f}, test_acc={test_acc:.5f}')print(f'loss {train_l:.3f}, train acc {train_acc:.3f}, test acc {test_acc:.3f}')print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on {str(device)}')def try_gpu(): if torch.backends.mps.is_available():return torch.device("mps")elif torch.cuda.is_available():return torch.device("cuda")else:return torch.device("cpu")device = try_gpu()net = nn.Sequential(nn.Conv2d(1, 6, kernel_size=5, padding=2), nn.Sigmoid(),nn.AvgPool2d(kernel_size=2, stride=2),nn.Conv2d(6, 16, kernel_size=5), nn.Sigmoid(),nn.AvgPool2d(kernel_size=2, stride=2),nn.Flatten(),nn.Linear(16 * 5 * 5, 120), nn.Sigmoid(),nn.Linear(120, 84), nn.Sigmoid(),nn.Linear(84, 10)
)lr, num_epochs = 0.9, 10
batch_size = 256
train_iter, test_iter = load_data_fashion_mnist(batch_size)
timer = Timer()
train_ch6(net, train_iter, test_iter, num_epochs, lr, try_gpu())
print(f'train takes {timer.stop():.2f} sec')

结果如下:

epoch 10, train_l=0.49363, test_acc=0.80840
loss 0.494, train acc 0.812, test acc 0.808
30582.1 examples/sec on mps
train takes 65.80 sec

可以看到其准确率并不比线性模型和多层感知机更高。如果想进一步提高准确率,需进一步调整LeNet的参数,如学习率,学习批次,训练次数等,大家自己尝试一下。经过测试,学习率越低,似乎效果更差一些。

相关文章:

  • 个人用户进行LLMs本地部署前如何自查和筛选
  • PHY6222 基本文件操作
  • 2023ICPC杭州题解
  • 设计模式——组合设计模式(结构型)
  • Spring如何实现组件扫描与@Component注解原理
  • 【Hexo】4.Hexo 博客文章进行加密
  • ArcGIS Pro 创建渔网格网过大,只有几个格网的解决方案
  • 智能制造之精读——RPA制造行业常见场景【附全文阅读】
  • STM32F407寄存器操作(ADC非连续扫描模式)
  • python打卡day42@浙大疏锦行
  • Adobe LiveCycle ES、LiveCycle DS 与 BlazeDS 关系解析与比较
  • java ExecutorService线程池使用(ExecutorService/Completable异步+ExecutorService线程池)
  • MATLAB实战:人脸检测与识别实现方案
  • vue3动态路由的实现以及目录权限的设置
  • 湖北理元理律师事务所:个人债务管理的温度与精度
  • C++输入与输出技术详解
  • LeetCode 热题 100 208. 实现 Trie (前缀树)
  • 机器学习算法-逻辑回归
  • 【计算机系统结构】习题2
  • Python进阶与常用库:探索高效编程的奥秘
  • wordpress 改 名字/seo优化工具软件
  • 寄生虫网站怎么做/自动优化app
  • 中科汇联网站建设手册/厦门网站关键词推广
  • 网站架构设计图/互联网推广中心
  • ps做任务挣钱的网站/免费加精准客源
  • 晟阳建设官方网站/企业员工培训总结