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

MixUp + Cutmix implementation is (badly!) incorrect #13490

Open
dibyaghosh opened this issue Dec 11, 2024 · 1 comment · May be fixed by #13491
Open

MixUp + Cutmix implementation is (badly!) incorrect #13490

dibyaghosh opened this issue Dec 11, 2024 · 1 comment · May be fixed by #13491
Assignees
Labels
models:official models that come under official repository stat:awaiting response Waiting on input from the contributor

Comments

@dibyaghosh
Copy link

dibyaghosh commented Dec 11, 2024

The code for sampling from a beta distribution in tfm.vision.augment.MixupAndCutmix._sample_from_beta is incorrect:

It should be

  @staticmethod
  def _sample_from_beta(alpha, beta, shape):
    sample_alpha = tf.random.gamma(shape, alpha, beta=1.0) # not: tf.random.gamma(shape, 1.0, beta=alpha)
    sample_beta = tf.random.gamma(shape, beta, beta=1.0) # not: tf.random.gamma(shape, 1.0, beta=beta)
    return sample_alpha / (sample_alpha + sample_beta)

https://github.com/tensorflow/models/blob/8c5c79c27c4bb2843308bf8623daa24f4f3cbe62/official/vision/ops/augment.py#L2712C1-L2713C1

This results in very bad mixup samples:

image

# code for plot
import tensorflow_models as tfm
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
a = tf.range(1000)
x = tfm.vision.augment.MixupAndCutmix._sample_from_beta(0.2, 0.2, tf.shape( tf.range(1000))).numpy()
sns.kdeplot(x, clip=(0, 1))
x = np.random.beta(0.2, 0.2, 100000)
sns.kdeplot(x, clip=(0, 1))
plt.show()
@dibyaghosh dibyaghosh linked a pull request Dec 11, 2024 that will close this issue
8 tasks
@LakshmiKalaKadali LakshmiKalaKadali self-assigned this Dec 11, 2024
@laxmareddyp laxmareddyp added the models:official models that come under official repository label Dec 11, 2024
@LakshmiKalaKadali
Copy link
Collaborator

Hi @dibyaghosh,

Thanks for bringing out the issue and raising PR. I have reproduced the issue and observed the deviation between beta distribution in tfm.vision.augment.MixupAndCutmix._sample_from_beta and np.random.beta(0.2, 0.2, 100000) . Here is the [gist].(https://colab.sandbox.google.com/gist/LakshmiKalaKadali/06533824610d6e85ea4aa3c6399819e6/tf_model_13490.ipynb). The observations are attached:
image
image

Please track the PR raised for this issue as well.

Thank You

@LakshmiKalaKadali LakshmiKalaKadali added the stat:awaiting response Waiting on input from the contributor label Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
models:official models that come under official repository stat:awaiting response Waiting on input from the contributor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants