yu4u / cutout-random-erasing

Cutout / Random Erasing implementation, especially for ImageDataGenerator in Keras
MIT License
167 stars 38 forks source link

Code not working for pixel level random erasing #6

Open Sicily-F opened 3 years ago

Sicily-F commented 3 years ago

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!

shuaibaslam2019 commented 3 years ago

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 commented 3 years ago

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