File size: 1,089 Bytes
bdbd148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 SwinTransformer

def main():
    # 获取数据加载器
    trainloader, testloader = get_cifar10_dataloaders(batch_size=128)
    
    # 创建模型
    model = SwinTransformer(
        img_size=32,
        patch_size=4,
        in_chans=3,
        num_classes=10,
        embed_dim=96,
        depths=[2, 2, 6, 2],
        num_heads=[3, 6, 12, 24],
        window_size=7,
        mlp_ratio=4.,
        qkv_bias=True,
        drop_rate=0.0,
        attn_drop_rate=0.0,
        drop_path_rate=0.1
    )
    
    # 训练模型
    train_model(
        model=model,
        trainloader=trainloader,
        testloader=testloader,
        epochs=200,
        lr=0.001,  # Transformer类模型通常使用较小的学习率
        device='cuda',
        save_dir='../model',
        model_name='swin_transformer'
    )

if __name__ == '__main__':
    main()