16
16
17
17
from . import register_model
18
18
from .classy_model import ClassyModel
19
+ from .common import SqueezeAndExcitationLayer
19
20
20
21
21
22
# global setting for in-place ReLU:
22
23
INPLACE = True
23
24
24
25
25
26
class _DenseLayer (nn .Sequential ):
26
- """
27
- Single layer of a DenseNet.
28
- """
29
-
30
- def __init__ (self , in_planes , growth_rate = 32 , expansion = 4 ):
27
+ """Single layer of a DenseNet."""
31
28
29
+ def __init__ (
30
+ self ,
31
+ in_planes ,
32
+ growth_rate = 32 ,
33
+ expansion = 4 ,
34
+ use_se = False ,
35
+ se_reduction_ratio = 16 ,
36
+ ):
32
37
# assertions:
33
38
assert is_pos_int (in_planes )
34
39
assert is_pos_int (growth_rate )
@@ -56,6 +61,13 @@ def __init__(self, in_planes, growth_rate=32, expansion=4):
56
61
bias = False ,
57
62
),
58
63
)
64
+ if use_se :
65
+ self .add_module (
66
+ "se" ,
67
+ SqueezeAndExcitationLayer (
68
+ growth_rate , reduction_ratio = se_reduction_ratio
69
+ ),
70
+ )
59
71
60
72
def forward (self , x ):
61
73
new_features = super (_DenseLayer , self ).forward (x )
@@ -98,22 +110,27 @@ def __init__(
98
110
expansion ,
99
111
small_input ,
100
112
final_bn_relu ,
113
+ use_se = False ,
114
+ se_reduction_ratio = 16 ,
101
115
):
102
116
"""
103
117
Implementation of a standard densely connected network (DenseNet).
104
118
105
- Set `small_input` to `True` for 32x32 sized image inputs.
106
-
107
- Set `final_bn_relu` to `False` to exclude the final batchnorm and ReLU
108
- layers. These settings are useful when
109
- training Siamese networks.
110
-
111
119
Contains the following attachable blocks:
112
120
block{block_idx}-{idx}: This is the output of each dense block,
113
121
indexed by the block index and the index of the dense layer
114
122
transition-{idx}: This is the output of the transition layers
115
123
trunk_output: The final output of the `DenseNet`. This is
116
124
where a `fully_connected` head is normally attached.
125
+
126
+ Args:
127
+ small_input: set to `True` for 32x32 sized image inputs.
128
+ final_bn_relu: set to `False` to exclude the final batchnorm and
129
+ ReLU layers. These settings are useful when training Siamese
130
+ networks.
131
+ use_se: Enable squeeze and excitation
132
+ se_reduction_ratio: The reduction ratio to apply in the excitation
133
+ stage. Only used if `use_se` is `True`.
117
134
"""
118
135
super ().__init__ ()
119
136
@@ -158,6 +175,8 @@ def __init__(
158
175
idx ,
159
176
growth_rate = growth_rate ,
160
177
expansion = expansion ,
178
+ use_se = use_se ,
179
+ se_reduction_ratio = se_reduction_ratio ,
161
180
)
162
181
blocks .append (block )
163
182
num_planes = num_planes + num_layers * growth_rate
@@ -192,7 +211,14 @@ def _make_trunk_output_block(self, num_planes, final_bn_relu):
192
211
return self .build_attachable_block ("trunk_output" , layers )
193
212
194
213
def _make_dense_block (
195
- self , num_layers , in_planes , block_idx , growth_rate = 32 , expansion = 4
214
+ self ,
215
+ num_layers ,
216
+ in_planes ,
217
+ block_idx ,
218
+ growth_rate = 32 ,
219
+ expansion = 4 ,
220
+ use_se = False ,
221
+ se_reduction_ratio = 16 ,
196
222
):
197
223
assert is_pos_int (in_planes )
198
224
assert is_pos_int (growth_rate )
@@ -208,6 +234,8 @@ def _make_dense_block(
208
234
in_planes + idx * growth_rate ,
209
235
growth_rate = growth_rate ,
210
236
expansion = expansion ,
237
+ use_se = use_se ,
238
+ se_reduction_ratio = se_reduction_ratio ,
211
239
),
212
240
)
213
241
)
@@ -233,6 +261,8 @@ def from_config(cls, config: Dict[str, Any]) -> "DenseNet":
233
261
"expansion" : config .get ("expansion" , 4 ),
234
262
"small_input" : config .get ("small_input" , False ),
235
263
"final_bn_relu" : config .get ("final_bn_relu" , True ),
264
+ "use_se" : config .get ("use_se" , False ),
265
+ "se_reduction_ratio" : config .get ("se_reduction_ratio" , 16 ),
236
266
}
237
267
return cls (** config )
238
268
0 commit comments