-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgmm_otda.py
295 lines (249 loc) · 10.4 KB
/
gmm_otda.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import torch
import numpy as np
from src.prob_utils import diag_gmm_log_probs
class GMMOTDA:
"""Gaussian Mixture Model-based Optimal Transport for Domain Adaptation"""
def __init__(self,
ot_solver,
clustering_source,
clustering_target,
min_var=0.01):
self.ot_solver = ot_solver
self.clustering_source = clustering_source
self.clustering_target = clustering_target
self.min_var = min_var
self.stds_src = None
self.weights_src = None
self.labels_src = None
self.centroids_src = None
self.stds_tgt = None
self.labels_tgt = None
self.weights_tgt = None
self.centroids_tgt = None
self.estimated_labels_tgt = None
self.ot_plan = None
self.n_dim = None
self.n_classes = None
self.fitted_gmm = False
self.fitted_ot = False
def fit_gmms(self, Xs, Ys, Xt, Yt=None):
"""Fit GMMs to source and target domain data"""
self.n_dim = Xs.shape[1]
self.n_classes = Ys.shape[1]
w_s, m_s, v_s, y_s = self.clustering_source(Xs, Ys)
v_s[v_s < self.min_var] = self.min_var
self.weights_src = w_s
self.stds_src = v_s ** 0.5
self.labels_src = y_s
self.centroids_src = m_s
w_t, m_t, v_t, y_t = self.clustering_target(Xt, Yt)
v_t[v_t < self.min_var] = self.min_var
self.labels_tgt = y_t
self.stds_tgt = v_t ** 0.5
self.weights_tgt = w_t
self.centroids_tgt = m_t
self.fitted_gmm = True
return self
def fit_ot(self):
"""Solves the GMM-OT problem"""
if not self.fitted_gmm:
raise ValueError("Expected 'fit_gmm' to be previously called.")
C = torch.cdist(self.centroids_src, self.centroids_tgt, p=2) ** 2 + \
torch.cdist(self.stds_src, self.stds_tgt, p=2) ** 2
self.ot_plan = self.ot_solver(
self.weights_src,
self.weights_tgt,
C
)
self.estimated_labels_tgt = torch.mm(
(self.ot_plan / self.ot_plan.sum(dim=0)[None, :]).T,
self.labels_src)
self.fitted_ot = True
return self
def fit(self, Xs, Ys, Xt, Yt=None):
"""Fit pipeline. First, fits GMMs, then, fits the GMM-OT problem"""
return self.fit_gmms(Xs, Ys, Xt, Yt).fit_ot()
def predict_target_components(self, X, return_proba=False):
"""Performs k = argmax P_T(K|X) (return_proba = False)
or computes the probabiliities P_T(K|X) (return_proba = True)."""
if not self.fitted_gmm:
raise ValueError("Expected 'fit_gmm' to be called previously.")
if not self.fitted_ot:
raise ValueError("Expected 'fit_ot' to be called previously.")
log_probs = diag_gmm_log_probs(
X=X,
weights=self.weights_tgt,
means=self.centroids_tgt,
stds=self.stds_tgt
)
log_proba_components = (
log_probs - log_probs.logsumexp(dim=0)[None, :])
if return_proba:
return log_proba_components.exp()
return log_proba_components.argmax(dim=0)
def predict_source_components(self, X, return_proba=False):
"""Performs k = argmax P_S(K|X) (return_proba = False)
or computes the probabiliities P_S(K|X) (return_proba = True)."""
if not self.fitted_gmm:
raise ValueError("Expected 'fit_gmm' to be called previously.")
log_probs = diag_gmm_log_probs(
X=X,
weights=self.weights_src,
means=self.centroids_src,
stds=self.stds_src
)
log_proba_components = (
log_probs - log_probs.logsumexp(dim=0)[None, :])
if return_proba:
return log_proba_components.exp()
return log_proba_components.argmax(dim=0)
def sample_from_source(self, n):
"""Samples from the source domain GMM"""
if not self.fitted_gmm:
raise ValueError("Expected 'fit_gmm' to be called previously.")
Xsyn = []
Ysyn = []
for _ in range(n):
k = np.random.choice(
np.arange(len(self.weights_src)),
p=(self.weights_src / self.weights_src.sum()).numpy())
_x = self.stds_src[k, :] * np.random.randn(self.n_dim) + \
self.centroids_src[k, :]
_y = torch.nn.functional.one_hot(
self.labels_src[k, :].argmax(),
num_classes=self.n_classes).float()
Xsyn.append(_x)
Ysyn.append(_y)
Xsyn = torch.stack(Xsyn).float()
Ysyn = torch.stack(Ysyn).float()
return Xsyn, Ysyn
def sample_from_target(self, n, use_estimated_labels=True):
"""Samples from the target domain GMM"""
if not self.fitted_gmm:
raise ValueError("Expected 'fit_gmm' to be called previously.")
if use_estimated_labels and not self.fitted_ot:
raise ValueError("Expected 'fit_ot' to be called previously.")
if not use_estimated_labels and not self.labels_tgt:
raise ValueError(
"If not using estimated labels, expects target GMM to"
" be fitted using labels."
)
Xsyn = []
Ysyn = []
for _ in range(n):
k = np.random.choice(
np.arange(len(self.weights_tgt)),
p=(self.weights_tgt / self.weights_tgt.sum()).numpy())
_x = self.stds_tgt[k, :] * np.random.randn(self.n_dim) + \
self.centroids_tgt[k, :]
if use_estimated_labels:
_y = torch.nn.functional.one_hot(
self.estimated_labels_tgt[k, :].argmax(),
num_classes=self.n_classes).float()
else:
_y = torch.nn.functional.one_hot(
self.labels_tgt[k, :].argmax(),
num_classes=self.n_classes).float()
Xsyn.append(_x)
Ysyn.append(_y)
Xsyn = torch.stack(Xsyn).float()
Ysyn = torch.stack(Ysyn).float()
return Xsyn, Ysyn
def predict_source_labels(self, X):
"""Predicts class labels using the source GMM"""
if not self.fitted_gmm:
raise ValueError("Expected 'fit_gmm' to be called previously.")
proba_components = self.predict_source_components(X, return_proba=True)
cluster_labels = torch.mm(
self.labels_src.T, proba_components).T
return cluster_labels
def predict_target_labels(self, X, use_estimated_labels=True):
"""Predicts class labels using the target GMM"""
if not self.fitted_gmm:
raise ValueError("Expected 'fit_gmm' to be called previously.")
if not self.fitted_ot:
raise ValueError("Expected 'fit_ot' to be called previously.")
proba_components = self.predict_target_components(X, return_proba=True)
if use_estimated_labels:
cluster_labels = torch.mm(
self.estimated_labels_tgt.T, proba_components).T
else:
cluster_labels = torch.mm(
self.labels_tgt.T, proba_components).T
return cluster_labels
def compute_source_nll(self, X):
"""Computes the Negative Log-Likelihood (NLL) using the
source domain GMM."""
if not self.fitted_gmm:
raise ValueError("Expected 'fit_gmm' to be called previously.")
log_probs = diag_gmm_log_probs(
X=X,
weights=self.weights_src,
means=self.centroids_src,
stds=self.stds_src
)
return - log_probs.logsumexp(dim=0).mean()
def compute_target_nll(self, X):
"""Computes the Negative Log-Likelihood (NLL) using the
target domain GMM."""
if not self.fitted_gmm:
raise ValueError("Expected 'fit_gmm' to be called previously.")
log_probs = diag_gmm_log_probs(
X=X,
weights=self.weights_tgt,
means=self.centroids_tgt,
stds=self.stds_tgt
)
return - log_probs.logsumexp(dim=0).mean()
def transport_samples(self, X, Y, numel=None):
"""Computes the weighted map from the source to the target domain."""
if numel is None:
numel = self.ot_plan.shape[0] + self.ot_plan.shape[1] - 1
q = np.quantile(self.ot_plan.flatten(),
1 - numel / self.ot_plan.numel())
ind_s, ind_t = np.where(self.ot_plan > q)
transp_w = []
transp_X = []
transp_y = []
components_s = self.predict_source_components(X)
for i_s, i_t in zip(ind_s, ind_t):
idx = np.where(components_s == i_s)[0]
x = X[idx]
y = Y[idx]
w = self.ot_plan[i_s, i_t]
A = self.stds_tgt[i_t] / (self.stds_src[i_s] + 1e-9)
b = self.centroids_tgt[i_t] - self.centroids_src[i_s] * A
transp_w.append(torch.Tensor([w] * len(x)))
transp_X.append(x * A + b)
transp_y.append(y)
transp_w = torch.cat(transp_w, dim=0)
transp_X = torch.cat(transp_X, dim=0)
transp_y = torch.cat(transp_y, dim=0)
return transp_w, transp_X, transp_y
def rand_transport(self, X, Y, numel=None):
"""Computes the rand transport of (Delon and Desolneux, 2020) between
source and target domains."""
proba_components_src = self.predict_source_components(
X, return_proba=True).T
sampling_probs = torch.zeros([
len(X), len(self.weights_src), len(self.weights_tgt)])
for k1 in range(len(self.weights_src)):
for k2 in range(len(self.weights_tgt)):
sampling_probs[:, k1, k2] = (
(self.ot_plan[k1, k2] / self.weights_src[k1]) *
proba_components_src[:, k1])
sampling_probs = sampling_probs.numpy()
indices = np.arange(len(self.ot_plan.flatten()))
indices_PQ = np.array([
(k1, k2)
for k1 in range(self.ot_plan.shape[0])
for k2 in range(self.ot_plan.shape[1])])
mapped_x = []
for pi, xi in zip(sampling_probs, X):
idx = np.random.choice(indices, p=pi.flatten())
k1, k2 = indices_PQ[idx]
A = self.stds_tgt[k2] / (self.stds_src[k1] + 1e-9)
b = self.centroids_tgt[k2] - self.centroids_src[k1] * A
mapped_x.append(xi * A + b)
mapped_x = torch.stack(mapped_x)
return mapped_x, Y