Spaces:
Runtime error
Runtime error
Upload encoders/timm_mobilenetv3.py
Browse files- encoders/timm_mobilenetv3.py +175 -0
encoders/timm_mobilenetv3.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import timm
|
2 |
+
import numpy as np
|
3 |
+
import torch.nn as nn
|
4 |
+
|
5 |
+
from ._base import EncoderMixin
|
6 |
+
|
7 |
+
|
8 |
+
def _make_divisible(x, divisible_by=8):
|
9 |
+
return int(np.ceil(x * 1. / divisible_by) * divisible_by)
|
10 |
+
|
11 |
+
|
12 |
+
class MobileNetV3Encoder(nn.Module, EncoderMixin):
|
13 |
+
def __init__(self, model_name, width_mult, depth=5, **kwargs):
|
14 |
+
super().__init__()
|
15 |
+
if "large" not in model_name and "small" not in model_name:
|
16 |
+
raise ValueError(
|
17 |
+
'MobileNetV3 wrong model name {}'.format(model_name)
|
18 |
+
)
|
19 |
+
|
20 |
+
self._mode = "small" if "small" in model_name else "large"
|
21 |
+
self._depth = depth
|
22 |
+
self._out_channels = self._get_channels(self._mode, width_mult)
|
23 |
+
self._in_channels = 3
|
24 |
+
|
25 |
+
# minimal models replace hardswish with relu
|
26 |
+
self.model = timm.create_model(
|
27 |
+
model_name=model_name,
|
28 |
+
scriptable=True, # torch.jit scriptable
|
29 |
+
exportable=True, # onnx export
|
30 |
+
features_only=True,
|
31 |
+
)
|
32 |
+
|
33 |
+
def _get_channels(self, mode, width_mult):
|
34 |
+
if mode == "small":
|
35 |
+
channels = [16, 16, 24, 48, 576]
|
36 |
+
else:
|
37 |
+
channels = [16, 24, 40, 112, 960]
|
38 |
+
channels = [3,] + [_make_divisible(x * width_mult) for x in channels]
|
39 |
+
return tuple(channels)
|
40 |
+
|
41 |
+
def get_stages(self):
|
42 |
+
if self._mode == 'small':
|
43 |
+
return [
|
44 |
+
nn.Identity(),
|
45 |
+
nn.Sequential(
|
46 |
+
self.model.conv_stem,
|
47 |
+
self.model.bn1,
|
48 |
+
self.model.act1,
|
49 |
+
),
|
50 |
+
self.model.blocks[0],
|
51 |
+
self.model.blocks[1],
|
52 |
+
self.model.blocks[2:4],
|
53 |
+
self.model.blocks[4:],
|
54 |
+
]
|
55 |
+
elif self._mode == 'large':
|
56 |
+
return [
|
57 |
+
nn.Identity(),
|
58 |
+
nn.Sequential(
|
59 |
+
self.model.conv_stem,
|
60 |
+
self.model.bn1,
|
61 |
+
self.model.act1,
|
62 |
+
self.model.blocks[0],
|
63 |
+
),
|
64 |
+
self.model.blocks[1],
|
65 |
+
self.model.blocks[2],
|
66 |
+
self.model.blocks[3:5],
|
67 |
+
self.model.blocks[5:],
|
68 |
+
]
|
69 |
+
else:
|
70 |
+
ValueError('MobileNetV3 mode should be small or large, got {}'.format(self._mode))
|
71 |
+
|
72 |
+
def forward(self, x):
|
73 |
+
stages = self.get_stages()
|
74 |
+
|
75 |
+
features = []
|
76 |
+
for i in range(self._depth + 1):
|
77 |
+
x = stages[i](x)
|
78 |
+
features.append(x)
|
79 |
+
|
80 |
+
return features
|
81 |
+
|
82 |
+
def load_state_dict(self, state_dict, **kwargs):
|
83 |
+
state_dict.pop('conv_head.weight', None)
|
84 |
+
state_dict.pop('conv_head.bias', None)
|
85 |
+
state_dict.pop('classifier.weight', None)
|
86 |
+
state_dict.pop('classifier.bias', None)
|
87 |
+
self.model.load_state_dict(state_dict, **kwargs)
|
88 |
+
|
89 |
+
|
90 |
+
mobilenetv3_weights = {
|
91 |
+
'tf_mobilenetv3_large_075': {
|
92 |
+
'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_large_075-150ee8b0.pth'
|
93 |
+
},
|
94 |
+
'tf_mobilenetv3_large_100': {
|
95 |
+
'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_large_100-427764d5.pth'
|
96 |
+
},
|
97 |
+
'tf_mobilenetv3_large_minimal_100': {
|
98 |
+
'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_large_minimal_100-8596ae28.pth'
|
99 |
+
},
|
100 |
+
'tf_mobilenetv3_small_075': {
|
101 |
+
'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_small_075-da427f52.pth'
|
102 |
+
},
|
103 |
+
'tf_mobilenetv3_small_100': {
|
104 |
+
'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_small_100-37f49e2b.pth'
|
105 |
+
},
|
106 |
+
'tf_mobilenetv3_small_minimal_100': {
|
107 |
+
'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_small_minimal_100-922a7843.pth'
|
108 |
+
},
|
109 |
+
|
110 |
+
|
111 |
+
}
|
112 |
+
|
113 |
+
pretrained_settings = {}
|
114 |
+
for model_name, sources in mobilenetv3_weights.items():
|
115 |
+
pretrained_settings[model_name] = {}
|
116 |
+
for source_name, source_url in sources.items():
|
117 |
+
pretrained_settings[model_name][source_name] = {
|
118 |
+
"url": source_url,
|
119 |
+
'input_range': [0, 1],
|
120 |
+
'mean': [0.485, 0.456, 0.406],
|
121 |
+
'std': [0.229, 0.224, 0.225],
|
122 |
+
'input_space': 'RGB',
|
123 |
+
}
|
124 |
+
|
125 |
+
|
126 |
+
timm_mobilenetv3_encoders = {
|
127 |
+
'timm-mobilenetv3_large_075': {
|
128 |
+
'encoder': MobileNetV3Encoder,
|
129 |
+
'pretrained_settings': pretrained_settings['tf_mobilenetv3_large_075'],
|
130 |
+
'params': {
|
131 |
+
'model_name': 'tf_mobilenetv3_large_075',
|
132 |
+
'width_mult': 0.75
|
133 |
+
}
|
134 |
+
},
|
135 |
+
'timm-mobilenetv3_large_100': {
|
136 |
+
'encoder': MobileNetV3Encoder,
|
137 |
+
'pretrained_settings': pretrained_settings['tf_mobilenetv3_large_100'],
|
138 |
+
'params': {
|
139 |
+
'model_name': 'tf_mobilenetv3_large_100',
|
140 |
+
'width_mult': 1.0
|
141 |
+
}
|
142 |
+
},
|
143 |
+
'timm-mobilenetv3_large_minimal_100': {
|
144 |
+
'encoder': MobileNetV3Encoder,
|
145 |
+
'pretrained_settings': pretrained_settings['tf_mobilenetv3_large_minimal_100'],
|
146 |
+
'params': {
|
147 |
+
'model_name': 'tf_mobilenetv3_large_minimal_100',
|
148 |
+
'width_mult': 1.0
|
149 |
+
}
|
150 |
+
},
|
151 |
+
'timm-mobilenetv3_small_075': {
|
152 |
+
'encoder': MobileNetV3Encoder,
|
153 |
+
'pretrained_settings': pretrained_settings['tf_mobilenetv3_small_075'],
|
154 |
+
'params': {
|
155 |
+
'model_name': 'tf_mobilenetv3_small_075',
|
156 |
+
'width_mult': 0.75
|
157 |
+
}
|
158 |
+
},
|
159 |
+
'timm-mobilenetv3_small_100': {
|
160 |
+
'encoder': MobileNetV3Encoder,
|
161 |
+
'pretrained_settings': pretrained_settings['tf_mobilenetv3_small_100'],
|
162 |
+
'params': {
|
163 |
+
'model_name': 'tf_mobilenetv3_small_100',
|
164 |
+
'width_mult': 1.0
|
165 |
+
}
|
166 |
+
},
|
167 |
+
'timm-mobilenetv3_small_minimal_100': {
|
168 |
+
'encoder': MobileNetV3Encoder,
|
169 |
+
'pretrained_settings': pretrained_settings['tf_mobilenetv3_small_minimal_100'],
|
170 |
+
'params': {
|
171 |
+
'model_name': 'tf_mobilenetv3_small_minimal_100',
|
172 |
+
'width_mult': 1.0
|
173 |
+
}
|
174 |
+
},
|
175 |
+
}
|