-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add NoisePerturbAugmentor and CHiME3 data preparation. #140
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9820595
Add NoisePerturbAugmentor and CHiME3 data preparation.
xinghai-sun 13eb144
Add ImpulseResponseAugmentor and augmentation.config file.
xinghai-sun 93c1aab
Update default config in augmentation.config for DS2.
xinghai-sun a52e52c
Update noise and impulse augmentor according to code review.
xinghai-sun 5656f45
Convert README.md's file mode to 644.
xinghai-sun 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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
"type": "shift", | ||
"params": {"min_shift_ms": -5, | ||
"max_shift_ms": 5}, | ||
"prob": 1.0 | ||
} | ||
] |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
[ | ||
{ | ||
"type": "noise", | ||
"params": {"min_snr_dB": 40, | ||
"max_snr_dB": 50, | ||
"noise_manifest_path": "datasets/manifest.noise"}, | ||
"prob": 0.6 | ||
}, | ||
{ | ||
"type": "impulse", | ||
"params": {"impulse_manifest_path": "datasets/manifest.impulse"}, | ||
"prob": 0.5 | ||
}, | ||
{ | ||
"type": "speed", | ||
"params": {"min_speed_rate": 0.95, | ||
"max_speed_rate": 1.05}, | ||
"prob": 0.5 | ||
}, | ||
{ | ||
"type": "shift", | ||
"params": {"min_shift_ms": -5, | ||
"max_shift_ms": 5}, | ||
"prob": 1.0 | ||
}, | ||
{ | ||
"type": "volume", | ||
"params": {"min_gain_dBFS": -10, | ||
"max_gain_dBFS": 10}, | ||
"prob": 0.0 | ||
}, | ||
{ | ||
"type": "bayesian_normal", | ||
"params": {"target_db": -20, | ||
"prior_db": -20, | ||
"prior_samples": 100}, | ||
"prob": 0.0 | ||
} | ||
] |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Contains the impulse response augmentation model.""" | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
from data_utils.augmentor.base import AugmentorBase | ||
from data_utils import utils | ||
from data_utils.audio import AudioSegment | ||
|
||
|
||
class ImpulseResponseAugmentor(AugmentorBase): | ||
"""Augmentation model for adding impulse response effect. | ||
|
||
:param rng: Random generator object. | ||
:type rng: random.Random | ||
:param impulse_manifest_path: Manifest path for impulse audio data. | ||
:type impulse_manifest_path: basestring | ||
""" | ||
|
||
def __init__(self, rng, impulse_manifest_path): | ||
self._rng = rng | ||
self._impulse_manifest = utils.read_manifest( | ||
manifest_path=impulse_manifest_path) | ||
|
||
def transform_audio(self, audio_segment): | ||
"""Add impulse response effect. | ||
|
||
Note that this is an in-place transformation. | ||
|
||
:param audio_segment: Audio segment to add effects to. | ||
:type audio_segment: AudioSegmenet|SpeechSegment | ||
""" | ||
impulse_json = self._rng.sample(self._impulse_manifest, 1)[0] | ||
impulse_segment = AudioSegment.from_file(impulse_json['audio_filepath']) | ||
audio_segment.convolve(impulse_segment, allow_resample=True) |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Contains the noise perturb augmentation model.""" | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
from data_utils.augmentor.base import AugmentorBase | ||
from data_utils import utils | ||
from data_utils.audio import AudioSegment | ||
|
||
|
||
class NoisePerturbAugmentor(AugmentorBase): | ||
"""Augmentation model for adding background noise. | ||
|
||
:param rng: Random generator object. | ||
:type rng: random.Random | ||
:param min_snr_dB: Minimal signal noise ratio, in decibels. | ||
:type min_snr_dB: float | ||
:param max_snr_dB: Maximal signal noise ratio, in decibels. | ||
:type max_snr_dB: float | ||
:param noise_manifest_path: Manifest path for noise audio data. | ||
:type noise_manifest_path: basestring | ||
""" | ||
|
||
def __init__(self, rng, min_snr_dB, max_snr_dB, noise_manifest_path): | ||
self._min_snr_dB = min_snr_dB | ||
self._max_snr_dB = max_snr_dB | ||
self._rng = rng | ||
self._noise_manifest = utils.read_manifest( | ||
manifest_path=noise_manifest_path) | ||
|
||
def transform_audio(self, audio_segment): | ||
"""Add background noise audio. | ||
|
||
Note that this is an in-place transformation. | ||
|
||
:param audio_segment: Audio segment to add effects to. | ||
:type audio_segment: AudioSegmenet|SpeechSegment | ||
""" | ||
noise_json = self._rng.sample(self._noise_manifest, 1)[0] | ||
if noise_json['duration'] < audio_segment.duration: | ||
raise RuntimeError("The duration of sampled noise audio is smaller " | ||
"than the audio segment to add effects to.") | ||
diff_duration = noise_json['duration'] - audio_segment.duration | ||
start = self._rng.uniform(0, diff_duration) | ||
end = start + audio_segment.duration | ||
noise_segment = AudioSegment.slice_from_file( | ||
noise_json['audio_filepath'], start=start, end=end) | ||
snr_dB = self._rng.uniform(self._min_snr_dB, self._max_snr_dB) | ||
audio_segment.add_noise( | ||
noise_segment, snr_dB, allow_downsampling=True, rng=self._rng) |
Empty file modified
0
deep_speech_2/data_utils/augmentor/online_bayesian_normalization.py
100755 → 100644
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"""Prepare CHiME3 background data. | ||
|
||
Download, unpack and create manifest files. | ||
Manifest file is a json-format file with each line containing the | ||
meta data (i.e. audio filepath, transcript and audio duration) | ||
of each audio file in the data set. | ||
""" | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import distutils.util | ||
import os | ||
import wget | ||
import zipfile | ||
import argparse | ||
import soundfile | ||
import json | ||
from paddle.v2.dataset.common import md5file | ||
|
||
DATA_HOME = os.path.expanduser('~/.cache/paddle/dataset/speech') | ||
|
||
URL = "https://d4s.myairbridge.com/packagev2/AG0Y3DNBE5IWRRTV/?dlid=W19XG7T0NNHB027139H0EQ" | ||
MD5 = "c3ff512618d7a67d4f85566ea1bc39ec" | ||
|
||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument( | ||
"--target_dir", | ||
default=DATA_HOME + "/chime3_background", | ||
type=str, | ||
help="Directory to save the dataset. (default: %(default)s)") | ||
parser.add_argument( | ||
"--manifest_filepath", | ||
default="manifest.chime3.background", | ||
type=str, | ||
help="Filepath for output manifests. (default: %(default)s)") | ||
args = parser.parse_args() | ||
|
||
|
||
def download(url, md5sum, target_dir, filename=None): | ||
"""Download file from url to target_dir, and check md5sum.""" | ||
if filename == None: | ||
filename = url.split("/")[-1] | ||
if not os.path.exists(target_dir): os.makedirs(target_dir) | ||
filepath = os.path.join(target_dir, filename) | ||
if not (os.path.exists(filepath) and md5file(filepath) == md5sum): | ||
print("Downloading %s ..." % url) | ||
wget.download(url, target_dir) | ||
print("\nMD5 Chesksum %s ..." % filepath) | ||
if not md5file(filepath) == md5sum: | ||
raise RuntimeError("MD5 checksum failed.") | ||
else: | ||
print("File exists, skip downloading. (%s)" % filepath) | ||
return filepath | ||
|
||
|
||
def unpack(filepath, target_dir): | ||
"""Unpack the file to the target_dir.""" | ||
print("Unpacking %s ..." % filepath) | ||
if filepath.endswith('.zip'): | ||
zip = zipfile.ZipFile(filepath, 'r') | ||
zip.extractall(target_dir) | ||
zip.close() | ||
elif filepath.endswith('.tar') or filepath.endswith('.tar.gz'): | ||
tar = zipfile.open(filepath) | ||
tar.extractall(target_dir) | ||
tar.close() | ||
else: | ||
raise ValueError("File format is not supported for unpacking.") | ||
|
||
|
||
def create_manifest(data_dir, manifest_path): | ||
"""Create a manifest json file summarizing the data set, with each line | ||
containing the meta data (i.e. audio filepath, transcription text, audio | ||
duration) of each audio file within the data set. | ||
""" | ||
print("Creating manifest %s ..." % manifest_path) | ||
json_lines = [] | ||
for subfolder, _, filelist in sorted(os.walk(data_dir)): | ||
for filename in filelist: | ||
if filename.endswith('.wav'): | ||
filepath = os.path.join(data_dir, subfolder, filename) | ||
audio_data, samplerate = soundfile.read(filepath) | ||
duration = float(len(audio_data)) / samplerate | ||
json_lines.append( | ||
json.dumps({ | ||
'audio_filepath': filepath, | ||
'duration': duration, | ||
'text': '' | ||
})) | ||
with open(manifest_path, 'w') as out_file: | ||
for line in json_lines: | ||
out_file.write(line + '\n') | ||
|
||
|
||
def prepare_chime3(url, md5sum, target_dir, manifest_path): | ||
"""Download, unpack and create summmary manifest file.""" | ||
if not os.path.exists(os.path.join(target_dir, "CHiME3")): | ||
# download | ||
filepath = download(url, md5sum, target_dir, | ||
"myairbridge-AG0Y3DNBE5IWRRTV.zip") | ||
# unpack | ||
unpack(filepath, target_dir) | ||
unpack( | ||
os.path.join(target_dir, 'CHiME3_background_bus.zip'), target_dir) | ||
unpack( | ||
os.path.join(target_dir, 'CHiME3_background_caf.zip'), target_dir) | ||
unpack( | ||
os.path.join(target_dir, 'CHiME3_background_ped.zip'), target_dir) | ||
unpack( | ||
os.path.join(target_dir, 'CHiME3_background_str.zip'), target_dir) | ||
else: | ||
print("Skip downloading and unpacking. Data already exists in %s." % | ||
target_dir) | ||
# create manifest json file | ||
create_manifest(target_dir, manifest_path) | ||
|
||
|
||
def main(): | ||
prepare_chime3( | ||
url=URL, | ||
md5sum=MD5, | ||
target_dir=args.target_dir, | ||
manifest_path=args.manifest_filepath) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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.
maybe we should do some checking here to make sure existence of audio files and transcription text files
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.
不需要,第一,noise文件不存在transcription;第二,这里的逻辑是是遍历所有的音频文件(不存在就不会被遍历到)。