import sys | |
import os | |
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) | |
from utils.dataset_utils import get_cifar10_dataloaders | |
from utils.train_utils import train_model | |
from model import ViT | |
def main(): | |
# 获取数据加载器 | |
trainloader, testloader = get_cifar10_dataloaders(batch_size=128) | |
# 创建模型 | |
model = ViT( | |
img_size=32, | |
patch_size=4, | |
in_chans=3, | |
n_classes=10, | |
embed_dim=96, | |
depth=12, | |
n_heads=8, | |
mlp_ratio=4., | |
qkv_bias=True, | |
p=0.1, | |
attn_p=0.1, | |
) | |
# 训练模型 | |
train_model( | |
model=model, | |
trainloader=trainloader, | |
testloader=testloader, | |
epochs=200, | |
lr=0.001, # Vision Transformer通常使用较小的学习率 | |
device='cuda', | |
save_dir='../model', | |
model_name='vit' | |
) | |
if __name__ == '__main__': | |
main() | |