@@ -51,7 +51,7 @@ Creates a bottleneck residual block (see [reference](https://arxiv.org/abs/1512.
5151 - `stride`: the stride of the block
5252 - `cardinality`: the number of groups in the convolution.
5353 - `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
5555 convolution.
5656 - `activation`: the activation function to use.
5757 - `norm_layer`: the normalization layer to use.
@@ -190,7 +190,10 @@ function resnet_stem(stem_type::Symbol = :default; inchannels::Integer = 3,
190190 return Chain (conv1, bn1, stempool)
191191end
192192
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
194197
195198function basicblock_builder (block_repeats:: Vector{<:Integer} ; inplanes:: Integer = 64 ,
196199 reduction_factor:: Integer = 1 , expansion:: Integer = 1 ,
@@ -201,24 +204,25 @@ function basicblock_builder(block_repeats::Vector{<:Integer}; inplanes::Integer
201204 downsample_tuple = (downsample_conv, downsample_identity))
202205 pathschedule = linear_scheduler (drop_path_rate; depth = sum (block_repeats))
203206 blockschedule = linear_scheduler (drop_block_rate; depth = sum (block_repeats))
207+ planes_vec = collect (planes_fn (block_repeats))
204208 # closure over `idxs`
205209 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
207215 # `resnet_stride` is a callback that the user can tweak to change the stride of the
208216 # blocks. It defaults to the standard behaviour as in the paper
209217 stride = stride_fn (stage_idx, block_idx)
210218 downsample_fn = (stride != 1 || inplanes != planes * expansion) ?
211219 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
214220 drop_path = DropPath (pathschedule[schedule_idx])
215221 drop_block = DropBlock (blockschedule[schedule_idx])
216222 block = basicblock (inplanes, planes; stride, reduction_factor, activation,
217223 norm_layer, revnorm, attn_fn, drop_path, drop_block)
218224 downsample = downsample_fn (inplanes, planes * expansion; stride, norm_layer,
219225 revnorm)
220- # inplanes increases by expansion after each block
221- inplanes = planes * expansion
222226 return block, downsample
223227 end
224228 return get_layers
@@ -234,9 +238,14 @@ function bottleneck_builder(block_repeats::Vector{<:Integer}; inplanes::Integer
234238 downsample_tuple = (downsample_conv, downsample_identity))
235239 pathschedule = linear_scheduler (drop_path_rate; depth = sum (block_repeats))
236240 blockschedule = linear_scheduler (drop_block_rate; depth = sum (block_repeats))
241+ planes_vec = collect (planes_fn (block_repeats))
237242 # closure over `idxs`
238243 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
240249 # `resnet_stride` is a callback that the user can tweak to change the stride of the
241250 # blocks. It defaults to the standard behaviour as in the paper
242251 stride = stride_fn (stage_idx, block_idx)
@@ -251,8 +260,6 @@ function bottleneck_builder(block_repeats::Vector{<:Integer}; inplanes::Integer
251260 attn_fn, drop_path, drop_block)
252261 downsample = downsample_fn (inplanes, planes * expansion; stride, norm_layer,
253262 revnorm)
254- # inplanes increases by expansion after each block
255- inplanes = planes * expansion
256263 return block, downsample
257264 end
258265 return get_layers
0 commit comments