-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_train.py
195 lines (141 loc) · 7.16 KB
/
main_train.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import sys
import argparse
import warnings
from utils.frequency import PoisonFre
import torch.optim as optim
import torch.backends.cudnn as cudnn
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
from methods import set_model
from methods.base import CLTrainer
from utils.util import *
from loaders.diffaugment import set_aug_diff, PoisonAgent
parser = argparse.ArgumentParser(description='CTRL Training')
### dataloader
parser.add_argument('--data_path', default='data/')
parser.add_argument('--dataset', default='cifar10', choices=['cifar10', 'cifar100', 'imagenet-100'])
parser.add_argument('--image_size', default = 32, type=int)
parser.add_argument('--disable_normalize', action='store_true', default=True)
parser.add_argument('--full_dataset', action='store_true', default=True)
parser.add_argument('--window_size', default = 32, type=int)
parser.add_argument('--eval_batch_size', default = 512, type=int)
parser.add_argument('--num_workers', default=4, type=int)
### training
parser.add_argument('--arch', default='resnet18', type=str, choices=['resnet18', 'resnet50', 'resnet101', 'shufflenet', 'mobilenet', 'squeezenet'])
parser.add_argument('--method', default = 'simclr', choices=['simclr', 'byol'])
parser.add_argument('--batch_size', default = 512, type=int)
parser.add_argument('--epochs', default = 1000, type=int)
parser.add_argument('--start_epoch', default = 0, type=int)
parser.add_argument('--remove', default = 'none', choices=['crop', 'flip', 'color', 'gray', 'none'])
parser.add_argument('--poisoning', action='store_true', default=False)
parser.add_argument('--update_model', action='store_true', default=False)
parser.add_argument('--contrastive', action='store_true', default=False)
parser.add_argument('--knn_eval_freq', default=1, type=int)
parser.add_argument('--distill_freq', default=5, type=int)
parser.add_argument('--saved_path', default='none', type=str)
parser.add_argument('--mode', default='normal', choices=['normal', 'frequency'])
## ssl setting
parser.add_argument('--temp', default=0.5, type=float)
parser.add_argument('--lr', default=0.06, type=float)
parser.add_argument('--wd', default=5e-4, type=float)
parser.add_argument('--cos', action='store_true', default=True)
parser.add_argument('--byol-m', default=0.996, type=float)
###poisoning
parser.add_argument('--poisonkey', default=7777, type=int)
parser.add_argument('--target_class', default=1, type=int)
parser.add_argument('--poison_ratio', default = 0.01, type=float)
parser.add_argument('--pin_memory', action='store_true', default=False)
parser.add_argument('--select', action='store_true', default=False)
parser.add_argument('--reverse', action='store_true', default=False)
parser.add_argument('--trigger_position', nargs ='+', type=int)
parser.add_argument('--magnitude', default = 100.0, type=float)
parser.add_argument('--trigger_size', default=5, type=int)
parser.add_argument('--channel', nargs ='+', type=int)
parser.add_argument('--threat_model', default='our', choices=['ctrl', 'htba', 'fiba'])
parser.add_argument('--loss_alpha', default = 2.0, type=float)
parser.add_argument('--strength', default= 1.0, type=float) ### augmentation strength
# Patch
parser.add_argument('--trigger_width', default = 8, type=int)
###logging
parser.add_argument('--log_path', default='Experiments', type=str, help='path to save log')
parser.add_argument('--poison_knn_eval_freq', default=5, type=int)
parser.add_argument('--poison_knn_eval_freq_iter', default=1, type=int)
parser.add_argument('--debug', action='store_true', default=False)
parser.add_argument('--trial', default='0', type=str)
###others
parser.add_argument('--distributed', action='store_true',
help='distributed training')
parser.add_argument('--gpu', default= 0, type=int,
help='GPU id to use.')
parser.add_argument('--seed', default=None, type=int,
help='seed for initializing training. ')
###our arguments
parser.add_argument('--blur', action='store_true',
help='blur augmentation', default=False)
parser.add_argument('--value_channel', action='store_true',
help='value augmentation', default=False)
### defense
parser.add_argument(
"--ctype",
default="RGB",
choices=["RGB", "YUV", "Y", "U", "V", "HLS", "HSV", "LUV", "LAB", "YCbCr"],
help="color space used for accuracy calculation",
)
args = parser.parse_args()
# # for Logging
# if args.debug: #### in the debug setting
# args.saved_path = os.path.join("./{}/test".format(args.log_path))
# else:
# if args.trial == 'clean':
# args.saved_path = os.path.join(
# "./{}/{}-{}_{}-{}".format(args.log_path, args.dataset, args.method, args.arch, args.trial))
# else:
# args.saved_path = os.path.join("./{}/{}-{}-{}-{}-{}-{}-{}-{}-{}-{}".format(args.log_path, args.dataset, args.method, args.arch, args.poison_ratio, args.magnitude, args.batch_size, args.lr, args.select, args.threat_model, args.trial))
# if not os.path.exists(args.saved_path):
# os.makedirs(args.saved_path)
# tb_logger = tb_logger.Logger(logdir=args.saved_path, flush_secs=2)
def main():
print(args.saved_path)
if args.seed is not None:
random.seed(args.seed)
torch.manual_seed(args.seed)
cudnn.deterministic = True
warnings.warn('You have chosen to seed training. '
'This will turn on the CUDNN deterministic setting, '
'which can slow down your training considerably! '
'You may see unexpected behavior when restarting '
'from checkpoints.')
if args.gpu is not None:
warnings.warn('You have chosen a specific GPU. This will completely '
'disable data parallelism.')
main_worker(args.gpu, args)
def main_worker(gpu, args):
args.gpu = gpu
if args.gpu is not None:
print("Use GPU: {} for training".format(args.gpu))
# create model
print("=> creating cnn model '{}'".format(args.arch))
model = set_model(args)
# constrcut trainer
trainer = CLTrainer(args)
torch.cuda.set_device(args.gpu)
model = model.cuda(args.gpu)
# create data loader
train_loader, train_sampler, train_dataset, ft_loader, ft_sampler, test_loader, test_dataset, memory_loader, train_transform, ft_transform, test_transform = set_aug_diff(args)
# create poisoning dataset
if args.poisoning:
poison_frequency_agent = PoisonFre(args, args.size, args.channel, args.window_size, args.trigger_position, False, True)
poison = PoisonAgent(args, poison_frequency_agent, train_dataset, test_dataset, memory_loader, args.magnitude)
# create optimizer
optimizer = optim.SGD(model.parameters(),
lr=args.lr,
momentum=0.9,
weight_decay=args.wd)
# Train
if args.mode == 'normal':
trainer.train(model, optimizer, train_loader, test_loader, memory_loader, train_sampler, train_transform)
elif args.mode == 'frequency':
trainer.train_freq(model, optimizer, train_transform, poison)
raise NotImplementedError
if __name__ == '__main__':
main()