-
-
Notifications
You must be signed in to change notification settings - Fork 67
Implementation of EfficientNetv2 and MNASNet #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
49faf09
Initial commit for EfficientNetv2
theabhirath d428da0
Cleanup
theabhirath 093f573
Add docs for EfficientNetv2
theabhirath add4d41
Add tests
theabhirath 7d56396
Fix Inception bug, and other misc. cleanup
theabhirath d3e4add
Refactor: `mbconv` instead of `invertedresidual`
theabhirath 6504f25
Refactor EfficientNets
theabhirath 3e7e5f7
Refactor EfficientNets
theabhirath 20a4b37
Fixes
theabhirath 34caab4
Merge branch 'effnetv2' of https://github.com/theabhirath/Metalhead.j…
theabhirath ea13dd5
Some refactors, some consistency, some features
theabhirath c998951
The real hero was `block_idx` all along
theabhirath fc03d70
Fix minor hiccups
theabhirath f2461a5
Moving closer to the one true function
theabhirath 9f6b987
Some more reorganisation
theabhirath 785a95a
Huge refactor of MobileNet and EfficientNet families
theabhirath 9e91783
Initial commit for EfficientNetv2
theabhirath 4a94569
Cleanup
theabhirath 69563a6
Add docs for EfficientNetv2
theabhirath 70841f6
Add tests
theabhirath 245eda0
Fix Inception bug, and other misc. cleanup
theabhirath 1c65159
Refactor: `mbconv` instead of `invertedresidual`
theabhirath 744f214
Refactor EfficientNets
theabhirath 9bc75fc
Fixes
theabhirath 3a37a70
Some refactors, some consistency, some features
theabhirath 593752f
The real hero was `block_idx` all along
theabhirath 55bc544
Fix minor hiccups
theabhirath 3d63f72
Moving closer to the one true function
theabhirath 818c584
Some more reorganisation
theabhirath 8092818
Huge refactor of MobileNet and EfficientNet families
theabhirath 510e913
Add MNASNet
theabhirath 8d323e6
Merge branch 'effnetv2' of https://github.com/theabhirath/Metalhead.j…
theabhirath ab2a15e
Add tests for MNASNet
theabhirath 2d310a9
Final cleanup, hopefully
theabhirath f76fadb
Minor refactor of `cnn_stages`
theabhirath 992f6a6
`_round_channels` all the way
theabhirath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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, 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, 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{<: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, activation; | ||
norm_layer, stride) | ||
return stride == 1 && inplanes == outplanes ? (identity, block) : (block,) | ||
end | ||
return get_layers, nrepeats | ||
end | ||
|
||
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{<: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(inplanes * scalings[1], 8), | ||
swish; norm_layer, stride = 2, pad = SamePad())) | ||
# building inverted residual blocks | ||
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][2] * scalings[1], 8), | ||
headplanes, swish; pad = SamePad())) | ||
return Chain(Chain(layers...), create_classifier(headplanes, nclasses; dropout_rate)) | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.