Example #1
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
Example #2
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')
Example #3
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
Example #4
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
Example #5
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)