Last active
January 22, 2021 02:50
-
-
Save hvy/174b6448a2de4b2568fd8501e4aa6012 to your computer and use it in GitHub Desktop.
Chainer implementation of pixel shuffle used in sub-pixel convolutions
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
import numpy as np | |
from chainer import Variable | |
from chainer import functions as F | |
# Pixel shuffle used in sub-pixel convolutions, in Chainer | |
# https://arxiv.org/pdf/1609.05158v2.pdf | |
# | |
# Example: | |
# Scaling factor: 3 | |
# In shape: (1, 9, 4, 4) | |
# Out shape: (1, 1, 12, 12) | |
# | |
# `np` may be replaced with `cupy` (after `import cupy`) to perform | |
# the same computations on the GPU | |
upscale_factor = 3 | |
x = np.empty((1, 9, 4, 4), dtype=np.float32) | |
n, c, w, h = x.shape | |
# Set all values in each feature map to its feature map index | |
for i in range(c): | |
x[0, i] = i | |
c_out = c // upscale_factor ** 2 | |
w_out = w * upscale_factor | |
h_out = h * upscale_factor | |
x = Variable(x) | |
x = F.reshape(x, (n, c_out, upscale_factor, upscale_factor, w, h)) | |
x = F.transpose(x, (0, 1, 4, 2, 5, 3)) | |
x = F.reshape(x, (n, c_out, w_out, h_out)) | |
assert(x.shape == (1, 1, 12, 12)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment