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

Feature/add spec augment layer #135

Conversation

MichaelisTrofficus
Copy link
Contributor

The PR incorporates SpecAugment's augmentation technique. Currently, it cannot be used for spectrograms deeper than 1 (although I am working on it, but the original paper does not discuss this issue). Also, image_warping has not been introduced because it gave problems when encapsulating it in a Keras Layer.I have tried to test those features that seemed important to me, but any suggestions are welcome! :)

Copy link
Owner

@keunwoochoi keunwoochoi left a comment

Choose a reason for hiding this comment

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

Hi, thank you for the PR! It looks pretty good and SpecAug would be useful for sure. There are some requests for changes :)

Generate a mask for the axis provided
Args:
inputs (`tuple`): A 3-tuple with the following structure:
inputs[0]: A spectrogram
Copy link
Owner

Choose a reason for hiding this comment

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

shape of spectrogram? (i guessed it's 4D)

Args:
inputs (`tuple`): A 3-tuple with the following structure:
inputs[0]: A spectrogram
inputs[1]: The axis where the mask will be applied
Copy link
Owner

Choose a reason for hiding this comment

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

(int)

inputs (`tuple`): A 3-tuple with the following structure:
inputs[0]: A spectrogram
inputs[1]: The axis where the mask will be applied
inputs[2]: The mask param as defined in the original paper
Copy link
Owner

Choose a reason for hiding this comment

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

..which is the max width of the mask, right? let's clarify.

inputs[1]: The axis where the mask will be applied
inputs[2]: The mask param as defined in the original paper
Returns:
A mask represented as a boolean tensor
Copy link
Owner

Choose a reason for hiding this comment

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

shall we say something about the shape here? (to be extremely clear about everything)

axis_indices = tf.reshape(axis_indices, (1, 1, -1))

mask_width = tf.random.uniform(shape=(), maxval=mask_param, dtype=tf.int32)
mask_start = tf.random.uniform(shape=(), maxval=axis_limit - mask_width, dtype=tf.int32)
Copy link
Owner

Choose a reason for hiding this comment

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

seems like axis_limit - mask_width can be <0. should we take care of this case?

by the axis provided.
Args:
x: The input spectrogram
axis: The axis where the masks will be applied
Copy link
Owner

Choose a reason for hiding this comment

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

(int)

Args:
x: The input spectrogram
axis: The axis where the masks will be applied
mask_param: The mask param as defined in the original paper
Copy link
Owner

Choose a reason for hiding this comment

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

same here - type and description.

axis_indices = tf.reshape(axis_indices, (-1, 1, 1))
elif axis == 1:
axis_indices = tf.reshape(axis_indices, (1, -1, 1))
else:
Copy link
Owner

Choose a reason for hiding this comment

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

would else be okay? or shall we add elif axis ==2: followed by else: raise NotImplementedError? i usually prefer the latter unless it's taken care of somewhere else very clearly.

model.add(spec_augment)

# We must force training to True to test properly if SpecAugment works as expected
spec_augmented = model(batch_src, training=True)[0]
Copy link
Owner

Choose a reason for hiding this comment

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

let's add a test to ensure it doesn't change anything when training != False.


# We must force training to True to test properly if SpecAugment works as expected
spec_augmented = model(batch_src, training=True)[0]

Copy link
Owner

Choose a reason for hiding this comment

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

i get that it's difficult to test the behavior of the whole layer. but can't there be any way at all..?
also, at least we can test the static methods we defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, you mean the method for generating masks, applyng masks and similar, right? I

Copy link
Owner

Choose a reason for hiding this comment

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

yup, that's right.

@MichaelisTrofficus
Copy link
Contributor Author

I had to refactor a bit the original code because I was having problems saving the layer because of the map_fn functions I was using. Now it works as expected.

@MichaelisTrofficus
Copy link
Contributor Author

Hi @keunwoochoi! Have you had a chance to look at the changes? Any suggestions please go ahead! 👍

@keunwoochoi
Copy link
Owner

keunwoochoi commented Dec 13, 2021

Thank you again for the solid work and sorry for my delay. It has been overwhelmingly busy these days.
Just ran the test locally, it passes 🎉

I have two final requests.

  • Please run black to format the code
  • Since we don't have a correctness test, could you add a simple Jupyter notebook to example/ that illustrates the result after SpecAug?

@keunwoochoi
Copy link
Owner

@MichaelisTrofficus looks great, thank you! i made some small changes. will do another review soon just in case; and merge.

@keunwoochoi keunwoochoi changed the base branch from master to kapre-0.3.7 January 21, 2022 20:00
@keunwoochoi keunwoochoi merged commit 7ce3bfa into keunwoochoi:kapre-0.3.7 Jan 21, 2022
keunwoochoi added a commit that referenced this pull request Jan 21, 2022
* Feature/add spec augment layer (#135)

* Add SpecAugment Layer

* Extend docstrings for methods in the main class

* Add __all__ to import augmentation techniques

* Improve docstrings and add exceptions handling

* Improve tests for SpecAugment augmentation layer

* Uncomment tests. Fix setup.py

* Solve saving issue with tf format / Refactor code

* Add Jupyter Notebook and apply black for code reformatting

* add version printing; add 5 figures

* bump version; 0.3.6 -> 0.3.7

Co-authored-by: Keunwoo Choi <keunwoo.choi@bytedance.com>

* add release note

Co-authored-by: Miguel Otero Pedrido <32078719+MichaelisTrofficus@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

None yet

2 participants