声明
本文章为个人学习使用,版面观感若有不适请谅解,文中知识仅代表个人观点,若出现错误,欢迎各位批评指正。
八、图像分类及数据集的获取
- 图像分类及可视化
import torch
import time
import torchvision
import matplotlib.pyplot as plt
from torch.utils import data
from torchvision import transforms
from matplotlib_inline import backend_inline
################ 图像分类数据集的下载及可视化处理 ################
class Timer:
def __init__(self):
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]
backend_inline.set_matplotlib_formats('svg')
trans = transforms.ToTensor() # 通过 ToTensor 实例将图像数据从 PIL 类型变换成32位浮点数格式
mnist_train = torchvision.datasets.FashionMNIST( # 通过内置函数下载数据集,由于本人已提前下好,故 download 为 False
root="../data", train=True, transform=trans, download=False)
mnist_test = torchvision.datasets.FashionMNIST(
root="../data", train=False, transform=trans, download=False)
print("训练集数量:", len(mnist_train), "测试集数量:", len(mnist_test))
print("训练集中第一个数据尺寸:", mnist_train[0][0].shape) # 通道数为 1,长宽分别为 28
def get_fashion_mnist_labels(labels): # 设置 Fashion-MNIST 数据集的文本标签
text_labels = ['T恤', '裤子', '套衫', '连衣裙', '外套',
'凉鞋', '衬衫', '运动鞋', '包', '短靴']
return [text_labels[int(i)] for i in labels]
def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5): # 通过数据集绘制图像
plt.rcParams['font.sans-serif'] = ['SimSun']
figsize = (num_cols * scale, num_rows * scale)
_, axes = plt.subplots(num_rows, num_cols, figsize=figsize)
axes = axes.flatten()
for i, (ax, img) in enumerate(zip(axes, imgs)):
if torch.is_tensor(img):
# 图片张量
ax.imshow(img.numpy())
else:
# PIL图片
ax.imshow(img)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
if titles:
ax.set_title(titles[i])
return axes
X, y = next(iter(data.DataLoader(mnist_train, batch_size=9))) # 利用 next 函数拿到下一个数据,总数为 9
show_images(X.reshape(9, 28, 28), 3, 3, titles=get_fashion_mnist_labels(y)) # 显示 3 * 3 的数据集
plt.show()
batch_size = 256 # 设置小批量为 256
def get_dataloader_workers():
"""使用 4 个进程来读取数据"""
return 4
train_iter = data.DataLoader(mnist_train, batch_size, shuffle=True, # 通过内置数据迭代器,随机打乱所有样本,并读取小批量
num_workers=get_dataloader_workers())
timer = Timer() # 获取当前时间
for X, y in train_iter: # 遍历训练集,并将训练集数据放入 GPU
X = X.cuda()
y = y.cuda()
continue
print(f'遍历一遍训练集所需时间:{timer.stop():.2f} sec')
def 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=False)
mnist_test = torchvision.datasets.FashionMNIST(
root="../data", train=False, transform=trans, download=False)
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()))
train_iter, test_iter = load_data_fashion_mnist(32, resize=64) # 测试定义的函数图像大小调整功能
for X, y in train_iter:
print("X.shape:", X.shape, "\nX.dtype:", X.dtype, "\ny.shape:", y.shape, "\ny.dtype:", y.dtype)
break
- softmax 回归的从零开始实现
import torch
import torchvision
from torch.utils import data
from torchvision import transforms
from IPython import display
import matplotlib.pyplot as plt
from matplotlib_inline import backend_inline
################ softmax 回归的从零开始实现 ################
def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5):
numpy = lambda x, *args, **kwargs: x.detach().numpy(*args, **kwargs)
figsize = (num_cols * scale, num_rows * scale)
_, axes = plt.subplots(num_rows, num_cols, figsize=figsize)
axes = axes.flatten()
for i, (ax, img) in enumerate(zip(axes, imgs)):
try:
img = numpy(img)
except:
pass
ax.imshow(img)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
if titles:
ax.set_title(titles[i])
return axes
def get_dataloader_workers():
"""使用 4 个进程来读取数据"""
return 4
def get_fashion_mnist_labels_chinese(labels): # 设置 Fashion-MNIST 数据集的文本标签
text_labels = ['T恤', '裤子', '套衫', '连衣裙', '外套',
'凉鞋', '衬衫', '运动鞋', '包', '短靴']
return [text_labels[int(i)] for i in labels]
def get_fashion_mnist_labels_english(labels):
text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat',
'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
return [text_labels[int(i)] for i in labels]
def 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=False)
mnist_test = torchvision.datasets.FashionMNIST(
root="../data", train=False, transform=trans, download=False)
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()))
batch_size = 256 # 设置批量大小为 256
train_iter, test_iter = load_data_fashion_mnist(batch_size) # 引入数据集,此处函数为图像分类及可视化中定义函数
num_inputs = 784 # 设置输入大小为 28*28=784
num_outputs = 10 # 设置输出大小为 10(10个类别)
W = torch.normal(0, 0.01, size=(num_inputs, num_outputs)) # 初始化参数 W
b = torch.zeros(num_outputs) # 初始化参数 b
W, b = W.cuda(), b.cuda() # 将参数 W, b 放入 GPU
W.requires_grad_(True) # 让 backward 可以追踪这个参数并且计算它的梯度
b.requires_grad_(True) # 让 backward 可以追踪这个参数并且计算它的梯度
def softmax(X): # 定义 Softmax 函数
X_exp = torch.exp(X).cuda()
partition = X_exp.sum(1, keepdim=True).cuda()
return X_exp / partition # 这里应用了广播机制
def net(X): # 实现 Softmax 回归模型
return softmax(torch.matmul(X.reshape((-1, W.shape[0])), W) + b) # y = Xw + b
def cross_entropy(y_hat, y): # 定义交叉熵损失函数
return - torch.log(y_hat[range(len(y_hat)), y]) # y = - [ yln(y_hat) + (1-y)ln(1-y_hat) ]
def accuracy(y_hat, y): # 定义一个函数来为预测正确的数量计数
"""计算预测正确的数量"""
if len(y_hat.shape) > 1 and y_hat.shape[1] > 1:
y_hat = y_hat.argmax(axis=1)
cmp = y_hat.type(y.dtype) == y # bool 类型,若预测结果与实际结果一致,则为 True
return float(cmp.type(y.dtype).sum())
def evaluate_accuracy(net, data_iter): # 定义一个函数来计算模型的精度
"""计算在指定数据集上模型的精度"""
if isinstance(net, torch.nn.Module):
net.eval().cuda() # 将模型设置为评估模式
metric = Accumulator(2) # 正确预测数、预测总数
with torch.no_grad():
for X, y in data_iter:
X, y = X.cuda(), y.cuda()
metric.add(accuracy(net(X), y), y.numel())
return metric[0] / metric[1]
class Accumulator: # 定义一个实用程序类 Accumulator,用于对多个变量进行累加
"""在n个变量上累加"""
def __init__(self, n):
self.data = [0.0] * n
def 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 train_epoch(net, train_iter, loss, updater): # 定义一个函数来训练一个迭代周期
"""训练模型一个迭代周期"""
if isinstance(net, torch.nn.Module):
net.train().cuda() # 将模型设置为训练模式
metric = Accumulator(3) # 训练损失总和、训练准确度总和、样本数
for X, y in train_iter: # 计算梯度并更新参数
X, y = X.cuda(), y.cuda()
y_hat = net(X)
l = loss(y_hat, y)
if isinstance(updater, torch.optim.Optimizer): # 使用PyTorch内置的优化器和损失函数
updater.zero_grad()
l.mean().backward()
updater.step()
else: # 使用定制的优化器和损失函数
l.sum().backward()
updater(X.shape[0])
metric.add(float(l.sum()), accuracy(y_hat, y), y.numel())
return metric[0] / metric[2], metric[1] / metric[2] # 返回训练损失和训练精度
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
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()
class Animator: # 定义一个在动画中绘制数据的实用程序类 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)):
# 增量地绘制多条线
if legend is None:
legend = []
backend_inline.set_matplotlib_formats('svg')
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, fmts
def add(self, x, y):
# 向图表中添加多个数据点
if not hasattr(y, "__len__"):
y = [y]
n = len(y)
if not hasattr(x, "__len__"):
x = [x] * n
if 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)
# 通过以下两行代码实现了在PyCharm中显示动图
plt.draw()
plt.pause(interval=0.001)
display.clear_output(wait=True)
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
def train(net, train_iter, test_iter, loss, num_epochs, updater): # 定义一个训练函数
"""训练模型"""
animator = Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0.3, 0.9],
legend=['train loss', 'train acc', 'test acc'])
for epoch in range(num_epochs): # 该训练函数将会运行 num_epochs 个迭代周期
train_metrics = train_epoch(net, train_iter, loss, updater)
test_acc = evaluate_accuracy(net, test_iter)
animator.add(epoch + 1, train_metrics + (test_acc,))
train_loss, train_acc = train_metrics
assert train_loss < 0.5, train_loss
assert train_acc <= 1 and train_acc > 0.7, train_acc
assert test_acc <= 1 and test_acc > 0.7, test_acc
# 定义优化算法
def sgd(params, lr, batch_size): # 给定参数,学习率以及每批量尺寸
with torch.no_grad(): # 不需计算梯度
for param in params: # 对样本中每个参数更新
param -= lr * param.grad / batch_size # param = param - lr * param.grad / batch_size,最后一个批量可能总数不满足 batch_size,但此例仅做演示,不做深层次研究
param.grad.zero_() # 计算梯度后需要归零,否则内存会遵循递增原则,影响下一次数据更新
lr = 0.1 # 设置学习率为0.1
def updater(batch_size): # 使用定义好的优化算法来优化模型的损失函数
return sgd([W, b], lr, batch_size)
num_epochs = 15 # 设定训练模型的迭代周期为 15
train(net, train_iter, test_iter, cross_entropy, num_epochs, updater) # 调用训练函数
plt.show()
def predict(net, test_iter, n=9): # 定义一个预测函数,预测 n=9 个数据
"""预测标签"""
for X, y in test_iter: # 获取测试集数据并存入 GPU
X, y = X.cuda(), y.cuda()
break
trues = get_fashion_mnist_labels_chinese(y) # 真实标签采用宋体
preds = get_fashion_mnist_labels_english(net(X).argmax(axis=1)) # 预测标签采用英文
titles = [true +'\n' + pred for true, pred in zip(trues, preds)]
for X, y in test_iter:
X = X.cpu() # 由于 numpy 不能处理 CUDA tensor,先将数据传回 CPU
break
show_images(
X[0:n].reshape((n, 28, 28)), 3, 3, titles=titles[0:n]) # 展现数据,3 行,3 列,由于此处仅为示例,故直接写为定值
predict(net, test_iter) # 调用预测函数
plt.show() # 为方便区分,预测标签为英文,真实标签为中文
- softmax 回归的简洁实现
import torch
import torchvision
from torch import nn
from torch.utils import data
from torchvision import transforms
from IPython import display
import matplotlib.pyplot as plt
from matplotlib_inline import backend_inline
################ softmax 回归的简洁实现 ################
def get_dataloader_workers():
"""使用 4 个进程来读取数据"""
return 4
def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5):
numpy = lambda x, *args, **kwargs: x.detach().numpy(*args, **kwargs)
figsize = (num_cols * scale, num_rows * scale)
_, axes = plt.subplots(num_rows, num_cols, figsize=figsize)
axes = axes.flatten()
for i, (ax, img) in enumerate(zip(axes, imgs)):
try:
img = numpy(img)
except:
pass
ax.imshow(img)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
if titles:
ax.set_title(titles[i])
return axes
def get_fashion_mnist_labels_chinese(labels): # 设置 Fashion-MNIST 数据集的文本标签
text_labels = ['T恤', '裤子', '套衫', '连衣裙', '外套',
'凉鞋', '衬衫', '运动鞋', '包', '短靴']
return [text_labels[int(i)] for i in labels]
def get_fashion_mnist_labels_english(labels):
text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat',
'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
return [text_labels[int(i)] for i in labels]
def 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=False)
mnist_test = torchvision.datasets.FashionMNIST(
root="../data", train=False, transform=trans, download=False)
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()))
batch_size = 256 # 设置批量大小为 256
train_iter, test_iter = load_data_fashion_mnist(batch_size) # 引入数据集,此处函数为图像分类及可视化中定义函数
def accuracy(y_hat, y): # 定义一个函数来为预测正确的数量计数
"""计算预测正确的数量"""
if len(y_hat.shape) > 1 and y_hat.shape[1] > 1:
y_hat = y_hat.argmax(axis=1)
cmp = y_hat.type(y.dtype) == y # bool 类型,若预测结果与实际结果一致,则为 True
return float(cmp.type(y.dtype).sum())
def evaluate_accuracy(net, data_iter): # 定义一个函数来计算模型的精度
"""计算在指定数据集上模型的精度"""
if isinstance(net, torch.nn.Module):
net.eval().cuda() # 将模型设置为评估模式
metric = Accumulator(2) # 正确预测数、预测总数
with torch.no_grad():
for X, y in data_iter:
X, y = X.cuda(), y.cuda()
metric.add(accuracy(net(X), y), y.numel())
return metric[0] / metric[1]
class Accumulator: # 定义一个实用程序类 Accumulator,用于对多个变量进行累加
"""在n个变量上累加"""
def __init__(self, n):
self.data = [0.0] * n
def 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 train_epoch(net, train_iter, loss, updater): # 定义一个函数来训练一个迭代周期
"""训练模型一个迭代周期"""
if isinstance(net, torch.nn.Module):
net.train().cuda() # 将模型设置为训练模式
metric = Accumulator(3) # 训练损失总和、训练准确度总和、样本数
for X, y in train_iter: # 计算梯度并更新参数
X, y = X.cuda(), y.cuda()
y_hat = net(X)
l = loss(y_hat, y)
if isinstance(updater, torch.optim.Optimizer): # 使用PyTorch内置的优化器和损失函数
updater.zero_grad()
l.mean().backward()
updater.step()
else: # 使用定制的优化器和损失函数
l.sum().backward()
updater(X.shape[0])
metric.add(float(l.sum()), accuracy(y_hat, y), y.numel())
return metric[0] / metric[2], metric[1] / metric[2] # 返回训练损失和训练精度
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
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()
class Animator: # 定义一个在动画中绘制数据的实用程序类 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)):
# 增量地绘制多条线
if legend is None:
legend = []
backend_inline.set_matplotlib_formats('svg')
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, fmts
def add(self, x, y):
# 向图表中添加多个数据点
if not hasattr(y, "__len__"):
y = [y]
n = len(y)
if not hasattr(x, "__len__"):
x = [x] * n
if 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)
# 通过以下两行代码实现了在PyCharm中显示动图
plt.draw()
plt.pause(interval=0.001)
display.clear_output(wait=True)
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
def train(net, train_iter, test_iter, loss, num_epochs, updater): # 定义一个训练函数
"""训练模型"""
animator = Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0.3, 0.9],
legend=['train loss', 'train acc', 'test acc'])
for epoch in range(num_epochs): # 该训练函数将会运行 num_epochs 个迭代周期
train_metrics = train_epoch(net, train_iter, loss, updater)
test_acc = evaluate_accuracy(net, test_iter)
animator.add(epoch + 1, train_metrics + (test_acc,))
train_loss, train_acc = train_metrics
assert train_loss < 0.5, train_loss
assert train_acc <= 1 and train_acc > 0.7, train_acc
assert test_acc <= 1 and test_acc > 0.7, test_acc
# PyTorch 不会隐式地调整输入的形状,因此在线性层前定义了展平层(flatten),来调整网络输入的形状
net = nn.Sequential(nn.Flatten(), nn.Linear(784, 10)).cuda() # 在 Sequential 中添加一个带有 10 个输出的全连接层
def init_weights(m): # 以均值 0 和标准差 0.01 随机初始化权重
if type(m) == nn.Linear:
nn.init.normal_(m.weight, std=0.01).cuda()
net.apply(init_weights).cuda()
# 定义损失函数,在交叉熵损失函数中传递未规范化的预测,并同时计算softmax及其对数,这是一种类似“LogSumExp 技巧”的方式
loss = nn.CrossEntropyLoss(reduction='none').cuda()
# 使用学习率为 0.1 的小批量随机梯度下降作为优化算法
trainer = torch.optim.SGD(net.parameters(), lr=0.1)
num_epochs = 5 # 设定训练模型的迭代周期为 5
train(net, train_iter, test_iter, loss, num_epochs, trainer) # 调用训练函数
plt.show()
def predict(net, test_iter, n=9): # 定义一个预测函数,预测 n=9 个数据
"""预测标签"""
for X, y in test_iter: # 获取测试集数据并存入 GPU
X, y = X.cuda(), y.cuda()
break
trues = get_fashion_mnist_labels_chinese(y) # 真实标签采用中文
preds = get_fashion_mnist_labels_english(net(X).argmax(axis=1)) # 预测标签采用英文
titles = [true +'\n' + pred for true, pred in zip(trues, preds)]
for X, y in test_iter:
X = X.cpu() # 由于 numpy 不能处理 CUDA tensor,先将数据传回 CPU
break
show_images(X[0:n].reshape((n, 28, 28)), 3, 3, titles=titles[0:n]) # 展现数据,3 行,3 列,由于此处仅为示例,故直接写为定值
predict(net, test_iter) # 调用预测函数
plt.show() # 为方便区分,预测标签为英文,真实标签为中文
文中部分知识参考:B 站 —— 跟李沐学AI;百度百科