-
-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1468: add Upsample and PixelShuffle layers r=DhairyaLGandhi a=CarloLucibello ### PR Checklist - [x] Tests are added - [x] Entry in NEWS.md - [x] Documentation, if applicable - [ ] Final review from `@dhairyagandhi96` (for API changes). Co-authored-by: Carlo Lucibello <carlo.lucibello@gmail.com> Co-authored-by: Carlo Lucibello <carlo.lucibello@unibocconi.it> Co-authored-by: Dhairya Gandhi <dhairya@juliacomputing.com>
- Loading branch information
Showing
12 changed files
with
184 additions
and
12 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
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
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
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) | ||
|
||
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 | ||
r::Int | ||
end | ||
|
||
(m::PixelShuffle)(x) = NNlib.pixel_shuffle(x, m.r) |
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
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 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