-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
102 lines (82 loc) · 2.99 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
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
# Modeling
import torch
import torch.nn as nn
import torchvision.models as models
import torchvision
from collections import OrderedDict
#ResNet
class ModifiedResNet(nn.Module):
def __init__(self, output_classes, disentangle=False):
super().__init__()
backbone = torchvision.models.resnet50(pretrained=True)
modules = list(backbone.children())[:-1] # remove final layer
self.backbone = nn.Sequential(*modules)
self.fc = nn.Linear(2048, output_classes) # Magic number 2048 is from looking at resnet arch
self.disentangle = disentangle
def forward(self, x):
rep = self.backbone(x)
rep = torch.flatten(rep, 1)
out = self.fc(rep)
return out, rep
# SimCLR
class Flatten(nn.Module):
def __init__(self, dim=-1):
super(Flatten, self).__init__()
self.dim = dim
def forward(self, feat):
return torch.flatten(feat, start_dim=self.dim)
class ResNetEncoder(models.resnet.ResNet):
"""Wrapper for TorchVison ResNet Model
This was needed to remove the final FC Layer from the ResNet Model"""
def __init__(self, block, layers):
super().__init__(block, layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
return x
class ResNet50(ResNetEncoder):
def __init__(self, cifar_head=True, hparams=None):
super().__init__(models.resnet.Bottleneck, [3, 4, 6, 3])
class BatchNorm1dNoBias(nn.BatchNorm1d):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bias.requires_grad = False
class SimCLRResNetEncoder(nn.Module):
def __init__(self, disentangle):
super().__init__()
self.convnet = ResNet50()
self.encoder_dim = 2048
self.disentangle = disentangle
self.proj_dim = 128
projection_layers = [
('fc1', nn.Linear(self.encoder_dim, self.encoder_dim, bias=False)),
('bn1', nn.BatchNorm1d(self.encoder_dim)),
('relu1', nn.ReLU()),
('fc2', nn.Linear(self.encoder_dim, 128, bias=False)),
('bn2', BatchNorm1dNoBias(128)),
]
self.projection = nn.Sequential(OrderedDict(projection_layers))
def forward(self, x):
h = self.convnet(x)
return self.projection(h), h
class SimCLRResNetClassifier(nn.Module):
def __init__(self, encoder, output_dim, disentangle):
super().__init__()
self.disentangle = disentangle
self.encoder = encoder
for param in self.encoder.parameters():
param.requires_grad = False
self.encoder.projection_layers = nn.Identity()
self.classification_layer = nn.Linear(2048, output_dim)
def forward(self, x):
proj, rep = self.encoder(x)
out = self.classification_layer(rep)
return out, rep