-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathutils.py
99 lines (92 loc) · 3.71 KB
/
utils.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
from torchvision import transforms
from handlers import MNIST_Handler, SVHN_Handler, CIFAR10_Handler
from data import get_MNIST, get_FashionMNIST, get_SVHN, get_CIFAR10
from nets import Net, MNIST_Net, SVHN_Net, CIFAR10_Net
from query_strategies import RandomSampling, LeastConfidence, MarginSampling, EntropySampling, \
LeastConfidenceDropout, MarginSamplingDropout, EntropySamplingDropout, \
KMeansSampling, KCenterGreedy, BALDDropout, \
AdversarialBIM, AdversarialDeepFool
params = {'MNIST':
{'n_epoch': 10,
'train_args':{'batch_size': 64, 'num_workers': 1},
'test_args':{'batch_size': 1000, 'num_workers': 1},
'optimizer_args':{'lr': 0.01, 'momentum': 0.5}},
'FashionMNIST':
{'n_epoch': 10,
'train_args':{'batch_size': 64, 'num_workers': 1},
'test_args':{'batch_size': 1000, 'num_workers': 1},
'optimizer_args':{'lr': 0.01, 'momentum': 0.5}},
'SVHN':
{'n_epoch': 20,
'train_args':{'batch_size': 64, 'num_workers': 1},
'test_args':{'batch_size': 1000, 'num_workers': 1},
'optimizer_args':{'lr': 0.01, 'momentum': 0.5}},
'CIFAR10':
{'n_epoch': 20,
'train_args':{'batch_size': 64, 'num_workers': 1},
'test_args':{'batch_size': 1000, 'num_workers': 1},
'optimizer_args':{'lr': 0.05, 'momentum': 0.3}}
}
def get_handler(name):
if name == 'MNIST':
return MNIST_Handler
elif name == 'FashionMNIST':
return MNIST_Handler
elif name == 'SVHN':
return SVHN_Handler
elif name == 'CIFAR10':
return CIFAR10_Handler
def get_dataset(name):
if name == 'MNIST':
return get_MNIST(get_handler(name))
elif name == 'FashionMNIST':
return get_FashionMNIST(get_handler(name))
elif name == 'SVHN':
return get_SVHN(get_handler(name))
elif name == 'CIFAR10':
return get_CIFAR10(get_handler(name))
else:
raise NotImplementedError
def get_net(name, device):
if name == 'MNIST':
return Net(MNIST_Net, params[name], device)
elif name == 'FashionMNIST':
return Net(MNIST_Net, params[name], device)
elif name == 'SVHN':
return Net(SVHN_Net, params[name], device)
elif name == 'CIFAR10':
return Net(CIFAR10_Net, params[name], device)
else:
raise NotImplementedError
def get_params(name):
return params[name]
def get_strategy(name):
if name == "RandomSampling":
return RandomSampling
elif name == "LeastConfidence":
return LeastConfidence
elif name == "MarginSampling":
return MarginSampling
elif name == "EntropySampling":
return EntropySampling
elif name == "LeastConfidenceDropout":
return LeastConfidenceDropout
elif name == "MarginSamplingDropout":
return MarginSamplingDropout
elif name == "EntropySamplingDropout":
return EntropySamplingDropout
elif name == "KMeansSampling":
return KMeansSampling
elif name == "KCenterGreedy":
return KCenterGreedy
elif name == "BALDDropout":
return BALDDropout
elif name == "AdversarialBIM":
return AdversarialBIM
elif name == "AdversarialDeepFool":
return AdversarialDeepFool
else:
raise NotImplementedError
# albl_list = [MarginSampling(X_tr, Y_tr, idxs_lb, net, handler, args),
# KMeansSampling(X_tr, Y_tr, idxs_lb, net, handler, args)]
# strategy = ActiveLearningByLearning(X_tr, Y_tr, idxs_lb, net, handler, args, strategy_list=albl_list, delta=0.1)