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

Code not working for pixel level random erasing #6

Open
Sicily-F opened this issue Feb 23, 2021 · 2 comments
Open

Code not working for pixel level random erasing #6

Sicily-F opened this issue Feb 23, 2021 · 2 comments

Comments

@Sicily-F
Copy link

Hi there,

I'm r eally happy with this code and the function, in order to deal with occlusion in my dataset. Howvwer, I can get the random_eraser function to work to produce the black boxes, but not with the randomised pixel from the image itself.

Here is the code I used:

TRAIN_DIR = 'F:/all_species_cropped_balanced/train'

BATCH_SIZE = 32 #was 32 before
IMG_HEIGHT = 224
IMG_WIDTH = 224

#copied from this repo 

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=True): # was False before, as #true still doesn't help me do the pixel level implementation 
    def eraser(input_img):
        if input_img.ndim == 3:
            img_h, img_w, img_c = input_img.shape
        elif input_img.ndim == 2:
            img_h, img_w = input_img.shape

        p_1 = np.random.rand()

        if p_1 > p:
            return input_img

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

            if left + w <= img_w and top + h <= img_h:
                break

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

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

        return input_img

    return eraser

datagen = ImageDataGenerator(
    rescale=1/255,
    preprocessing_function=get_random_eraser(v_l=0, v_h=1, pixel_level=True)) 

train_gen = datagen.flow_from_directory(
    TRAIN_DIR, 
    target_size=(IMG_HEIGHT, IMG_WIDTH), 
    batch_size=BATCH_SIZE
)

When I plot 5 images from the data set, they still have these black boxes, even though I have set pixel_level=True (see image attached)

Does anyone have any ideas? I would be very grateful!

@Sicily-F Sicily-F changed the title Code not working for pixel erasing at the random level Code not working for pixel level random erasing Feb 23, 2021
@shuaibaslam2019
Copy link

shuaibaslam2019 commented Apr 5, 2021

Hi @Sicily-F ,

You're rescaling your data twice. Firstly, with rescale=1./255 and second time with v_h=1 when calling random erasing. Use default v_h=255.

The following code will work
datagen = ImageDataGenerator( rescale=1/255, preprocessing_function=get_random_eraser(v_l=0, v_h=255, pixel_level=True))
Best regards,

@Sicily-F
Copy link
Author

Sicily-F commented Apr 5, 2021

Hello! Ah ok, that makes so much sense, I didn't pick up on that. THANK YOU! Success
Uploading image.png…

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

2 participants