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 #4003

Merged
merged 25 commits into from
Dec 18, 2020
Merged

add pixel shuffle #4003

merged 25 commits into from
Dec 18, 2020

Conversation

MARD1NO
Copy link
Contributor

@MARD1NO MARD1NO commented Dec 11, 2020

增加 Pixelshuffle 算子

PixelShufflev1是宽高都是相同的缩放因子(主要是为了对齐其他框架)
效果如下

图片

import oneflow as flow
import oneflow.typing as tp
import numpy as np 


@flow.global_function()
def PixelShufflev2Job(input: tp.Numpy.Placeholder(shape=(1, 8, 2, 2), dtype=flow.float32))->tp.Numpy:
    out = flow.nn.PixelShufflev2(input, h_upscale_factor=2, w_upscale_factor=2)

    return out

input = np.random.uniform(size=(1, 8, 2, 2)).astype(np.float32)
out = PixelShuffleJob(input)

# out.shape (1, 2, 4, 4)

PixelShuffle2允许用户对宽高设定不同的缩放因子
图片

import oneflow as flow
import oneflow.typing as tp
import numpy as np 


@flow.global_function()
def PixelShufflev2Job(input: tp.Numpy.Placeholder(shape=(1, 8, 2, 4), dtype=flow.float32))->tp.Numpy:
    out = flow.nn.PixelShufflev2(input, h_upscale_factor=2, w_upscale_factor=4)

    return out

input = np.random.uniform(size=(1, 8, 2, 4)).astype(np.float32)
out = PixelShuffleJob(input)

# out.shape (1, 1, 4, 16)

附上循环实现代码,方便后续重构为C++算子

import numpy as np
batchsize = 2
channel = 6
height = 2
width = 2
h_scale = 1
w_scale = 3
#
#
# input = np.ones(shape=(batchsize, 1, height, width))
input = np.random.random(size=(batchsize, channel, height, width)).astype(np.float32)
# for i in range(1, channel):
#     new_input = np.ones(shape=(batchsize, 1, height, width)) * (i + 1)
#     input = np.concatenate([input, new_input], axis=1)
out_h = height * h_scale
out_w = width * w_scale
out_c = channel // (h_scale * w_scale)
fp_result = np.zeros((batchsize, out_c, out_h, out_w)).astype(np.float32)
#
#
def getchannel(c_idx, h_idx, w_idx, h_scale, w_scale):
    inner_h_idx = h_idx - int(h_idx / h_scale) * h_scale
    inner_w_idx = w_idx - int(w_idx / w_scale) * w_scale
    # Compute the index, Because the index is left->right, up->down,
    # so the index of height should multiply `scale`
    print(h_idx)
    print("inner h idx: ", inner_h_idx)
    print("inner w idx: ", inner_w_idx)
    inner_1d_idx = inner_h_idx * w_scale + inner_w_idx
    # print(inner_1d_idx)
    return (c_idx * h_scale * w_scale + inner_1d_idx) % channel
#
#
# # ======forward======
for b in range(batchsize):
    for c in range(out_c):
        for h in range(out_h):
            for w in range(out_w):
                in_c = getchannel(c, h, w, h_scale, w_scale)
                in_h = int(h / h_scale)
                in_w = int(w / w_scale)
                fp_result[b, c, h, w] = input[b, in_c, in_h, in_w]
print(input)
print(fp_result)

@oneflow-ci-bot oneflow-ci-bot removed their request for review December 14, 2020 12:21
@Ldpe2G Ldpe2G marked this pull request as ready for review December 16, 2020 11:38
@oneflow-ci-bot oneflow-ci-bot removed their request for review December 17, 2020 11:40
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot December 17, 2020 13:13
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot December 17, 2020 16:05
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot December 17, 2020 18:49
@MARD1NO MARD1NO marked this pull request as draft December 18, 2020 04:18
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot December 18, 2020 09:50
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot December 18, 2020 12:40
@MARD1NO MARD1NO marked this pull request as ready for review December 18, 2020 12:57
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot December 18, 2020 15:25
@oneflow-ci-bot oneflow-ci-bot merged commit b68d4a8 into master Dec 18, 2020
@oneflow-ci-bot oneflow-ci-bot deleted the dev_pixel_shuffle branch December 18, 2020 18:11
liujuncheng pushed a commit that referenced this pull request Jun 3, 2021
* add pixel shuffle

* add grad test

* add name

* fix test case

* add pixel shufflev2

* add pixelshufflev2

* fix doc

* remove useless code

* add random grad check

* fix devices

* fix format

* fix test case

* fix reshape

Co-authored-by: Liang Depeng <liangdepeng@gmail.com>
Co-authored-by: oneflow-ci-bot <69100618+oneflow-ci-bot@users.noreply.github.com>
Former-commit-id: b68d4a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants