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 pixel_shuffle #260

Merged
merged 7 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
# `allow-failure` not available yet https://github.com/actions/toolkit/issues/399
## `allow-failure` not available yet https://github.com/actions/toolkit/issues/399
continue-on-error: ${{ matrix.julia-version == 'nightly' }}
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
continue-on-error: ${{ matrix.julia-version == 'nightly' }}
# continue-on-error: ${{ matrix.julia-version == 'nightly' }}
env:
cache-name: cache-artifacts
with:
Expand All @@ -65,11 +65,11 @@ jobs:
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
continue-on-error: ${{ matrix.julia-version == 'nightly' }}
# continue-on-error: ${{ matrix.julia-version == 'nightly' }}
- uses: julia-actions/julia-runtest@v1
continue-on-error: ${{ matrix.julia-version == 'nightly' }}
# continue-on-error: ${{ matrix.julia-version == 'nightly' }}
- uses: codecov/codecov-action@v1
continue-on-error: ${{ matrix.version == 'nightly' }}
# continue-on-error: ${{ matrix.version == 'nightly' }}
with:
file: lcov.info

Expand Down
1 change: 1 addition & 0 deletions src/NNlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ end
include("activations.jl")

include("softmax.jl")
include("misc.jl")
include("batched/batchedmul.jl")
include("gemm.jl")
include("conv.jl")
Expand Down
22 changes: 22 additions & 0 deletions src/misc.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export pixel_shuffle

"""
pixel_shuffle(x, r)

Pixel shuffling operation. `r` is the scale factor for shuffling.
The operation converts an input of size [W,H,r²C,N] to size [rW,rH,C,N]
Used extensively in super-resolution networks to upsample
towards high resolution features.

Reference : https://arxiv.org/pdf/1609.05158.pdf
"""
function pixel_shuffle(x::AbstractArray{T, 4}, r::Integer) where T <:Number
w, h, c, n = size(x)
@assert c % r^2 == 0
c_out = c ÷ r^2
w_out = w * r
h_out = h * r
x = reshape(x, (w, h, r, r, c_out, n))
x = permutedims(x, (3, 1, 4, 2, 5, 6))
return reshape(x, (w_out, h_out, c_out, n))
end
34 changes: 34 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@testset "pixel_shuffle" begin
x = reshape(1:16, (2, 2, 4, 1))
# [:, :, 1, 1] =
# 1 3
# 2 4
# [:, :, 2, 1] =
# 5 7
# 6 8
# [:, :, 3, 1] =
# 9 11
# 10 12
# [:, :, 4, 1] =
# 13 15
# 14 16

y_true = [1 9 3 11
5 13 7 15
2 10 4 12
6 14 8 16][:,:,:,:]
CarloLucibello marked this conversation as resolved.
Show resolved Hide resolved

y = pixel_shuffle(x, 2)
@test size(y) == (4, 4, 1, 1)
@test y_true == y

x = reshape(1:4*3*27*2, (4,3,27,2))
y = pixel_shuffle(x, 3)
@test size(y) == (12, 9, 3, 2)
x1 = x[:,:,:,[1]]
x2 = x[:,:,:,[2]]
y1 = pixel_shuffle(x1, 3)
y2 = pixel_shuffle(x2, 3)
@test cat(y1, y2, dims=4) == y
gradtest(x -> pixel_shuffle(x, 2), rand(3,3,8,2))
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ end
@testset "Softmax" begin
include("softmax.jl")
end

@testset "Misc Stuff" begin
include("misc.jl")
end