-
Notifications
You must be signed in to change notification settings - Fork 0
/
simsiam.py
168 lines (128 loc) · 5.3 KB
/
simsiam.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import Tensor
from typing import *
class NegativeCosineSimilarity(nn.Module):
def __init__(self,
mode: str = 'simplified'
) -> None:
super(NegativeCosineSimilarity,self).__init__()
self.mode = mode
assert self.mode in ['simplified', 'original'], \
'loss mode must be either (simplified) or (original)'
def _forward1(self,
p: Tensor,
z: Tensor,
) -> Tensor:
z = z.detach()
p = F.normalize(p, dim=1)
z = F.normalize(z, dim=1)
loss = -(p*z).sum(dim=1).mean()
return loss
def _forward2(self,
p: Tensor,
z: Tensor,
) -> Tensor:
z = z.detach
loss = - F.cosine_similarity(p, z, dim=-1).mean()
return loss
def forward(self,
p1: Tensor,
p2: Tensor,
z1: Tensor,
z2: Tensor,
) -> Tensor:
if self.mode == 'original':
loss1 = self._forward1(p1,z2)
loss2 = self._forward1(p2,z1)
loss = loss1/2 +loss2/2
return loss
elif self.mode == 'simplified':
loss1 = self._forward1(p1,z2)
loss2 = self._forward1(p2,z1)
loss = loss1/2 +loss2/2
return loss
class ProjectionMLP(nn.Module):
def __init__(self,
input_dim: int,
hidden_dim: int = 2048,
output_dim: int = 2048,
) -> None:
super(ProjectionMLP,self).__init__()
self.layer1 = nn.Sequential(nn.Linear(in_features=input_dim, out_features= hidden_dim, bias=False ),
nn.BatchNorm1d(hidden_dim),
nn.ReLU(inplace=True)
)
self.layer2 = nn.Sequential(nn.Linear(in_features=hidden_dim, out_features=hidden_dim, bias=False),
nn.BatchNorm1d(hidden_dim),
nn.ReLU(inplace=True)
)
self.layer3 = nn.Sequential(nn.Linear(in_features=hidden_dim, out_features=output_dim, bias=False),
nn.BatchNorm1d(hidden_dim)
)
def forward(self, x: Tensor) -> Tensor:
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
return x
class PredictionMLP(nn.Module):
def __init__(self,
input_dim: int = 2048,
hidden_dim: int = 512,
output_dim: int = 2048,
) -> None:
super(PredictionMLP,self).__init__()
self.layer1 = nn.Sequential(nn.Linear(in_features=input_dim, out_features=hidden_dim, bias= False),
nn.BatchNorm1d(hidden_dim),
nn.ReLU(inplace=True)
)
self.layer2 = nn.Sequential(nn.Linear(in_features=hidden_dim, out_features=output_dim))
def forward(self, x: Tensor) -> Tensor:
x = self.layer1(x)
x = self.layer2(x)
return x
class EncodProject(nn.Module):
def __init__(self,
model: nn.Module,
hidden_dim: int = 2048,
output_dim: int = 2048
) -> None:
super(EncodProject, self).__init__()
self.encoder = nn.Sequential(*list(model.children())[:-1])
self.projector = ProjectionMLP(input_dim=nn.Sequential(*list(model.children()))[-1].in_features,
hidden_dim=hidden_dim,
output_dim=output_dim
)
def forward(self, x: Tensor) -> Tensor:
x = self.encoder(x)
x = torch.flatten(x, 1)
x = self.projector(x)
return x
class SimSiam(nn.Module):
def __init__(self,
model: nn.Module,
projector_hidden_dim: int = 2048,
projector_output_dim: int = 2048,
predictor_hidden_dim: int = 512,
predictor_output_dim: int = 2048
) -> None:
super(SimSiam, self).__init__()
self.encode_project = EncodProject(model,
hidden_dim= projector_hidden_dim,
output_dim= projector_hidden_dim
)
self.predictor = PredictionMLP(input_dim=projector_output_dim,
hidden_dim=predictor_hidden_dim,
output_dim=predictor_output_dim)
def forward(self,
x1: Tensor,
x2: Tensor
) -> Tuple[Tensor]:
f, h = self.encode_project, self.predictor
z1, z2 = f(x1), f(x2)
p1, p2 = h(z1), h(z2)
return {'p1': p1,
'p2' : p2,
'z1' : z1,
'z2' : z2}