test_topi_resize.py 3.33 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 21 22 23
"""Test code for bilinear scale """
import numpy as np
import tvm
import topi
import topi.testing
import math

24 25
from common import get_all_backend

26
def verify_resize(batch, in_channel, in_height, in_width, out_height, out_width, layout='NCHW', align_corners=True, method="bilinear"):
27 28 29 30 31 32 33 34 35 36 37 38 39 40
    if layout == 'NCHW':
        A = tvm.placeholder((batch, in_channel, in_height, in_width), name='A', dtype='float32')
        dtype = A.dtype
        out_shape = (batch, in_channel, out_height, out_width)
        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='float32')
        dtype = A.dtype
        out_shape = (batch, out_height, out_width, in_channel)
        a_np = np.random.uniform(size=(batch, in_height, in_width, in_channel)).astype(dtype)
    else:
        raise NotImplementedError(
            'Layout not supported {} '.format(layout))

41
    B = topi.image.resize(A, (out_height, out_width), layout=layout, align_corners=align_corners, method=method)
42

43
    if method == "bilinear":
44 45 46 47 48
        b_np = topi.testing.bilinear_resize_python(a_np, (out_height, out_width), layout, align_corners)
    else:
        scale_h = out_height / in_height
        scale_w = out_width / in_width
        b_np = topi.testing.upsampling_python(a_np, (scale_h, scale_w), layout)
49 50 51 52 53 54 55 56 57 58 59 60 61 62

    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)

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

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

68

69 70
def test_resize():
    # Scale NCHW
71
    verify_resize(4, 16, 32, 32, 50, 50, 'NCHW')
72
    # Scale NCHW + Align Corners
73
    verify_resize(6, 32, 64, 64, 20, 20, 'NCHW', True)
74
    # Scale NHWC
75
    verify_resize(4, 16, 32, 32, 50, 50, "NHWC")
76
    # Scale NHWC + Align Corners
77 78
    verify_resize(6, 32, 64, 64, 20, 20, "NHWC", True)
    # Nearest + Fractional
79 80
    verify_resize(4, 16, 32, 32, 50, 50, 'NCHW', method="nearest_neighbor", align_corners=False)
    verify_resize(4, 16, 32, 32, 50, 50, 'NHWC', method="nearest_neighbor", align_corners=False)
81 82 83

if __name__ == "__main__":
    test_resize()