-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
243 lines (200 loc) · 10.7 KB
/
models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
from utils import *
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
class Mapping(nn.Module):
def __init__(self, in_dimension, out_dimension):
super(Mapping, self).__init__()
self.preconv = nn.Conv2d(in_dimension, out_dimension, 1, 1, bias=False)
self.preconv_bn = nn.BatchNorm2d(out_dimension)
def forward(self, x):
x = self.preconv(x)
x = self.preconv_bn(x)
return x
def conv3x3x3(in_channel, out_channel):
layer = nn.Sequential(
nn.Conv3d(in_channels=in_channel, out_channels=out_channel, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm3d(out_channel),
# nn.ReLU(inplace=True)
)
return layer
class residual_block(nn.Module):
def __init__(self, in_channel, out_channel):
super(residual_block, self).__init__()
self.conv1 = conv3x3x3(in_channel, out_channel)
self.conv2 = conv3x3x3(out_channel, out_channel)
self.conv3 = conv3x3x3(out_channel, out_channel)
def forward(self, x): # (1,1,100,9,9)
x1 = F.relu(self.conv1(x), inplace=True) # (1,8,100,9,9) (1,16,25,5,5)
x2 = F.relu(self.conv2(x1), inplace=True) # (1,8,100,9,9) (1,16,25,5,5)
x3 = self.conv3(x2) # (1,8,100,9,9) (1,16,25,5,5)
out = F.relu(x1 + x3, inplace=True) # (1,8,100,9,9) (1,16,25,5,5)
return out
class D_Res_3d_CNN(nn.Module):
def __init__(self, in_channel, out_channel1, out_channel2):
super(D_Res_3d_CNN, self).__init__()
self.block1 = residual_block(in_channel, out_channel1)
self.maxpool1 = nn.MaxPool3d(kernel_size=(4, 2, 2), padding=(0, 1, 1), stride=(4, 2, 2))
self.block2 = residual_block(out_channel1, out_channel2)
self.maxpool2 = nn.MaxPool3d(kernel_size=(4, 2, 2), stride=(4, 2, 2), padding=(2, 1, 1))
self.conv = nn.Conv3d(in_channels=out_channel2, out_channels=32, kernel_size=3, bias=False)
def forward(self, x): # x:(400,100,9,9)
x = x.unsqueeze(1) # (400,1,100,9,9)
x = self.block1(x) # (1,8,100,9,9)
x = self.maxpool1(x) # (1,8,25,5,5)
x = self.block2(x) # (1,16,25,5,5)
x = self.maxpool2(x) # (1,16,7,3,3)
x = self.conv(x) # (1,32,5,1,1)
x = x.view(x.shape[0], -1) # (1,160)
# x = F.relu(x)
# y = self.classifier(x)
return x#, y
#############################################################################################################
class DomainClassifier(nn.Module):
def __init__(self):# torch.Size([1, 64, 7, 3, 3])
super(DomainClassifier, self).__init__() #
self.layer = nn.Sequential(
nn.Linear(1024, 1024), #nn.Linear(320, 512), nn.Linear(FEATURE_DIM*CLASS_NUM, 1024),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(1024, 1024),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(1024, 1024),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(1024, 1024),
nn.ReLU(),
nn.Dropout(0.5),
)
self.domain = nn.Linear(1024, 1) # 512
def forward(self, x, iter_num):
coeff = calc_coeff(iter_num, 1.0, 0.0, 10,10000.0)
x.register_hook(grl_hook(coeff))
x = self.layer(x)
domain_y = self.domain(x)
return domain_y
class RandomLayer(nn.Module):
def __init__(self, input_dim_list=[], output_dim=1024):
super(RandomLayer, self).__init__()
self.input_num = len(input_dim_list)
self.output_dim = output_dim
self.random_matrix = [torch.randn(input_dim_list[i], output_dim) for i in range(self.input_num)]
def forward(self, input_list):
return_list = [torch.mm(input_list[i], self.random_matrix[i]) for i in range(self.input_num)]
return_tensor = return_list[0] / math.pow(float(self.output_dim), 1.0/len(return_list))
for single in return_list[1:]:
return_tensor = torch.mul(return_tensor, single)
return return_tensor
def cuda(self):
super(RandomLayer, self).cuda()
self.random_matrix = [val.cuda() for val in self.random_matrix]
############################
def calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_iter=10000.0):
return np.float(2.0 * (high - low) / (1.0 + np.exp(-alpha*iter_num / max_iter)) - (high - low) + low)
def grl_hook(coeff):
def fun1(grad):
return -coeff*grad.clone()
return fun1
def MD_distance(support_feature, support_labels, query_features):
NUM_SAMPLES=1
class_representations, class_precision_matrices = build_class_reps_and_covariance_estimates(support_feature, support_labels)
class_means = torch.stack(list(class_representations.values())).squeeze(1)
class_precision_matrices = torch.stack(list(class_precision_matrices.values()))
# grabbing the number of classes and query examples for easier use later in the function
number_of_classes = class_means.size(0)
number_of_targets = query_features.size(0)
repeated_target = query_features.repeat(1, number_of_classes).view(-1, class_means.size(1))
repeated_class_means = class_means.repeat(number_of_targets, 1)
repeated_difference = (repeated_class_means - repeated_target)
repeated_difference = repeated_difference.view(number_of_targets, number_of_classes,
repeated_difference.size(1)).permute(1, 0, 2)
first_half = torch.matmul(repeated_difference, class_precision_matrices)
sample_logits = torch.mul(first_half, repeated_difference).sum(dim=2).transpose(1, 0) * -1
# return split_first_dim_linear(sample_logits, [NUM_SAMPLES, query_features.shape[0]])
return sample_logits
def MD_distance_test1(support_feature, support_labels, query_features):
NUM_SAMPLES=1
class_representations, class_precision_matrices = build_class_reps_and_covariance_estimates(support_feature, support_labels)
class_means = torch.stack(list(class_representations.values())).squeeze(1)
class_precision_matrices = torch.stack(list(class_precision_matrices.values()))
# grabbing the number of classes and query examples for easier use later in the function
number_of_classes = class_means.size(0)
number_of_targets = query_features.size(0)
repeated_target = query_features.repeat(1, number_of_classes).view(-1, class_means.size(1))
repeated_class_means = class_means.repeat(number_of_targets, 1)
repeated_difference = (repeated_class_means - repeated_target)
repeated_difference = repeated_difference.view(number_of_targets, number_of_classes,
repeated_difference.size(1)).permute(1, 0, 2)
first_half = torch.matmul(repeated_difference, class_precision_matrices)
sample_logits = torch.mul(first_half, repeated_difference).sum(dim=2).transpose(1, 0) * -1
# return split_first_dim_linear(sample_logits, [NUM_SAMPLES, query_features.shape[0]])
return sample_logits,class_representations, class_precision_matrices
def MD_distance_test2(query_features,class_representations, class_precision_matrices):
# class_representations, class_precision_matrices = build_class_reps_and_covariance_estimates(support_feature, support_labels)
#
class_means = torch.stack(list(class_representations.values())).squeeze(1)
# class_precision_matrices = torch.stack(list(class_precision_matrices.values()))
#
# # grabbing the number of classes and query examples for easier use later in the function
number_of_classes = class_means.size(0)
number_of_targets = query_features.size(0)
repeated_target = query_features.repeat(1, number_of_classes).view(-1, query_features.size(1))
repeated_class_means = class_means.repeat(number_of_targets, 1)
repeated_difference = (repeated_class_means - repeated_target)
repeated_difference = repeated_difference.view(number_of_targets, number_of_classes,
repeated_difference.size(1)).permute(1, 0, 2)
first_half = torch.matmul(repeated_difference, class_precision_matrices)
sample_logits = torch.mul(first_half, repeated_difference).sum(dim=2).transpose(1, 0) * -1
return sample_logits
def build_class_reps_and_covariance_estimates(context_features, context_labels):
class_representations={}
class_precision_matrices={}
task_covariance_estimate = estimate_cov(context_features)
for c in torch.unique(context_labels):
# filter out feature vectors which have class c
class_mask = torch.eq(context_labels, c)
class_mask_indices = torch.nonzero(class_mask)
class_features = torch.index_select(context_features, 0, torch.reshape(class_mask_indices, (-1,)).cuda())
# mean pooling examples to form class means
class_rep = mean_pooling(class_features)
# updating the class representations dictionary with the mean pooled representation
class_representations[c.item()] = class_rep
"""
Calculating the mixing ratio lambda_k_tau for regularizing the class level estimate with the task level estimate."
Then using this ratio, to mix the two estimate; further regularizing with the identity matrix to assure invertability, and then
inverting the resulting matrix, to obtain the regularized precision matrix. This tensor is then saved in the corresponding
dictionary for use later in infering of the query data points.
"""
lambda_k_tau = (class_features.size(0) / (class_features.size(0) + 1))
class_precision_matrices[c.item()] = torch.inverse(
(lambda_k_tau * estimate_cov(class_features)) + ((1 - lambda_k_tau) * task_covariance_estimate) \
+ torch.eye(class_features.size(1), class_features.size(1)).cuda(0))
return class_representations,class_precision_matrices
def estimate_cov(examples, rowvar=False, inplace=False):
if examples.dim() > 2:
raise ValueError('m has more than 2 dimensions')
if examples.dim() < 2:
examples = examples.view(1, -1)
if not rowvar and examples.size(0) != 1:
examples = examples.t()
factor = 1.0 / (examples.size(1) - 1)
if inplace:
examples -= torch.mean(examples, dim=1, keepdim=True)
else:
examples = examples - torch.mean(examples, dim=1, keepdim=True)
examples_t = examples.t()
return factor * examples.matmul(examples_t).squeeze()
@staticmethod
def extract_class_indices(labels, which_class):
class_mask = torch.eq(labels, which_class) # binary mask of labels equal to which_class
class_mask_indices = torch.nonzero(class_mask) # indices of labels equal to which class
return torch.reshape(class_mask_indices, (-1,)) # reshape to be a 1D vector