Tim77777767
commited on
Commit
·
827f017
1
Parent(s):
c76cec1
Anpassungen am Head, damit bias akzeptiert
Browse files
modeling_my_segformer.py
CHANGED
@@ -32,7 +32,6 @@ class MySegformerForSemanticSegmentation(PreTrainedModel):
|
|
32 |
)
|
33 |
|
34 |
# Head initialization
|
35 |
-
# Use config.decode_head.in_channels directly, as it's defined in the config.
|
36 |
in_channels = config.decode_head["in_channels"]
|
37 |
|
38 |
self.segmentation_head = SegformerHead(
|
@@ -40,11 +39,9 @@ class MySegformerForSemanticSegmentation(PreTrainedModel):
|
|
40 |
in_index=list(config.out_indices),
|
41 |
channels=config.decode_head["channels"],
|
42 |
dropout_ratio=config.decode_head["dropout_ratio"],
|
43 |
-
# REMOVED: num_classes=getattr(config, "num_classes", 19), <--- Entfernen Sie diese Zeile
|
44 |
align_corners=config.decode_head["align_corners"],
|
45 |
-
|
46 |
-
|
47 |
-
interpolate_mode=config.decode_head["interpolate_mode"] # <-- Hinzugefügt
|
48 |
)
|
49 |
|
50 |
self.post_init()
|
|
|
32 |
)
|
33 |
|
34 |
# Head initialization
|
|
|
35 |
in_channels = config.decode_head["in_channels"]
|
36 |
|
37 |
self.segmentation_head = SegformerHead(
|
|
|
39 |
in_index=list(config.out_indices),
|
40 |
channels=config.decode_head["channels"],
|
41 |
dropout_ratio=config.decode_head["dropout_ratio"],
|
|
|
42 |
align_corners=config.decode_head["align_corners"],
|
43 |
+
interpolate_mode=config.decode_head["interpolate_mode"],
|
44 |
+
use_conv_bias_in_convmodules=False
|
|
|
45 |
)
|
46 |
|
47 |
self.post_init()
|
segformer_plusplus/model/head/segformer_head.py
CHANGED
@@ -18,6 +18,10 @@ class SegformerHead(BaseModule):
|
|
18 |
Args:
|
19 |
interpolate_mode: The interpolate mode of MLP head upsample operation.
|
20 |
Default: 'bilinear'.
|
|
|
|
|
|
|
|
|
21 |
"""
|
22 |
|
23 |
def __init__(self,
|
@@ -28,7 +32,9 @@ class SegformerHead(BaseModule):
|
|
28 |
out_channels=19,
|
29 |
norm_cfg=None,
|
30 |
align_corners=False,
|
31 |
-
interpolate_mode='bilinear'
|
|
|
|
|
32 |
super().__init__()
|
33 |
|
34 |
self.in_channels = in_channels
|
@@ -39,6 +45,7 @@ class SegformerHead(BaseModule):
|
|
39 |
self.norm_cfg = norm_cfg
|
40 |
self.align_corners = align_corners
|
41 |
self.interpolate_mode = interpolate_mode
|
|
|
42 |
|
43 |
self.act_cfg = dict(type='ReLU')
|
44 |
self.conv_seg = nn.Conv2d(channels, self.out_channels, kernel_size=1)
|
@@ -49,9 +56,15 @@ class SegformerHead(BaseModule):
|
|
49 |
|
50 |
num_inputs = len(self.in_channels)
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
self.convs = nn.ModuleList()
|
57 |
for i in range(num_inputs):
|
@@ -62,13 +75,17 @@ class SegformerHead(BaseModule):
|
|
62 |
kernel_size=1,
|
63 |
stride=1,
|
64 |
norm_cfg=self.norm_cfg,
|
65 |
-
act_cfg=self.act_cfg
|
|
|
|
|
66 |
|
67 |
self.fusion_conv = ConvModule(
|
68 |
in_channels=self.channels * num_inputs,
|
69 |
out_channels=self.channels,
|
70 |
kernel_size=1,
|
71 |
-
norm_cfg=self.norm_cfg
|
|
|
|
|
72 |
|
73 |
def cls_seg(self, feat):
|
74 |
"""Classify each pixel."""
|
@@ -94,4 +111,4 @@ class SegformerHead(BaseModule):
|
|
94 |
|
95 |
out = self.cls_seg(out)
|
96 |
|
97 |
-
return out
|
|
|
18 |
Args:
|
19 |
interpolate_mode: The interpolate mode of MLP head upsample operation.
|
20 |
Default: 'bilinear'.
|
21 |
+
use_conv_bias_in_convmodules (bool | str): If True, ConvModules will use bias.
|
22 |
+
If False, they won't. If 'auto', they follow ConvModule's default.
|
23 |
+
This is added for compatibility with models trained with no conv bias
|
24 |
+
when followed by BatchNorm, while keeping default local behavior.
|
25 |
"""
|
26 |
|
27 |
def __init__(self,
|
|
|
32 |
out_channels=19,
|
33 |
norm_cfg=None,
|
34 |
align_corners=False,
|
35 |
+
interpolate_mode='bilinear',
|
36 |
+
use_conv_bias_in_convmodules: bool | str = 'auto'
|
37 |
+
):
|
38 |
super().__init__()
|
39 |
|
40 |
self.in_channels = in_channels
|
|
|
45 |
self.norm_cfg = norm_cfg
|
46 |
self.align_corners = align_corners
|
47 |
self.interpolate_mode = interpolate_mode
|
48 |
+
self.use_conv_bias_in_convmodules = use_conv_bias_in_convmodules # Speichern des neuen Parameters
|
49 |
|
50 |
self.act_cfg = dict(type='ReLU')
|
51 |
self.conv_seg = nn.Conv2d(channels, self.out_channels, kernel_size=1)
|
|
|
56 |
|
57 |
num_inputs = len(self.in_channels)
|
58 |
|
59 |
+
conv_module_bias_setting = use_conv_bias_in_convmodules
|
60 |
+
if use_conv_bias_in_convmodules == 'auto':
|
61 |
+
pass
|
62 |
+
elif isinstance(use_conv_bias_in_convmodules, bool):
|
63 |
+
# Wenn True/False explizit übergeben wird, verwenden wir das
|
64 |
+
conv_module_bias_setting = use_conv_bias_in_convmodules
|
65 |
+
else:
|
66 |
+
raise ValueError("use_conv_bias_in_convmodules must be 'auto', True, or False")
|
67 |
+
|
68 |
|
69 |
self.convs = nn.ModuleList()
|
70 |
for i in range(num_inputs):
|
|
|
75 |
kernel_size=1,
|
76 |
stride=1,
|
77 |
norm_cfg=self.norm_cfg,
|
78 |
+
act_cfg=self.act_cfg,
|
79 |
+
bias=conv_module_bias_setting # Verwende den bestimmten Bias-Wert
|
80 |
+
))
|
81 |
|
82 |
self.fusion_conv = ConvModule(
|
83 |
in_channels=self.channels * num_inputs,
|
84 |
out_channels=self.channels,
|
85 |
kernel_size=1,
|
86 |
+
norm_cfg=self.norm_cfg,
|
87 |
+
bias=conv_module_bias_setting # Verwende den bestimmten Bias-Wert
|
88 |
+
)
|
89 |
|
90 |
def cls_seg(self, feat):
|
91 |
"""Classify each pixel."""
|
|
|
111 |
|
112 |
out = self.cls_seg(out)
|
113 |
|
114 |
+
return out
|