Giter Site home page Giter Site logo

yu4u / cutout-random-erasing Goto Github PK

View Code? Open in Web Editor NEW
166.0 5.0 37.0 120 KB

Cutout / Random Erasing implementation, especially for ImageDataGenerator in Keras

License: MIT License

Python 12.93% Jupyter Notebook 87.07%
deep-learning deeplearning keras cutout random-erasing

cutout-random-erasing's Introduction

Cutout / Random Erasing

This is a Cutout [1] / Random Erasing [2] implementation. In particular, it is easily used with ImageDataGenerator in Keras. Please check random_eraser.py for implementation details.

About Cutout / Random Erasing

Cutout or Random Erasing is a kind of image augmentation methods for convolutional neural networks (CNN). They are very similar methods and were proposed almost at the same time.

They try to regularize models using training images that are randomly masked with random values.

Usage

With ImageDataGenerator in Keras

It is very easy to use if you are using ImageDataGenerator in Keras; get eraser function by get_random_eraser(), and then pass it to ImageDataGenerator as preprocessing_function. By doing so, all images are randomly erased before standard augmentation done by ImageDataGenerator.

Please check cifar10_resnet.py, which is imported from official Keras examples.

What I did is adding only two lines:

...
from random_eraser import get_random_eraser  # added
...

    datagen = ImageDataGenerator(
    ...
        preprocessing_function=get_random_eraser(v_l=0, v_h=1))  # added

Erase a single image

Of cause, you can erase a single image using eraser function. Please note that eraser function works in inplace mode; the input image itself will be modified (therefore, img = eraser(img) can be replaced by eraser(img) in the following example).

from random_eraser import get_random_eraser
eraser = get_random_eraser()

# load image to img
img = eraser(img)

Pleae check example.ipynb for complete example.

Parameters

Parameters are fully configurable as:

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)
  • p : the probability that random erasing is performed
  • s_l, s_h : minimum / maximum proportion of erased area against input image
  • r_1, r_2 : minimum / maximum aspect ratio of erased area
  • v_l, v_h : minimum / maximum value for erased area
  • pixel_level : pixel-level randomization for erased area

Results

The original cifar10_resnet.py result (w/o cutout / random erasing):

Test loss: 0.539187009859
Test accuracy: 0.9077

With cutout / random erasing:

Test loss: 0.445597583055
Test accuracy: 0.9182

With cutout / random erasing (pixel-level):

Test loss: 0.446407950497
Test accuracy: 0.9213

References

[1] T. DeVries and G. W. Taylor, "Improved Regularization of Convolutional Neural Networks with Cutout," in arXiv:1708.04552, 2017.

[2] Z. Zhong, L. Zheng, G. Kang, S. Li, and Y. Yang, "Random Erasing Data Augmentation," in arXiv:1708.04896, 2017.

cutout-random-erasing's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cutout-random-erasing's Issues

Need not use while loop.

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.

Questions about parameter r and v

Hello,
Can you explain what r_1 and r_2 stand for ?
And why do you use random value to erase targeted area instead of 0? will this random value cause bad parameter update since it is not 0?

The program on line 35.

Thank you for sharing this very useful program.
I have a question about the program on line 35 (input_img[top:top + h, left:left + w] = c).
When I run this line the variable is treated as mutable. Is this a problem in my implementation?

Code not working for pixel level random erasing

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!

Training Accuracy stops improving

When I apply your purposed data augmentation technique, it stops my training accuracy to 95.90 ? What Could be the reason?

preprocessing_function=get_random_eraser(p=0.8, v_l=0, v_h=1, pixel_level=False))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.