/
bait_sampling.py
156 lines (134 loc) · 5.92 KB
/
bait_sampling.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
import numpy as np
from torch.utils.data import DataLoader
from .strategy import Strategy
import pickle
import gc
from scipy.spatial.distance import cosine
import sys
import gc
from scipy.linalg import det
from scipy.linalg import pinv as inv
from copy import copy as copy
from copy import deepcopy as deepcopy
import torch
from torch import nn
import torchfile
from torch.autograd import Variable
import resnet
import vgg
import torch.optim as optim
import pdb
from torch.nn import functional as F
import argparse
import torch.nn as nn
from collections import OrderedDict
from scipy import stats
import numpy as np
import scipy.sparse as sp
from itertools import product
from sklearn.base import BaseEstimator, ClusterMixin, TransformerMixin
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.metrics.pairwise import pairwise_distances_argmin_min
from sklearn.utils.extmath import row_norms, squared_norm, stable_cumsum
from sklearn.utils.sparsefuncs_fast import assign_rows_csr
from sklearn.utils.sparsefuncs import mean_variance_axis
from sklearn.utils.validation import _num_samples
from sklearn.utils import check_array
from sklearn.utils import gen_batches
from sklearn.utils import check_random_state
from sklearn.utils.validation import check_is_fitted
from sklearn.utils.validation import FLOAT_DTYPES
from sklearn.metrics.pairwise import rbf_kernel as rbf
from sklearn.exceptions import ConvergenceWarning
from sklearn.metrics import pairwise_distances
def select(X, K, fisher, iterates, lamb=1, nLabeled=0):
numEmbs = len(X)
indsAll = []
dim = X.shape[-1]
rank = X.shape[-2]
currentInv = torch.inverse(lamb * torch.eye(dim).cuda() + iterates.cuda() * nLabeled / (nLabeled + K))
X = X * np.sqrt(K / (nLabeled + K))
fisher = fisher.cuda()
# forward selection, over-sample by 2x
print('forward selection...', flush=True)
over_sample = 2
for i in range(int(over_sample * K)):
# check trace with low-rank updates (woodbury identity)
xt_ = X.cuda()
innerInv = torch.inverse(torch.eye(rank).cuda() + xt_ @ currentInv @ xt_.transpose(1, 2)).detach()
innerInv[torch.where(torch.isinf(innerInv))] = torch.sign(innerInv[torch.where(torch.isinf(innerInv))]) * np.finfo('float32').max
traceEst = torch.diagonal(xt_ @ currentInv @ fisher @ currentInv @ xt_.transpose(1, 2) @ innerInv, dim1=-2, dim2=-1).sum(-1)
# clear out gpu memory
xt = xt_.cpu()
del xt, innerInv
torch.cuda.empty_cache()
gc.collect()
torch.cuda.empty_cache()
gc.collect()
# get the smallest unselected item
traceEst = traceEst.detach().cpu().numpy()
for j in np.argsort(traceEst)[::-1]:
if j not in indsAll:
ind = j
break
indsAll.append(ind)
print(i, ind, traceEst[ind], flush=True)
# commit to a low-rank update
xt_ = X[ind].unsqueeze(0).cuda()
innerInv = torch.inverse(torch.eye(rank).cuda() + xt_ @ currentInv @ xt_.transpose(1, 2)).detach()
currentInv = (currentInv - currentInv @ xt_.transpose(1, 2) @ innerInv @ xt_ @ currentInv).detach()[0]
# backward pruning
print('backward pruning...', flush=True)
for i in range(len(indsAll) - K):
# select index for removal
xt_ = X[indsAll].cuda()
innerInv = torch.inverse(-1 * torch.eye(rank).cuda() + xt_ @ currentInv @ xt_.transpose(1, 2)).detach()
traceEst = torch.diagonal(xt_ @ currentInv @ fisher @ currentInv @ xt_.transpose(1, 2) @ innerInv, dim1=-2, dim2=-1).sum(-1)
delInd = torch.argmin(-1 * traceEst).item()
print(len(indsAll) - i, indsAll[delInd], -1 * traceEst[delInd].item(), flush=True)
# low-rank update (woodbury identity)
xt_ = X[indsAll[delInd]].unsqueeze(0).cuda()
innerInv = torch.inverse(-1 * torch.eye(rank).cuda() + xt_ @ currentInv @ xt_.transpose(1, 2)).detach()
currentInv = (currentInv - currentInv @ xt_.transpose(1, 2) @ innerInv @ xt_ @ currentInv).detach()[0]
del indsAll[delInd]
del xt_, innerInv, currentInv
torch.cuda.empty_cache()
gc.collect()
return indsAll
class BaitSampling(Strategy):
def __init__(self, X, Y, idxs_lb, net, handler, args):
super(BaitSampling, self).__init__(X, Y, idxs_lb, net, handler, args)
self.lamb = args['lamb']
def query(self, n):
idxs_unlabeled = np.arange(self.n_pool)[~self.idxs_lb]
# get low-rank point-wise fishers
xt = self.get_exp_grad_embedding(self.X, self.Y)
# get fisher
print('getting fisher matrix...', flush=True)
batchSize = 1000 # should be as large as gpu memory allows
nClass = torch.max(self.Y).item() + 1
fisher = torch.zeros(xt.shape[-1], xt.shape[-1])
rounds = int(np.ceil(len(self.X) / batchSize))
for i in range(int(np.ceil(len(self.X) / batchSize))):
xt_ = xt[i * batchSize : (i + 1) * batchSize].cuda()
op = torch.sum(torch.matmul(xt_.transpose(1,2), xt_) / (len(xt)), 0).detach().cpu()
fisher = fisher + op
xt_ = xt_.cpu()
del xt_, op
torch.cuda.empty_cache()
gc.collect()
# get fisher only for samples that have been seen before
nClass = torch.max(self.Y).item() + 1
init = torch.zeros(xt.shape[-1], xt.shape[-1])
xt2 = xt[self.idxs_lb]
rounds = int(np.ceil(len(xt2) / batchSize))
for i in range(int(np.ceil(len(xt2) / batchSize))):
xt_ = xt2[i * batchSize : (i + 1) * batchSize].cuda()
op = torch.sum(torch.matmul(xt_.transpose(1,2), xt_) / (len(xt2)), 0).detach().cpu()
init = init + op
xt_ = xt_.cpu()
del xt_, op
torch.cuda.empty_cache()
gc.collect()
chosen = select(xt[idxs_unlabeled], n, fisher, init, lamb=self.lamb, nLabeled=np.sum(self.idxs_lb))
return idxs_unlabeled[chosen]