josedolot commited on
Commit
5bf5f42
·
1 Parent(s): 3a2b5dd

Upload encoders/mobilenet.py

Browse files
Files changed (1) hide show
  1. encoders/mobilenet.py +83 -0
encoders/mobilenet.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 torchvision
27
+ import torch.nn as nn
28
+
29
+ from ._base import EncoderMixin
30
+
31
+
32
+ class MobileNetV2Encoder(torchvision.models.MobileNetV2, EncoderMixin):
33
+
34
+ def __init__(self, out_channels, depth=5, **kwargs):
35
+ super().__init__(**kwargs)
36
+ self._depth = depth
37
+ self._out_channels = out_channels
38
+ self._in_channels = 3
39
+ del self.classifier
40
+
41
+ def get_stages(self):
42
+ return [
43
+ nn.Identity(),
44
+ self.features[:2],
45
+ self.features[2:4],
46
+ self.features[4:7],
47
+ self.features[7:14],
48
+ self.features[14:],
49
+ ]
50
+
51
+ def forward(self, x):
52
+ stages = self.get_stages()
53
+
54
+ features = []
55
+ for i in range(self._depth + 1):
56
+ x = stages[i](x)
57
+ features.append(x)
58
+
59
+ return features
60
+
61
+ def load_state_dict(self, state_dict, **kwargs):
62
+ state_dict.pop("classifier.1.bias", None)
63
+ state_dict.pop("classifier.1.weight", None)
64
+ super().load_state_dict(state_dict, **kwargs)
65
+
66
+
67
+ mobilenet_encoders = {
68
+ "mobilenet_v2": {
69
+ "encoder": MobileNetV2Encoder,
70
+ "pretrained_settings": {
71
+ "imagenet": {
72
+ "mean": [0.485, 0.456, 0.406],
73
+ "std": [0.229, 0.224, 0.225],
74
+ "url": "https://download.pytorch.org/models/mobilenet_v2-b0353104.pth",
75
+ "input_space": "RGB",
76
+ "input_range": [0, 1],
77
+ },
78
+ },
79
+ "params": {
80
+ "out_channels": (3, 16, 24, 32, 96, 1280),
81
+ },
82
+ },
83
+ }