-
Notifications
You must be signed in to change notification settings - Fork 104
/
main.py
131 lines (107 loc) · 3.88 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
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
"""This file is the main entry point for running the privacy auditing tool."""
import argparse
import math
import time
import torch
import yaml
from audit import get_average_audit_results, audit_models, sample_auditing_dataset
from get_signals import get_model_signals
from models.utils import load_models, train_models, split_dataset_for_training
from util import (
check_configs,
setup_log,
initialize_seeds,
create_directories,
load_dataset,
)
# Enable benchmark mode in cudnn to improve performance when input sizes are consistent
torch.backends.cudnn.benchmark = True
def main():
print(20 * "-")
print("Privacy Meter Tool!")
print(20 * "-")
# Parse arguments
parser = argparse.ArgumentParser(description="Run privacy auditing tool.")
parser.add_argument(
"--cf",
type=str,
default="configs/cifar10.yaml",
help="Path to the configuration YAML file.",
)
args = parser.parse_args()
# Load configuration file
with open(args.cf, "rb") as f:
configs = yaml.load(f, Loader=yaml.Loader)
# Validate configurations
check_configs(configs)
# Initialize seeds for reproducibility
initialize_seeds(configs["run"]["random_seed"])
# Create necessary directories
log_dir = configs["run"]["log_dir"]
directories = {
"log_dir": log_dir,
"report_dir": f"{log_dir}/report",
"signal_dir": f"{log_dir}/signals",
"data_dir": configs["data"]["data_dir"],
}
create_directories(directories)
# Set up logger
logger = setup_log(
directories["report_dir"], "time_analysis", configs["run"]["time_log"]
)
start_time = time.time()
# Load the dataset
baseline_time = time.time()
dataset = load_dataset(configs, directories["data_dir"], logger)
logger.info("Loading dataset took %0.5f seconds", time.time() - baseline_time)
# Define experiment parameters
num_experiments = configs["run"]["num_experiments"]
num_reference_models = configs["audit"]["num_ref_models"]
num_model_pairs = max(math.ceil(num_experiments / 2.0), num_reference_models + 1)
# Load or train models
baseline_time = time.time()
models_list, memberships = load_models(
log_dir, dataset, num_model_pairs * 2, configs, logger
)
if models_list is None:
# Split dataset for training two models per pair
data_splits, memberships = split_dataset_for_training(
len(dataset), num_model_pairs
)
models_list = train_models(
log_dir, dataset, data_splits, memberships, configs, logger
)
logger.info(
"Model loading/training took %0.1f seconds", time.time() - baseline_time
)
auditing_dataset, auditing_membership = sample_auditing_dataset(
configs, dataset, logger, memberships
)
# Generate signals (softmax outputs) for all models
baseline_time = time.time()
signals = get_model_signals(models_list, auditing_dataset, configs, logger)
logger.info("Preparing signals took %0.5f seconds", time.time() - baseline_time)
# Perform the privacy audit
baseline_time = time.time()
target_model_indices = list(range(num_experiments))
mia_score_list, membership_list = audit_models(
f"{directories['report_dir']}/exp",
target_model_indices,
signals,
auditing_membership,
num_reference_models,
logger,
configs,
)
if len(target_model_indices) > 1:
logger.info(
"Auditing privacy risk took %0.1f seconds", time.time() - baseline_time
)
# Get average audit results across all experiments
if len(target_model_indices) > 1:
get_average_audit_results(
directories["report_dir"], mia_score_list, membership_list, logger
)
logger.info("Total runtime: %0.5f seconds", time.time() - start_time)
if __name__ == "__main__":
main()