-
Notifications
You must be signed in to change notification settings - Fork 33.4k
[Whisper] Add model for audio classification #21754
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
Merged
sanchit-gandhi
merged 9 commits into
huggingface:main
from
sanchit-gandhi:whisper-audio-classification
Mar 7, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4595027
[Whisper] Add model for audio classification
0ee86b3
make fix-copies
37d2dc6
add to docs
6e16fb2
add docstring
3df6c2e
empty returns
d6cba06
add code example
299c348
switch to fleurs
6eb61b2
Merge branch 'main' into whisper-audio-classification
sanchit-gandhi fc8cb9a
stick everything on one line
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| BaseModelOutputWithPastAndCrossAttentions, | ||
| Seq2SeqLMOutput, | ||
| Seq2SeqModelOutput, | ||
| SequenceClassifierOutput, | ||
| ) | ||
| from ...modeling_utils import PreTrainedModel | ||
| from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings | ||
|
|
@@ -701,6 +702,33 @@ def _get_feat_extract_output_lengths(self, input_lengths: torch.LongTensor): | |
| Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. | ||
| """ | ||
|
|
||
| WHISPER_ENCODER_INPUTS_DOCSTRING = r""" | ||
| Args: | ||
| input_features (`torch.FloatTensor` of shape `(batch_size, feature_size, sequence_length)`): | ||
| Float values mel features extracted from the raw speech waveform. Raw speech waveform can be obtained by | ||
| loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via | ||
| the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the | ||
| [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a | ||
| tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] | ||
| head_mask (`torch.Tensor` of shape `(encoder_layers, encoder_attention_heads)`, *optional*): | ||
| Mask to nullify selected heads of the attention modules in the encoder. Mask values selected in `[0, 1]`: | ||
|
|
||
| - 1 indicates the head is **not masked**, | ||
| - 0 indicates the head is **masked**. | ||
| encoder_outputs (`tuple(tuple(torch.FloatTensor)`, *optional*): | ||
| Tuple consists of (`last_hidden_state`, *optional*: `hidden_states`, *optional*: `attentions`) | ||
| `last_hidden_state` of shape `(batch_size, sequence_length, hidden_size)`, *optional*) is a sequence of | ||
| hidden-states at the output of the last layer of the encoder. | ||
| output_attentions (`bool`, *optional*): | ||
| Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned | ||
| tensors for more detail. | ||
| output_hidden_states (`bool`, *optional*): | ||
| Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for | ||
| more detail. | ||
| return_dict (`bool`, *optional*): | ||
| Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. | ||
| """ | ||
|
|
||
|
|
||
| class WhisperEncoder(WhisperPreTrainedModel): | ||
| """ | ||
|
|
@@ -1578,3 +1606,123 @@ def _reorder_cache(past_key_values, beam_idx): | |
| for layer_past in past_key_values: | ||
| reordered_past += (tuple(past_state.index_select(0, beam_idx) for past_state in layer_past),) | ||
| return reordered_past | ||
|
|
||
|
|
||
| @add_start_docstrings( | ||
| """ | ||
| Whisper Encoder Model with a sequence classification head on top (a linear layer over the pooled output) for tasks | ||
| like SUPERB Keyword Spotting. | ||
| """, | ||
| WHISPER_ENCODER_INPUTS_DOCSTRING, | ||
| ) | ||
| class WhisperForAudioClassification(WhisperPreTrainedModel): | ||
| def __init__(self, config): | ||
| super().__init__(config) | ||
|
|
||
| self.encoder = WhisperEncoder(config) | ||
| num_layers = config.num_hidden_layers + 1 # transformer layers + input embeddings | ||
| if config.use_weighted_layer_sum: | ||
| self.layer_weights = nn.Parameter(torch.ones(num_layers) / num_layers) | ||
| self.projector = nn.Linear(config.hidden_size, config.classifier_proj_size) | ||
| self.classifier = nn.Linear(config.classifier_proj_size, config.num_labels) | ||
|
|
||
| # Initialize weights and apply final processing | ||
| self.post_init() | ||
|
|
||
| def freeze_encoder(self): | ||
| """ | ||
| Calling this function will disable the gradient computation for the Whisper encoder so that its parameters will | ||
| not be updated during training. Only the projection layers and classification head will be updated. | ||
| """ | ||
| self.encoder._freeze_parameters() | ||
|
|
||
| @add_start_docstrings_to_model_forward(WHISPER_ENCODER_INPUTS_DOCSTRING) | ||
| @replace_return_docstrings(output_type=SequenceClassifierOutput, config_class=_CONFIG_FOR_DOC) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think we could also use your pretrained model here if it is sota?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved in d6cba06 |
||
| def forward( | ||
| self, | ||
| input_features: Optional[torch.LongTensor] = None, | ||
| head_mask: Optional[torch.Tensor] = None, | ||
| encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, | ||
| labels: Optional[torch.LongTensor] = None, | ||
| output_attentions: Optional[bool] = None, | ||
| output_hidden_states: Optional[bool] = None, | ||
| return_dict: Optional[bool] = None, | ||
| ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: | ||
| r""" | ||
| labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): | ||
| Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., | ||
| config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If | ||
| `config.num_labels > 1` a classification loss is computed (Cross-Entropy). | ||
|
|
||
| Returns: | ||
|
|
||
| Example: | ||
|
|
||
| ```python | ||
| >>> import torch | ||
| >>> from transformers import AutoFeatureExtractor, WhisperForAudioClassification | ||
| >>> from datasets import load_dataset | ||
|
|
||
| >>> feature_extractor = AutoFeatureExtractor.from_pretrained("sanchit-gandhi/whisper-medium-fleurs-lang-id") | ||
| >>> model = WhisperForAudioClassification.from_pretrained("sanchit-gandhi/whisper-medium-fleurs-lang-id") | ||
|
|
||
| >>> ds = load_dataset("google/fleurs", "all", split="validation", streaming=True) | ||
| >>> sample = next(iter(ds)) | ||
|
|
||
| >>> inputs = feature_extractor( | ||
| ... sample["audio"]["array"], sampling_rate=sample["audio"]["sampling_rate"], return_tensors="pt" | ||
| ... ) | ||
| >>> input_features = inputs.input_features | ||
|
|
||
| >>> with torch.no_grad(): | ||
| ... logits = model(input_features).logits | ||
|
|
||
| >>> predicted_class_ids = torch.argmax(logits).item() | ||
| >>> predicted_label = model.config.id2label[predicted_class_ids] | ||
| >>> predicted_label | ||
| 'af_za' | ||
| ```""" | ||
|
|
||
| output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions | ||
| output_hidden_states = ( | ||
| output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states | ||
| ) | ||
| return_dict = return_dict if return_dict is not None else self.config.use_return_dict | ||
|
|
||
| if encoder_outputs is None: | ||
| encoder_outputs = self.encoder( | ||
| input_features, | ||
| head_mask=head_mask, | ||
| output_attentions=output_attentions, | ||
| output_hidden_states=output_hidden_states, | ||
| return_dict=return_dict, | ||
| ) | ||
|
|
||
| if self.config.use_weighted_layer_sum: | ||
| hidden_states = torch.stack(encoder_outputs, dim=1) | ||
| norm_weights = nn.functional.softmax(self.layer_weights, dim=-1) | ||
| hidden_states = (hidden_states * norm_weights.view(-1, 1, 1)).sum(dim=1) | ||
| else: | ||
| hidden_states = encoder_outputs[0] | ||
|
|
||
| hidden_states = self.projector(hidden_states) | ||
| pooled_output = hidden_states.mean(dim=1) | ||
|
|
||
| logits = self.classifier(pooled_output) | ||
|
|
||
| loss = None | ||
|
|
||
| if labels is not None: | ||
| loss_fct = CrossEntropyLoss() | ||
| loss = loss_fct(logits.view(-1, self.config.num_labels), labels.view(-1)) | ||
|
|
||
| if not return_dict: | ||
| output = (logits,) + encoder_outputs[1:] | ||
| return ((loss,) + output) if loss is not None else output | ||
|
|
||
| return SequenceClassifierOutput( | ||
| loss=loss, | ||
| logits=logits, | ||
| hidden_states=encoder_outputs.hidden_states, | ||
| attentions=encoder_outputs.attentions, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.