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

Need not use while loop. #3

Closed
PistonY opened this issue Aug 6, 2019 · 3 comments
Closed

Need not use while loop. #3

PistonY opened this issue Aug 6, 2019 · 3 comments

Comments

@PistonY
Copy link

PistonY commented Aug 6, 2019

Actually you could not use while loop, you need this cause by wrong range of r.
In your conditions:

s = np.random.uniform(s_l, s_h)

sqrt(H*W*s/r) <= W
and
sqrt(W*H*s*r) <= H
then get rage of r shoud be
(H*s) / W <= r <= H / (W*s)

Then you code could be

import numpy as np


def get_random_eraser(p=0.5, s_l=0.02, s_h=0.4, r_1=0.3, r_2=1/0.3, v_l=0, v_h=255, pixel_level=False):
    def eraser(input_img):
        img_h, img_w, img_c = input_img.shape
        p_1 = np.random.rand()

        if p_1 > p:
            return input_img

        
        s = np.random.uniform(s_l, s_h)
        r = np.random.uniform(r_1, r_2)
        r = np.clip(r, (img_h*s)/img_w, img_h / (img_w*s))
        s = s * img_h * img_w
        w = int(np.sqrt(s / r))
        h = int(np.sqrt(s * r))
        left = np.random.randint(0, img_w - w)
        top = np.random.randint(0, img_h - h)

        if pixel_level:
            c = np.random.uniform(v_l, v_h, (h, w, img_c))
        else:
            c = np.random.uniform(v_l, v_h)

        input_img[top:top + h, left:left + w, :] = c

        return input_img

    return eraser

It's should be the same thing cause even you use while you just waiting the random r in right range.

@yu4u
Copy link
Owner

yu4u commented Aug 6, 2019

Thank you for your comment.
Clipping seems to change the distribution of r.
It might be enough to derive r from right range instead.

@PistonY
Copy link
Author

PistonY commented Aug 7, 2019

If W==H, just set r to 0.4~1/0.4 is ok.
Or you may fix range of it.

r_1 = max(r_1, (H*s) / W)
r_2 = min(r_2, H / (W*s))

@PistonY PistonY closed this as completed Aug 7, 2019
@Sicily-F
Copy link

Sicily-F commented Apr 5, 2021

hi again- any chance you could check out my question here: #6
I've still not had a reply from the owner of this repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants