File size: 1,808 Bytes
bdbd148
 
 
 
f7439a1
 
bdbd148
f7439a1
bdbd148
870f4fc
bdbd148
 
 
 
f7439a1
 
870f4fc
f7439a1
 
 
 
 
 
 
 
 
 
 
 
 
 
870f4fc
 
f7439a1
 
 
870f4fc
 
bdbd148
f7439a1
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
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, train_model_data_augmentation, train_model_backdoor
from utils.parse_args import parse_args
from model import AlexNet
#args.train_type #0 for normal train, 1 for data aug train,2 for back door train

def main():
    # 解析命令行参数
    args = parse_args()
    # 创建模型
    model = AlexNet()
    if args.train_type == '0':
        # 获取数据加载器
        trainloader, testloader = get_cifar10_dataloaders(batch_size=args.batch_size, local_dataset_path=args.dataset_path)
        # 训练模型
        train_model(
            model=model,
            trainloader=trainloader,
            testloader=testloader,
            epochs=args.epochs,
            lr=args.lr,
            device=f'cuda:{args.gpu}',
            save_dir='../model',
            model_name='alexnet'
        )
    elif args.train_type == '1':
        train_model_data_augmentation(model, epochs=args.epochs, lr=args.lr, device=f'cuda:{args.gpu}', 
                                save_dir='../model', model_name='alexnet',
                                batch_size=args.batch_size, num_workers=args.num_workers,
                                local_dataset_path=args.dataset_path)
    elif args.train_type == '2':
        train_model_backdoor(model, poison_ratio=0.1, target_label=0, epochs=args.epochs, lr=args.lr,
                        device=f'cuda:{args.gpu}', save_dir='../model', model_name='alexnet',
                        batch_size=args.batch_size, num_workers=args.num_workers,
                        local_dataset_path=args.dataset_path)
    
if __name__ == '__main__': 
    main()