Skip to content

add bilinear upsample layer #1136

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

Closed
wants to merge 6 commits into from
Closed
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 src/Flux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ include("layers/basic.jl")
include("layers/conv.jl")
include("layers/recurrent.jl")
include("layers/normalise.jl")
include("layers/upsample.jl")

include("data/Data.jl")

Expand Down
61 changes: 61 additions & 0 deletions src/layers/upsample.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
BilinearUpsample(factor::Tuple{Integer,Integer})

Create an upsampling layer that uses bilinear interpolation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Create an upsampling layer that uses bilinear interpolation.
Create an upsampling layer that uses bilinear interpolation to upscale the first two dimensions of 4D input.


The width and height dimensions grow by the `factor` tuple.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 1st dimension is height because in Julia it's column first.

Suggested change
The width and height dimensions grow by the `factor` tuple.
The input is commonly interpreted as a batch of images, where the height(1st) and width(2nd) dimensions grow by the `factor` tuple.


# Examples
```jldoctest; setup = :(using Flux: BilinearUpsample; using Random; Random.seed!(0))
julia> b = Flux.BilinearUpsample((2, 2))
BilinearUpsample(2, 2)

julia> b(rand(2, 2, 1, 1))
4×4×1×1 Array{Float64,4}:
[:, :, 1, 1] =
0.823648 0.658877 0.329336 0.164566
0.845325 0.675933 0.337149 0.167757
0.888679 0.710044 0.352773 0.174138
0.910357 0.7271 0.360586 0.177329```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- 0.910357  0.7271    0.360586  0.177329```
+ 0.910357  0.7271    0.360586  0.177329
+ ```

"""
struct BilinearUpsample{T<:Integer}
factor::Tuple{T,T}
end

function (b::BilinearUpsample)(x::AbstractArray)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function (b::BilinearUpsample)(x::AbstractArray)
function (b::BilinearUpsample)(x::AbstractArray{T, 4}) where T

W, H, C, N = size(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to swap W and H but it's still okay to keep it as it is, since most people confuse them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data in Flux is stored in WHCN order isn’t it?

Data should be stored in WHCN order (width, height, # channels, batch size).

Copy link
Contributor

@johnnychen94 johnnychen94 Apr 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, but I prefer to read it as a misunderstanding. I just wanted to mention it here in case you're not aware of it.

It's okay to abuse the usage of WH since it's relative to the column/row-first order.


newW = W * b.factor[1]
newH = H * b.factor[2]

out = similar(x, (newW, newH, C, N))

@inbounds for w = 1:newW, h = 1:newH
w₀ = (w - 0.5) / b.factor[1] + 0.5
h₀ = (h - 0.5) / b.factor[2] + 0.5

w1 = floor(Int, w₀)
w2 = w1 + 1
h1 = floor(Int, h₀)
h2 = h1 + 1

i1 = clamp(w1, 1, W)
i2 = clamp(w2, 1, W)
j1 = clamp(h1, 1, H)
j2 = clamp(h2, 1, H)

@views out[w, h, :, :] =
(
x[i1, j1, :, :] * (w2 - w₀) * (h2 - h₀) +
x[i1, j2, :, :] * (w2 - w₀) * (h₀ - h1) +
x[i2, j1, :, :] * (w₀ - w1) * (h2 - h₀) +
x[i2, j2, :, :] * (w₀ - w1) * (h₀ - h1)
) / ((w2 - w1) * (h2 - h1))
end

out
end

function Base.show(io::IO, b::BilinearUpsample)
print(io, "BilinearUpsample(", b.factor[1], ", ", b.factor[2], ")")
end
11 changes: 11 additions & 0 deletions test/layers/upsample.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Flux: BilinearUpsample
using Test

@testset "BilinearUpsample" begin
@test size(BilinearUpsample((2, 2))(rand(2, 2, 1, 1))) == (4, 4, 1, 1)
@test size(BilinearUpsample((3, 3))(rand(2, 2, 1, 1))) == (6, 6, 1, 1)
@test size(BilinearUpsample((2, 2))(rand(2, 2, 10, 10))) == (4, 4, 10, 10)
@test size(BilinearUpsample((3, 3))(rand(2, 2, 10, 10))) == (6, 6, 10, 10)

@test_throws BoundsError BilinearUpsample((2, 2))(rand(2, 2))
end
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Flux
using Flux
using Flux.Data
using Test
using Test
using Random, Statistics, LinearAlgebra
using Documenter
using IterTools: ncycle
Expand Down Expand Up @@ -30,6 +30,7 @@ Random.seed!(0)
include("layers/normalisation.jl")
include("layers/stateless.jl")
include("layers/conv.jl")
include("layers/upsample.jl")
end

@testset "CUDA" begin
Expand Down