test_topi_upsampling.py 3.26 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
def verify_upsampling(batch, in_channel, in_height, in_width, scale, layout='NCHW', method="nearest_neighbor"):
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

    if layout == 'NCHW':
        A = tvm.placeholder((batch, in_channel, in_height, in_width), name='A')
        dtype = A.dtype
        out_shape = (batch, in_channel, in_height*scale, in_width*scale)
        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
        out_shape = (batch, in_height*scale, in_width*scale, 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))

43
    B = topi.nn.upsampling(A, scale, layout=layout, method=method, align_corners=False)
44

45
    if method == "bilinear":
46
        out_size = (in_height*scale, in_width*scale)
47
        b_np = topi.testing.bilinear_resize_python(a_np, out_size, layout, align_corners=False)
48
    else:
49
        b_np = topi.testing.upsampling_python(a_np, (scale, scale), layout)
50 51

    def check_device(device):
52 53
        ctx = tvm.context(device, 0)
        if not ctx.exist:
54 55 56 57 58 59 60 61 62 63
            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)

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

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

def test_upsampling():
70
    # nearest_neighbor - NCHW
71
    verify_upsampling(8, 16, 32, 32, 2)
72
    verify_upsampling(2, 32, 64, 64, 3)
73

74
    ## nearest_neighbor - NHWC
75
    verify_upsampling(8, 16, 32, 32, 2, layout="NHWC")
76
    verify_upsampling(2, 32, 64, 64, 3, layout="NHWC")
77

78 79 80
    # bilinear - NCHW
    verify_upsampling(2, 2, 32, 32, 2, method="bilinear")
    verify_upsampling(2, 2, 32, 32, 3, method="bilinear")
81

82 83 84
    # bilinear - NHWC
    verify_upsampling(2, 2, 32, 32, 2, layout="NHWC", method="bilinear")
    verify_upsampling(2, 2, 32, 32, 3, layout="NHWC", method="bilinear")
85 86 87

if __name__ == "__main__":
    test_upsampling()