1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! # Input Stems
//!
//! This is incompletely implemented.
//!
//! The target surface is this pile from ``class ResNet`` in ``timm``:
//! ```python,ignore
//! # Stem
//! deep_stem = 'deep' in stem_type
//! inplanes = stem_width * 2 if deep_stem else 64
//! if deep_stem:
//! stem_chs = (stem_width, stem_width)
//! if 'tiered' in stem_type:
//! stem_chs = (3 * (stem_width // 4), stem_width)
//! self.conv1 = nn.Sequential(*[
//! nn.Conv2d(in_chans, stem_chs[0], 3, stride=2, padding=1, bias=False),
//! norm_layer(stem_chs[0]),
//! act_layer(inplace=True),
//! nn.Conv2d(stem_chs[0], stem_chs[1], 3, stride=1, padding=1, bias=False),
//! norm_layer(stem_chs[1]),
//! act_layer(inplace=True),
//! nn.Conv2d(stem_chs[1], inplanes, 3, stride=1, padding=1, bias=False)
//! ])
//! else:
//! self.conv1 = nn.Conv2d(in_chans, inplanes, kernel_size=7, stride=2, padding=3, bias=False)
//!
//! self.bn1 = norm_layer(inplanes)
//! self.act1 = act_layer(inplace=True)
//! self.feature_info = [dict(num_chs=inplanes, reduction=2, module='act1')]
//!
//! # Stem pooling. The name 'maxpool' remains for weight compatibility.
//! if replace_stem_pool:
//! self.maxpool = nn.Sequential(*filter(None, [
//! nn.Conv2d(inplanes, inplanes, 3, stride=1 if aa_layer else 2, padding=1, bias=False),
//! create_aa(aa_layer, channels=inplanes, stride=2) if aa_layer is not None else None,
//! norm_layer(inplanes),
//! act_layer(inplace=True),
//! ]))
//! else:
//! if aa_layer is not None:
//! if issubclass(aa_layer, nn.AvgPool2d):
//! self.maxpool = aa_layer(2)
//! else:
//! self.maxpool = nn.Sequential(*[
//! nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
//! aa_layer(channels=inplanes, stride=2)
//! ])
//! else:
//! self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
//! ```
//!
//! `ConvNormAct`:
//! conv
//! norm
//! act
//!
//! Stem:
//! ```text,ignore
//! head: vec<(`ConvNormAct`)>
//! tail: Union[
//! Conv/[AA]?/Norm/Act |
//! [Max|AvgPool]? [AA]?
//! ]
//! ```
use ;
use crate;
/// `ResNet` input stem contract configuration.
///
/// High-level choice of stem variant (default single `7x7` conv, or the deep
/// three-`3x3`-conv variants). Lowers to a [`ResNetStemStructureConfig`] via
/// [`ResNetStemContractConfig::to_structure`], which in turn describes the
/// [`ResNetStem`] input module that downsamples and lifts an image batch into
/// the network's feature space.
/// `ResNet` input stem structure configuration.
///
/// The concrete layer layout of a [`ResNetStem`]: one to three conv/norm/act
/// stages plus an optional pooling layer. Produced by
/// [`ResNetStemContractConfig::to_structure`].
/// `ResNet` input stem module.
///
/// The first stage of a `ResNet`: it applies one to three conv/norm/act blocks
/// followed by an optional max-pool, downsampling the input image and lifting
/// it into the network's initial feature space before the residual stages.
/// [`ResNetStem::forward`] maps a `[batch, in_channels, h, w]` image batch to a
/// `[batch, planes, h', w']` feature tensor.
///
/// Its structure is described by [`ResNetStemStructureConfig`], selected via
/// the high-level [`ResNetStemContractConfig`].