# Expanding an Audio Dataset with datasets.map()?

**URL:** <https://discuss.huggingface.co/t/expanding-an-audio-dataset-with-datasets-map/15764>\
**Category:** Beginners\
**Created:** [March 16, 2022, 9:17am UTC](https://discuss.huggingface.co/t/expanding-an-audio-dataset-with-datasets-map/15764 "2022-03-16T09:17:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scasutt](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@scasutt](https://discuss.huggingface.co/u/scasutt)\
**Post date:** [March 16, 2022, 9:17am UTC](https://discuss.huggingface.co/t/expanding-an-audio-dataset-with-datasets-map/15764/1 "2022-03-16T09:17:11Z")

</div>

Hello  
After a couple of hours trying to get this to work I need to ask 🙂

I’m trying to expand a dataset (used with Wav2Vec2 for ASR) with following Idea:

dataset\_expansion = dataset

#adding simple noise to dataset expansion  
def add\_simple\_noise(batch):  
audio = batch[‘audio\_path’]  
noise = np.asarray(0.01\*np.random.randn(len(audio[“array”])))  
audio[“array”] = audio[“array”] + noise  
return batch

#map simple noise to training set  
dataset\_expansion = dataset\_expansion.map(add\_simple\_noise)  
dataset\_expansion = dataset\_expansion.cast\_column(“audio\_path”, datasets.Audio(sampling\_rate=16\_000))

The mapping itself seems to work and noise is added.

But the mapping does not seem to be correct:

By trying to concat the datasets together:  
dataset = datasets.concatenate\_datasets([dataset[“train”],dataset\_expansion[“train”]])

Throws following error:  
ArrowInvalid: Schema at index 1 was different:  
audio\_path: string  
text: string  
sampling\_rate: int64  
train\_or\_test: string  
vs  
audio\_path: struct\<array: list\<item: double\>, path: string, sampling\_rate: int64\>  
text: string  
sampling\_rate: int64  
train\_or\_test: string

Checking the features:  
dataset[“train”].features  
{‘audio\_path’: Audio(sampling\_rate=16000, mono=True, id=None),  
‘text’: Value(dtype=‘string’, id=None),  
‘sampling\_rate’: Value(dtype=‘int64’, id=None),  
‘train\_or\_test’: Value(dtype=‘string’, id=None)}

dataset\_expansion[“train”].features  
{‘audio\_path’: Audio(sampling\_rate=16000, mono=True, id=None),  
‘text’: Value(dtype=‘string’, id=None),  
‘sampling\_rate’: Value(dtype=‘int64’, id=None),  
‘train\_or\_test’: Value(dtype=‘string’, id=None)}

The dataset was loaded as follows:  
feature\_dict = {“audio\_path”: datasets.Audio(sampling\_rate=16\_000),“text”: datasets.Value(“string”)}  
data\_features = datasets.Features(feature\_dict)

dataset = load\_dataset(“csv”,  
data\_files={“train”:“toy\_train\_data.csv”,  
“test”:“toy\_test\_data.csv”},  
)  
dataset = dataset.cast\_column(“audio\_path”, datasets.Audio(sampling\_rate=16\_000,mono=True))  
dataset = dataset.remove\_columns(“Unnamed: 0”)

I might just be missing something really small. But I just can’t seem to find whatever needs to be done to get this to work 🙂

The alternative to doing this on the fly would be to make a copy of the data and add noise there.

If anyone could point me in the right direction, I’d really appreciate it.

Thank you and have a great day.

---

<div class="post-metadata">

**Author:** ![scasutt](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@scasutt](https://discuss.huggingface.co/u/scasutt)\
**Post date:** [March 16, 2022, 10:02am UTC](https://discuss.huggingface.co/t/expanding-an-audio-dataset-with-datasets-map/15764/2 "2022-03-16T10:02:40Z")

</div>

Additional information:

dataset[“train”][0][“audio\_path”]  
{‘path’: ‘./audio/ch\_ag\_0006.wav’,  
‘array’: array([-3.0517578e-05, -3.0517578e-05, -3.0517578e-05, …,  
-1.2207031e-04, -9.1552734e-05, 0.0000000e+00], dtype=float32),  
‘sampling\_rate’: 16000}

dataset\_expansion[“train”][0][“audio\_path”]

AttributeError: ‘dict’ object has no attribute ‘endswith’

---

<div class="post-metadata">

**Author:** ![mariosasko](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mariosasko/32/31548_2.png) [@mariosasko](https://discuss.huggingface.co/u/mariosasko)\
**Post date:** [March 21, 2022, 12:00pm UTC](https://discuss.huggingface.co/t/expanding-an-audio-dataset-with-datasets-map/15764/3 "2022-03-21T12:00:06Z")

</div>

Hi! Which version of `datasets` are you using? I’m pretty sure this issue can be resolved by using the newest version of `datasets`, which you can install as follows:

```auto
pip install -U datasets

```

Let me know if that doesn’t help.

---

<div class="post-metadata">

**Author:** ![scasutt](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@scasutt](https://discuss.huggingface.co/u/scasutt)\
**Post date:** [March 21, 2022, 1:08pm UTC](https://discuss.huggingface.co/t/expanding-an-audio-dataset-with-datasets-map/15764/4 "2022-03-21T13:08:09Z")

</div>

Hello Mario  
Thank you for your time.  
That was one of my assumptions at first, as the original version ran on 1.17. (and didn’t work as expected). This version of the script is running on datasets 2.0.0 (and transformers 4.17.0).

In the meantime I’m doing the audio expansion locally and uploading the expanded audio to run my tests. Not quite as elegant as I would like, but it works 🙂

Cheers

Stefan

---

<div class="post-metadata">

**Author:** ![ai-nikolai](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ai-nikolai/32/32811_2.png) [@ai-nikolai](https://discuss.huggingface.co/u/ai-nikolai)\
**Post date:** [December 5, 2024, 9:39am UTC](https://discuss.huggingface.co/t/expanding-an-audio-dataset-with-datasets-map/15764/5 "2024-12-05T09:39:25Z")

</div>

Any luck with this one?
