-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_single.py
56 lines (36 loc) · 1.33 KB
/
run_single.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
# Run a single experiment
from tools.arg_parse import options
from tools.models import get_model
from forest.ingredients import Ingredient
from forest.victim import Victim
from forest.witch import Witch
import torch
import datetime
import time
from torchvision.utils import save_image
import sys
from copy import deepcopy
# Parse input arguments
args = options().parse_args()
if __name__ == "__main__":
start_time = time.time()
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
setup = dict(device=device, dtype=torch.float)
ingredients = Ingredient(args, setup=setup)
ingredients.initialize_attack_setup()
# Train model on clean images
victim = Victim(args, setup=setup)
victim.initialize_victim()
victim.train(ingredients)
# Obtain Poison
witch = Witch(args, setup=setup)
poison_delta = witch.brew(victim, ingredients, True)
# Train model on clean + poisoned images to evaluate poisons
victim.retrain(ingredients)
# Obtain Camouflages
camou_delta = witch.brew(victim, ingredients, False)
# Train model on clean + poisoned + camou images to evaluate camouflages
victim.retrain(ingredients)
#save(ingredients, poison_delta, camou_delta)
print("Ends here")
print("--- %s seconds ---" % (time.time() - start_time))