Skip to content

Fix loading of pooling layers #2598

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 3 commits into from
Apr 15, 2025
Merged

Conversation

adrhill
Copy link
Contributor

@adrhill adrhill commented Mar 31, 2025

As discussed on Slack, I tried my hand at fixing #2584 and FluxML/Metalhead.jl#287.
This line of code appears to be enough to make Metalhead's VGG models load.

Without it, the following error is thrown:

julia> using Metalhead

julia> VGG(19; pretrain=true)
ERROR: ArgumentError: Tried to load Base.OneTo(0) into (:pad, :k, :stride) but the structures do not match.
Stacktrace:
  [1] loadmodel!(dst::MaxPool{2, 4}, src::Tuple{}; filter::Function, cache::IdSet{Any})
    @ Flux ~/Developer/Flux.jl/src/loading.jl:104
  [2] loadmodel!(dst::Tuple{…}, src::Tuple{…}; filter::Function, cache::IdSet{…})
    @ Flux ~/Developer/Flux.jl/src/loading.jl:118
  [3] loadmodel!(dst::Chain{Tuple{…}}, src::@NamedTuple{layers::Tuple{…}}; filter::Function, cache::IdSet{Any})
    @ Flux ~/Developer/Flux.jl/src/loading.jl:118

I could use some guidance on whether this fix is adequate and how to test it.

@mcabbott
Copy link
Member

mcabbott commented Apr 2, 2025

To be clear the before-and-after states are

julia> Flux.state(MaxPool((2,3)))
()

(jl_gaQBh1) pkg> st Flux
Status `/private/var/folders/yq/4p2zwd614y59gszh7y9ypyhh0000gn/T/jl_gaQBh1/Project.toml`
⌃ [587475ba] Flux v0.14.25

and

julia> Flux.state(MaxPool((2,3)))
(k = (2, 3), pad = (0, 0, 0, 0), stride = (2, 3))

(@v1.11) pkg> st Flux
Status `~/.julia/environments/v1.11/Project.toml`
  [587475ba] Flux v0.16.3

So this looks fine?

@adrhill
Copy link
Contributor Author

adrhill commented Apr 13, 2025

For the sake of documentation:

Flux v0.14:

julia> Flux.state(MaxPool((5, 5)))
()

julia> Flux.state(MeanPool((5, 5)))
()

julia> Flux.state(AdaptiveMaxPool((5, 5)))
()

julia> Flux.state(AdaptiveMeanPool((5, 5)))
()

julia> Flux.state(GlobalMaxPool())
()

julia> Flux.state(GlobalMeanPool())
()

Flux v0.16:

julia> Flux.state(MaxPool((5, 5)))
(k = (5, 5), pad = (0, 0, 0, 0), stride = (5, 5))

julia> Flux.state(MeanPool((5, 5)))
(k = (5, 5), pad = (0, 0, 0, 0), stride = (5, 5))

julia> Flux.state(AdaptiveMaxPool((5, 5)))
(out = (5, 5),)

julia> Flux.state(AdaptiveMeanPool((5, 5)))
(out = (5, 5),)

julia> Flux.state(GlobalMaxPool())
()

julia> Flux.state(GlobalMeanPool())
()

So I guess the global pooling layers require no modification?

@adrhill adrhill changed the title Fix loading of MaxPool layers Fix loading of pooling layers Apr 13, 2025
@mcabbott mcabbott merged commit 0e36af9 into FluxML:master Apr 15, 2025
1 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants