一、项目介绍
在计算机视觉领域,图像分类是一项基础且重要的任务。本文将详细介绍如何使用预训练的Inception-V3模型在CIFAR-100数据集上进行迁移学习,实现高精度的图像分类。
1.1 项目背景
CIFAR-100是一个经典的图像分类数据集,包含100个类别,每个类别有600张32×32像素的彩色图像。虽然图像分辨率较低,但类别多样性强,是一个具有挑战性的基准测试集。
1.2 技术选型
我们选择Inception-V3作为基础模型,这是Google在2015年提出的深度学习架构,具有以下优势:
-
采用了Inception模块,能够高效提取多尺度特征
-
引入了辅助分类器,有助于梯度传播和模型收敛
-
在ImageNet等大型数据集上预训练,具有良好的特征提取能力
1.3 迁移学习策略
由于CIFAR-100数据规模相对较小,我们采用迁移学习的方法:
-
使用ImageNet预训练的权重初始化模型
-
替换最后几层以适应100类分类任务
-
微调整个网络以学习CIFAR-100特定的特征
二、环境配置与数据准备
2.1 环境依赖
python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import torchvision.models as models
import numpy as np
from tqdm import tqdm
import time
2.2 数据集预处理
python
def prepare_data(batch_size=128, num_workers=2):
# CIFAR-100的标准化参数(与CIFAR-10不同)
transform_train = transforms.Compose([
transforms.Resize(299), # Inception-V3要求输入为299x299
transforms.RandomCrop(299, padding=32), # 数据增强
transforms.RandomHorizontalFlip(), # 随机水平翻转
transforms.RandomRotation(15), # 随机旋转
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408),
(0.2675, 0.2565, 0.2761)),
])
# 测试集只进行必要的转换
transform_test = transforms.Compose([
transforms.Resize(299),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408),
(0.2675, 0.2565, 0.2761)),
])
关键点说明:
-
将32×32的图像上采样到299×299以适应Inception-V3
-
训练时使用多种数据增强技术防止过拟合
-
使用CIFAR-100特定的均值和标准差进行归一化
三、模型架构与修改
3.1 加载预训练模型
python
def load_pretrained_model(num_classes=100):
# 加载预训练模型,启用辅助分类器
model = models.inception_v3(
weights='Inception_V3_Weights.DEFAULT',
aux_logits=True
)
# 修改辅助分类器
model.AuxLogits.fc = nn.Linear(768, num_classes)
# 修改主分类器
model.fc = nn.Linear(2048, num_classes)
return model
3.2 Inception-V3的特殊性
Inception-V3在训练时有两个输出分支:
-
主分类器:最终的分类结果
-
辅助分类器:中间层的分类输出,帮助梯度传播
在实现时需要特别注意:
python
# 训练时同时使用两个输出
outputs, aux_outputs = model(inputs)
loss1 = criterion(outputs, targets)
loss2 = criterion(aux_outputs, targets)
loss = loss1 + 0.3 * loss2 # 根据原论文设置权重
# 测试时只使用主输出
outputs = model(inputs)
四、训练策略
4.1 优化器配置
python
# 使用SGD优化器,适合大规模分类任务
optimizer = optim.SGD(model.parameters(),
lr=0.001,
momentum=0.9,
weight_decay=5e-4)
# 余弦退火学习率调度
scheduler = optim.lr_scheduler.CosineAnnealingLR(
optimizer,
T_max=num_epochs
)
4.2 训练循环设计
python
def train_epoch(model, device, train_loader, optimizer, criterion, epoch):
model.train()
train_loss = 0
correct = 0
total = 0
# 使用tqdm显示进度
pbar = tqdm(train_loader, desc=f'Epoch {epoch}: Training')
for batch_idx, (inputs, targets) in enumerate(pbar):
# 前向传播
outputs, aux_outputs = model(inputs)
# 计算复合损失
loss1 = criterion(outputs, targets)
loss2 = criterion(aux_outputs, targets)
loss = loss1 + 0.3 * loss2 # 辅助损失权重0.3
# 反向传播
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 实时显示训练指标
pbar.set_postfix({
'Loss': f'{train_loss/(batch_idx+1):.3f}',
'Acc': f'{100.*correct/total:.2f}%'
})
五、实验结果与分析
5.1 训练性能
运行30个epoch后,模型在CIFAR-100上通常能达到以下性能:
-
训练准确率:75-80%
-
测试准确率:70-75%
-
最佳模型保存:自动保存测试集上表现最好的模型
5.2 关键训练技巧
-
学习率调度:
-
使用余弦退火策略,学习率从初始值平滑下降到0
-
避免了学习率突然下降导致的训练不稳定
-
-
数据增强:
-
随机裁剪和水平翻转提升模型泛化能力
-
随机旋转增强模型对方向变化的鲁棒性
-
-
损失函数设计:
-
主损失和辅助损失结合,加速收敛
-
交叉熵损失适合多分类任务
-
5.3 可视化训练过程
python
# 绘制训练曲线
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
# 损失曲线
ax1.plot(history['train_loss'], label='Train Loss')
ax1.set_xlabel('Epoch')
ax1.set_ylabel('Loss')
# 准确率曲线
ax2.plot(history['train_acc'], label='Train Acc')
ax2.plot(history['test_acc'], label='Test Acc')
ax2.set_xlabel('Epoch')
ax2.set_ylabel('Accuracy (%)')
六、推理与应用
6.1 单图像推理
python
def inference(model, device, image_tensor):
model.eval()
# 添加batch维度
if len(image_tensor.shape) == 3:
image_tensor = image_tensor.unsqueeze(0)
with torch.no_grad():
output = model(image_tensor)
probabilities = torch.nn.functional.softmax(output, dim=1)
confidence, predicted = torch.max(probabilities, 1)
return predicted.item(), confidence.item()
6.2 模型部署
-
模型量化:可考虑使用PyTorch的量化功能减小模型大小
-
ONNX导出:转换为ONNX格式便于跨平台部署
-
TensorRT优化:NVIDIA GPU上可使用TensorRT加速推理
6.3 运行结果展示
(mlstat) ➜ cf100 git:(master) ✗ python cf100.py
CUDA is available. GPU: NVIDIA A10
Memory allocated: 0.00 MB
Using device: cuda
Loading pre-trained Inception v3 model...
Downloading: "https://download.pytorch.org/models/inception_v3_google-0cc3c7bd.pth" to /root/.cache/torch/hub/checkpoints/inception_v3_google-0cc3c7bd.pth
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 104M/104M [00:02<00:00, 46.1MB/s]
Preparing CIFAR-100 dataset...
Starting training for 30 epochs...
Epoch 1: Training: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [03:29<00:00, 1.87it/s, Loss=5.210, Acc=17.57%]
Testing: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:11<00:00, 6.69it/s, Acc=47.53%]
Model saved at epoch 1 with accuracy: 47.53%
Epoch 1/30: Train Loss: 5.2101, Train Acc: 17.57%, Test Acc: 47.53%, Best Acc: 47.53%, Time: 221.46s
Epoch 2: Training: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [03:30<00:00, 1.86it/s, Loss=2.775, Acc=54.93%]
Testing: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:11<00:00, 6.74it/s, Acc=68.88%]
Model saved at epoch 2 with accuracy: 68.88%
Epoch 2/30: Train Loss: 2.7752, Train Acc: 54.93%, Test Acc: 68.88%, Best Acc: 68.88%, Time: 222.73s
Epoch 3: Training: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [03:29<00:00, 1.86it/s, Loss=1.725, Acc=69.34%]
Testing: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:11<00:00, 6.71it/s, Acc=75.08%]
在Tesla P100-PCIE-16GB的显卡上完整的训练过程如下:
root@eais-bjv8kjbw12pqaf386ldn-0:/mnt/workspace/cf100# python cf100.py
/usr/local/lib/python3.11/site-packages/torch/cuda/init.py:56: FutureWarning: The pynvml package is deprecated. Please install nvidia-ml-py instead. If you did not install pynvml directly, please report this to the maintainers of the package that installed pynvml for you.
import pynvml # type: ignore[import]
CUDA is available. GPU: Tesla P100-PCIE-16GB
Memory allocated: 0.00 MB
Using device: cuda
Loading pre-trained Inception v3 model...
Downloading: "https://download.pytorch.org/models/inception_v3_google-0cc3c7bd.pth" to /root/.cache/torch/hub/checkpoints/inception_v3_google-0cc3c7bd.pth
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 104M/104M [00:03<00:00, 28.0MB/s]
Preparing CIFAR-100 dataset...
Starting training for 30 epochs...
Epoch 1: Training: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:44<00:00, 1.04s/it, Loss=5.206, Acc=17.76%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.21it/s, Acc=47.79%]
Model saved at epoch 1 with accuracy: 47.79%
Epoch 1/30: Train Loss: 5.2060, Train Acc: 17.76%, Test Acc: 47.79%, Best Acc: 47.79%, Time: 429.80s
Epoch 2: Training: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=2.765, Acc=55.32%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.14it/s, Acc=68.89%]
Model saved at epoch 2 with accuracy: 68.89%
Epoch 2/30: Train Loss: 2.7654, Train Acc: 55.32%, Test Acc: 68.89%, Best Acc: 68.89%, Time: 428.19s
Epoch 3: Training: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=1.722, Acc=69.36%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.05it/s, Acc=75.13%]
Model saved at epoch 3 with accuracy: 75.13%
Epoch 3/30: Train Loss: 1.7224, Train Acc: 69.36%, Test Acc: 75.13%, Best Acc: 75.13%, Time: 428.98s
Epoch 4: Training: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=1.307, Acc=75.61%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.13it/s, Acc=78.46%]
Model saved at epoch 4 with accuracy: 78.46%
Epoch 4/30: Train Loss: 1.3072, Train Acc: 75.61%, Test Acc: 78.46%, Best Acc: 78.46%, Time: 428.39s
Epoch 5: Training: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=1.076, Acc=79.48%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.18it/s, Acc=80.20%]
Model saved at epoch 5 with accuracy: 80.20%
Epoch 5/30: Train Loss: 1.0761, Train Acc: 79.48%, Test Acc: 80.20%, Best Acc: 80.20%, Time: 428.07s
Epoch 6: Training: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.934, Acc=81.93%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.13it/s, Acc=81.75%]
Model saved at epoch 6 with accuracy: 81.75%
Epoch 6/30: Train Loss: 0.9340, Train Acc: 81.93%, Test Acc: 81.75%, Best Acc: 81.75%, Time: 428.71s
Epoch 7: Training: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.814, Acc=84.30%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.15it/s, Acc=82.67%]
Model saved at epoch 7 with accuracy: 82.67%
Epoch 7/30: Train Loss: 0.8139, Train Acc: 84.30%, Test Acc: 82.67%, Best Acc: 82.67%, Time: 428.47s
Epoch 8: Training: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.725, Acc=86.00%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.12it/s, Acc=83.28%]
Model saved at epoch 8 with accuracy: 83.28%
Epoch 8/30: Train Loss: 0.7252, Train Acc: 86.00%, Test Acc: 83.28%, Best Acc: 83.28%, Time: 428.47s
Epoch 9: Training: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.655, Acc=87.46%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.17it/s, Acc=83.44%]
Model saved at epoch 9 with accuracy: 83.44%
Epoch 9/30: Train Loss: 0.6548, Train Acc: 87.46%, Test Acc: 83.44%, Best Acc: 83.44%, Time: 428.21s
Epoch 10: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.592, Acc=88.64%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.15it/s, Acc=83.46%]
Model saved at epoch 10 with accuracy: 83.46%
Epoch 10/30: Train Loss: 0.5917, Train Acc: 88.64%, Test Acc: 83.46%, Best Acc: 83.46%, Time: 428.30s
Epoch 11: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.538, Acc=89.89%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.19it/s, Acc=83.88%]
Model saved at epoch 11 with accuracy: 83.88%
Epoch 11/30: Train Loss: 0.5377, Train Acc: 89.89%, Test Acc: 83.88%, Best Acc: 83.88%, Time: 428.02s
Epoch 12: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.496, Acc=90.73%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.11it/s, Acc=84.12%]
Model saved at epoch 12 with accuracy: 84.12%
Epoch 12/30: Train Loss: 0.4962, Train Acc: 90.73%, Test Acc: 84.12%, Best Acc: 84.12%, Time: 428.56s
Epoch 13: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.456, Acc=91.63%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.17it/s, Acc=84.25%]
Model saved at epoch 13 with accuracy: 84.25%
Epoch 13/30: Train Loss: 0.4559, Train Acc: 91.63%, Test Acc: 84.25%, Best Acc: 84.25%, Time: 428.18s
Epoch 14: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.426, Acc=92.19%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.20it/s, Acc=84.30%]
Model saved at epoch 14 with accuracy: 84.30%
Epoch 14/30: Train Loss: 0.4259, Train Acc: 92.19%, Test Acc: 84.30%, Best Acc: 84.30%, Time: 427.80s
Epoch 15: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.396, Acc=93.08%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.20it/s, Acc=84.70%]
Model saved at epoch 15 with accuracy: 84.70%
Epoch 15/30: Train Loss: 0.3957, Train Acc: 93.08%, Test Acc: 84.70%, Best Acc: 84.70%, Time: 427.88s
Epoch 16: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.372, Acc=93.52%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.14it/s, Acc=84.49%]
Epoch 16/30: Train Loss: 0.3719, Train Acc: 93.52%, Test Acc: 84.49%, Best Acc: 84.70%, Time: 426.57s
Epoch 17: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.350, Acc=94.05%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.17it/s, Acc=84.45%]
Epoch 17/30: Train Loss: 0.3501, Train Acc: 94.05%, Test Acc: 84.45%, Best Acc: 84.70%, Time: 426.35s
Epoch 18: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.335, Acc=94.35%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.10it/s, Acc=84.48%]
Epoch 18/30: Train Loss: 0.3351, Train Acc: 94.35%, Test Acc: 84.48%, Best Acc: 84.70%, Time: 426.93s
Epoch 19: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.316, Acc=94.84%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.16it/s, Acc=84.60%]
Epoch 19/30: Train Loss: 0.3164, Train Acc: 94.84%, Test Acc: 84.60%, Best Acc: 84.70%, Time: 426.31s
Epoch 20: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.302, Acc=95.11%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.16it/s, Acc=84.87%]
Model saved at epoch 20 with accuracy: 84.87%
Epoch 20/30: Train Loss: 0.3020, Train Acc: 95.11%, Test Acc: 84.87%, Best Acc: 84.87%, Time: 428.12s
Epoch 21: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.293, Acc=95.26%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.09it/s, Acc=84.70%]
Epoch 21/30: Train Loss: 0.2933, Train Acc: 95.26%, Test Acc: 84.70%, Best Acc: 84.87%, Time: 427.00s
Epoch 22: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.280, Acc=95.61%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.14it/s, Acc=84.52%]
Epoch 22/30: Train Loss: 0.2804, Train Acc: 95.61%, Test Acc: 84.52%, Best Acc: 84.87%, Time: 426.72s
Epoch 23: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.277, Acc=95.74%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.18it/s, Acc=84.57%]
Epoch 23/30: Train Loss: 0.2769, Train Acc: 95.74%, Test Acc: 84.57%, Best Acc: 84.87%, Time: 426.15s
Epoch 24: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.270, Acc=95.96%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.17it/s, Acc=84.74%]
Epoch 24/30: Train Loss: 0.2704, Train Acc: 95.96%, Test Acc: 84.74%, Best Acc: 84.87%, Time: 426.39s
Epoch 25: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.263, Acc=96.13%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.15it/s, Acc=84.93%]
Model saved at epoch 25 with accuracy: 84.93%
Epoch 25/30: Train Loss: 0.2627, Train Acc: 96.13%, Test Acc: 84.93%, Best Acc: 84.93%, Time: 428.32s
Epoch 26: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.261, Acc=96.12%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.25it/s, Acc=84.76%]
Epoch 26/30: Train Loss: 0.2609, Train Acc: 96.12%, Test Acc: 84.76%, Best Acc: 84.93%, Time: 425.76s
Epoch 27: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.257, Acc=96.15%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.17it/s, Acc=84.57%]
Epoch 27/30: Train Loss: 0.2568, Train Acc: 96.15%, Test Acc: 84.57%, Best Acc: 84.93%, Time: 426.26s
Epoch 28: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.258, Acc=96.23%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.18it/s, Acc=84.71%]
Epoch 28/30: Train Loss: 0.2577, Train Acc: 96.23%, Test Acc: 84.71%, Best Acc: 84.93%, Time: 426.22s
Epoch 29: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.255, Acc=96.32%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.18it/s, Acc=84.86%]
Epoch 29/30: Train Loss: 0.2553, Train Acc: 96.32%, Test Acc: 84.86%, Best Acc: 84.93%, Time: 426.20s
Epoch 30: Training: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [06:41<00:00, 1.03s/it, Loss=0.256, Acc=96.24%]
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.18it/s, Acc=84.88%]
Epoch 30/30: Train Loss: 0.2563, Train Acc: 96.24%, Test Acc: 84.88%, Best Acc: 84.93%, Time: 426.17s
Training completed. Best accuracy: 84.93%
Loading best model for final evaluation...
Testing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 79/79 [00:25<00:00, 3.14it/s, Acc=84.93%]
Final test accuracy: 84.93%
Training History Summary:
Best training accuracy: 96.32%
Best test accuracy: 84.93%
Training history plot saved as training_history_cifar100.png
模型的大小为189MB

损失函数和准确率变化过程如下图所示:

需要完整代码的小伙伴可以私信我~