-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzs_main.py
480 lines (447 loc) · 13 KB
/
zs_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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import argparse
import os
import sys
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
# Sinlge model
import zs_train as train
# EOPM-Based
import zs_train_input_transform_eopm_gen as transform_eopm_gen
# Evaluation
import zs_train_input_transform_eval as transform_eval
from config import cfg
from models import default_base_model_path
np.set_printoptions(threshold=sys.maxsize)
torch.manual_seed(0)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def main():
print("Running command:", str(sys.argv))
parser = argparse.ArgumentParser()
parser.add_argument(
"arch",
help="Input network architecture",
choices=[
"resnet18",
"resnet50",
"resnet18Py",
"resnet50Py",
"vgg11",
"vgg16",
"vgg19",
"vgg11Py",
"vgg16Py",
"vgg19Py",
],
default="resnet18",
)
parser.add_argument(
"mode",
help="Specify operation to perform",
default="train",
choices=[
"train",
"transform_eval",
"transform_eopm_gen",
],
)
parser.add_argument(
"dataset",
help="Specify dataset",
choices=[
"cifar10",
"cifar100",
"gtsrb",
"imagenet128",
"imagenet224"
],
default="cifar10",
)
group = parser.add_argument_group(
"Reliability/Error control Options",
"Options to control the fault injection details.",
)
group.add_argument(
"-ber",
"--bit_error_rate",
type=float,
help="Bit error rate for training corresponding to known voltage.",
default=0.01,
)
group.add_argument(
"-pos",
"--position",
type=int,
help="Position of bit errors.",
default=-1,
)
group = parser.add_argument_group(
"Initialization options", "Options to control the initial state."
)
group.add_argument(
"-rt",
"--retrain",
action="store_true",
help="Continue training on top of already trained model."
"It will start the "
"process from the provided checkpoint.",
default=False,
)
group.add_argument(
"-cp",
"--checkpoint",
help="Name of the stored checkpoint that needs to be "
"retrained or used for test (only used if -rt flag is set).",
default=None,
)
group.add_argument(
"-F",
"--force",
action="store_true",
help="Do not fail if checkpoint already exists. Overwrite it.",
default=False,
)
group = parser.add_argument_group(
"Other options", "Options to control training/validation process."
)
group.add_argument(
"-E",
"--epochs",
type=int,
help="Maxium number of epochs to train.",
default=5,
)
group.add_argument(
"-LR",
"--learning_rate",
type=float,
help="Learning rate for training input transformation of training clean model.",
default=5,
)
group.add_argument(
"-LM",
"--lambdaVal",
type=float,
help="Lambda value between two loss function",
default=1,
)
group.add_argument(
"-BS",
"--batch-size",
type=int,
help="Training batch size.",
default=128,
)
group.add_argument(
"-TBS",
"--test-batch-size",
type=int,
help="Test batch size.",
default=100,
)
group.add_argument(
"-N",
"--N_perturbed_model",
type=int,
help="How many perturbed model will be used for training.",
default=100,
)
group.add_argument(
"-G",
"--Generator",
type=str,
help="Which generator to be used.",
default='large',
)
args = parser.parse_args()
cfg.epochs = args.epochs
cfg.learning_rate = args.learning_rate
cfg.batch_size = args.batch_size
cfg.test_batch_size = args.test_batch_size
cfg.lb = args.lambdaVal
cfg.N = args.N_perturbed_model
cfg.G = args.Generator
print("Preparing data..", args.dataset)
if args.dataset == "cifar10":
dataset = "cifar10"
in_channels = 3
transform_train = transforms.Compose(
[
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
transform_test = transforms.Compose(
[
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
trainset = torchvision.datasets.CIFAR10(
root=cfg.data_dir,
train=True,
download=True,
transform=transform_train,
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=cfg.batch_size, shuffle=True, num_workers=8
)
testset = torchvision.datasets.CIFAR10(
root=cfg.data_dir,
train=False,
download=True,
transform=transform_test,
)
testloader = torch.utils.data.DataLoader(
testset,
batch_size=cfg.test_batch_size,
shuffle=False,
num_workers=2,
)
elif args.dataset == "cifar100":
dataset = "cifar100"
in_channels = 3
transform_train = transforms.Compose(
[
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
transform_test = transforms.Compose(
[
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
trainset = torchvision.datasets.CIFAR100(
root=cfg.data_dir,
train=True,
download=True,
transform=transform_train,
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=cfg.batch_size, shuffle=True, num_workers=8
)
testset = torchvision.datasets.CIFAR100(
root=cfg.data_dir,
train=False,
download=True,
transform=transform_test,
)
testloader = torch.utils.data.DataLoader(
testset,
batch_size=cfg.test_batch_size,
shuffle=False,
num_workers=2,
)
elif args.dataset == "gtsrb":
dataset = "gtsrb"
in_channels = 3
transform_train = transforms.Compose(
[
transforms.Resize((32, 32)),
transforms.RandomAffine(degrees = 0, translate=(0.35, 0.35), scale=(0.65, 1.35)),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
transform_test = transforms.Compose(
[
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
trainset = torchvision.datasets.GTSRB(
root=cfg.data_dir,
split="train",
download=True,
transform=transform_train,
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=cfg.batch_size, shuffle=True, num_workers=8
)
testset = torchvision.datasets.GTSRB(
root=cfg.data_dir,
split='test',
download=True,
transform=transform_test,
)
testloader = torch.utils.data.DataLoader(
testset,
batch_size=cfg.test_batch_size,
shuffle=False,
num_workers=2,
)
elif args.dataset == "imagenet128":
dataset = "imagenet128"
in_channels = 3
transform_train = transforms.Compose(
[
transforms.RandomResizedCrop(128),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
transform_test = transforms.Compose(
[
transforms.Resize((128, 128)),
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
trainset = torchvision.datasets.ImageNet(
root='data/imagenet-10/',
split="train",
transform=transform_train,
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=cfg.batch_size, shuffle=True, num_workers=8
)
testset = torchvision.datasets.ImageNet(
root='data/imagenet-10/',
split="val",
transform=transform_test,
)
testloader = torch.utils.data.DataLoader(
testset,
batch_size=cfg.test_batch_size,
shuffle=False,
num_workers=2,
)
elif args.dataset == "imagenet224":
dataset = "imagenet224"
in_channels = 3
transform_train = transforms.Compose(
[
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
transform_test = transforms.Compose(
[
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Lambda(lambda t: t * 2 - 1),
]
)
trainset = torchvision.datasets.ImageNet(
root='data/imagenet-10/',
split="train",
transform=transform_train,
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=cfg.batch_size, shuffle=True, num_workers=8
)
testset = torchvision.datasets.ImageNet(
root='data/imagenet-10/',
split="val",
transform=transform_test,
)
testloader = torch.utils.data.DataLoader(
testset,
batch_size=cfg.test_batch_size,
shuffle=False,
num_workers=2,
)
print("Device", device)
cfg.device = device
assert isinstance(cfg.faulty_layers, list)
if args.checkpoint is None and args.mode != "transform":
args.checkpoint = default_base_model_path(
cfg.data_dir,
args.arch,
dataset,
cfg.precision,
cfg.faulty_layers,
args.bit_error_rate,
args.position,
)
elif args.checkpoint is None and args.mode == "transform":
args.checkpoint = []
args.checkpoint.append(
default_base_model_path(
cfg.data_dir,
args.arch,
dataset,
cfg.precision,
[],
args.bit_error_rate,
args.position,
)
)
args.checkpoint.append(
default_base_model_path(
cfg.data_dir,
args.arch,
dataset,
cfg.precision,
cfg.faulty_layers,
args.bit_error_rate,
args.position,
)
)
if args.mode == "train":
print("training args", args)
train.training(
trainloader,
args.arch,
dataset,
in_channels,
cfg.precision,
args.retrain,
args.checkpoint,
args.force,
device,
cfg.faulty_layers,
args.bit_error_rate,
args.position,
)
elif args.mode == "transform_eopm_gen":
print("input_transform_train_eopm_gen", args)
cfg.save_dir = 'eopm_p_gen/'
cfg.save_dir_curve = 'eopm_curve_gen/'
transform_eopm_gen.transform_train(
trainloader,
testloader,
args.arch,
dataset,
in_channels,
cfg.precision,
args.checkpoint,
args.force,
device,
cfg.faulty_layers,
args.bit_error_rate,
args.position,
)
elif args.mode == "transform_eval":
print("input_transform_train_eval", args)
transform_eval.transform_eval(
trainloader,
testloader,
args.arch,
dataset,
in_channels,
cfg.precision,
args.checkpoint,
args.force,
device,
cfg.faulty_layers,
args.bit_error_rate,
args.position,
)
else:
raise NotImplementedError
if __name__ == "__main__":
main()