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
Create Audio feature #2324
Merged
albertvillanova
merged 180 commits into
huggingface:master
from
aiinnova:features-audio
Oct 13, 2021
Merged
Create Audio feature #2324
Changes from 93 commits
Commits
Show all changes
180 commits
Select commit
Hold shift + click to select a range
c27da03
Refactor features as a package
albertvillanova 4d99ff9
Move translation features into own module
albertvillanova e8a7457
Create Audio feature
albertvillanova 5110c68
Fix style
albertvillanova f69a7a9
Make late import for audio library
albertvillanova fcdb110
Fix imports
albertvillanova e6e8ff1
Ignore flake8 errors
albertvillanova 2d2adcd
Fix imports
albertvillanova e7d8904
Fix imports
albertvillanova 585bba7
Fix imports of private classes/functions
albertvillanova 827b263
Fix imports of private classes/functions
albertvillanova dc5b582
Fix test patch
albertvillanova d8e7441
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova 76cb67d
Mimic features package for tests
albertvillanova 7b72de8
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova e0b77c1
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova 3c28a61
Add required Feature attributes to Audio
albertvillanova 496a1fb
Implement __call__
albertvillanova 158c545
Add coding_format attribute
albertvillanova f13b7e8
Validate audio coding format
albertvillanova 7b658f9
Add Audio docs
albertvillanova 22f4131
Test Audio instantiation
albertvillanova e95d3f9
Add audio test data
albertvillanova 100ceb1
Add Audio dependency requirements
albertvillanova 1d29232
Fix Audio call
albertvillanova e51a8a9
Test Audio encode example
albertvillanova b16bc65
Fix style
albertvillanova 977fa33
Add audio dependency requirements to tests
albertvillanova e0672e4
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova e5d87a7
Skip test for linux
albertvillanova e02e387
Fix import of private function
albertvillanova 340989a
Return 1D array
albertvillanova bd98f78
Encode example for Audio feature
albertvillanova 4230843
Test dataset with Audio feature
albertvillanova bfe7517
Replace Audio encode_example with decode_example
albertvillanova 1a230bd
Implement Features decode_example
albertvillanova c153e6d
Fix Audio __call__
albertvillanova 13a1341
Test decoding of dataset with Audio feature
albertvillanova 9501298
Replace soundfile with librosa
albertvillanova e9e6879
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova d25b1d7
Remove array reshape
albertvillanova 34d5b7b
Fix tests
albertvillanova 85fa108
Flatten decode_nested_example
albertvillanova 1342eba
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova 401327f
Implement Features.decode_batch
albertvillanova bd312d1
Decode features in _getitem
albertvillanova a848928
Refactor test_dataset_with_audio_feature
albertvillanova f91cf6b
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova 5dc0ba8
Fix style
albertvillanova 42ea474
Implement PythonFeaturesDecoder
albertvillanova 8afdb25
Compose Formatter with PythonFeaturesDecoder
albertvillanova e4eef07
Refactor PythonFormatter.format_row to use PythonFeaturesDecoder
albertvillanova 80a3d06
Pass features to instantiate formatter
albertvillanova c314ec5
Fix style
albertvillanova a122552
Refactor decode_nested_example with default for the rest of features
albertvillanova c0de3aa
Fix missing pass features to instantiate formatter
albertvillanova b3214e1
Revert flatten of decode_nested_example to return nested examples
albertvillanova 42426e8
Fix test_dataset_with_audio_feature for nested output
albertvillanova 2067083
Return also audio path in decode_example
albertvillanova af5fc26
Add path to audio tests
albertvillanova 1499f3e
Fix Formatter and NumpyFormatter init
albertvillanova 23973f2
Fix format_table with python_formatter without features
albertvillanova 56e0b7d
Fix PythonFeaturesDecoder.decode_row only if features
albertvillanova a8d836e
Fix all Formatter subclasses init
albertvillanova f5b1d13
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova caf2153
Fix typo
albertvillanova 6c1ea4b
Test batch in dataset with Audio feature
albertvillanova 5ec9031
Implement PythonFeaturesDecoder.decode_batch
albertvillanova ead8001
Use PythonFeaturesDecoder.decode_batch in PythonFormatter.format_batch
albertvillanova 1f1f730
Add docstrings
albertvillanova 8930573
Add mono attribute to Audio feature
albertvillanova c4e7905
Test formatted dataset with Audio feature
albertvillanova 4623cdd
Implement ArrowFeaturesDecoder
albertvillanova a7071cc
Compose Formatter with ArrowFeaturesDecoder
albertvillanova 90c5873
Make NumpyFormatter.format_row use SimpleArrowExtractor and ArrowFeat…
albertvillanova 217782c
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova 8587ea1
Fix decode_nested_example to decode only keys present in example
albertvillanova 6a916e4
Refactor NumpyFormatter.format_row
albertvillanova b648074
Test pandas formatted dataset with Audio feature
albertvillanova 5f274e1
Implement PandasFeaturesDecoder
albertvillanova 7dac12a
Compose Formatter with PandasFeaturesDecoder
albertvillanova 7af69e6
Make PandasFormatter.format_row use PandasFeaturesDecoder
albertvillanova 4e34d21
Fix PandasFeaturesDecoder.decode_row for None features and keys not i…
albertvillanova 77f67a8
Fix PandasFeaturesDecoder.decode_row to call transform only for featu…
albertvillanova 2d87e96
Fix unused imports
albertvillanova 8ede53b
Remove ArrowFeaturesDecoder and _nest
albertvillanova 53e18bd
Fix typo
albertvillanova fdb050e
Remove test skip if linux
albertvillanova 329abc5
Revert "Remove test skip if linux"
albertvillanova da7edc5
Fix PandasFeaturesDecoder.decode_row to transform and assign transfor…
albertvillanova 149dc51
Make Audio instances hashable
albertvillanova 109c186
Make Audio.decode_example return original value if dependencies not i…
albertvillanova 0c66689
Fix style
albertvillanova b3939b1
Test audio resampling
albertvillanova f2e29ac
Test Audio feature decode mp3
albertvillanova 9c09f4c
Refactor Audio.decode_example to support mp3 with torchaudio
albertvillanova 0fd87d7
Fix style
albertvillanova 4c08a52
Fix logic in Audio.decode_example
albertvillanova 7c16d8a
Require torchaudio dependency for tests
albertvillanova fa3e068
Require torch to test audio mp3
albertvillanova 423dbba
Refactor decoding with torchaudio with mono and librosa resampling
albertvillanova 8174fc2
Set sox_io backend when decoding with torchaudio
albertvillanova 5881d9a
Fix test_audio with more specific pytest markers
albertvillanova 7c0bd9d
Fix unused torchaudio.functional
albertvillanova 443d4e0
Fix requirement of sndfile
albertvillanova b0e345f
Fix require_sox
albertvillanova 6a23bdb
Refactor import of find_spec
albertvillanova 09ac25f
Revert torchaudio resampling using librosa
albertvillanova 0b36596
Simplify torchaudio resampling
albertvillanova 15cea1e
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova 799b138
Fix require_sndfile
albertvillanova e64b30a
Implement conditionally decoding
albertvillanova 22984b0
Implement decoded param in _getitem
albertvillanova 4069c82
Pass decoded=False when iterating in map
albertvillanova 86d33a1
Rename sampling_rate
albertvillanova 2e5dae9
Fix NumpyFormatter
albertvillanova f58f802
Test dataset with not decoded Audio feature
albertvillanova 3597d44
Test map dataset with Audio feature is decoded
albertvillanova d51f1a6
Use lazy dict to decorate arg of mapped function
albertvillanova e9188ba
Fix decorator in map
albertvillanova e108697
Fix sampling_rate by torchaudio
albertvillanova 13ff8aa
Fix test decoding mp3
albertvillanova bacf5d1
Add GitHub Action for audio CI tests
albertvillanova 3169d0f
Remove audio dependencies from test dependencies
albertvillanova 1548bca
Comment unused audio pytest marker
albertvillanova 3ce50cd
Refactor LazyDict
albertvillanova 9d7c3e8
Fix tests
albertvillanova ac7ef25
Run audio tests in parallel
albertvillanova 46cdebd
Rename audio test job
albertvillanova 4a76773
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova 902d173
Call parallel test on Linux as on Windows
albertvillanova 02b1572
Implement Audio decode_batch
albertvillanova b7cf206
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova 9a1b63d
Test batched map dataset with Audio feature is decoded
albertvillanova 835a4d4
Fix _map_single to avoid decoding of batches
albertvillanova 87cb4fd
Implement Example and Batch from LazyDict
albertvillanova 0c46754
Decorate mapped function with Example/Batch lazy dict
albertvillanova 137b29e
Fix PythonFormatter for batch conditional decoding
albertvillanova 58e6a01
Fix typo
albertvillanova c68505a
Refactor iter
albertvillanova 955bd01
Fix _map_single for batched
albertvillanova 07b6bc5
Refactor _get_item
albertvillanova 0f80e6e
Fix style
albertvillanova cd5311b
Refactor resampling using torchaudio
albertvillanova 98d962d
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova c26a60e
Add docstring and comments to decorate function
albertvillanova c22cb6b
Remove comment
albertvillanova c0d6eec
Test batch numpy formatted dataset with Audio feature
albertvillanova efba528
Make NumpyFormatter.format_batch use decoder
albertvillanova 71891f0
Test batch pandas formatted dataset with Audio feature
albertvillanova 57720c7
Implement PandasFeaturesDecoder.decode_batch
albertvillanova da5d138
Make PandasFormatter.format_batch use decoder
albertvillanova a8ffee8
Fix style
albertvillanova ec32d2f
Make CustomFormatter use decoder
albertvillanova 3aee1f1
Change Features.decode_example/batch
albertvillanova f5bd62a
Fix CustomFormatter
albertvillanova 0ce0832
Test resampling when loading a dataset
albertvillanova e85c61b
Test resampling after loading a dataset
albertvillanova 53d6d73
Implement cast a column to a feature for decoding w/o caching
albertvillanova 633ef09
Make cast_column call cast if not decoding
albertvillanova 9c56ee9
Test decode column in dataset with Audio feature
albertvillanova 2afa6b6
Implement Features.decode_column
albertvillanova 7088e17
Implement PythonFeaturesDecoder.decode_column
albertvillanova 4dd3160
Make PythonFormatter.format_column use PythonFeaturesDecoder
albertvillanova 377cfcb
Add type hints and rename column to column_name
albertvillanova c0515f2
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova a4a6f25
Fix typo
albertvillanova 7854ea9
Update fingerprint within cast_column
albertvillanova bf94ac9
Improve docstring
albertvillanova eb7b8c5
Test decode column in formatted dataset with Audio feature
albertvillanova 0678af2
Make NumpyFormatter.format_column use PythonFeaturesDecoder
albertvillanova b1ed47e
Implement PandasFeaturesDecoder.decode_column
albertvillanova a05c9a5
Make PandasFormatter.format_column use PandasFeaturesDecoder
albertvillanova 97bf4ed
Rename variable in PandasFeaturesDecoder.decode_row
albertvillanova 583b24d
Merge remote-tracking branch 'upstream/master' into features-audio
albertvillanova 98bcbc8
Add type hint to cast_column
albertvillanova d29cfee
Implement cast_column for DatasetDict
albertvillanova 556a7a1
Add cast_column to API docs
albertvillanova 50cbe21
Add example of cast_column to docs how-to guide
albertvillanova 87943c2
Fix Sphinx role name
albertvillanova File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
Array3D, | ||
Array4D, | ||
Array5D, | ||
Audio, | ||
ClassLabel, | ||
Features, | ||
Sequence, | ||
|
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# flake8: noqa | ||
from .audio import Audio | ||
from .features import * | ||
from .features import ( | ||
_ArrayXD, | ||
_ArrayXDExtensionType, | ||
_arrow_to_datasets_dtype, | ||
_cast_to_python_objects, | ||
_is_zero_copy_only, | ||
) | ||
from .translation import Translation, TranslationVariableLanguages |
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Any, ClassVar, Optional | ||
|
||
import pyarrow as pa | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Audio: | ||
"""Audio Feature to extract audio data from an audio file. | ||
|
||
Args: | ||
sampling_rate (:obj:`int`, optional): Target sampling rate. If `None`, the native sampling rate is used. | ||
mono (:obj:`bool`, default ```True``): Whether to convert the audio signal to mono by averaging samples across channels. | ||
""" | ||
|
||
sampling_rate: int = None | ||
mono: bool = True | ||
id: Optional[str] = None | ||
# Automatically constructed | ||
dtype: ClassVar[str] = "dict" | ||
pa_type: ClassVar[Any] = None | ||
_type: str = field(default="Audio", init=False, repr=False) | ||
|
||
def __call__(self): | ||
return pa.string() | ||
|
||
def decode_example(self, value): | ||
"""Decode example audio file into audio data. | ||
|
||
Args: | ||
value: Audio file path. | ||
|
||
Returns: | ||
dict | ||
""" | ||
try: | ||
import librosa | ||
except ImportError: | ||
return value | ||
|
||
with open(value, "rb") as f: | ||
array, sample_rate = librosa.load(f, sr=self.sampling_rate, mono=self.mono) | ||
return {"path": value, "array": array, "sampling_rate": sample_rate} |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
xopen
(currently from the streaming_download_manager.py) but feel free to move it if necessary) to support reading from remote files in streaming mode :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a bit problematic as it will always fail if the value is of type
.mp3
(which is Common Voice's format). To fix it, I think we should do the following:-> we should also add a couple of tests for
.mp3
IMO (one that fails gracefully and one that correctly loads MP3 with ffmpeg installedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also see: librosa/librosa#945
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be problematic for
streaming
@lhoestq ? I sadly don't think there is a way to open the file with and then pass the binary buffer tolibrosa
for MP3There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For audio file support, I would like to again link to that excellent comment by
adefossez
: huggingface/transformers#11337 (comment)Shouldn't we have support for torchaudio too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For MP3
torchaudio
is the best choice IMO as well, but it's a pretty heavy dependency withtorch
, but it's clearly the fastest. In my experiments,torchaudio
gives a 50x speed up compared tolibrosa
for MP3. This is pretty significant - for distributed training the fallback of librosa for MP3 actually makes the preprocessing take more time than the actual trainingCould we maybe add something like "if format == mp3" -> check if torchaudio is installed - if not then throw error that
torchaudio
needs to be installed formp3
?Having played around with the feature a bit,
librosa + ffmpeg
is very very slow compared totorchaudio
and I think the better choice is here to actually throw an error instead of falling back tolibrosa + ffmpeg
.@anton-l - do you know any fast decoding libraries apart
torchaudio
? What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrickvonplaten if I understand, mp3 is not supported by
torchaudio
in Windows machines. Then, no support for mp3 for Windows?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah windows users will have to implement their own decoding then