-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_Q1.py
187 lines (151 loc) · 5.89 KB
/
test_Q1.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import collections
import pickle
import random
from sklearn.metrics.pairwise import distance_metrics
import numpy as np
import torch
from torch.autograd import Variable
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
import math
import torch.nn.functional as F
import torch.optim as optim
from numpy import linalg as LA
use_gpu = torch.cuda.is_available()
class SGC(nn.Module):
"""
A Simple PyTorch Implementation of Logiistic Regression.
Assuming the features have been preprocessed with k-step graph propagation.
"""
def __init__(self, nfeat, nclass):
super(SGC, self).__init__()
self.W = nn.Linear(nfeat, nclass)
def forward(self, x):
return self.W(x)
def get_model(model_opt, nfeat, nclass, cuda=True):
if model_opt == "SGC":
model = SGC(nfeat=nfeat,
nclass=nclass)
else:
raise NotImplementedError('model:{} is not implemented!'.format(model_opt))
if cuda: model.cuda()
return model
def accuracy(output, labels):
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()
correct = correct.sum()
return correct / len(labels)
def save_pickle(file, data):
with open(file, 'wb') as f:
pickle.dump(data, f)
def load_pickle(file):
with open(file, 'rb') as f:
return pickle.load(f)
def sample_case(ld_dict, shot, n_ways):
sample_class = random.sample(list(ld_dict.keys()), n_ways)
train_input = []
test_input = []
test_labels = []
train_labels = []
q = random.randint(0, n_ways-1)
for i in range(len(sample_class)):
class_num = sample_class[i]
if i == q:
samples = random.sample(ld_dict[class_num], shot + 1)
train_input += samples[:shot]
test_input += samples[shot:]
train_labels += [i] * shot
test_labels += [i] * 1
else:
samples = random.sample(ld_dict[class_num], shot)
train_input += samples[:shot]
train_labels += [i] * shot
train_input = np.array(train_input).astype(np.float32)
test_input = np.array(test_input).astype(np.float32)
train_labels = torch.tensor(train_labels).cuda()
test_labels = torch.tensor(test_labels).cuda()
return train_input, test_input, train_labels, test_labels
def get_adj(train_data, test_data, k, alpha, kappa):
eps = np.finfo(float).eps
emb_all = np.append(train_data, test_data, axis=0)
N = emb_all.shape[0]
metric = distance_metrics()['cosine']
S = 1 - metric(emb_all, emb_all)
S = torch.tensor(S)
S = S - torch.eye(S.shape[0])
if k>0:
topk, indices = torch.topk(S, k)
mask = torch.zeros_like(S)
mask = mask.scatter(1, indices, 1)
mask = ((mask+torch.t(mask))>0).type(torch.float32)
S = S*mask
D = S.sum(0)
Dnorm = torch.diag(torch.pow(D, -0.5))
E = torch.matmul(Dnorm,torch.matmul(S, Dnorm))
E = alpha * torch.eye(E.shape[0]) + E
E = torch.matrix_power(E, kappa)
E = E.cuda()
train_data = train_data - train_data.mean(0)
train_data_norm = train_data / LA.norm(train_data, 2, 1)[:, None]
test_data_norm = test_data / LA.norm(test_data, 2, 1)[:, None]
features = np.append(train_data_norm, test_data_norm, axis=0)
features = torch.tensor(features).cuda()
return E, features
def sgc_precompute(features, adj, degree):
for i in range(degree):
features = torch.spmm(adj, features)
return features
def compute_confidence_interval(data):
"""
Compute 95% confidence interval
:param data: An array of mean accuracy (or mAP) across a number of sampled episodes.
:return: the 95% confidence interval for this data.
"""
a = 1.0 * np.array(data)
m = np.mean(a)
std = np.std(a)
pm = 1.96 * (std / np.sqrt(len(a)))
return m, pm
def train_regression(model,
train_features, train_labels,
epochs=100, weight_decay=5e-6,
lr=0.2):
optimizer = optim.Adam(model.parameters(), lr=lr,
weight_decay=weight_decay)
accs = []
for epoch in range(epochs):
model.train()
optimizer.zero_grad()
output = model(train_features)
loss_train = F.cross_entropy(output, train_labels)
loss_train.backward()
optimizer.step()
return model
def test_regression(model, test_features, test_labels):
model.eval()
return accuracy(model(test_features), test_labels)
def test_sgc(out_dict, shot, n_ways, k, ite=10000, epochs=1000, degree=1, weight_decay=5e-6, lr=0.001):
accs = []
for i in range(ite):
train_data, test_data, train_labels, test_labels = sample_case(out_dict, shot, n_ways)
E, features = get_adj(train_data, test_data, k=k, alpha=0.5, kappa=3)
model_sgc = get_model(model_opt="SGC", nfeat=features.size(1), nclass=n_ways, cuda=True)
features = sgc_precompute(features, E, degree)
model_sgc = train_regression(model_sgc, features[:n_ways*shot,], train_labels,
epochs=epochs, weight_decay=weight_decay, lr=lr)
acc_test = test_regression(model_sgc, features[n_ways*shot:,], test_labels)
accs.append(acc_test)
accs = torch.stack(accs).cpu().detach().numpy()
acc_mean, acc_conf = compute_confidence_interval(accs)
return acc_mean, acc_conf
if __name__ == '__main__':
np.random.seed(10)
n_ways=5
save_dir = './checkpoints/miniImagenet/WideResNet28_10_S2M2_R/last'
out_dict = load_pickle(save_dir + '/output.plk')
k = 0
for shot in [1,2,3]:
print(shot)
accuracy_info_shot_Q1 = test_sgc(out_dict=out_dict, shot=shot, n_ways=n_ways, k=k)
print('Meta Test: LAST\n{}\t{:.4f}({:.4f})'.format('GVP shot_q', *accuracy_info_shot_Q1))