Spaces:
Runtime error
Runtime error
Upload encoders/timm_gernet.py
Browse files- encoders/timm_gernet.py +124 -0
encoders/timm_gernet.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from timm.models import ByoModelCfg, ByoBlockCfg, ByobNet
|
2 |
+
|
3 |
+
from ._base import EncoderMixin
|
4 |
+
import torch.nn as nn
|
5 |
+
|
6 |
+
|
7 |
+
class GERNetEncoder(ByobNet, EncoderMixin):
|
8 |
+
def __init__(self, out_channels, depth=5, **kwargs):
|
9 |
+
super().__init__(**kwargs)
|
10 |
+
self._depth = depth
|
11 |
+
self._out_channels = out_channels
|
12 |
+
self._in_channels = 3
|
13 |
+
|
14 |
+
del self.head
|
15 |
+
|
16 |
+
def get_stages(self):
|
17 |
+
return [
|
18 |
+
nn.Identity(),
|
19 |
+
self.stem,
|
20 |
+
self.stages[0],
|
21 |
+
self.stages[1],
|
22 |
+
self.stages[2],
|
23 |
+
nn.Sequential(self.stages[3], self.stages[4], self.final_conv)
|
24 |
+
]
|
25 |
+
|
26 |
+
def forward(self, x):
|
27 |
+
stages = self.get_stages()
|
28 |
+
|
29 |
+
features = []
|
30 |
+
for i in range(self._depth + 1):
|
31 |
+
x = stages[i](x)
|
32 |
+
features.append(x)
|
33 |
+
|
34 |
+
return features
|
35 |
+
|
36 |
+
def load_state_dict(self, state_dict, **kwargs):
|
37 |
+
state_dict.pop("head.fc.weight", None)
|
38 |
+
state_dict.pop("head.fc.bias", None)
|
39 |
+
super().load_state_dict(state_dict, **kwargs)
|
40 |
+
|
41 |
+
|
42 |
+
regnet_weights = {
|
43 |
+
'timm-gernet_s': {
|
44 |
+
'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-ger-weights/gernet_s-756b4751.pth',
|
45 |
+
},
|
46 |
+
'timm-gernet_m': {
|
47 |
+
'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-ger-weights/gernet_m-0873c53a.pth',
|
48 |
+
},
|
49 |
+
'timm-gernet_l': {
|
50 |
+
'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-ger-weights/gernet_l-f31e2e8d.pth',
|
51 |
+
},
|
52 |
+
}
|
53 |
+
|
54 |
+
pretrained_settings = {}
|
55 |
+
for model_name, sources in regnet_weights.items():
|
56 |
+
pretrained_settings[model_name] = {}
|
57 |
+
for source_name, source_url in sources.items():
|
58 |
+
pretrained_settings[model_name][source_name] = {
|
59 |
+
"url": source_url,
|
60 |
+
'input_range': [0, 1],
|
61 |
+
'mean': [0.485, 0.456, 0.406],
|
62 |
+
'std': [0.229, 0.224, 0.225],
|
63 |
+
'num_classes': 1000
|
64 |
+
}
|
65 |
+
|
66 |
+
timm_gernet_encoders = {
|
67 |
+
'timm-gernet_s': {
|
68 |
+
'encoder': GERNetEncoder,
|
69 |
+
"pretrained_settings": pretrained_settings["timm-gernet_s"],
|
70 |
+
'params': {
|
71 |
+
'out_channels': (3, 13, 48, 48, 384, 1920),
|
72 |
+
'cfg': ByoModelCfg(
|
73 |
+
blocks=(
|
74 |
+
ByoBlockCfg(type='basic', d=1, c=48, s=2, gs=0, br=1.),
|
75 |
+
ByoBlockCfg(type='basic', d=3, c=48, s=2, gs=0, br=1.),
|
76 |
+
ByoBlockCfg(type='bottle', d=7, c=384, s=2, gs=0, br=1 / 4),
|
77 |
+
ByoBlockCfg(type='bottle', d=2, c=560, s=2, gs=1, br=3.),
|
78 |
+
ByoBlockCfg(type='bottle', d=1, c=256, s=1, gs=1, br=3.),
|
79 |
+
),
|
80 |
+
stem_chs=13,
|
81 |
+
stem_pool=None,
|
82 |
+
num_features=1920,
|
83 |
+
)
|
84 |
+
},
|
85 |
+
},
|
86 |
+
'timm-gernet_m': {
|
87 |
+
'encoder': GERNetEncoder,
|
88 |
+
"pretrained_settings": pretrained_settings["timm-gernet_m"],
|
89 |
+
'params': {
|
90 |
+
'out_channels': (3, 32, 128, 192, 640, 2560),
|
91 |
+
'cfg': ByoModelCfg(
|
92 |
+
blocks=(
|
93 |
+
ByoBlockCfg(type='basic', d=1, c=128, s=2, gs=0, br=1.),
|
94 |
+
ByoBlockCfg(type='basic', d=2, c=192, s=2, gs=0, br=1.),
|
95 |
+
ByoBlockCfg(type='bottle', d=6, c=640, s=2, gs=0, br=1 / 4),
|
96 |
+
ByoBlockCfg(type='bottle', d=4, c=640, s=2, gs=1, br=3.),
|
97 |
+
ByoBlockCfg(type='bottle', d=1, c=640, s=1, gs=1, br=3.),
|
98 |
+
),
|
99 |
+
stem_chs=32,
|
100 |
+
stem_pool=None,
|
101 |
+
num_features=2560,
|
102 |
+
)
|
103 |
+
},
|
104 |
+
},
|
105 |
+
'timm-gernet_l': {
|
106 |
+
'encoder': GERNetEncoder,
|
107 |
+
"pretrained_settings": pretrained_settings["timm-gernet_l"],
|
108 |
+
'params': {
|
109 |
+
'out_channels': (3, 32, 128, 192, 640, 2560),
|
110 |
+
'cfg': ByoModelCfg(
|
111 |
+
blocks=(
|
112 |
+
ByoBlockCfg(type='basic', d=1, c=128, s=2, gs=0, br=1.),
|
113 |
+
ByoBlockCfg(type='basic', d=2, c=192, s=2, gs=0, br=1.),
|
114 |
+
ByoBlockCfg(type='bottle', d=6, c=640, s=2, gs=0, br=1 / 4),
|
115 |
+
ByoBlockCfg(type='bottle', d=5, c=640, s=2, gs=1, br=3.),
|
116 |
+
ByoBlockCfg(type='bottle', d=4, c=640, s=1, gs=1, br=3.),
|
117 |
+
),
|
118 |
+
stem_chs=32,
|
119 |
+
stem_pool=None,
|
120 |
+
num_features=2560,
|
121 |
+
)
|
122 |
+
},
|
123 |
+
},
|
124 |
+
}
|