josedolot commited on
Commit
405c134
·
1 Parent(s): 1ce1d40

Upload encoders/inceptionresnetv2.py

Browse files
Files changed (1) hide show
  1. encoders/inceptionresnetv2.py +90 -0
encoders/inceptionresnetv2.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
26
+ import torch.nn as nn
27
+ from pretrainedmodels.models.inceptionresnetv2 import InceptionResNetV2
28
+ from pretrainedmodels.models.inceptionresnetv2 import pretrained_settings
29
+
30
+ from ._base import EncoderMixin
31
+
32
+
33
+ class InceptionResNetV2Encoder(InceptionResNetV2, EncoderMixin):
34
+ def __init__(self, out_channels, depth=5, **kwargs):
35
+ super().__init__(**kwargs)
36
+
37
+ self._out_channels = out_channels
38
+ self._depth = depth
39
+ self._in_channels = 3
40
+
41
+ # correct paddings
42
+ for m in self.modules():
43
+ if isinstance(m, nn.Conv2d):
44
+ if m.kernel_size == (3, 3):
45
+ m.padding = (1, 1)
46
+ if isinstance(m, nn.MaxPool2d):
47
+ m.padding = (1, 1)
48
+
49
+ # remove linear layers
50
+ del self.avgpool_1a
51
+ del self.last_linear
52
+
53
+ def make_dilated(self, stage_list, dilation_list):
54
+ raise ValueError("InceptionResnetV2 encoder does not support dilated mode "
55
+ "due to pooling operation for downsampling!")
56
+
57
+ def get_stages(self):
58
+ return [
59
+ nn.Identity(),
60
+ nn.Sequential(self.conv2d_1a, self.conv2d_2a, self.conv2d_2b),
61
+ nn.Sequential(self.maxpool_3a, self.conv2d_3b, self.conv2d_4a),
62
+ nn.Sequential(self.maxpool_5a, self.mixed_5b, self.repeat),
63
+ nn.Sequential(self.mixed_6a, self.repeat_1),
64
+ nn.Sequential(self.mixed_7a, self.repeat_2, self.block8, self.conv2d_7b),
65
+ ]
66
+
67
+ def forward(self, x):
68
+
69
+ stages = self.get_stages()
70
+
71
+ features = []
72
+ for i in range(self._depth + 1):
73
+ x = stages[i](x)
74
+ features.append(x)
75
+
76
+ return features
77
+
78
+ def load_state_dict(self, state_dict, **kwargs):
79
+ state_dict.pop("last_linear.bias", None)
80
+ state_dict.pop("last_linear.weight", None)
81
+ super().load_state_dict(state_dict, **kwargs)
82
+
83
+
84
+ inceptionresnetv2_encoders = {
85
+ "inceptionresnetv2": {
86
+ "encoder": InceptionResNetV2Encoder,
87
+ "pretrained_settings": pretrained_settings["inceptionresnetv2"],
88
+ "params": {"out_channels": (3, 64, 192, 320, 1088, 1536), "num_classes": 1000},
89
+ }
90
+ }