# Misunderstanding around creating audio datasets from Local files

**URL:** <https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451>\
**Category:** 🤗Datasets\
**Created:** [January 26, 2023, 4:19am UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451 "2023-01-26T04:19:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![asennoussi](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/asennoussi/32/13665_2.png) [@asennoussi](https://discuss.huggingface.co/u/asennoussi)\
**Post date:** [January 26, 2023, 4:19am UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/1 "2023-01-26T04:19:20Z")

</div>

Hi, in the [documentation](https://huggingface.co/docs/datasets/audio_dataset#local-files), it only states how to add audio files, but I want to add audio files and their transcriptions.

How can I do that so I can build a dataset of snippets / transcription that I can train on?

Also, if I want to have 2 separate datasets, one for test and one for training, what’s the approach to follow? Send everything and tag in the metadata.csv or create 2 folders and upload the snippets/transcription with?

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [January 26, 2023, 9:54am UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/2 "2023-01-26T09:54:50Z")

</div>

Hi ! Here is an example in python:

```python
ds = Dataset.from_dict({
    "audio": ["path/to/audio_1", "path/to/audio_2", ..., "path/to/audio_n"],
    "transcription": ["First transcript", "Second transcript", ..., "Last transcript"],
}).cast_column("audio", Audio())

```

Alternatively you can also define an AudioFolder (see [docs](https://huggingface.co/docs/datasets/audio_dataset#audiofolder)):

```auto
my_dataset/
├── README.md
├── metadata.csv
└── data/
    ├── audio_0.wav
    ...
    └── audio_n.wav

```

> Also, if I want to have 2 separate datasets, one for test and one for training, what’s the approach to follow? Send everything and tag in the metadata.csv or create 2 folders and upload the snippets/transcription with?

You can structure your AudioFolder like this:

```auto
my_dataset/
├── README.md
├── metadata.csv
├── test/
| ├── audio_0.wav
| ...
| └── audio_n.wav
└── train/
    ├── audio_0.wav
    ...
    └── audio_n.wav

```

It’s also possible to have one `metadata.csv` in `train/` and one in `test/` if you want

---

<div class="post-metadata">

**Author:** ![sriniu](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sriniu/32/14806_2.png) [@sriniu](https://discuss.huggingface.co/u/sriniu)\
**Post date:** [March 19, 2023, 7:16pm UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/3 "2023-03-19T19:16:54Z")

</div>

@lhoestq ,

Thanks for posting the answer. One related question, Will the created dataset be already available to public or should we manually upload them?

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [March 20, 2023, 11:08am UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/4 "2023-03-20T11:08:48Z")

</div>

You can upload the directory described above to a dataset repository on [https://huggingface.co](https://huggingface.co) to make it available to public 🙂

see [Share a dataset to the Hub](https://huggingface.co/docs/datasets/upload_dataset)

---

<div class="post-metadata">

**Author:** ![sriniu](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sriniu/32/14806_2.png) [@sriniu](https://discuss.huggingface.co/u/sriniu)\
**Post date:** [March 20, 2023, 1:50pm UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/5 "2023-03-20T13:50:38Z")

</div>

@lhoestq Thanks.  
I have a local dataset currently and i created a datatset instance to use it with trained.train.  
I use a batch size equals 4. However i do see that my GPU memory is almost full.I was wondering if trainer.train loads all the data into the memory though I have specified the batch size?

Just to give some idea, my train size is 84K samples with each samples being of length (16000,) (which is 1second of audio). My eval set is also of 84K samples.

Any leads to solve this issue?

---

<div class="post-metadata">

**Author:** ![asennoussi](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/asennoussi/32/13665_2.png) [@asennoussi](https://discuss.huggingface.co/u/asennoussi)\
**Post date:** [March 20, 2023, 2:24pm UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/6 "2023-03-20T14:24:44Z")

</div>

Use streaming mode.

---

<div class="post-metadata">

**Author:** ![sriniu](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sriniu/32/14806_2.png) [@sriniu](https://discuss.huggingface.co/u/sriniu)\
**Post date:** [March 20, 2023, 6:20pm UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/7 "2023-03-20T18:20:04Z")

</div>

In my case i have a dataset locally on my computer and its confidential. So Can i use streaming mode for local data?

---

<div class="post-metadata">

**Author:** ![asennoussi](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/asennoussi/32/13665_2.png) [@asennoussi](https://discuss.huggingface.co/u/asennoussi)\
**Post date:** [March 21, 2023, 5:29am UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/8 "2023-03-21T05:29:19Z")

</div>

Yes, you create a private dataset on huggingface and you stream from it.

---

<div class="post-metadata">

**Author:** ![sriniu](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sriniu/32/14806_2.png) [@sriniu](https://discuss.huggingface.co/u/sriniu)\
**Post date:** [March 22, 2023, 9:14am UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/9 "2023-03-22T09:14:45Z")

</div>

Thanks,  
Could you please guide me how to handle to Out of memory issue while executing “Trainer.evaluate”

Here is my setup

```auto
#getting encoded datset
#preprocess_function(audio_dataset[:5])
def preprocess_function(examples):
    audio_arrays = [x["array"] for x in examples["audio"]]
    inputs = feature_extractor(
        audio_arrays, 
        sampling_rate=feature_extractor.sampling_rate, 
        max_length=int(128), 
        truncation=True, 
    )
    return inputs
audio_dict2 = {}
num_samples = 84000
audio_dict2['audio'] = ['../data/20230319_audioinput_1s_wav/test/' + str(int(y)) + '/audio_'+str(int(x))+'.wav' for x,y in zip(df_test.head(num_samples).index, df_test.head(num_samples).label)]
audio_dict2['label'] = [x for x in df_test.head(num_samples).label]
audio_dict2['split'] = len(df_test.head(num_samples))*['train']

audio_dataset2 = Dataset.from_dict(audio_dict2).cast_column("audio", Audio())
encoded_dataset2 = audio_dataset2.map(preprocess_function, remove_columns=["audio"], batched=True)

#training args

model_name = model_checkpoint.split("/")[-1]
batch_size = 4
metric = load_metric("accuracy")
args = TrainingArguments(
    f"{model_name}-finetuned-ks1",
    evaluation_strategy = "epoch",
    save_strategy = "epoch",
    learning_rate=3e-5,
    per_device_train_batch_size=batch_size,
    gradient_accumulation_steps=batch_size,
    per_device_eval_batch_size=batch_size,
    num_train_epochs=25,
    warmup_ratio=0.1,
    logging_steps=1,
    load_best_model_at_end=True,
    metric_for_best_model="accuracy",
    push_to_hub=False,
)

trainer = Trainer(
    model,
    args,
    train_dataset=encoded_dataset2,
    eval_dataset=encoded_dataset2,
    tokenizer=feature_extractor,
    compute_metrics=compute_metrics
)

## I ge the OOM error here!

outputs = trainer.evaluate(encoded_dataset2)

```

In my case the dataset is available locally, Is there an option to batch process this evaluate process?

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [March 23, 2023, 10:14am UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/10 "2023-03-23T10:14:59Z")

</div>

You can try saving some RAM by caching your dataset on disk by passing `cache_file_name=` to map()

Indeed creating it using from\_dict() keeps it in RAM

---

<div class="post-metadata">

**Author:** ![RajSang](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@RajSang](https://discuss.huggingface.co/u/RajSang)\
**Post date:** [July 17, 2023, 7:57am UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/11 "2023-07-17T07:57:58Z")

</div>

Hi @lhoestq, will an audio dataset that is created through Dataset.from\_dict or through AudioFolder and then pushed to hub, support streaming automatically? If not is there a way to add this while creating AudioFolder/ audio dataset from local paths and not through the script method?

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [July 17, 2023, 9:32am UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/12 "2023-07-17T09:32:19Z")

</div>

Yes it will support streaming 🙂

---

<div class="post-metadata">

**Author:** ![RajSang](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@RajSang](https://discuss.huggingface.co/u/RajSang)\
**Post date:** [July 17, 2023, 6:08pm UTC](https://discuss.huggingface.co/t/misunderstanding-around-creating-audio-datasets-from-local-files/30451/13 "2023-07-17T18:08:35Z")

</div>

Thanks a lot! 😃
