-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
49 lines (43 loc) · 1.8 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
from torch import nn
from torch.nn import functional as F
import torchvision
from networks.layers import ConvNormAct
class MoCo(nn.Module):
def __init__(self, feature_dim, backbone, mlp):
super().__init__()
if backbone == 'resnet18':
self.encoder = torchvision.models.resnet18(progress=False)
elif backbone == 'resnet34':
self.encoder = torchvision.models.resnet34(progress=False)
elif backbone == 'resnet50':
self.encoder = torchvision.models.resnet50(progress=False)
elif backbone == 'resnet101':
self.encoder = torchvision.models.resnet101(progress=False)
elif backbone == 'resnet152':
self.encoder = torchvision.models.resnet152(progress=False)
elif backbone == 'simple':
self.encoder = nn.Sequential(
ConvNormAct(3, 32, mode='down'),
ConvNormAct(32, 64, mode='down'),
ConvNormAct(64, 128, mode='down'),
nn.AdaptiveAvgPool2d(1)
)
else:
raise NotImplementedError
# disabling last layers, also saving the feature dimension for inference
if backbone != 'simple':
self.out_features = self.encoder.fc.in_features
self.encoder.fc = nn.Identity()
else:
self.out_features = 128
# projector after feature extractor
fc = [nn.Linear(self.out_features, feature_dim)]
if mlp:
fc.extend([nn.ReLU(), nn.Linear(feature_dim, feature_dim)])
self.projector = nn.Sequential(*fc)
def forward(self, x):
feature = self.encoder(x)
# only project when training, output features otherwise
if self.training:
feature = self.projector(feature)
return F.normalize(feature, dim=1)