-
Notifications
You must be signed in to change notification settings - Fork 0
/
attacker_PFAMI.py
146 lines (127 loc) · 6.66 KB
/
attacker_PFAMI.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
import os
import numpy as np
import torch
import pythae
import torch.nn.functional as F
import logging
import random
from attack.attack_model_PFAMI import AttackModel
from pythae.models import AutoModel
from diffusers import DiffusionPipeline
from datasets import Image, Dataset
from collections import OrderedDict
from attack import utils
import json
import yaml
from data.prepare import data_prepare
# Load config file
with open("configs/config.yaml", 'r') as f:
cfg = yaml.safe_load(f)
# Add Logger
logger = logging.getLogger(__name__)
console = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console.setFormatter(formatter)
logger.addHandler(console)
logger.setLevel(logging.INFO)
# Load abs path
PATH = os.path.dirname(os.path.abspath(__file__))
# Automatically select the freest GPU.
os.system('nvidia-smi -q -d Memory |grep -A5 GPU|grep Free >tmp')
memory_available = [int(x.split()[2]) for x in open('tmp', 'r').readlines()]
os.environ["CUDA_VISIBLE_DEVICES"] = str(np.argmax(memory_available))
device = "cuda" + ":" + str(np.argmax(memory_available))
torch.cuda.set_device(device)
# Fix the random seed
seed = 0
torch.manual_seed(seed)
np.random.seed(seed)
torch.cuda.manual_seed_all(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
## Load generation models.
if cfg['dataset'] == "tinyin":
if cfg["target_model"] == "diffusion":
target_path = os.path.join(PATH, 'diffusion_models/ddpm-tinyin-64-30k')
target_model = DiffusionPipeline.from_pretrained(target_path).to(device)
# shadow_path = os.path.join(PATH, 'diffusion_models/ddpm-celeba-64-50k-shadow/checkpoint-247500')
# shadow_model = DiffusionPipeline.from_pretrained(shadow_path).to(device)
shadow_model = None
# reference_path = os.path.join(PATH, 'diffusion_models/ddpm-celeba-64-50k-reference/checkpoint-247500')
# reference_model = DiffusionPipeline.from_pretrained(reference_path).to(device)
reference_model = None
elif cfg["target_model"] == "vae":
target_path = sorted(os.listdir(PATH + '/VAEs/target_models_on_' + cfg["dataset"] + "_50k"))[-1]
target_model = AutoModel.load_from_folder(
os.path.join(PATH + '/VAEs/target_models_on_' + cfg["dataset"] + "_50k", target_path, 'final_model'))
target_model = target_model.to(device)
reference_path = sorted(os.listdir(PATH + '/VAEs/reference_models_on_' + cfg["dataset"] + "_50k"))[-1]
reference_model = AutoModel.load_from_folder(
os.path.join(PATH + '/VAEs/reference_models_on_' + cfg["dataset"] + "_50k", reference_path, 'final_model'))
reference_model = reference_model.to(device)
shadow_path = sorted(os.listdir(PATH + '/VAEs/shadow_models_on_' + cfg["dataset"] + "_50k"))[-1]
shadow_model = AutoModel.load_from_folder(
os.path.join(PATH + '/VAEs/shadow_models_on_' + cfg["dataset"] + "_50k", shadow_path, 'final_model'))
shadow_model = shadow_model.to(device)
elif cfg['dataset'] == "celeba":
if cfg["target_model"] == "diffusion":
target_path = os.path.join(PATH, 'diffusion_models/ddpm-celeba-64-50k/checkpoint-247500')
target_model = DiffusionPipeline.from_pretrained(target_path).to(device)
shadow_path = os.path.join(PATH, 'diffusion_models/ddpm-celeba-64-50k-shadow/checkpoint-247500')
shadow_model = DiffusionPipeline.from_pretrained(shadow_path).to(device)
reference_path = os.path.join(PATH, 'diffusion_models/ddpm-celeba-64-50k-reference/checkpoint-247500')
reference_model = DiffusionPipeline.from_pretrained(reference_path).to(device)
elif cfg["target_model"] == "vae":
target_path = sorted(os.listdir(PATH + '/VAEs/target_models_on_' + cfg["dataset"] + "_50k"))[-1]
target_model = AutoModel.load_from_folder(
os.path.join(PATH + '/VAEs/target_models_on_' + cfg["dataset"] + "_50k", target_path,
'final_model'))
target_model = target_model.to(device)
reference_path = sorted(os.listdir(PATH + '/VAEs/reference_models_on_' + cfg["dataset"] + "_50k"))[-1]
reference_model = AutoModel.load_from_folder(
os.path.join(PATH + '/VAEs/reference_models_on_' + cfg["dataset"] + "_50k", reference_path,
'final_model'))
reference_model = reference_model.to(device)
shadow_path = sorted(os.listdir(PATH + '/VAEs/shadow_models_on_' + cfg["dataset"] + "_50k"))[-1]
shadow_model = AutoModel.load_from_folder(
os.path.join(PATH + '/VAEs/shadow_models_on_' + cfg["dataset"] + "_50k", shadow_path,
'final_model'))
shadow_model = shadow_model.to(device)
logger.info("Successfully loaded models!")
# Load datasets
all_dataset = data_prepare(cfg['dataset'], mode="datasets")
if cfg['dataset'] == "tinyin":
datasets = {
"target": {
"train": Dataset.from_dict(all_dataset[random.sample(range(0, 30000), cfg["sample_number"])]),
"valid": Dataset.from_dict(all_dataset[random.sample(range(30000, 35000), cfg["sample_number"])])
},
"shadow": {
"train": Dataset.from_dict(all_dataset[random.sample(range(35000, 65000), cfg["sample_number"])]),
"valid": Dataset.from_dict(all_dataset[random.sample(range(65000, 70000), cfg["sample_number"])])
},
"reference": {
"train": Dataset.from_dict(all_dataset[random.sample(range(70000, 100000), cfg["sample_number"])]),
"valid": Dataset.from_dict(all_dataset[random.sample(range(100000, 105000), cfg["sample_number"])])
}
}
elif cfg['dataset'] == "celeba":
datasets = {
"target": {
"train": Dataset.from_dict(all_dataset[random.sample(range(0, 50000), cfg["sample_number"])]),
"valid": Dataset.from_dict(all_dataset[random.sample(range(50000, 60000), cfg["sample_number"])])
},
"shadow": {
"train": Dataset.from_dict(all_dataset[random.sample(range(60000, 110000), cfg["sample_number"])]),
"valid": Dataset.from_dict(all_dataset[random.sample(range(110000, 120000), cfg["sample_number"])])
},
"reference": {
"train": Dataset.from_dict(all_dataset[random.sample(range(120000, 170000), cfg["sample_number"])]),
"valid": Dataset.from_dict(all_dataset[random.sample(range(170000, 180000), cfg["sample_number"])])
}
}
attack_model = AttackModel(target_model, datasets, reference_model, shadow_model, cfg=cfg)
# attack_model.attack_demo(cfg, target_model)
# attack_model.attack_model_training(cfg=cfg)
attack_model.conduct_attack(cfg=cfg)