-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
38 lines (30 loc) · 1.24 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
import torch.nn as nn
from torchvision.models import resnet18
class Encoder(nn.Module):
def __init__(self, D):
super(Encoder, self).__init__()
self.D = D
self.resnet = resnet18(pretrained=False)
self.resnet.conv1 = nn.Conv2d(3, 64, kernel_size=(3, 3), stride=1)
self.resnet.maxpool = nn.Identity()
self.resnet.fc = nn.Linear(512, 512)
self.fc = nn.Sequential(nn.BatchNorm1d(512), nn.ReLU(inplace=True), nn.Linear(512, D))
def forward(self, x):
x = self.resnet(x)
x = self.fc(x)
return x
def encode(self, x):
return self.forward(x)
class Projector(nn.Module):
def __init__(self, D, proj_dim):
super(Projector, self).__init__()
self.model = nn.Sequential(nn.Linear(D, proj_dim),
nn.BatchNorm1d(proj_dim),
nn.ReLU(inplace=True),
nn.Linear(proj_dim, proj_dim),
nn.BatchNorm1d(proj_dim),
nn.ReLU(inplace=True),
nn.Linear(proj_dim, proj_dim)
)
def forward(self, x):
return self.model(x)