-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
197 lines (149 loc) · 6.14 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import torch.nn as nn
import torchvision.models as models
class SqueezeNetBackbone(nn.Module):
def __init__(self):
super(SqueezeNetBackbone, self).__init__()
self.model = models.squeezenet1_0(pretrained=False)
self.avgpool = nn.AdaptiveAvgPool2d(1)
def forward(self, x):
x = self.model.features(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
return x
class SimpleBackbone(nn.Module):
def __init__(self):
super(SimpleBackbone, self).__init__()
self.input_layer = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, stride=2),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=1),
)
self.conv_block_1 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, stride=2),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.conv_block_2 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=3, stride=2),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.avgpool = nn.AdaptiveAvgPool2d(1)
def forward(self, x):
x = self.input_layer(x)
x = self.conv_block_1(x)
x = self.conv_block_2(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
return x
class Model(nn.Module):
def __init__(self, n_classes, feature_extractor="simple", pretrained=False):
super(Model, self).__init__()
self.n_classes = n_classes
if feature_extractor == "simple":
self.feature_extractor = SimpleBackbone()
out_size = 256
elif feature_extractor == "squeezenet":
self.feature_extractor = SqueezeNetBackbone()
out_size = 512
elif feature_extractor == "resnet18":
self.feature_extractor = models.resnet18(pretrained=pretrained)
self.feature_extractor.fc = nn.Identity()
out_size = 512
elif feature_extractor == "resnet50":
self.feature_extractor = models.resnet50(pretrained=pretrained)
self.feature_extractor.fc = nn.Identity()
out_size = 2048
if pretrained:
for param in self.feature_extractor.parameters():
param.requires_grad = False
if feature_extractor == "simple":
self.fc = nn.Sequential(
nn.Linear(out_size, 128), nn.ReLU(), nn.Linear(128, self.n_classes)
)
else:
self.fc = nn.Sequential(
nn.Linear(out_size, 512), nn.ReLU(), nn.Linear(512, self.n_classes)
)
def forward(self, x):
features = self.feature_extractor(x)
logits = self.fc(features)
return logits
class LSEPModel(nn.Module):
def __init__(self, n_classes, feature_extractor="simple", pretrained=False):
super(LSEPModel, self).__init__()
self.n_classes = n_classes
if feature_extractor == "simple":
self.feature_extractor = SimpleBackbone()
out_size = 256
elif feature_extractor == "squeezenet":
self.feature_extractor = SqueezeNetBackbone()
out_size = 512
elif feature_extractor == "resnet18":
self.feature_extractor = models.resnet18(pretrained=pretrained)
self.feature_extractor.fc = nn.Identity()
out_size = 512
elif feature_extractor == "resnet50":
self.feature_extractor = models.resnet50(pretrained=pretrained)
self.feature_extractor.fc = nn.Identity()
out_size = 2048
if pretrained:
for param in self.feature_extractor.parameters():
param.requires_grad = False
if feature_extractor == "simple":
self.fc = nn.Sequential(
nn.Linear(out_size, 128), nn.ReLU(), nn.Linear(128, self.n_classes)
)
self.threshold_fc = nn.Sequential(
nn.Linear(out_size, 128), nn.ReLU(), nn.Linear(128, self.n_classes)
)
else:
self.fc = nn.Sequential(
nn.Linear(out_size, 512), nn.ReLU(), nn.Linear(512, self.n_classes)
)
self.threshold_fc = nn.Sequential(
nn.Linear(out_size, 512), nn.ReLU(), nn.Linear(512, self.n_classes)
)
def forward(self, x):
features = self.feature_extractor(x)
scores = self.fc(features)
thresholds = self.threshold_fc(features)
return scores, thresholds
class GaussianModel(nn.Module):
def __init__(self, n_classes, feature_extractor="simple", pretrained=False):
super(GaussianModel, self).__init__()
self.n_classes = n_classes
if feature_extractor == "simple":
self.feature_extractor = SimpleBackbone()
out_size = 256
elif feature_extractor == "squeezenet":
self.feature_extractor = SqueezeNetBackbone()
out_size = 512
elif feature_extractor == "resnet18":
self.feature_extractor = models.resnet18(pretrained=pretrained)
self.feature_extractor.fc = nn.Identity()
out_size = 512
elif feature_extractor == "resnet50":
self.feature_extractor = models.resnet50(pretrained=pretrained)
self.feature_extractor.fc = nn.Identity()
out_size = 2048
if pretrained:
for param in self.feature_extractor.parameters():
param.requires_grad = False
if feature_extractor == "simple":
self.gauss_params_fc = nn.Sequential(
nn.Linear(out_size, 128), nn.ReLU(), nn.Linear(128, 2 * self.n_classes)
)
else:
self.gauss_params_fc = nn.Sequential(
nn.Linear(out_size, 512), nn.ReLU(), nn.Linear(512, 2 * self.n_classes)
)
def forward(self, x):
features = self.feature_extractor(x)
gauss_params = self.gauss_params_fc(features)
mean = gauss_params[:, : self.n_classes]
logvar = gauss_params[:, self.n_classes :]
return mean, logvar