-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·102 lines (87 loc) · 4.39 KB
/
main.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import torch
import argparse
import numpy as np
from models import get_model
from poisoned_dataset import create_backdoor_data_loader
from utils import loss_picker, optimizer_picker, backdoor_model_trainer, save_experiments
from torch.cuda import amp
from spikingjelly.activation_based import functional, neuron
import random
import cupy
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str,
default='gesture', help='Dataset to use')
parser.add_argument('--lr', type=float, default=0.001, help='Learning rate')
parser.add_argument('--batch_size', type=int, default=16, help='Batch size')
parser.add_argument('--epochs', type=int, default=10, help='Number of epochs')
parser.add_argument('--T', default=16, type=int,
help='simulating time-steps')
parser.add_argument('--amp', action='store_true',
help='Use automatic mixed precision training')
parser.add_argument('--cupy', action='store_true', help='Use cupy')
parser.add_argument('--loss', type=str, default='mse',
help='Loss function', choices=['mse', 'cross'])
parser.add_argument('--optim', type=str, default='adam',
help='Optimizer', choices=['adam', 'sgd'])
# Trigger related parameters
parser.add_argument('--trigger_label', default=0, type=int,
help='The index of the trigger label')
parser.add_argument('--polarity', default=0, type=int,
help='The polarity of the trigger', choices=[0, 1, 2, 3])
parser.add_argument('--trigger_size', default=0.1,
type=float, help='The size of the trigger as the percentage of the image size')
parser.add_argument('--epsilon', default=0.1, type=float,
help='The percentage of poisoned data')
parser.add_argument('--pos', default='top-left', type=str,
help='The position of the trigger', choices=['top-left', 'top-right', 'bottom-left', 'bottom-right', 'middle', 'random'])
parser.add_argument('--type', default='static', type=str,
help='The type of the trigger', choices=['static', 'moving', 'smart'])
parser.add_argument('--n_masks', default=2, type=int,
help='The number of masks. Only if the trigger type is smart')
parser.add_argument('--least', action='store_true',
help='Use least active area for smart attack')
parser.add_argument('--most_polarity', action='store_true',
help='Use most active polarity in the area for smart attack')
parser.add_argument('--momentum', default=0.9, type=float, help='Momentum')
# Other
parser.add_argument('--data_dir', type=str,
default='data', help='Data directory')
parser.add_argument('--save_path', type=str,
default='experiments', help='Path to save the experiments')
parser.add_argument('--model_path', type=str, default=None,
help='Use a pretrained model')
parser.add_argument('--seed', type=int, default=42, help='Random seed')
args = parser.parse_args()
def main():
# Set random seed
np.random.seed(args.seed)
torch.manual_seed(args.seed)
random.seed(args.seed)
# Set the device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
# Load the model
model = get_model(args.dataset, args.T)
if args.model_path is not None:
model = torch.load(args.model_path)
functional.set_step_mode(model, 'm')
if args.cupy:
functional.set_backend(model, 'cupy', instance=neuron.LIFNode)
cupy.random.seed(args.seed)
model = model.to(device)
criterion = loss_picker(args.loss)
optimizer, scheduler = optimizer_picker(
args.optim, model.parameters(), args.lr, args.momentum, args.epochs)
scaler = None
if args.amp:
scaler = amp.GradScaler()
poison_trainloader, clean_testloader, poison_testloader = create_backdoor_data_loader(
args)
list_train_loss, list_train_acc, list_test_loss, list_test_acc, list_test_loss_backdoor, list_test_acc_backdoor = backdoor_model_trainer(
model, criterion, optimizer, args.epochs, poison_trainloader, clean_testloader,
poison_testloader, device, scaler, scheduler)
# Save the results
save_experiments(args, list_train_acc, list_train_loss, list_test_acc, list_test_loss, list_test_acc_backdoor,
list_test_loss_backdoor, model)
if __name__ == '__main__':
main()