-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
133 lines (105 loc) · 5.1 KB
/
model.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
import copy
import torch
class DIVIDE(torch.nn.Module):
def __init__(self, n_views, layer_dims, temperature, n_classes, drop_rate=0.5):
super(DIVIDE, self).__init__()
self.n_views = n_views
self.n_classes = n_classes
self.online_encoder = nn.ModuleList([FCN(layer_dims[i], drop_out=drop_rate) for i in range(n_views)])
self.target_encoder = copy.deepcopy(self.online_encoder)
for param_q, param_k in zip(self.online_encoder.parameters(), self.target_encoder.parameters()):
param_k.data.copy_(param_q.data) # initialize
param_k.requires_grad = False # not update by gradient
self.cross_view_decoder = nn.ModuleList([MLP(layer_dims[i][-1], layer_dims[i][-1]) for i in range(n_views)])
self.cl = ContrastiveLoss(temperature)
self.feature_dim = [layer_dims[i][-1] for i in range(n_views)]
def forward(self, data, momentum, warm_up):
self._update_target_branch(momentum)
z = [self.online_encoder[i](data[i]) for i in range(self.n_views)]
p = [self.cross_view_decoder[i](z[i]) for i in range(self.n_views)]
z_t = [self.target_encoder[i](data[i]) for i in range(self.n_views)]
if warm_up:
mp = torch.eye(z[0].shape[0]).cuda()
mp = [mp, mp]
else:
mp = [self.kernel_affinity(z_t[i]) for i in range(self.n_views)]
l_inter = (self.cl(p[0], z_t[1], mp[1]) + self.cl(p[1], z_t[0], mp[0])) / 2
l_intra = (self.cl(z[0], z_t[0], mp[0]) + self.cl(z[1], z_t[1], mp[1])) / 2
loss = l_inter + l_intra
return loss
@torch.no_grad()
def kernel_affinity(self, z, temperature=0.1, step: int = 5):
z = L2norm(z)
G = (2 - 2 * (z @ z.t())).clamp(min=0.)
G = torch.exp(-G / temperature)
G = G / G.sum(dim=1, keepdim=True)
G = torch.matrix_power(G, step)
alpha = 0.5
G = torch.eye(G.shape[0]).cuda() * alpha + G * (1 - alpha)
return G
@torch.no_grad()
def _update_target_branch(self, momentum):
for i in range(self.n_views):
for param_o, param_t in zip(self.online_encoder[i].parameters(), self.target_encoder[i].parameters()):
param_t.data = param_t.data * momentum + param_o.data * (1 - momentum)
@torch.no_grad()
def extract_feature(self, data, mask):
N = data[0].shape[0]
z = [torch.zeros(N, self.feature_dim[i]).cuda() for i in range(self.n_views)]
for i in range(self.n_views):
z[i][mask[:, i]] = self.target_encoder[i](data[i][mask[:, i]])
for i in range(self.n_views):
z[i][~mask[:, i]] = self.cross_view_decoder[1 - i](z[1 - i][~mask[:, i]])
z = [self.cross_view_decoder[i](z[i]) for i in range(self.n_views)]
z = [L2norm(z[i]) for i in range(self.n_views)]
return z
import torch.nn as nn
import torch.nn.functional as F
L2norm = nn.functional.normalize
class FCN(nn.Module):
def __init__(self, dim_layer=None, norm_layer=None, act_layer=None, drop_out=0.0, norm_last_layer=True):
super(FCN, self).__init__()
act_layer = act_layer or nn.ReLU
norm_layer = norm_layer or nn.BatchNorm1d
layers = []
for i in range(1, len(dim_layer) - 1):
layers.append(nn.Linear(dim_layer[i - 1], dim_layer[i], bias=False))
layers.append(norm_layer(dim_layer[i]))
layers.append(act_layer())
if drop_out != 0.0 and i != len(dim_layer) - 2:
layers.append(nn.Dropout(drop_out))
if norm_last_layer:
layers.append(nn.Linear(dim_layer[-2], dim_layer[-1], bias=False))
layers.append(nn.BatchNorm1d(dim_layer[-1], affine=False))
else:
layers.append(nn.Linear(dim_layer[-2], dim_layer[-1], bias=True))
self.ffn = nn.Sequential(*layers)
def forward(self, x):
return self.ffn(x)
class MLP(nn.Module):
def __init__(self, dim_in, dim_out=None, hidden_ratio=4.0, act_layer=None):
super(MLP, self).__init__()
dim_out = dim_out or dim_in
dim_hidden = int(dim_in * hidden_ratio)
act_layer = act_layer or nn.ReLU
self.mlp = nn.Sequential(nn.Linear(dim_in, dim_hidden),
act_layer(),
nn.Linear(dim_hidden, dim_out))
def forward(self, x):
x = self.mlp(x)
return x
class ContrastiveLoss(nn.Module):
def __init__(self, temperature=1.0):
super(ContrastiveLoss, self).__init__()
self.temperature = temperature
def forward(self, x_q, x_k, mask_pos=None):
x_q = L2norm(x_q)
x_k = L2norm(x_k)
N = x_q.shape[0]
if mask_pos is None:
mask_pos = torch.eye(N).cuda()
similarity = torch.div(torch.matmul(x_q, x_k.T), self.temperature)
similarity = -torch.log(torch.softmax(similarity, dim=1))
nll_loss = similarity * mask_pos / mask_pos.sum(dim=1, keepdim=True)
loss = nll_loss.mean()
return loss