IndexError: Invalid key: 16 is out of bounds for size 0

Shaariqah
Fri, 05 May 2023 01:28:37 +0000

Im facing a similar issue currently. I created a dataset out of a folder in my drive as follows:

import os
import librosa
import pandas as pd
from datasets import Dataset

Step 2: Read file paths

def get_file_paths(audio_folder, transcript_folder):
audio_files = sorted([os.path.join(audio_folder, f) for f in os.listdir(audio_folder) if f.endswith(‘.wav’)])
transcript_files = sorted([os.path.join(transcript_folder, f) for f in os.listdir(transcript_folder) if f.endswith(‘.txt’)])
return audio_files, transcript_files

Step 3: Load and align audio and transcripts

def load_and_align_data(audio_files, transcript_files):
data =
for audio_file, transcript_file in zip(audio_files, transcript_files):
with open(transcript_file, ‘r’) as f:
transcript = f.read().strip().upper()
data.append({‘audio’: audio_file, ‘text’: transcript})
return data

Step 4: Apply alignment function

audio_folder = “/content/drive/MyDrive/TrainingDataset/Audio”
transcript_folder = “/content/drive/MyDrive/TrainingDataset/Transcripts”
audio_files, transcript_files = get_file_paths(audio_folder, transcript_folder)
aligned_data = load_and_align_data(audio_files, transcript_files)

Step 5: Create custom dataset

custom_dataset = Dataset.from_dict({“audio”: [d[“audio”] for d in aligned_data], “text”: [d[“text”] for d in aligned_data]})

loaded the models and defined the training arguments:
from transformers import TrainingArguments, Trainer, Wav2Vec2ForCTC, Wav2Vec2Tokenizer, Wav2Vec2Processor

#load pre-trained model, tokenizer, processor
tokenizer = Wav2Vec2Tokenizer.from_pretrained(“facebook/wav2vec2-base-960h”)
model = Wav2Vec2ForCTC.from_pretrained(“facebook/wav2vec2-base-960h”)
processor = Wav2Vec2Processor.from_pretrained(“facebook/wav2vec2-base-960h”)

split_ratio = 0.1
num_samples = len(custom_dataset)
train_dataset = custom_dataset.select(range(int(num_samples * (1 - split_ratio))))
val_dataset = custom_dataset.select(range(int(num_samples * (1 - split_ratio)), num_samples))

Step 4: Define the training arguments

training_args = TrainingArguments(
remove_unused_columns=False,
output_dir=“/content/drive/MyDrive/ASR_Results”, # output directory for the checkpoints and evaluation results
evaluation_strategy=“steps”, # evaluation strategy to adopt during training
eval_steps=500, # number of steps between evaluations on the validation set
save_total_limit=2, # limit the total amount of checkpoints
learning_rate=3e-4, # learning rate for the optimizer
per_device_train_batch_size=4, # batch size for training
per_device_eval_batch_size=4, # batch size for evaluation
num_train_epochs=5, # total number of training epochs
weight_decay=0.01, # weight decay for regularization
push_to_hub=False,
logging_dir=“./logs”, # directory for storing logs
logging_steps=500, # number of steps between logging messages
)

from datasets import load_metric
from transformers import DataCollatorForTokenClassification

print(len(train_dataset))
print(len(val_dataset))

data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer, padding=True)

training

training_args = TrainingArguments(output_dir=f"wav2vec2")

Step 5: Instantiate the Trainer class

trainer = Trainer(
model=model,
data_collator=data_collator,
args=training_args,
tokenizer=tokenizer,
train_dataset=train_dataset,

)

Step 6: Train the model

trainer.train()

please help, it would mean a lot