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

Example on using CutMix Augmentation for Image Classification #425

Merged
merged 57 commits into from Jun 13, 2021
Merged

Example on using CutMix Augmentation for Image Classification #425

merged 57 commits into from Jun 13, 2021

Conversation

sayannath
Copy link
Contributor

Though there are many augmentation techniques like MixUp and CutOut techniques, there are some issues as well.
It is because in CutOut Augmentation we remove a part(square) from a picture with either a Gaussian Noise or a Black pixel, which results in a decrease of important portions of an image during training. This can lead to a limitation in the case of CNN.
In the case of MixUp Augmentation, the images which are generated are somewhere not natural and confuses the model, especially for the localization task.

In this example how we use the CutMix Augmentation technique to overcome these issues. In this example, I have used the CIFAR10 dataset and the CutMix Augmentation technique performs better and yields better results than the simple one.

@google-cla
Copy link

google-cla bot commented Mar 25, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Mar 25, 2021
@sayannath
Copy link
Contributor Author

@googlebot I signed it!

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.

What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@googlebot I signed it!

@google-cla google-cla bot added cla: yes and removed cla: no labels Mar 25, 2021
@sayannath
Copy link
Contributor Author

@fchollet could you please check and review my PR, if you have time. Thank You!

@fchollet
Copy link
Member

fchollet commented Apr 1, 2021

Thanks for the PR! I'll get to reviewing it soon. I've been pretty swamped.

@sayannath
Copy link
Contributor Author

It's totally understandable!! @fchollet

@8bitmp3
Copy link
Contributor

8bitmp3 commented Apr 1, 2021

I'll check out your awesome example soon, thanks for your 👍 effort

@sayannath
Copy link
Contributor Author

Thank you! @8bitmp3

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
Copy link
Contributor

@8bitmp3 8bitmp3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff @sayannath I've added some suggestions, PTAL 👍

Thanks, I learned a lot from the paper and your example.

examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
@8bitmp3
Copy link
Contributor

8bitmp3 commented Apr 12, 2021

By the way, hope the first paragraph refactoring suggestion is OK. The first sentence explains the why and the second - what CutMix does (using the words from the original paper by Yun et al.):

_CutMix_ is a data augmentation technique that addresses the issue of
information loss and inefficiency present in regional dropout strategies. 
Instead of removing pixels and filling them with black or grey pixels or
Gaussian noise, you replace the removed regions with a patch from another image,
while the ground truth labels are mixed proportionally to the number of pixels
of combined images. CutMix was proposed in
[CutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features](https://arxiv.org/pdf/1905.04899.pdf)
(Yun et al., 2019)

sayannath and others added 11 commits April 13, 2021 11:52
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! It's looking great. I think we can improve the choice of model.

examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
examples/vision/cutmix.py Outdated Show resolved Hide resolved
Co-authored-by: 8bitmp3 <19637339+8bitmp3@users.noreply.github.com>
@fchollet
Copy link
Member

fchollet commented May 2, 2021

Is the PR ready for review at this time?

@sayannath
Copy link
Contributor Author

Almost there. Improvising the things there.

@fchollet
Copy link
Member

fchollet commented May 2, 2021

Ok, please ping me when you want me to look at it!

@fchollet
Copy link
Member

fchollet commented Jun 7, 2021

Are you still working on this?

@sayannath
Copy link
Contributor Author

sayannath commented Jun 7, 2021

Are you still working on this?

@fchollet Yes but I have tried with MobileNetV2 but not giving me desired result.

@fchollet
Copy link
Member

fchollet commented Jun 7, 2021

Ok, resnet seems fine then. Any other changes you're still working on?

@sayannath
Copy link
Contributor Author

Ok, resnet seems fine then. Any other changes you're still working on?

Training it to some more epochs and I will commit the changes today and will notify you.

@sayannath sayannath requested a review from fchollet June 7, 2021 18:44
@sayannath
Copy link
Contributor Author

sayannath commented Jun 10, 2021

@fchollet you can look into it now, I have incorporated all the changes you have told me to do.

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! It looks great. Have you tried using larger input images?

Note that I pushed some copyedits, please pull them first.


AUTO = tf.data.experimental.AUTOTUNE
BATCH_SIZE = 32
IMG_SHAPE = 32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this IMG_SIZE. IMG_SHAPE would be like (32, 32).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya Sure! Will do that



def training_model():
return resnet_v20((32, 32, 3), 20)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: in practice you get much better accuracy on CIFAR-10 by upscaling the input images (perhaps this is what you saw better accuracy with ResNet-20...), i.e. by using a (None, None, 3) as input shape and starting the model with a Rescaling(150, 150) layer. Have you tried that?

Copy link
Contributor Author

@sayannath sayannath Jun 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No haven't tried that.

@sayannath
Copy link
Contributor Author

Call this IMG_SIZE. IMG_SHAPE would be like (32, 32).

Incorporated this change. @fchollet

@fchollet
Copy link
Member

Looks good, please add the generated files.

@sayannath
Copy link
Contributor Author

@fchollet I have added the generated files.

@sayannath sayannath requested a review from fchollet June 13, 2021 04:16
Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the great contribution! 👍

@fchollet fchollet merged commit 284e42c into keras-team:master Jun 13, 2021
@sayannath
Copy link
Contributor Author

I learned a lot from this PR. Thanks, @fchollet and @8bitmp3 for guiding me.

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

Successfully merging this pull request may close these issues.

None yet

3 participants