@@ -51,7 +51,7 @@ Creates a bottleneck residual block (see [reference](https://arxiv.org/abs/1512.
51
51
- `stride`: the stride of the block
52
52
- `cardinality`: the number of groups in the convolution.
53
53
- `base_width`: the number of output feature maps for each convolutional group.
54
- - `reduction_factor`: the reduction factor that the input feature maps are reduced by before the first
54
+ - `reduction_factor`: the factor by which the input feature maps are reduced before the first
55
55
convolution.
56
56
- `activation`: the activation function to use.
57
57
- `norm_layer`: the normalization layer to use.
@@ -190,7 +190,10 @@ function resnet_stem(stem_type::Symbol = :default; inchannels::Integer = 3,
190
190
return Chain (conv1, bn1, stempool)
191
191
end
192
192
193
- resnet_planes (stage_idx:: Integer ) = 64 * 2 ^ (stage_idx - 1 )
193
+ function resnet_planes (block_repeats:: Vector{<:Integer} )
194
+ return Iterators. flatten ((64 * 2 ^ (stage_idx - 1 ) for _ in 1 : stages)
195
+ for (stage_idx, stages) in enumerate (block_repeats))
196
+ end
194
197
195
198
function basicblock_builder (block_repeats:: Vector{<:Integer} ; inplanes:: Integer = 64 ,
196
199
reduction_factor:: Integer = 1 , expansion:: Integer = 1 ,
@@ -201,24 +204,25 @@ function basicblock_builder(block_repeats::Vector{<:Integer}; inplanes::Integer
201
204
downsample_tuple = (downsample_conv, downsample_identity))
202
205
pathschedule = linear_scheduler (drop_path_rate; depth = sum (block_repeats))
203
206
blockschedule = linear_scheduler (drop_block_rate; depth = sum (block_repeats))
207
+ planes_vec = collect (planes_fn (block_repeats))
204
208
# closure over `idxs`
205
209
function get_layers (stage_idx:: Integer , block_idx:: Integer )
206
- planes = planes_fn (stage_idx)
210
+ # DropBlock, DropPath both take in rates based on a linear scaling schedule
211
+ # This is also needed for block `inplanes` and `planes` calculations
212
+ schedule_idx = sum (block_repeats[1 : (stage_idx - 1 )]) + block_idx
213
+ planes = planes_vec[schedule_idx]
214
+ inplanes = schedule_idx == 1 ? inplanes : planes_vec[schedule_idx - 1 ] * expansion
207
215
# `resnet_stride` is a callback that the user can tweak to change the stride of the
208
216
# blocks. It defaults to the standard behaviour as in the paper
209
217
stride = stride_fn (stage_idx, block_idx)
210
218
downsample_fn = (stride != 1 || inplanes != planes * expansion) ?
211
219
downsample_tuple[1 ] : downsample_tuple[2 ]
212
- # DropBlock, DropPath both take in rates based on a linear scaling schedule
213
- schedule_idx = sum (block_repeats[1 : (stage_idx - 1 )]) + block_idx
214
220
drop_path = DropPath (pathschedule[schedule_idx])
215
221
drop_block = DropBlock (blockschedule[schedule_idx])
216
222
block = basicblock (inplanes, planes; stride, reduction_factor, activation,
217
223
norm_layer, revnorm, attn_fn, drop_path, drop_block)
218
224
downsample = downsample_fn (inplanes, planes * expansion; stride, norm_layer,
219
225
revnorm)
220
- # inplanes increases by expansion after each block
221
- inplanes = planes * expansion
222
226
return block, downsample
223
227
end
224
228
return get_layers
@@ -234,9 +238,14 @@ function bottleneck_builder(block_repeats::Vector{<:Integer}; inplanes::Integer
234
238
downsample_tuple = (downsample_conv, downsample_identity))
235
239
pathschedule = linear_scheduler (drop_path_rate; depth = sum (block_repeats))
236
240
blockschedule = linear_scheduler (drop_block_rate; depth = sum (block_repeats))
241
+ planes_vec = collect (planes_fn (block_repeats))
237
242
# closure over `idxs`
238
243
function get_layers (stage_idx:: Integer , block_idx:: Integer )
239
- planes = planes_fn (stage_idx)
244
+ # DropBlock, DropPath both take in rates based on a linear scaling schedule
245
+ # This is also needed for block `inplanes` and `planes` calculations
246
+ schedule_idx = sum (block_repeats[1 : (stage_idx - 1 )]) + block_idx
247
+ planes = planes_vec[schedule_idx]
248
+ inplanes = schedule_idx == 1 ? inplanes : planes_vec[schedule_idx - 1 ] * expansion
240
249
# `resnet_stride` is a callback that the user can tweak to change the stride of the
241
250
# blocks. It defaults to the standard behaviour as in the paper
242
251
stride = stride_fn (stage_idx, block_idx)
@@ -251,8 +260,6 @@ function bottleneck_builder(block_repeats::Vector{<:Integer}; inplanes::Integer
251
260
attn_fn, drop_path, drop_block)
252
261
downsample = downsample_fn (inplanes, planes * expansion; stride, norm_layer,
253
262
revnorm)
254
- # inplanes increases by expansion after each block
255
- inplanes = planes * expansion
256
263
return block, downsample
257
264
end
258
265
return get_layers
0 commit comments