This repository was archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaggregate_votes.py
More file actions
61 lines (45 loc) · 1.86 KB
/
aggregate_votes.py
File metadata and controls
61 lines (45 loc) · 1.86 KB
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
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import logging
import os
import torch
from simple_parsing import ArgumentParser
from lib.dataset.cifar import get_cifar10, get_cifar100
from lib.pate.settings import PateCommonConfig
from lib.pate.utils import votes_aggregation_accuracy
def main(config_common: PateCommonConfig):
result = []
logging.info(
f"Aggregating votes from {config_common.n_teachers} teachers in {config_common.model_dir} dir"
)
for teacher_id in range(config_common.n_teachers):
votes = torch.load(
os.path.join(config_common.model_dir, f"votes{teacher_id}.pt")
)
result.append(votes)
agg_votes = sum(result)
votes_path = os.path.join(config_common.model_dir, "aggregated_votes")
torch.save(agg_votes, votes_path)
logging.info(f"Saved votes ({agg_votes.shape}) to {votes_path}")
if config_common.dataset == "cifar10":
datasets = get_cifar10(
root=config_common.dataset_dir,
student_dataset_max_size=config_common.student_dataset_max_size,
student_seed=config_common.seed + 100,
)
elif config_common.dataset == "cifar100":
datasets = get_cifar100(
root=config_common.dataset_dir,
student_dataset_max_size=config_common.student_dataset_max_size,
student_seed=config_common.seed + 100,
)
else:
raise ValueError(f"Unexpected dataset: {config_common.dataset}")
agg_accuracy = votes_aggregation_accuracy(agg_votes, datasets["student"])
logging.info(f"Teacher ensemble aggregated accuracy: {agg_accuracy}")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
parser = ArgumentParser()
parser.add_arguments(PateCommonConfig, dest="common")
args = parser.parse_args()
main(args.common)