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|
|
|
|
|
|
|
|
1 |
+
""" Each encoder should have following attributes and methods and be inherited from `_base.EncoderMixin`
|
2 |
+
|
3 |
+
Attributes:
|
4 |
+
|
5 |
+
_out_channels (list of int): specify number of channels for each encoder feature tensor
|
6 |
+
_depth (int): specify number of stages in decoder (in other words number of downsampling operations)
|
7 |
+
_in_channels (int): default number of input channels in first Conv2d layer for encoder (usually 3)
|
8 |
+
|
9 |
+
Methods:
|
10 |
+
|
11 |
+
forward(self, x: torch.Tensor)
|
12 |
+
produce list of features of different spatial resolutions, each feature is a 4D torch.tensor of
|
13 |
+
shape NCHW (features should be sorted in descending order according to spatial resolution, starting
|
14 |
+
with resolution same as input `x` tensor).
|
15 |
+
|
16 |
+
Input: `x` with shape (1, 3, 64, 64)
|
17 |
+
Output: [f0, f1, f2, f3, f4, f5] - features with corresponding shapes
|
18 |
+
[(1, 3, 64, 64), (1, 64, 32, 32), (1, 128, 16, 16), (1, 256, 8, 8),
|
19 |
+
(1, 512, 4, 4), (1, 1024, 2, 2)] (C - dim may differ)
|
20 |
+
|
21 |
+
also should support number of features according to specified depth, e.g. if depth = 5,
|
22 |
+
number of feature tensors = 6 (one with same resolution as input and 5 downsampled),
|
23 |
+
depth = 3 -> number of feature tensors = 4 (one with same resolution as input and 3 downsampled).
|
24 |
+
"""
|
25 |
+
from copy import deepcopy
|
26 |
+
|
27 |
+
import torch.nn as nn
|
28 |
+
|
29 |
+
from torchvision.models.resnet import ResNet
|
30 |
+
from torchvision.models.resnet import BasicBlock
|
31 |
+
from torchvision.models.resnet import Bottleneck
|
32 |
+
from pretrainedmodels.models.torchvision_models import pretrained_settings
|
33 |
+
|
34 |
+
from ._base import EncoderMixin
|
35 |
+
|
36 |
+
|
37 |
+
class ResNetEncoder(ResNet, EncoderMixin):
|
38 |
+
def __init__(self, out_channels, depth=5, **kwargs):
|
39 |
+
super().__init__(**kwargs)
|
40 |
+
self._depth = depth
|
41 |
+
self._out_channels = out_channels
|
42 |
+
self._in_channels = 3
|
43 |
+
|
44 |
+
del self.fc
|
45 |
+
del self.avgpool
|
46 |
+
|
47 |
+
def get_stages(self):
|
48 |
+
return [
|
49 |
+
nn.Identity(),
|
50 |
+
nn.Sequential(self.conv1, self.bn1, self.relu),
|
51 |
+
nn.Sequential(self.maxpool, self.layer1),
|
52 |
+
self.layer2,
|
53 |
+
self.layer3,
|
54 |
+
self.layer4,
|
55 |
+
]
|
56 |
+
|
57 |
+
def forward(self, x):
|
58 |
+
stages = self.get_stages()
|
59 |
+
|
60 |
+
features = []
|
61 |
+
for i in range(self._depth + 1):
|
62 |
+
x = stages[i](x)
|
63 |
+
features.append(x)
|
64 |
+
|
65 |
+
return features
|
66 |
+
|
67 |
+
def load_state_dict(self, state_dict, **kwargs):
|
68 |
+
state_dict.pop("fc.bias", None)
|
69 |
+
state_dict.pop("fc.weight", None)
|
70 |
+
super().load_state_dict(state_dict, **kwargs)
|
71 |
+
|
72 |
+
|
73 |
+
new_settings = {
|
74 |
+
"resnet18": {
|
75 |
+
"ssl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_supervised_resnet18-d92f0530.pth",
|
76 |
+
"swsl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_weakly_supervised_resnet18-118f1556.pth"
|
77 |
+
},
|
78 |
+
"resnet50": {
|
79 |
+
"ssl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_supervised_resnet50-08389792.pth",
|
80 |
+
"swsl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_weakly_supervised_resnet50-16a12f1b.pth"
|
81 |
+
},
|
82 |
+
"resnext50_32x4d": {
|
83 |
+
"imagenet": "https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth",
|
84 |
+
"ssl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_supervised_resnext50_32x4-ddb3e555.pth",
|
85 |
+
"swsl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_weakly_supervised_resnext50_32x4-72679e44.pth",
|
86 |
+
},
|
87 |
+
"resnext101_32x4d": {
|
88 |
+
"ssl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_supervised_resnext101_32x4-dc43570a.pth",
|
89 |
+
"swsl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_weakly_supervised_resnext101_32x4-3f87e46b.pth"
|
90 |
+
},
|
91 |
+
"resnext101_32x8d": {
|
92 |
+
"imagenet": "https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth",
|
93 |
+
"instagram": "https://download.pytorch.org/models/ig_resnext101_32x8-c38310e5.pth",
|
94 |
+
"ssl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_supervised_resnext101_32x8-2cfe2f8b.pth",
|
95 |
+
"swsl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_weakly_supervised_resnext101_32x8-b4712904.pth",
|
96 |
+
},
|
97 |
+
"resnext101_32x16d": {
|
98 |
+
"instagram": "https://download.pytorch.org/models/ig_resnext101_32x16-c6f796b0.pth",
|
99 |
+
"ssl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_supervised_resnext101_32x16-15fffa57.pth",
|
100 |
+
"swsl": "https://dl.fbaipublicfiles.com/semiweaksupervision/model_files/semi_weakly_supervised_resnext101_32x16-f3559a9c.pth",
|
101 |
+
},
|
102 |
+
"resnext101_32x32d": {
|
103 |
+
"instagram": "https://download.pytorch.org/models/ig_resnext101_32x32-e4b90b00.pth",
|
104 |
+
},
|
105 |
+
"resnext101_32x48d": {
|
106 |
+
"instagram": "https://download.pytorch.org/models/ig_resnext101_32x48-3e41cc8a.pth",
|
107 |
+
}
|
108 |
+
}
|
109 |
+
|
110 |
+
pretrained_settings = deepcopy(pretrained_settings)
|
111 |
+
for model_name, sources in new_settings.items():
|
112 |
+
if model_name not in pretrained_settings:
|
113 |
+
pretrained_settings[model_name] = {}
|
114 |
+
|
115 |
+
for source_name, source_url in sources.items():
|
116 |
+
pretrained_settings[model_name][source_name] = {
|
117 |
+
"url": source_url,
|
118 |
+
'input_size': [3, 224, 224],
|
119 |
+
'input_range': [0, 1],
|
120 |
+
'mean': [0.485, 0.456, 0.406],
|
121 |
+
'std': [0.229, 0.224, 0.225],
|
122 |
+
'num_classes': 1000
|
123 |
+
}
|
124 |
+
|
125 |
+
|
126 |
+
resnet_encoders = {
|
127 |
+
"resnet18": {
|
128 |
+
"encoder": ResNetEncoder,
|
129 |
+
"pretrained_settings": pretrained_settings["resnet18"],
|
130 |
+
"params": {
|
131 |
+
"out_channels": (3, 64, 64, 128, 256, 512),
|
132 |
+
"block": BasicBlock,
|
133 |
+
"layers": [2, 2, 2, 2],
|
134 |
+
},
|
135 |
+
},
|
136 |
+
"resnet34": {
|
137 |
+
"encoder": ResNetEncoder,
|
138 |
+
"pretrained_settings": pretrained_settings["resnet34"],
|
139 |
+
"params": {
|
140 |
+
"out_channels": (3, 64, 64, 128, 256, 512),
|
141 |
+
"block": BasicBlock,
|
142 |
+
"layers": [3, 4, 6, 3],
|
143 |
+
},
|
144 |
+
},
|
145 |
+
"resnet50": {
|
146 |
+
"encoder": ResNetEncoder,
|
147 |
+
"pretrained_settings": pretrained_settings["resnet50"],
|
148 |
+
"params": {
|
149 |
+
"out_channels": (3, 64, 256, 512, 1024, 2048),
|
150 |
+
"block": Bottleneck,
|
151 |
+
"layers": [3, 4, 6, 3],
|
152 |
+
},
|
153 |
+
},
|
154 |
+
"resnet101": {
|
155 |
+
"encoder": ResNetEncoder,
|
156 |
+
"pretrained_settings": pretrained_settings["resnet101"],
|
157 |
+
"params": {
|
158 |
+
"out_channels": (3, 64, 256, 512, 1024, 2048),
|
159 |
+
"block": Bottleneck,
|
160 |
+
"layers": [3, 4, 23, 3],
|
161 |
+
},
|
162 |
+
},
|
163 |
+
"resnet152": {
|
164 |
+
"encoder": ResNetEncoder,
|
165 |
+
"pretrained_settings": pretrained_settings["resnet152"],
|
166 |
+
"params": {
|
167 |
+
"out_channels": (3, 64, 256, 512, 1024, 2048),
|
168 |
+
"block": Bottleneck,
|
169 |
+
"layers": [3, 8, 36, 3],
|
170 |
+
},
|
171 |
+
},
|
172 |
+
"resnext50_32x4d": {
|
173 |
+
"encoder": ResNetEncoder,
|
174 |
+
"pretrained_settings": pretrained_settings["resnext50_32x4d"],
|
175 |
+
"params": {
|
176 |
+
"out_channels": (3, 64, 256, 512, 1024, 2048),
|
177 |
+
"block": Bottleneck,
|
178 |
+
"layers": [3, 4, 6, 3],
|
179 |
+
"groups": 32,
|
180 |
+
"width_per_group": 4,
|
181 |
+
},
|
182 |
+
},
|
183 |
+
"resnext101_32x4d": {
|
184 |
+
"encoder": ResNetEncoder,
|
185 |
+
"pretrained_settings": pretrained_settings["resnext101_32x4d"],
|
186 |
+
"params": {
|
187 |
+
"out_channels": (3, 64, 256, 512, 1024, 2048),
|
188 |
+
"block": Bottleneck,
|
189 |
+
"layers": [3, 4, 23, 3],
|
190 |
+
"groups": 32,
|
191 |
+
"width_per_group": 4,
|
192 |
+
},
|
193 |
+
},
|
194 |
+
"resnext101_32x8d": {
|
195 |
+
"encoder": ResNetEncoder,
|
196 |
+
"pretrained_settings": pretrained_settings["resnext101_32x8d"],
|
197 |
+
"params": {
|
198 |
+
"out_channels": (3, 64, 256, 512, 1024, 2048),
|
199 |
+
"block": Bottleneck,
|
200 |
+
"layers": [3, 4, 23, 3],
|
201 |
+
"groups": 32,
|
202 |
+
"width_per_group": 8,
|
203 |
+
},
|
204 |
+
},
|
205 |
+
"resnext101_32x16d": {
|
206 |
+
"encoder": ResNetEncoder,
|
207 |
+
"pretrained_settings": pretrained_settings["resnext101_32x16d"],
|
208 |
+
"params": {
|
209 |
+
"out_channels": (3, 64, 256, 512, 1024, 2048),
|
210 |
+
"block": Bottleneck,
|
211 |
+
"layers": [3, 4, 23, 3],
|
212 |
+
"groups": 32,
|
213 |
+
"width_per_group": 16,
|
214 |
+
},
|
215 |
+
},
|
216 |
+
"resnext101_32x32d": {
|
217 |
+
"encoder": ResNetEncoder,
|
218 |
+
"pretrained_settings": pretrained_settings["resnext101_32x32d"],
|
219 |
+
"params": {
|
220 |
+
"out_channels": (3, 64, 256, 512, 1024, 2048),
|
221 |
+
"block": Bottleneck,
|
222 |
+
"layers": [3, 4, 23, 3],
|
223 |
+
"groups": 32,
|
224 |
+
"width_per_group": 32,
|
225 |
+
},
|
226 |
+
},
|
227 |
+
"resnext101_32x48d": {
|
228 |
+
"encoder": ResNetEncoder,
|
229 |
+
"pretrained_settings": pretrained_settings["resnext101_32x48d"],
|
230 |
+
"params": {
|
231 |
+
"out_channels": (3, 64, 256, 512, 1024, 2048),
|
232 |
+
"block": Bottleneck,
|
233 |
+
"layers": [3, 4, 23, 3],
|
234 |
+
"groups": 32,
|
235 |
+
"width_per_group": 48,
|
236 |
+
},
|
237 |
+
},
|
238 |
+
}
|