Skip to content

Commit

Permalink
Fix wav2vec2 is_batched check to include 2-D numpy arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
LWprogramming committed May 9, 2023
1 parent 312b104 commit d4762fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/transformers/models/wav2vec2/feature_extraction_wav2vec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ def __call__(
"Failing to do so can result in silent errors that might be hard to debug."
)

is_batched = bool(
isinstance(raw_speech, (list, tuple))
and (isinstance(raw_speech[0], np.ndarray) or isinstance(raw_speech[0], (tuple, list)))
is_batched_numpy = isinstance(raw_speech, np.ndarray) and len(raw_speech.shape) == 2
if is_batched_numpy:
assert (
len(raw_speech.shape) == 2
), f"Only mono-channel audio is supported for input to {self.__class__.__name__}"
is_batched = is_batched_numpy or (
isinstance(raw_speech, (list, tuple)) and (isinstance(raw_speech[0], (np.ndarray, tuple, list)))
)

# always return batch
Expand Down
10 changes: 7 additions & 3 deletions src/transformers/models/wav2vec2/tokenization_wav2vec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,13 @@ def __call__(
values, a list of numpy arrayr or a list of list of float values.
"""

is_batched = bool(
isinstance(raw_speech, (list, tuple))
and (isinstance(raw_speech[0], np.ndarray) or isinstance(raw_speech[0], (tuple, list)))
is_batched_numpy = isinstance(raw_speech, np.ndarray) and len(raw_speech.shape) == 2
if is_batched_numpy:
assert (
len(raw_speech.shape) == 2
), f"Only mono-channel audio is supported for input to {self.__class__.__name__}"
is_batched = is_batched_numpy or (
isinstance(raw_speech, (list, tuple)) and (isinstance(raw_speech[0], (np.ndarray, tuple, list)))
)

# make sure input is in list format
Expand Down

0 comments on commit d4762fd

Please sign in to comment.