test_topi_upsampling.py 7.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17 18 19 20
"""Test code for upsampling"""
import numpy as np
import tvm
import topi
21
import topi.testing
22 23
import math

24 25
from common import get_all_backend

26 27
def verify_upsampling(batch, in_channel, in_height, in_width, scale_h, scale_w,
                      layout='NCHW', method="nearest_neighbor"):
28 29 30
    if layout == 'NCHW':
        A = tvm.placeholder((batch, in_channel, in_height, in_width), name='A')
        dtype = A.dtype
31
        out_shape = (batch, in_channel, int(round(in_height*scale_h)), int(round(in_width*scale_w)))
32 33 34 35
        a_np = np.random.uniform(size=(batch, in_channel, in_height, in_width)).astype(dtype)
    elif layout == 'NHWC':
        A = tvm.placeholder((batch, in_height, in_width, in_channel), name='A')
        dtype = A.dtype
36
        out_shape = (batch, int(round(in_height*scale_h)), int(round(in_width*scale_w)), in_channel)
37 38 39 40 41
        a_np = np.random.uniform(size=(batch, in_height, in_width, in_channel)).astype(dtype)
    else:
        raise NotImplementedError(
            'Layout not supported {} '.format(layout))

42
    B = topi.nn.upsampling(A, scale_h, scale_w, layout=layout, method=method, align_corners=False)
43

44
    if method == "bilinear":
45
        out_size = (int(round(in_height*scale_h)), int(round(in_width*scale_w)))
46
        b_np = topi.testing.bilinear_resize_python(a_np, out_size, layout, "asymmetric")
47
    else:
48
        b_np = topi.testing.upsampling_python(a_np, (scale_h, scale_w), layout)
49 50

    def check_device(device):
51 52
        ctx = tvm.context(device, 0)
        if not ctx.exist:
53 54 55 56 57 58 59 60 61 62
            print("Skip because %s is not enabled" % device)
            return
        print("Running on target: %s" % device)
        with tvm.target.create(device):
            s = topi.generic.schedule_injective(B)
        a = tvm.nd.array(a_np, ctx)
        b = tvm.nd.array(np.zeros(out_shape, dtype=dtype), ctx)
        f = tvm.build(s, [A, B], device)
        f(a, b)

63
        tvm.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5, atol=1e-5)
64

65
    for device in get_all_backend():
66 67 68
        check_device(device)

def test_upsampling():
69
    # nearest_neighbor - NCHW
70 71 72
    verify_upsampling(8, 16, 32, 32, 2.0, 2.0)
    verify_upsampling(2, 32, 64, 64, 3.0, 3.0)
    verify_upsampling(1, 64, 22, 32, 1.954545497894287, 2.0)
73

74
    ## nearest_neighbor - NHWC
75 76 77
    verify_upsampling(8, 16, 32, 32, 2.0, 2.0, layout="NHWC")
    verify_upsampling(2, 32, 64, 64, 3.0, 3.0, layout="NHWC")
    verify_upsampling(1, 64, 22, 32, 1.954545497894287, 2.0, layout="NHWC")
78

79
    # bilinear - NCHW
80 81 82
    verify_upsampling(2, 2, 32, 32, 2.0, 2.0, method="bilinear")
    verify_upsampling(2, 2, 32, 32, 3.0, 3.0, method="bilinear")
    verify_upsampling(1, 64, 22, 32, 1.954545497894287, 2.0, method="bilinear")
83

84
    # bilinear - NHWC
85 86 87
    verify_upsampling(2, 2, 32, 32, 2.0, 2.0, layout="NHWC", method="bilinear")
    verify_upsampling(2, 2, 32, 32, 3.0, 3.0, layout="NHWC", method="bilinear")
    verify_upsampling(1, 64, 22, 32,  3.0, 3.0, layout="NHWC", method="bilinear")
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
def verify_upsampling3d(batch, in_channel, in_depth, in_height, in_width, scale_d, scale_h, scale_w,
                        layout='NCDHW', method="nearest_neighbor"):
    if layout == 'NCDHW':
        A = tvm.placeholder((batch, in_channel, in_depth, in_height, in_width), name='A')
        dtype = A.dtype
        out_shape = (batch, in_channel, int(round(in_depth*scale_d)), int(round(in_height*scale_h)),
                     int(round(in_width*scale_w)))
        a_np = np.random.uniform(size=(batch, in_channel, in_depth, in_height, in_width)).astype(dtype)
    elif layout == 'NDHWC':
        A = tvm.placeholder((batch, in_depth, in_height, in_width, in_channel), name='A')
        dtype = A.dtype
        out_shape = (batch, int(round(in_depth*scale_d)), int(round(in_height*scale_h)),
                     int(round(in_width*scale_w)), in_channel)
        a_np = np.random.uniform(size=(batch, in_depth, in_height, in_width, in_channel)).astype(dtype)
    else:
        raise NotImplementedError(
            'Layout not supported {} '.format(layout))

    B = topi.nn.upsampling3d(A, scale_d, scale_h, scale_w, layout=layout, method=method,
                             coordinate_transformation_mode="half_pixel")

    if method == "trilinear":
        out_size = (int(round(in_depth*scale_d)), int(round(in_height*scale_h)), int(round(in_width*scale_w)))
        b_np = topi.testing.trilinear_resize3d_python(a_np, out_size, layout,
                                                      coordinate_transformation_mode="half_pixel")
    else:
        b_np = topi.testing.upsampling3d_python(a_np, (scale_d, scale_h, scale_w), layout)

    def check_device(device):
        ctx = tvm.context(device, 0)
        if not ctx.exist:
            print("Skip because %s is not enabled" % device)
            return
        print("Running on target: %s" % device)
        with tvm.target.create(device):
            s = topi.generic.schedule_injective(B)
        a = tvm.nd.array(a_np, ctx)
        b = tvm.nd.array(np.zeros(out_shape, dtype=dtype), ctx)
        f = tvm.build(s, [A, B], device)
        f(a, b)

        tvm.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5, atol=1e-5)

    for device in get_all_backend():
        check_device(device)

def test_upsampling3d():
    # nearest_neighbor - NCDHW
    verify_upsampling3d(8, 8, 16, 16, 16, 2.0, 2.0, 2.0)
    verify_upsampling3d(2, 16, 32, 32, 32, 3.0, 3.0, 3.0)
    verify_upsampling3d(1, 8, 11, 16, 6, 1.954545497894287, 2.0, 1.5)

    ## nearest_neighbor - NDHWC
    verify_upsampling3d(8, 8, 16, 16, 16, 2.0, 2.0, 2.0, layout="NDHWC")
    verify_upsampling3d(2, 16, 32, 32, 32, 3.0, 3.0, 3.0, layout="NDHWC")
    verify_upsampling3d(1, 8, 11, 16, 6, 1.954545497894287, 2.0, 1.5, layout="NDHWC")

    # trilinear - NCDHW
    verify_upsampling3d(2, 2, 16, 16, 16, 2.0, 2.0, 2.0, method="trilinear")
    verify_upsampling3d(2, 2, 32, 32, 32, 3.0, 3.0, 3.0, method="trilinear")
    verify_upsampling3d(1, 2, 11, 16, 6, 1.954545497894287, 2.0, 1.5, method="trilinear")

    # trilinear - NDHWC
    verify_upsampling3d(2, 2, 16, 16, 16, 2.0, 2.0, 2.0, layout="NDHWC", method="trilinear")
    verify_upsampling3d(2, 2, 32, 32, 32, 3.0, 3.0, 3.0, layout="NDHWC", method="trilinear")
    verify_upsampling3d(1, 2, 11, 16, 6, 1.954545497894287, 2.0, 1.5, layout="NDHWC", method="trilinear")

156 157
if __name__ == "__main__":
    test_upsampling()
158
    test_upsampling3d()