11"""
22 mobilenetv3(configs::AbstractVector{<:Tuple}; width_mult::Real = 1,
3- max_width::Integer = 1024, inchannels::Integer = 3 ,
4- nclasses::Integer = 1000)
3+ max_width::Integer = 1024, dropout_rate = 0.2 ,
4+ inchannels::Integer = 3, nclasses::Integer = 1000)
55
66Create a MobileNetv3 model.
77([reference](https://arxiv.org/abs/1905.02244)).
@@ -19,38 +19,49 @@ Create a MobileNetv3 model.
1919
2020 - `width_mult`: Controls the number of output feature maps in each block
2121 (with 1 being the default in the paper; this is usually a value between 0.1 and 1.4.)
22- - `inchannels`: The number of input channels.
2322 - `max_width`: The maximum number of feature maps in any layer of the network
23+ - `dropout_rate`: The dropout rate to use in the classifier head. Set to `nothing` to disable.
24+ - `inchannels`: The number of input channels.
2425 - `nclasses`: the number of output classes
2526"""
2627function mobilenetv3(configs:: AbstractVector{<:Tuple} ; width_mult:: Real = 1 ,
27- max_width:: Integer = 1024 , dropout_rate = 0.2 ,
28+ max_width:: Integer = 1024 , reduced_tail:: Bool = false ,
29+ tail_dilated:: Bool = false , dropout_rate = 0.2 ,
2830 inchannels:: Integer = 3 , nclasses:: Integer = 1000 )
2931 # building first layer
3032 inplanes = _round_channels(16 * width_mult, 8 )
3133 layers = []
3234 append!(layers,
3335 conv_norm((3 , 3 ), inchannels, inplanes, hardswish; stride = 2 , pad = 1 ))
3436 explanes = 0
37+ nstages = length(configs)
38+ reduced_divider = 1
3539 # building inverted residual blocks
36- for (k, t, c, reduction, activation, stride) in configs
40+ for (i, (k, t, c, reduction, activation, stride)) in enumerate(configs)
41+ dilation = 1
42+ if nstages - i <= 2
43+ if reduced_tail
44+ reduced_divider = 2
45+ c /= reduced_divider
46+ end
47+ if tail_dilated
48+ dilation = 2
49+ end
50+ end
3751 # inverted residual layers
3852 outplanes = _round_channels(c * width_mult, 8 )
3953 explanes = _round_channels(inplanes * t, 8 )
4054 push!(layers,
4155 mbconv((k, k), inplanes, explanes, outplanes, activation;
42- stride, reduction))
56+ stride, reduction, dilation ))
4357 inplanes = outplanes
4458 end
4559 # building last layers
46- headplanes = width_mult > 1.0 ? _round_channels(max_width * width_mult, 8 ) :
47- max_width
60+ headplanes = _round_channels(max_width ÷ reduced_divider * width_mult, 8 )
4861 append!(layers, conv_norm((1 , 1 ), inplanes, explanes, hardswish))
49- classifier = Chain(AdaptiveMeanPool((1 , 1 )), MLUtils. flatten,
50- Dense(explanes, headplanes, hardswish),
51- Dropout(dropout_rate),
52- Dense(headplanes, nclasses))
53- return Chain(Chain(layers... ), classifier)
62+ return Chain(Chain(layers... ),
63+ create_classifier(explanes, headplanes, nclasses,
64+ (hardswish, identity); dropout_rate))
5465end
5566
5667# Layer configurations for small and large models for MobileNetv3
0 commit comments