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

Upload encoders/inceptionv4.py

Browse files
Files changed (1) hide show
  1. encoders/inceptionv4.py +93 -0
encoders/inceptionv4.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.inceptionv4 import InceptionV4, BasicConv2d
28
+ from pretrainedmodels.models.inceptionv4 import pretrained_settings
29
+
30
+ from ._base import EncoderMixin
31
+
32
+
33
+ class InceptionV4Encoder(InceptionV4, EncoderMixin):
34
+ def __init__(self, stage_idxs, out_channels, depth=5, **kwargs):
35
+ super().__init__(**kwargs)
36
+ self._stage_idxs = stage_idxs
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.last_linear
51
+
52
+ def make_dilated(self, stage_list, dilation_list):
53
+ raise ValueError("InceptionV4 encoder does not support dilated mode "
54
+ "due to pooling operation for downsampling!")
55
+
56
+ def get_stages(self):
57
+ return [
58
+ nn.Identity(),
59
+ self.features[: self._stage_idxs[0]],
60
+ self.features[self._stage_idxs[0]: self._stage_idxs[1]],
61
+ self.features[self._stage_idxs[1]: self._stage_idxs[2]],
62
+ self.features[self._stage_idxs[2]: self._stage_idxs[3]],
63
+ self.features[self._stage_idxs[3]:],
64
+ ]
65
+
66
+ def forward(self, x):
67
+
68
+ stages = self.get_stages()
69
+
70
+ features = []
71
+ for i in range(self._depth + 1):
72
+ x = stages[i](x)
73
+ features.append(x)
74
+
75
+ return features
76
+
77
+ def load_state_dict(self, state_dict, **kwargs):
78
+ state_dict.pop("last_linear.bias", None)
79
+ state_dict.pop("last_linear.weight", None)
80
+ super().load_state_dict(state_dict, **kwargs)
81
+
82
+
83
+ inceptionv4_encoders = {
84
+ "inceptionv4": {
85
+ "encoder": InceptionV4Encoder,
86
+ "pretrained_settings": pretrained_settings["inceptionv4"],
87
+ "params": {
88
+ "stage_idxs": (3, 5, 9, 15),
89
+ "out_channels": (3, 64, 192, 384, 1024, 1536),
90
+ "num_classes": 1001,
91
+ },
92
+ }
93
+ }