Skip to content
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

[minor] Support setting the n_mels parameter #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ def __init__(
tokenizer: transformers.PreTrainedTokenizer,
max_len: int = 512,
inference: bool = False,
n_mels: int = 80,
):
super(SpeechDataset, self).__init__()
print("Formatting inputs...")
self.tokenizer = tokenizer
self.max_len = max_len
self.inference = inference
self.n_mels = n_mels
self.raw_data = []
with open(data_path, "r") as f:
for line in f:
Expand All @@ -55,7 +57,7 @@ def __getitem__(self, i) -> Dict[str, torch.Tensor]:
audio = torchaudio.transforms.Resample(sample_rate, 16000)(audio)
audio = audio[0] # get the first channel
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio)
mel = whisper.log_mel_spectrogram(audio, n_mels=self.n_mels)
ids_audio = [0] * int(mel.shape[1] / 10) # 10x downsample
tgt_audio = [IGNORE_TOKEN_ID] * len(ids_audio)
chat = [{"role": "user", "content": "Transcribe the speech"}]
Expand Down
2 changes: 1 addition & 1 deletion speech_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@ def init_model(model_args):
model = SpeechLLM(config, llm_model, encoder, projector)
if model_args.projector_model_path is not None:
model.load_projector(model_args.projector_model_path)
return model
return model, encoder.dims.n_mels
8 changes: 5 additions & 3 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
training_args,
) = parser.parse_args_into_dataclasses()

model = init_model(model_args)
model, n_mels = init_model(model_args)
model.freeze_llm()
model.freeze_encoder()

Expand All @@ -48,11 +48,13 @@ def main():
print("Loading data...")
train_dataset = SpeechDataset(data_args.data_path,
tokenizer=tokenizer,
max_len=training_args.model_max_length)
max_len=training_args.model_max_length,
n_mels=n_mels)
if data_args.eval_data_path:
eval_dataset = SpeechDataset(data_args.eval_data_path,
tokenizer=tokenizer,
max_len=training_args.model_max_length)
max_len=training_args.model_max_length,
n_mels=n_mels)
else:
eval_dataset = None
# Start trainer
Expand Down