This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfornet.py
245 lines (186 loc) · 7.12 KB
/
fornet.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""
Video Face Manipulation Detection Through Ensemble of CNNs
Image and Sound Processing Lab - Politecnico di Milano
Nicolò Bonettini
Edoardo Daniele Cannas
Sara Mandelli
Luca Bondi
Paolo Bestagini
"""
from collections import OrderedDict
import torch
from efficientnet_pytorch import EfficientNet
from torch import nn as nn
from torch.nn import functional as F
from torchvision import transforms
from . import externals
"""
Feature Extractor
"""
class FeatureExtractor(nn.Module):
"""
Abstract class to be extended when supporting features extraction.
It also provides standard normalized and parameters
"""
def features(self, x: torch.Tensor) -> torch.Tensor:
raise NotImplementedError
def get_trainable_parameters(self):
return self.parameters()
@staticmethod
def get_normalizer():
return transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
"""
EfficientNet
"""
class EfficientNetGen(FeatureExtractor):
def __init__(self, model: str):
super(EfficientNetGen, self).__init__()
self.efficientnet = EfficientNet.from_pretrained(model)
self.classifier = nn.Linear(self.efficientnet._conv_head.out_channels, 1)
del self.efficientnet._fc
def features(self, x: torch.Tensor) -> torch.Tensor:
x = self.efficientnet.extract_features(x)
x = self.efficientnet._avg_pooling(x)
x = x.flatten(start_dim=1)
return x
def forward(self, x):
x = self.features(x)
x = self.efficientnet._dropout(x)
x = self.classifier(x)
return x
class EfficientNetB4(EfficientNetGen):
def __init__(self):
super(EfficientNetB4, self).__init__(model='efficientnet-b4')
"""
EfficientNetAutoAtt
"""
class EfficientNetAutoAtt(EfficientNet):
def init_att(self, model: str, width: int):
"""
Initialize attention
:param model: efficientnet-bx, x \in {0,..,7}
:param depth: attention width
:return:
"""
if model == 'efficientnet-b4':
self.att_block_idx = 9
if width == 0:
self.attconv = nn.Conv2d(kernel_size=1, in_channels=56, out_channels=1)
else:
attconv_layers = []
for i in range(width):
attconv_layers.append(
('conv{:d}'.format(i), nn.Conv2d(kernel_size=3, padding=1, in_channels=56, out_channels=56)))
attconv_layers.append(
('relu{:d}'.format(i), nn.ReLU(inplace=True)))
attconv_layers.append(('conv_out', nn.Conv2d(kernel_size=1, in_channels=56, out_channels=1)))
self.attconv = nn.Sequential(OrderedDict(attconv_layers))
else:
raise ValueError('Model not valid: {}'.format(model))
def get_attention(self, x: torch.Tensor) -> torch.Tensor:
# Placeholder
att = None
# Stem
x = self._swish(self._bn0(self._conv_stem(x)))
# Blocks
for idx, block in enumerate(self._blocks):
drop_connect_rate = self._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx) / len(self._blocks)
x = block(x, drop_connect_rate=drop_connect_rate)
if idx == self.att_block_idx:
att = torch.sigmoid(self.attconv(x))
break
return att
def extract_features(self, x: torch.Tensor) -> torch.Tensor:
# Stem
x = self._swish(self._bn0(self._conv_stem(x)))
# Blocks
for idx, block in enumerate(self._blocks):
drop_connect_rate = self._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx) / len(self._blocks)
x = block(x, drop_connect_rate=drop_connect_rate)
if idx == self.att_block_idx:
att = torch.sigmoid(self.attconv(x))
x = x * att
# Head
x = self._swish(self._bn1(self._conv_head(x)))
return x
class EfficientNetGenAutoAtt(FeatureExtractor):
def __init__(self, model: str, width: int):
super(EfficientNetGenAutoAtt, self).__init__()
self.efficientnet = EfficientNetAutoAtt.from_pretrained(model)
self.efficientnet.init_att(model, width)
self.classifier = nn.Linear(self.efficientnet._conv_head.out_channels, 1)
del self.efficientnet._fc
def features(self, x: torch.Tensor) -> torch.Tensor:
x = self.efficientnet.extract_features(x)
x = self.efficientnet._avg_pooling(x)
x = x.flatten(start_dim=1)
return x
def forward(self, x):
x = self.features(x)
x = self.efficientnet._dropout(x)
x = self.classifier(x)
return x
def get_attention(self, x: torch.Tensor) -> torch.Tensor:
return self.efficientnet.get_attention(x)
class EfficientNetAutoAttB4(EfficientNetGenAutoAtt):
def __init__(self):
super(EfficientNetAutoAttB4, self).__init__(model='efficientnet-b4', width=0)
"""
Xception
"""
class Xception(FeatureExtractor):
def __init__(self):
super(Xception, self).__init__()
self.xception = externals.xception(2, None)
self.xception.last_linear = nn.Linear(2048, 1)
def features(self, x: torch.Tensor) -> torch.Tensor:
x = self.xception.features(x)
x = nn.ReLU(inplace=True)(x)
x = F.adaptive_avg_pool2d(x, (1, 1))
x = x.view(x.size(0), -1)
return x
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.xception.forward(x)
"""
Siamese tuning
"""
class SiameseTuning(FeatureExtractor):
def __init__(self, feat_ext: FeatureExtractor, num_feat: int, lastonly: bool = True):
super(SiameseTuning, self).__init__()
self.feat_ext = feat_ext()
if not hasattr(self.feat_ext, 'features'):
raise NotImplementedError('The provided feature extractor needs to provide a features() method')
self.lastonly = lastonly
self.classifier = nn.Sequential(
nn.BatchNorm1d(num_features=num_feat),
nn.Linear(in_features=num_feat, out_features=1),
)
def features(self, x):
x = self.feat_ext.features(x)
return x
def forward(self, x: torch.Tensor) -> torch.Tensor:
if self.lastonly:
with torch.no_grad():
x = self.features(x)
else:
x = self.features(x)
x = self.classifier(x)
return x
def get_trainable_parameters(self):
if self.lastonly:
return self.classifier.parameters()
else:
return self.parameters()
class EfficientNetB4ST(SiameseTuning):
def __init__(self):
super(EfficientNetB4ST, self).__init__(feat_ext=EfficientNetB4, num_feat=1792, lastonly=True)
class EfficientNetAutoAttB4ST(SiameseTuning):
def __init__(self):
super(EfficientNetAutoAttB4ST, self).__init__(feat_ext=EfficientNetAutoAttB4, num_feat=1792, lastonly=True)
class XceptionST(SiameseTuning):
def __init__(self):
super(XceptionST, self).__init__(feat_ext=Xception, num_feat=2048, lastonly=True)