案例演示微调:热狗识别
- 导入功能包
import os
import torch
import torchvision
import matplotlib.pyplot as plt
- 获取数据集
我们使用的热狗数据集(已上传博客资源)是从网上抓取的,它含有1400张包含热狗的正类图像,和同样多包含其他食品的负类图像。各类的1000张图像被用于训练,其余则用于测试。
data_dir = r"C:\Users\52xj\Desktop\pytorch\data\hotdog"
dir = os.listdir(os.path.join(data_dir, "train"))
# 加载训练图片
train_imgs = torchvision.datasets.ImageFolder(os.path.join(data_dir, 'train'))
# 加载测试图片
test_imgs = torchvision.datasets.ImageFolder(os.path.join(data_dir, 'test'))
- 显示前 8 个正面图片和最后 8 张负面图片
# 第一维表示第几张图片,第二维的0表示图片,1表示label
# 每个文件夹有hotdog', 'not-hotdog'两个文件,各有1000张图片,
# Hotdog 0~999;not-hotdog1000~1;
# 图片索引是0~1999,前者在前,后者在后
hotdogs = [train_imgs[i][0] for i in range(8)]
not_hotdogs = [train_imgs[-i - 1][0] for i in range(8)]
hotdogs_labels = [train_imgs[i][1] for i in range(8)]
not_hotdogs_labels = [train_imgs[-i-1][1] for i in range(8)]
def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5):
"""Plot a list of images."""
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):
# Tensor Image
ax.imshow(img.numpy())
else:
# PIL Image
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
images = hotdogs+not_hotdogs
title = hotdogs_labels + not_hotdogs_labels
labels = get_hotdog_labels(title)
show_images(images, 2,8, scale=1.4)
plt.show()
-
图片效果
-
添加标签
def get_hotdog_labels(labels):
"""Return text labels for the hotdog dataset."""
text_labels = ['hotdog', 'not-hotdog']
return [text_labels[int(i)] for i in labels]
images = hotdogs+not_hotdogs
title = hotdogs_labels + not_hotdogs_labels
labels = get_hotdog_labels(title)
show_images(images, 2,8, scale=1.4, titles=labels)
plt.show()
- 图片效果
训练部分
- 图像增广设置
# 使用三个RGB通道的均值和标准偏差,以标准化每个通道
normalize = torchvision.transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
# 图像增广方法
# 叠加多个图像增广方法
train_augs = torchvision.transforms.Compose([
# 随机高宽比剪裁
torchvision.transforms.RandomResizedCrop(224),
# 水平(左右)翻转
torchvision.transforms.RandomHorizontalFlip(),
# 先由HWC转置为CHW格式;
# 再转为float类型;
# 最后,每个像素除以255。
torchvision.transforms.ToTensor(),
# 标准化
normalize])
test_augs = torchvision.transforms.Compose([
# 对于PIL Image对象进行resize的运算。
torchvision.transforms.Resize(256),
# 在图片的中间区域进行裁剪
torchvision.transforms.CenterCrop(224),
torchvision.transforms.ToTensor(),
# 和预训练时作同样的预处理
normalize])
- 定义和初始化模型
# 定义和初始化模型
# 指定pretrained=True来自动下载并加载预训练的模型参数。
finetune_net = torchvision.models.resnet18(pretrained=True)
# 将最后的fc成修改我们需要的输出类别数
finetune_net.fc = nn.Linear(finetune_net.fc.in_features, 2)
nn.init.xavier_uniform_(finetune_net.fc.weight);
- 训练函数(同图像增广)
# 训练函数
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def train_batch_ch13(net, X, y, loss, trainer, device):
if isinstance(X, list):
# 微调BERT中所需
X = [x.to(device) for x in X]
else:
X = X.to(device)
y = y.to(device)
net.train()
trainer.zero_grad()
pred = net(X)
l = loss(pred, y)
l.sum().backward()
trainer.step()
train_loss_sum = l.sum()
train_acc_sum = MF.accuracy(pred, y)
return train_loss_sum, train_acc_sum
def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,device=device):
print("Start Training...")
nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print("==========" * 8 + "%s" % nowtime)
print(f'training on {device}:{torch.cuda.get_device_name()}')
net.to(device)
timer = MF.Timer()
for epoch in range(num_epochs):
train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
start = time.time()
with tqdm(train_iter) as t:
for features, labels in t:
timer.start()
l, acc = train_batch_ch13(net, features, labels, loss, trainer,
device=device)
n += labels.shape[0]
train_l_sum += l
train_acc_sum += acc
train_l = train_l_sum / n
train_acc = train_acc_sum / n
timer.stop()
# 设置进度条左边显示的信息
t.set_description(f"epoch:{epoch+1}/{num_epochs}")
# 设置进度条右边显示的信息
t.set_postfix(loss="%.3f" % train_l, train_acc="%.3f" % train_acc, epoch_time="%.3f sec" % (time.time() - start))
# 保存模型参数
torch.save(net.state_dict(), "C:/Users/52xj/Desktop/pytorch/data/finetuning-%d.pth" %(epoch))
test_acc = MF.evaluate_accuracy_ch13(net, test_iter)
print(f'epoch:{epoch+1},loss {train_l:.3f}, train_acc {train_acc:.3f}, test_acc {test_acc:.3f}')
print(f'{n * num_epochs / timer.sum():.1f} examples/sec on {str(device)}')
nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print("==========" * 8 + "%s" % nowtime)
print('Finished Training...')
- 微调模型
# 微调模型
# 如果 `param_group=True`,输出层中的模型参数将使用十倍的学习率
def train_fine_tuning(net, learning_rate, batch_size=128, num_epochs=5,
param_group=True):
train_iter = torch.utils.data.DataLoader(
torchvision.datasets.ImageFolder(os.path.join(data_dir, 'train'),
transform=train_augs),
batch_size=batch_size, shuffle=True)
test_iter = torch.utils.data.DataLoader(
torchvision.datasets.ImageFolder(os.path.join(data_dir, 'test'),
transform=test_augs),
batch_size=batch_size)
# reduction="none" 表示保留每一个样本的loss, 默认reduction="mean"
loss = nn.CrossEntropyLoss(reduction="none")
# 对模型的不同部分设置不同的学习参数
if param_group:
# 过滤出所有模型参数中不属于全连接层中的参数元素
params_1x = [param for name, param in net.named_parameters()
if name not in ["fc.weight", "fc.bias"]]
# 对模型的不同部分设置不同的学习参数
trainer = torch.optim.SGD([{'params': params_1x},
{'params': net.fc.parameters(),'lr': learning_rate * 10}],
lr=learning_rate,
weight_decay=0.001)
else: # 使用统一的优化器学习参数
trainer = torch.optim.SGD(net.parameters(), lr=learning_rate,
weight_decay=0.001)
train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, device=device)
- 使用较小的学习率,通过微调预训练获得的模型参数
# 使用较小的学习率,通过微调预训练获得的模型参数。
train_fine_tuning(finetune_net, 5e-5)
- 训练结果