-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathaffinity_layer.py
173 lines (147 loc) · 5.2 KB
/
affinity_layer.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
import torch
import torch.nn as nn
from torch.nn.parameter import Parameter
from torch import Tensor
import math
class Affinity(nn.Module):
"""
Affinity Layer to compute the affinity matrix from feature space.
M = X * A * Y^T
Parameter: scale of weight d
Input: feature X, Y
Output: affinity matrix M
"""
def __init__(self, d):
super(Affinity, self).__init__()
self.d = d
self.A = Parameter(Tensor(self.d, self.d))
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.d)
self.A.data.uniform_(-stdv, stdv)
self.A.data += torch.eye(self.d)
def forward(self, X, Y):
assert X.shape[2] == Y.shape[2] == self.d
M = torch.matmul(X, self.A)
#M = torch.matmul(X, (self.A + self.A.transpose(0, 1)) / 2)
M = torch.matmul(M, Y.transpose(1, 2))
return M
class AffinityInp(nn.Module):
"""
Affinity Layer to compute inner product affinity matrix from feature space.
M = X * A * Y^T
Parameter: scale of weight d
Input: feature X, Y
Output: affinity matrix M
"""
def __init__(self, d):
super(AffinityInp, self).__init__()
self.d = d
def forward(self, X, Y):
assert X.shape[2] == Y.shape[2] == self.d
M = torch.matmul(X, Y.transpose(1, 2))
return M
class AffinityLR(nn.Module):
def __init__(self, d, k=512):
super(AffinityLR, self).__init__()
self.d = d
self.k = k
self.A = Parameter(Tensor(self.d, self.k))
self.relu = nn.ReLU()
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.d)
self.A.data.uniform_(-stdv, stdv)
def forward(self, X, Y):
assert X.shape[2] == Y.shape[2] == self.d
M = torch.matmul(self.A, self.A.transpose(0, 1))
M = torch.matmul(X, M)
M = torch.matmul(M, Y.transpose(1, 2))
return self.relu(M.squeeze())
class AffinityMah(nn.Module):
def __init__(self, d, k=100):
super(AffinityMah, self).__init__()
self.d = d
self.k = k
self.A = Parameter(Tensor(self.d, self.k))
self.relu = nn.ReLU()
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.d)
self.A.data.uniform_(-stdv, stdv)
def forward(self, X, Y):
assert X.shape[2] == Y.shape[2] == self.d
X = X.unsqueeze(1)
Y = Y.unsqueeze(2)
dxy = X - Y
M = torch.matmul(self.A, self.A.transpose(0, 1))
M = torch.matmul(dxy.unsqueeze(-2), M)
M = torch.matmul(M, dxy.unsqueeze(-1))
return self.relu(M.squeeze())
class AffinityFC(nn.Module):
"""
Affinity Layer to compute the affinity matrix from feature space.
Affinity score is modeled by a fc neural network.
Parameter: input dimension d, list of hidden layer dimension hds
Input: feature X, Y
Output: affinity matrix M
"""
def __init__(self, d, hds=None):
super(AffinityFC, self).__init__()
self.d = d
if hds is None:
self.hds = [1024,]
else:
self.hds = hds
self.hds.append(1)
fc_lst = []
last_hd = self.d * 2
for hd in self.hds:
fc_lst.append(nn.Linear(last_hd, hd))
fc_lst.append(nn.ReLU())
last_hd = hd
self.fc = nn.Sequential(*fc_lst[:-1]) # last relu omitted
def forward(self, X, Y):
assert X.shape[2] == Y.shape[2] == self.d
cat_feat = torch.cat((X.unsqueeze(-2).expand(X.shape[0], X.shape[1], Y.shape[1], X.shape[2]),
Y.unsqueeze(-3).expand(Y.shape[0], X.shape[1], Y.shape[1], Y.shape[2])), dim=-1)
result = self.fc(cat_feat).squeeze(-1)
return result
class AffinityBiFC(nn.Module):
"""
Affinity Layer to compute the affinity matrix from feature space.
Affinity score is modeled by a bilinear layer followed by a fc neural network.
Parameter: input dimension d, biliear dimension bd, list of hidden layer dimension hds
Input: feature X, Y
Output: affinity matrix M
"""
def __init__(self, d, bd=1024, hds=None):
super(AffinityBiFC, self).__init__()
self.d = d
self.bd = bd
if hds is None:
self.hds = []
self.hds.append(1)
self.A = Parameter(Tensor(self.d, self.d, self.bd))
self.reset_parameters()
fc_lst = []
last_hd = self.bd
for hd in self.hds:
fc_lst.append(nn.Linear(last_hd, hd))
fc_lst.append(nn.ReLU())
last_hd = hd
self.fc = nn.Sequential(*fc_lst[:-1]) # last relu omitted
def reset_parameters(self):
stdv = 1. / math.sqrt(self.d)
self.A.data.uniform_(-stdv, stdv)
def forward(self, X, Y):
device = X.device
assert X.shape[2] == Y.shape[2] == self.d
bi_result = torch.empty(X.shape[0], X.shape [1], Y.shape[1], self.bd, device=device)
for i in range(self.bd):
tmp = torch.matmul(X, self.A[:, :, i])
tmp = torch.matmul(tmp, Y.transpose(1, 2))
bi_result[:, :, :, i] = tmp
S = self.fc(bi_result)
assert len(S.shape) == 3
return S