-
-
Notifications
You must be signed in to change notification settings - Fork 611
add Upsample and PixelShuffle layers #1468
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 all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
662ef5f
add Upsample and PixelShuffle layers
CarloLucibello 3db3dad
scale; add news; docs
CarloLucibello 39b892d
Update src/layers/upsample.jl
CarloLucibello 96ab813
small fixes
CarloLucibello d3c9cde
fix
CarloLucibello d50df60
add neirest
CarloLucibello 320d9a8
add gpu tests
CarloLucibello f531206
arg error
CarloLucibello 93287dd
update
CarloLucibello bf06f0c
update to latest nnlib
CarloLucibello 6d1d55f
fix outputsize
CarloLucibello e252e46
new interface
CarloLucibello 9c91491
style cleanups
DhairyaLGandhi e2bc4e8
update docstring
CarloLucibello 05a9a2d
add other constructor + small changes
CarloLucibello 8503f4b
add test
CarloLucibello 91cac26
fix test
CarloLucibello 75094d6
fix test
CarloLucibello 9acaae9
Println -> print
DhairyaLGandhi 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
Upsample(mode = :nearest; [scale, size]) | ||
Upsample(scale, mode = :nearest) | ||
|
||
An upsampling layer. One of two keywords must be given: | ||
|
||
If `scale` is a number, this applies to all but the last two dimensions (channel and batch) of the input. | ||
It may also be a tuple, to control dimensions individually. Alternatively, keyword | ||
`size` accepts a tuple, to directly specify the leading dimensions of the output. | ||
|
||
Currently supported upsampling `mode`s | ||
and corresponding NNlib's methods are: | ||
- `:nearest` -> [`NNlib.upsample_nearest`](@ref) | ||
- `:bilinear` -> [`NNlib.upsample_bilinear`](@ref) | ||
|
||
# Examples | ||
|
||
```juliarepl | ||
julia> m = Upsample(scale = (2, 3)) | ||
Upsample(:nearest, scale = (2, 3)) | ||
|
||
julia> m(ones(2, 2, 1, 1)) |> size | ||
(4, 6, 1, 1) | ||
|
||
julia> m = Upsample(:bilinear, size = (4, 5)) | ||
Upsample(:bilinear, size = (4, 5)) | ||
|
||
julia> m(ones(2, 2, 1, 1)) |> size | ||
(4, 5, 1, 1) | ||
""" | ||
struct Upsample{mode, S, T} | ||
scale::S | ||
size::T | ||
end | ||
|
||
function Upsample(mode::Symbol = :nearest; scale = nothing, size = nothing) | ||
mode in [:nearest, :bilinear] || | ||
throw(ArgumentError("mode=:$mode is not supported.")) | ||
if !(isnothing(scale) ⊻ isnothing(size)) | ||
throw(ArgumentError("Either scale or size should be specified (but not both).")) | ||
end | ||
return Upsample{mode,typeof(scale),typeof(size)}(scale, size) | ||
end | ||
|
||
Upsample(scale, mode::Symbol = :nearest) = Upsample(mode; scale) | ||
|
||
(m::Upsample{:nearest})(x::AbstractArray) = | ||
NNlib.upsample_nearest(x, m.scale) | ||
function (m::Upsample{:nearest, Int})(x::AbstractArray{T, N}) where {T, N} | ||
NNlib.upsample_nearest(x, ntuple(i -> m.scale, N-2)) | ||
end | ||
(m::Upsample{:nearest, Nothing})(x::AbstractArray) = | ||
NNlib.upsample_nearest(x; size=m.size) | ||
|
||
(m::Upsample{:bilinear})(x::AbstractArray) = | ||
NNlib.upsample_bilinear(x, m.scale) | ||
(m::Upsample{:bilinear, Nothing})(x::AbstractArray) = | ||
NNlib.upsample_bilinear(x; size=m.size) | ||
CarloLucibello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function Base.show(io::IO, u::Upsample{mode}) where {mode} | ||
print(io, "Upsample(") | ||
print(io, ":", mode) | ||
u.scale !== nothing && print(io, ", scale = $(u.scale)") | ||
u.size !== nothing && print(io, ", size = $(u.size)") | ||
print(io, ")") | ||
end | ||
|
||
""" | ||
PixelShuffle(r::Int) | ||
|
||
Pixel shuffling layer with upscale factor `r`. | ||
|
||
See [`NNlib.pixel_shuffle`](@ref). | ||
""" | ||
struct PixelShuffle | ||
CarloLucibello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r::Int | ||
end | ||
|
||
(m::PixelShuffle)(x) = NNlib.pixel_shuffle(x, m.r) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
@testset "upsample bilinear" begin | ||
m = Upsample(:bilinear, scale=(2, 3)) | ||
x = rand(Float32, 3, 4, 2, 3) | ||
y = m(x) | ||
@test y isa Array{Float32, 4} | ||
@test size(y) == (6, 12, 2, 3) | ||
|
||
m = Upsample(:bilinear, scale=3) | ||
x = rand(Float32, 3, 4, 2, 3) | ||
y = m(x) | ||
@test y isa Array{Float32, 4} | ||
@test size(y) == (9, 12, 2, 3) | ||
|
||
m = Upsample(:bilinear, size=(4, 6)) | ||
x = rand(Float32, 3, 4, 2, 3) | ||
y = m(x) | ||
@test y isa Array{Float32, 4} | ||
@test size(y) == (4, 6, 2, 3) | ||
end | ||
|
||
@testset "upsample nearest" begin | ||
x = rand(Float32, 3, 2, 3) | ||
m = Upsample(:nearest, scale=(2,)) | ||
y = m(x) | ||
@test y isa Array{Float32, 3} | ||
@test size(y) == (6, 2, 3) | ||
|
||
x = rand(Float32, 3, 4, 2, 3) | ||
|
||
m = Upsample(:nearest, scale=(2, 3)) | ||
y = m(x) | ||
@test y isa Array{Float32, 4} | ||
@test size(y) == (6, 12, 2, 3) | ||
|
||
m = Upsample(:nearest, scale=(2,)) | ||
y = m(x) | ||
@test y isa Array{Float32, 4} | ||
@test size(y) == (6, 4, 2, 3) | ||
|
||
m = Upsample(:nearest, scale=2) | ||
y = m(x) | ||
@test y isa Array{Float32, 4} | ||
@test size(y) == (6, 8, 2, 3) | ||
|
||
m = Upsample(2) | ||
y2 = m(x) | ||
@test y2 ≈ y | ||
|
||
m = Upsample(:nearest, size=(6,8)) | ||
y = m(x) | ||
@test y isa Array{Float32, 4} | ||
@test size(y) == (6, 8, 2, 3) | ||
end | ||
|
||
@testset "PixelShuffle" begin | ||
m = PixelShuffle(2) | ||
x = rand(Float32, 3, 18, 3) | ||
y = m(x) | ||
@test y isa Array{Float32, 3} | ||
@test size(y) == (6, 9, 3) | ||
|
||
m = PixelShuffle(3) | ||
x = rand(Float32, 3, 4, 18, 3) | ||
y = m(x) | ||
@test y isa Array{Float32, 4} | ||
@test size(y) == (9, 12, 2, 3) | ||
end |
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
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.