Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use opencv function for resize #4875

Closed
vizero1 opened this issue Feb 13, 2020 · 2 comments
Closed

Use opencv function for resize #4875

vizero1 opened this issue Feb 13, 2020 · 2 comments

Comments

@vizero1
Copy link
Contributor

vizero1 commented Feb 13, 2020

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.

@tqchen
Copy link
Member

tqchen commented Feb 13, 2020

A PR is more than welcomed

@tqchen tqchen changed the title Image preprocessing for darknet takes too long Use opencv function for resize Feb 13, 2020
@vizero1
Copy link
Contributor Author

vizero1 commented Feb 21, 2020

PR is here:
#4883

@tqchen tqchen closed this as completed Feb 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants