-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving closer to the one true function
- Loading branch information
1 parent
fc03d70
commit 8e54d47
Showing
5 changed files
with
89 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,66 @@ | ||
function mbconv_builder(block_configs::AbstractVector{NTuple{6, Int}}, | ||
stage_idx::Integer; scalings::NTuple{2, Real} = (1, 1), | ||
norm_layer = BatchNorm) | ||
function mbconv_builder(block_configs::AbstractVector{<:Tuple}, | ||
inplanes::Integer, stage_idx::Integer; | ||
scalings::NTuple{2, Real} = (1, 1), norm_layer = BatchNorm, | ||
round_fn = planes -> _round_channels(planes, 8)) | ||
width_mult, depth_mult = scalings | ||
k, inplanes, outplanes, expansion, stride, nrepeats = block_configs[stage_idx] | ||
inplanes = _round_channels(inplanes * width_mult, 8) | ||
k, outplanes, expansion, stride, nrepeats, reduction, activation = block_configs[stage_idx] | ||
inplanes = stage_idx == 1 ? inplanes : block_configs[stage_idx - 1][2] | ||
inplanes = round_fn(inplanes * width_mult) | ||
outplanes = _round_channels(outplanes * width_mult, 8) | ||
function get_layers(block_idx) | ||
inplanes = block_idx == 1 ? inplanes : outplanes | ||
explanes = _round_channels(inplanes * expansion, 8) | ||
stride = block_idx == 1 ? stride : 1 | ||
block = mbconv((k, k), inplanes, explanes, outplanes, swish; norm_layer, | ||
stride, reduction = 4) | ||
block = mbconv((k, k), inplanes, explanes, outplanes, activation; norm_layer, | ||
stride, reduction) | ||
return stride == 1 && inplanes == outplanes ? (identity, block) : (block,) | ||
end | ||
return get_layers, ceil(Int, nrepeats * depth_mult) | ||
end | ||
|
||
function fused_mbconv_builder(block_configs::AbstractVector{NTuple{6, Int}}, | ||
stage_idx::Integer; scalings::NTuple{2, Real} = (1, 1), | ||
norm_layer = BatchNorm) | ||
k, inplanes, outplanes, expansion, stride, nrepeats = block_configs[stage_idx] | ||
function fused_mbconv_builder(block_configs::AbstractVector{<:Tuple}, | ||
inplanes::Integer, stage_idx::Integer; | ||
scalings::NTuple{2, Real} = (1, 1), norm_layer = BatchNorm) | ||
k, outplanes, expansion, stride, nrepeats, _, activation = block_configs[stage_idx] | ||
inplanes = stage_idx == 1 ? inplanes : block_configs[stage_idx - 1][2] | ||
function get_layers(block_idx) | ||
inplanes = block_idx == 1 ? inplanes : outplanes | ||
explanes = _round_channels(inplanes * expansion, 8) | ||
stride = block_idx == 1 ? stride : 1 | ||
block = fused_mbconv((k, k), inplanes, explanes, outplanes, swish; | ||
block = fused_mbconv((k, k), inplanes, explanes, outplanes, activation; | ||
norm_layer, stride) | ||
return stride == 1 && inplanes == outplanes ? (identity, block) : (block,) | ||
end | ||
return get_layers, nrepeats | ||
end | ||
|
||
function efficientnet_builder(block_configs::AbstractVector{NTuple{6, Int}}, | ||
residual_fns::AbstractVector; | ||
scalings::NTuple{2, Real} = (1, 1), norm_layer = BatchNorm) | ||
bxs = [residual_fn(block_configs, stage_idx; scalings, norm_layer) | ||
function mbconv_stack_builder(block_configs::AbstractVector{<:Tuple}, | ||
residual_fns::AbstractVector; inplanes::Integer, | ||
scalings::NTuple{2, Real} = (1, 1), | ||
norm_layer = BatchNorm) | ||
bxs = [residual_fn(block_configs, inplanes, stage_idx; scalings, norm_layer) | ||
for (stage_idx, residual_fn) in enumerate(residual_fns)] | ||
return (stage_idx, block_idx) -> first.(bxs)[stage_idx](block_idx), last.(bxs) | ||
end | ||
|
||
function efficientnet(block_configs::AbstractVector{NTuple{6, Int}}, | ||
residual_fns::AbstractVector; scalings::NTuple{2, Real} = (1, 1), | ||
function efficientnet(block_configs::AbstractVector{<:Tuple}, | ||
residual_fns::AbstractVector; inplanes::Integer, | ||
scalings::NTuple{2, Real} = (1, 1), | ||
headplanes::Integer = block_configs[end][3] * 4, | ||
norm_layer = BatchNorm, dropout_rate = nothing, | ||
inchannels::Integer = 3, nclasses::Integer = 1000) | ||
layers = [] | ||
# stem of the model | ||
append!(layers, | ||
conv_norm((3, 3), inchannels, | ||
_round_channels(block_configs[1][2] * scalings[1], 8), swish; | ||
norm_layer, stride = 2, pad = SamePad())) | ||
conv_norm((3, 3), inchannels, _round_channels(inplanes * scalings[1], 8), | ||
swish; norm_layer, stride = 2, pad = SamePad())) | ||
# building inverted residual blocks | ||
get_layers, block_repeats = efficientnet_builder(block_configs, residual_fns; | ||
scalings, norm_layer) | ||
get_layers, block_repeats = mbconv_stack_builder(block_configs, residual_fns; | ||
inplanes, scalings, norm_layer) | ||
append!(layers, resnet_stages(get_layers, block_repeats, +)) | ||
# building last layers | ||
append!(layers, | ||
conv_norm((1, 1), _round_channels(block_configs[end][3] * scalings[1], 8), | ||
conv_norm((1, 1), _round_channels(block_configs[end][2] * scalings[1], 8), | ||
headplanes, swish; pad = SamePad())) | ||
return Chain(Chain(layers...), create_classifier(headplanes, nclasses; dropout_rate)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters