Skip to content

Use opencv function for resize #4875

@vizero1

Description

@vizero1

Hi,

I was working on this tutorial https://docs.tvm.ai/tutorials/frontend/from_darknet.html#sphx-glr-tutorials-frontend-from-darknet-py and it seems that the preprocessing of the image takes several seconds.
I did a few tests and most of the time is spend on this function in tvm/relay/testing/darknet.py

def _resize_image(img, w_in, h_in):
    """Resize the image to the given height and width."""
    imc, imh, imw = img.shape
    h_in = int(h_in)
    w_in = int(w_in)
    part = np.zeros((imc, imh, w_in))
    resized = np.zeros((imc, h_in, w_in))
    w_scale = (imw - 1) / (w_in - 1)
    h_scale = (imh - 1) / (h_in - 1)
    for k in range(imc):
        for j in range(imh):
            for c in range(w_in):
                if c == w_in - 1 or imw == 1:
                    part[k][j][c] = img[k][j][imw - 1]
                else:
                    fdx, idx = math.modf(c * w_scale)
                    part[k][j][c] = (1 - fdx) * img[k][j][int(idx)] + \
                                            fdx * img[k][j][int(idx) + 1]
    for k in range(imc):
        for j in range(h_in):
            fdy, idy = math.modf(j * h_scale)
            for c in range(w_in):
                resized[k][j][c] = (1 - fdy)*part[k][int(idy)][c]
            if (j == h_in - 1) or (imh == 1):
                continue
            for c in range(w_in):
                resized[k][j][c] += fdy * part[k][int(idy) + 1][c]
    return resized

Is there any specific reason why the script is not using the opencv.resize function and instead has an own implementation which takes much more time?
I just rewrote the image processing part by using the opencv resize function and now it takes less than 1 second.
I am interested if there is any idea behind this specific implementation.
If not I would create a PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions