Skip to content
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

Add trilinear Upsample layer #1792

Merged
merged 3 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v0.12.9
* Fixed incorrect output and added GPU compatibility for [AlphaDropout](https://github.com/FluxML/Flux.jl/pull/1781).
* Add trilinear [Upsample layer](https://github.com/FluxML/Flux.jl/pull/1792).

## v0.12.8
* Optimized inference and gradient calculation of OneHotMatrix[pr](https://github.com/FluxML/Flux.jl/pull/1756)
Expand Down
1 change: 1 addition & 0 deletions docs/src/models/nnlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ NNlib.depthwiseconv
```@docs
NNlib.upsample_nearest
NNlib.upsample_bilinear
NNlib.upsample_trilinear
NNlib.pixel_shuffle
```

Expand Down
8 changes: 7 additions & 1 deletion src/layers/upsample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Currently supported upsampling `mode`s
and corresponding NNlib's methods are:
- `:nearest` -> [`NNlib.upsample_nearest`](@ref)
- `:bilinear` -> [`NNlib.upsample_bilinear`](@ref)
- `:trilinear` -> [`NNlib.upsample_trilinear`](@ref)

# Examples

Expand All @@ -35,7 +36,7 @@ struct Upsample{mode, S, T}
end

function Upsample(mode::Symbol = :nearest; scale = nothing, size = nothing)
mode in [:nearest, :bilinear] ||
mode in [:nearest, :bilinear, :trilinear] ||
throw(ArgumentError("mode=:$mode is not supported."))
if !(isnothing(scale) ⊻ isnothing(size))
throw(ArgumentError("Either scale or size should be specified (but not both)."))
Expand All @@ -58,6 +59,11 @@ end
(m::Upsample{:bilinear, Nothing})(x::AbstractArray) =
NNlib.upsample_bilinear(x; size=m.size)

(m::Upsample{:trilinear})(x::AbstractArray) =
NNlib.upsample_trilinear(x, m.scale)
(m::Upsample{:trilinear, Nothing})(x::AbstractArray) =
NNlib.upsample_trilinear(x; size=m.size)

function Base.show(io::IO, u::Upsample{mode}) where {mode}
print(io, "Upsample(")
print(io, ":", mode)
Expand Down
20 changes: 20 additions & 0 deletions test/layers/upsample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@
@test size(y) == (4, 6, 2, 3)
end

@testset "upsample trilinear" begin
m = Upsample(:trilinear, scale=(2, 3, 2))
x = rand(Float32, 3, 4, 2, 3, 4)
y = m(x)
@test y isa Array{Float32, 5}
@test size(y) == (6, 12, 4, 3, 4)

m = Upsample(:trilinear, scale=3)
x = rand(Float32, 3, 4, 2, 3, 4)
y = m(x)
@test y isa Array{Float32, 5}
@test size(y) == (9, 12, 6, 3, 4)

m = Upsample(:trilinear, size=(4, 6, 4))
x = rand(Float32, 3, 4, 2, 3, 4)
y = m(x)
@test y isa Array{Float32, 5}
@test size(y) == (4, 6, 4, 3, 4)
end

@testset "upsample nearest" begin
x = rand(Float32, 3, 2, 3)
m = Upsample(:nearest, scale=(2,))
Expand Down