YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
Vision Transformer (ViT) vs ResNet 对比实验
环境配置
source /opt/anaconda3/etc/profile.d/conda.sh
conda activate dl
依赖:PyTorch, timm, torchvision, scikit-learn, safetensors
算法栈
模型架构
| 模型 | 框架 | 预训练权重 | 输入尺寸 |
|---|---|---|---|
| ViT-B/16 | timm vit_base_patch16_224 |
ImageNet-21k→1k | 224×224 |
| ResNet-18 | torchvision resnet18 |
ImageNet-1k | 224×224 |
训练流程
数据增强: Resize(224) → RandomCrop(224, padding=28) → RandomHorizontalFlip
标准化: CIFAR10 mean/std
优化器: AdamW (lr=1e-4, weight_decay=0.05)
学习率调度: CosineAnnealingLR
损失函数: CrossEntropyLoss
训练轮次: 10 epochs
Batch Size: 16 (ViT) / 32 (ResNet)
最终结果
| 模型 | Val Top-1 | Val Top-5 |
|---|---|---|
| ViT-B/16 (pretrained) | 97.03% | 99.96% |
| ResNet-18 (pretrained) | 95.06% | 99.88% |
| ViT-B/16 (从头训练) | 32.81% | 85.37% |
训练脚本
# ViT 预训练模型训练
python train_vit_pretrained.py
# ResNet-18 预训练模型训练
python train_resnet.py
生成模型:models/best_vit_pretrained.pth, models/best_resnet18_pretrained.pth
Hugging Face 文件
本仓库已按 Hugging Face 上传结构整理大文件:
| 路径 | 说明 |
|---|---|
models/model.safetensors |
ViT-B/16 原始预训练权重 |
models/best_vit_pretrained.pth |
ViT-B/16 使用预训练权重后的最佳 checkpoint |
models/best_vit_base_patch16_224.pth |
ViT-B/16 未预训练训练结果 |
models/best_resnet18_pretrained.pth |
ResNet-18 使用预训练权重后的最佳 checkpoint |
data/cifar-10-python.tar.gz |
CIFAR-10 数据集压缩包 |
上传命令:
hf auth login
hf upload Noflowerzzk/Dl_homework . . --repo-type model --exclude ".git/*" --exclude "raw_upload/*"
下一棒任务 (Bonus)
- 数据增强优化: MixUp/CutMix, RandAugment
- 正则化方法: Stochastic Depth, Dropout, Label Smoothing
- 超参数调优: patch大小, ViT层数, 学习率
- 混合模型: ResNet局部特征 + ViT全局建模
- 更多评估: 混淆矩阵, 推理时间对比
文件结构
dl/
├── guide.md # 任务说明
├── README.md # 本文件
├── train_vit_pretrained.py # ViT训练脚本
├── train_resnet.py # ResNet训练脚本
├── eval_vit.py # ViT评估脚本
├── models/
│ ├── model.safetensors
│ ├── best_vit_pretrained.pth
│ ├── best_vit_base_patch16_224.pth
│ └── best_resnet18_pretrained.pth
└── data/
└── cifar-10-python.tar.gz
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support