Exemplo n.º 1
0
    def _img_scale(b, m, i, n):
        for j in range(new_h):
            for k in range(new_w):
                if coordinate_transformation_mode == "half_pixel":
                    in_y = (j + 0.5) * height_scale - 0.5
                else:
                    in_y = j * height_scale
                y0 = int(math.floor(in_y))
                y1 = max(min(y0 + 1, h - 1), 0)
                y0 = max(y0, 0)
                y_lerp = in_y - math.floor(in_y)

                if coordinate_transformation_mode == "half_pixel":
                    in_x = (k + 0.5) * width_scale - 0.5
                else:
                    in_x = k * width_scale
                x0 = int(math.floor(in_x))
                x1 = max(min(x0 + 1, w - 1), 0)
                x0 = max(x0, 0)
                x_lerp = in_x - math.floor(in_x)

                if layout == 'NHWC':
                    A = image[b][y0][x0][i]
                    B = image[b][y0][x1][i]
                    C = image[b][y1][x0][i]
                    D = image[b][y1][x1][i]
                elif nchw_pack_layout(layout):
                    A = image[b][i][y0][x0][m][n]
                    B = image[b][i][y0][x1][m][n]
                    C = image[b][i][y1][x0][m][n]
                    D = image[b][i][y1][x1][m][n]
                else:
                    A = image[b][i][y0][x0]
                    B = image[b][i][y0][x1]
                    C = image[b][i][y1][x0]
                    D = image[b][i][y1][x1]

                top = _lerp(A, B, x_lerp)
                bottom = _lerp(C, D, x_lerp)

                pixel = np.float32(_lerp(top, bottom, y_lerp))

                if layout == 'NHWC':
                    scaled_image[b][j][k][i] = pixel
                elif nchw_pack_layout(layout):
                    scaled_image[b][i][j][k][m][n] = pixel
                else:
                    scaled_image[b][i][j][k] = pixel
Exemplo n.º 2
0
def resize_shape_func(attrs, inputs, _):
    """
    Shape function for dyn.image.resize op.
    """
    layout = attrs.layout
    if nchw_pack_layout(layout) or nchw_xc_layout(layout):
        out = [
            _resize_shape_func(inputs[0].shape, inputs[1],
                               convert(len(inputs[0].shape)), convert(2),
                               convert(3))
        ]
    else:
        height_axis = width_axis = 1
        for i, letter in enumerate(layout):
            if letter == "H":
                height_axis = i
            if letter == "W":
                width_axis = i
        out = [
            _resize_shape_func(
                inputs[0].shape,
                inputs[1],
                convert(len(inputs[0].shape)),
                convert(height_axis),
                convert(width_axis),
            )
        ]
    return out
Exemplo n.º 3
0
def verify_upsampling(batch, in_channel, in_height, in_width, scale_h, scale_w,
                      layout='NCHW', method="nearest_neighbor",
                      in_batch_block = 0, in_channel_block = 0):
    if layout == 'NCHW':
        A = te.placeholder((batch, in_channel, in_height, in_width), name='A')
        dtype = A.dtype
        out_shape = (batch, in_channel, int(round(in_height*scale_h)), int(round(in_width*scale_w)))
        a_np = np.random.uniform(size=(batch, in_channel, in_height, in_width)).astype(dtype)
    elif nchw_pack_layout(layout):
        A = te.placeholder((batch, in_channel, in_height, in_width, in_batch_block, in_channel_block),
                             name='A')
        dtype = A.dtype
        out_shape = (batch, in_channel, int(round(in_height*scale_h)), int(round(in_width*scale_w)),
                     in_batch_block, in_channel_block)
        a_np = np.random.uniform(size=(batch, in_channel, in_height, in_width,
                                 in_batch_block, in_channel_block)).astype(dtype)
    elif layout == 'NHWC':
        A = te.placeholder((batch, in_height, in_width, in_channel), name='A')
        dtype = A.dtype
        out_shape = (batch, int(round(in_height*scale_h)), int(round(in_width*scale_w)), 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))

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

    if method == "bilinear":
        out_size = (int(round(in_height*scale_h)), int(round(in_width*scale_w)))
        b_np = tvm.topi.testing.bilinear_resize_python(a_np, out_size, layout, "asymmetric")
    else:
        b_np = tvm.topi.testing.upsampling_python(a_np, (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 = tvm.topi.testing.get_injective_schedule(device)(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)
Exemplo n.º 4
0
def upsampling_python(data, scale, layout="NCHW"):
    """ Python version of scaling using nearest neighbour """

    ishape = data.shape
    if layout == "NCHW":
        oshape = (
            ishape[0],
            ishape[1],
            int(round(ishape[2] * scale[0])),
            int(round(ishape[3] * scale[1])),
        )
        output_np = np.zeros(oshape, dtype=data.dtype)
        for b in range(oshape[0]):
            for c in range(oshape[1]):
                output_np[b, c, :, :] = upsample_nearest(data[b, c, :, :], scale)
        return output_np
    # NCHWinic
    if nchw_pack_layout(layout):
        oshape = (
            ishape[0],
            ishape[1],
            int(round(ishape[2] * scale[0])),
            int(round(ishape[3] * scale[1])),
            ishape[4],
            ishape[5],
        )
        output_np = np.zeros(oshape, dtype=data.dtype)
        for b in range(oshape[0]):
            for ib in range(oshape[4]):
                for c in range(oshape[1]):
                    for ic in range(oshape[5]):
                        output_np[b, c, :, :, ib, ic] = upsample_nearest(
                            data[b, c, :, :, ib, ic], scale
                        )
        return output_np

    if layout == "NHWC":
        oshape = (
            ishape[0],
            int(round(ishape[1] * scale[0])),
            int(round(ishape[2] * scale[1])),
            ishape[3],
        )
        output_np = np.zeros(oshape, dtype=data.dtype)
        for b in range(oshape[0]):
            for c in range(oshape[3]):
                output_np[b, :, :, c] = upsample_nearest(data[b, :, :, c], scale)
        return output_np
    raise ValueError("not support this layout {} yet".format(layout))
Exemplo n.º 5
0
def get_2d_pixel(data, layout, boxes, image_height, image_width, n, c, y, x, cc, ib, ic):
    """ Get 2d pixel """
    if boxes is None:
        y = tvm.te.max(tvm.te.min(y, image_height - 1), 0)
        x = tvm.te.max(tvm.te.min(x, image_width - 1), 0)
    if layout == 'NHWC':
        return data(n, y, x, c).astype('float')
    if layout == 'NCHW':
        return data(n, c, y, x).astype('float')
    if nchw_pack_layout(layout):
        return data(n, c, y, x, ib, ic).astype('float')

    # else must be NCHWxc
    assert nchw_xc_layout(layout)
    return data(n, c, y, x, cc).astype('float')
Exemplo n.º 6
0
def get_2d_indices(indices, layout='NCHW'):
    """ Get 2d indices """
    (cc, inum, ic) = (0, 0, 0)
    if layout == 'NHWC':
        n, y, x, c = indices
        cc = None
    elif layout == 'NCHW':
        n, c, y, x = indices
        cc = None
    elif nchw_pack_layout(layout):
        n, c, y, x, inum, ic = indices
    else:
        # else must be NCHWxc
        assert nchw_xc_layout(layout)
        n, c, y, x, cc = indices

    return n, c, y, x, cc, inum, ic
Exemplo n.º 7
0
def resize_shape_func(attrs, inputs, _):
    """
    Shape function for dyn.image.resize op.
    """
    layout = attrs.layout
    if layout == 'NHWC':
        out = [
            _NHWC_resize_shape_func(inputs[0].shape, inputs[1],
                                    convert(len(inputs[0].shape)))
        ]
    elif (layout
          == 'NCHW') or nchw_pack_layout(layout) or nchw_xc_layout(layout):
        out = [
            _NCHW_resize_shape_func(inputs[0].shape, inputs[1],
                                    convert(len(inputs[0].shape)))
        ]
    else:
        raise ValueError("Resize Unsupported Layout", layout)
    return out
Exemplo n.º 8
0
def bilinear_resize_python(image,
                           out_size,
                           layout,
                           coordinate_transformation_mode="align_corners"):
    """ Bilinear scaling using python"""
    (new_h, new_w) = out_size
    (ib, ic) = (1, 1)

    if layout == 'NHWC':
        (batch, h, w, channel) = image.shape
        scaled_image = np.ones((batch, new_h, new_w, channel))
    # NCHWinic
    elif nchw_pack_layout(layout):
        (batch, channel, h, w, ib, ic) = image.shape
        scaled_image = np.ones((batch, channel, new_h, new_w, ib, ic))
    else:
        (batch, channel, h, w) = image.shape
        scaled_image = np.ones((batch, channel, new_h, new_w))

    if coordinate_transformation_mode == "align_corners":
        height_scale = np.float32(h - 1) / np.float32(out_size[0] - 1)
        width_scale = np.float32(w - 1) / np.float32(out_size[1] - 1)
    else:
        height_scale = np.float32(h) / np.float32(out_size[0])
        width_scale = np.float32(w) / np.float32(out_size[1])

    def _lerp(A, B, t):
        return A * (1.0 - t) + B * t

    def _img_scale(b, m, i, n):
        for j in range(new_h):
            for k in range(new_w):
                if coordinate_transformation_mode == "half_pixel":
                    in_y = (j + 0.5) * height_scale - 0.5
                else:
                    in_y = j * height_scale
                y0 = int(math.floor(in_y))
                y1 = max(min(y0 + 1, h - 1), 0)
                y0 = max(y0, 0)
                y_lerp = in_y - math.floor(in_y)

                if coordinate_transformation_mode == "half_pixel":
                    in_x = (k + 0.5) * width_scale - 0.5
                else:
                    in_x = k * width_scale
                x0 = int(math.floor(in_x))
                x1 = max(min(x0 + 1, w - 1), 0)
                x0 = max(x0, 0)
                x_lerp = in_x - math.floor(in_x)

                if layout == 'NHWC':
                    A = image[b][y0][x0][i]
                    B = image[b][y0][x1][i]
                    C = image[b][y1][x0][i]
                    D = image[b][y1][x1][i]
                elif nchw_pack_layout(layout):
                    A = image[b][i][y0][x0][m][n]
                    B = image[b][i][y0][x1][m][n]
                    C = image[b][i][y1][x0][m][n]
                    D = image[b][i][y1][x1][m][n]
                else:
                    A = image[b][i][y0][x0]
                    B = image[b][i][y0][x1]
                    C = image[b][i][y1][x0]
                    D = image[b][i][y1][x1]

                top = _lerp(A, B, x_lerp)
                bottom = _lerp(C, D, x_lerp)

                pixel = np.float32(_lerp(top, bottom, y_lerp))

                if layout == 'NHWC':
                    scaled_image[b][j][k][i] = pixel
                elif nchw_pack_layout(layout):
                    scaled_image[b][i][j][k][m][n] = pixel
                else:
                    scaled_image[b][i][j][k] = pixel

    for b in range(batch):
        for m in range(ib):
            for i in range(channel):
                for n in range(ic):
                    _img_scale(b, m, i, n)

    return scaled_image
Exemplo n.º 9
0
def resize(data,
           size,
           layout="NCHW",
           method="bilinear",
           coordinate_transformation_mode="half_pixel",
           out_dtype=None,
           output_shape=None):
    """Perform resize operation on the data.

    Parameters
    ----------
    data : tvm.te.Tensor
        inputs is a 4-D tensor with shape
        [batch, channel, in_height, in_width]
        or  [batch, in_height, in_width, channel]

    size: Tuple
        Output resolution scale to

    layout: string, optional
        "NCHW", "NHWC", or "NCHWc".

    coordinate_transformation_mode: string, optional
        Describes how to transform the coordinate in the resized tensor
        to the coordinate in the original tensor.
        Refer to the ONNX Resize operator specification for details.
        Available options are "half_pixel", "align_corners" and "asymmetric".

    method: {"bilinear", "nearest_neighbor", "bicubic"}
        Method to be used for resizing.

    out_dtype: string, optional
        Type to return. If left None will be same as input type.

    output_shape: optional
        Shape to return. If left None will be inferred

    Returns
    -------
    output : tvm.te.Tensor
        4-D with shape [batch, channel, in_height*scale, in_width*scale]
        or [batch, in_height*scale, in_width*scale, channel]
        or 5-D with shape [batch, channel-major, in_height*scale, in_width*scale, channel-minor]
    """

    method = method.lower()
    if layout == 'NHWC':
        in_n, in_h, in_w, in_c = data.shape
        if output_shape is None:
            output_shape = [in_n, size[0], size[1], in_c]
    elif layout == 'NCHW':
        in_n, in_c, in_h, in_w = data.shape
        if output_shape is None:
            output_shape = [in_n, in_c, size[0], size[1]]
    elif nchw_pack_layout(layout):  # for NCHWinic
        in_n, in_c, in_h, in_w, in_inum, in_ic = data.shape
        if output_shape is None:
            output_shape = [in_n, in_c, size[0], size[1], in_inum, in_ic]
    elif nchw_xc_layout(layout):  # for NCHWxc
        in_n, in_c, in_h, in_w, in_cc = data.shape
        if output_shape is None:
            output_shape = [in_n, in_c, size[0], size[1], in_cc]
    else:
        raise ValueError('%s layout is not supported.' % layout)

    def _nearest_neighbor(*indices):
        return resize_nearest_neighbor(indices, data, in_h, in_w,
                                       size[0], size[1], layout=layout,
                                       coordinate_transformation_mode= \
                                       coordinate_transformation_mode,
                                       out_dtype=out_dtype)

    def _bilinear(*indices):
        return resize_bilinear(indices, data, in_h, in_w,
                               size[0], size[1], layout=layout,
                               coordinate_transformation_mode= \
                               coordinate_transformation_mode,
                               out_dtype=out_dtype)

    def _bicubic(*indices):
        return resize_bicubic(indices, data, in_h, in_w,
                              size[0], size[1], layout,
                              coordinate_transformation_mode= \
                              coordinate_transformation_mode,
                              out_dtype=out_dtype)

    # Determine which interpolation method to use then run it.
    if method == "nearest_neighbor":
        compute_func = _nearest_neighbor
    elif method == "bilinear":
        compute_func = _bilinear
    elif method == "bicubic":
        compute_func = _bicubic
    else:
        raise ValueError('%s method is not supported.' % method)

    return te.compute(output_shape,
                      compute_func,
                      name='resize',
                      tag=tag.INJECTIVE)