test_topi_region.py 1.68 KB
Newer Older
1 2 3 4 5
"""Example code to do region."""
import numpy as np
import topi
from topi.util import get_const_tuple
import tvm
6 7
import topi.testing

8 9 10 11 12
def verify_region(batch, in_size, in_channel, n, classes, coords, background, l_softmax):
    '''Verify region operator by comparing outputs from tvm and numpy implementation'''
    in_height = in_width = in_size

    A = tvm.placeholder((batch, in_channel, in_height, in_width), name='A')
13
    B = topi.vision.yolo.region(A, n, classes, coords, background, l_softmax)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

    a_shape = get_const_tuple(A.shape)
    dtype = A.dtype

    def get_ref_data_region():
        a_np = np.random.uniform(size=a_shape).astype(dtype)
        b_np = topi.testing.region_python(a_np, n, classes, coords, background, l_softmax)
        return a_np, b_np

    a_np, b_np = get_ref_data_region()
    def check_device(device):
        '''Cheching devices is enabled or not'''
        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):
32 33 34 35
            if device == 'llvm':
                s = topi.generic.vision.schedule_region([B])
            else:
                s = topi.cuda.vision.schedule_region([B])
36 37 38 39
        a = tvm.nd.array(a_np, ctx)
        b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=B.dtype), ctx)
        func = tvm.build(s, [A, B], device)
        func(a, b)
40
        tvm.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5)
41 42 43 44 45 46 47 48 49

    for device in ['llvm', 'cuda']:
        check_device(device)

def test_region():
    verify_region(1, 19, 425, 5, 80, 4, 0, 1)

if __name__ == "__main__":
    test_region()