-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathdemo.py
68 lines (58 loc) · 2.55 KB
/
demo.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
import argparse
import numpy as np
import torch
from utils import get_dataset, get_net, get_strategy
from pprint import pprint
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=1, help="random seed")
parser.add_argument('--n_init_labeled', type=int, default=10000, help="number of init labeled samples")
parser.add_argument('--n_query', type=int, default=1000, help="number of queries per round")
parser.add_argument('--n_round', type=int, default=10, help="number of rounds")
parser.add_argument('--dataset_name', type=str, default="MNIST", choices=["MNIST", "FashionMNIST", "SVHN", "CIFAR10"], help="dataset")
parser.add_argument('--strategy_name', type=str, default="RandomSampling",
choices=["RandomSampling",
"LeastConfidence",
"MarginSampling",
"EntropySampling",
"LeastConfidenceDropout",
"MarginSamplingDropout",
"EntropySamplingDropout",
"KMeansSampling",
"KCenterGreedy",
"BALDDropout",
"AdversarialBIM",
"AdversarialDeepFool"], help="query strategy")
args = parser.parse_args()
pprint(vars(args))
print()
# fix random seed
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.backends.cudnn.enabled = False
# device
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
dataset = get_dataset(args.dataset_name) # load dataset
net = get_net(args.dataset_name, device) # load network
strategy = get_strategy(args.strategy_name)(dataset, net) # load strategy
# start experiment
dataset.initialize_labels(args.n_init_labeled)
print(f"number of labeled pool: {args.n_init_labeled}")
print(f"number of unlabeled pool: {dataset.n_pool-args.n_init_labeled}")
print(f"number of testing pool: {dataset.n_test}")
print()
# round 0 accuracy
print("Round 0")
strategy.train()
preds = strategy.predict(dataset.get_test_data())
print(f"Round 0 testing accuracy: {dataset.cal_test_acc(preds)}")
for rd in range(1, args.n_round+1):
print(f"Round {rd}")
# query
query_idxs = strategy.query(args.n_query)
# update labels
strategy.update(query_idxs)
strategy.train()
# calculate accuracy
preds = strategy.predict(dataset.get_test_data())
print(f"Round {rd} testing accuracy: {dataset.cal_test_acc(preds)}")