Happy new years
This commit is contained in:
1 parent
c7175dd7ff
commit
8c4865effb
53 files changed
+4604
-61
No files matched your search
@@ -0,0 +1,52 @@
|
||||
# !pip install transformers
|
||||
# !pip install datasets
|
||||
import soundfile as sf
|
||||
import torch
|
||||
from datasets import load_dataset
|
||||
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
||||
|
||||
# load pretrained model
|
||||
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
|
||||
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
|
||||
|
||||
|
||||
librispeech_samples_ds = load_dataset("patrickvonplaten/librispeech_asr_dummy", "clean", split="validation")
|
||||
path_file = librispeech_samples_ds[1]["file"]
|
||||
|
||||
print("file = ", path_file)
|
||||
|
||||
|
||||
# load audio
|
||||
audio_input, sample_rate = sf.read(path_file)
|
||||
|
||||
print(f"audio_input shape = {audio_input.shape}, sr = {sample_rate}, duration = {(len(audio_input)/sample_rate)}")
|
||||
|
||||
# pad input values and return pt tensor
|
||||
input_values = processor(audio_input, sampling_rate=sample_rate, return_tensors="pt").input_values
|
||||
|
||||
# INFERENCE
|
||||
|
||||
# retrieve logits & take argmax
|
||||
logits = model(input_values).logits
|
||||
|
||||
print(f"logit shape = {logits.shape}")
|
||||
|
||||
# predicted_ids = torch.argmax(logits, dim=-1)
|
||||
|
||||
# # transcribe
|
||||
# transcription = processor.decode(predicted_ids[0])
|
||||
|
||||
# # FINE-TUNE
|
||||
|
||||
# target_transcription = "A MAN SAID TO THE UNIVERSE I EXIST"
|
||||
|
||||
# # encode labels
|
||||
# with processor.as_target_processor():
|
||||
# labels = processor(target_transcription, return_tensors="pt").input_ids
|
||||
|
||||
# # compute loss by passing labels
|
||||
# loss = model(input_values, labels=labels).loss
|
||||
# print("loss", loss)
|
||||
|
||||
# # loss.backward()
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import torch
|
||||
import torchaudio
|
||||
import torch.nn.functional as F
|
||||
import soundfile as sf
|
||||
import librosa
|
||||
from datasets import load_dataset
|
||||
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor,Wav2Vec2Model
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
|
||||
|
||||
LANG_ID = "zh-CN"
|
||||
MODEL_ID = "jonatasgrosman/wav2vec2-large-xlsr-53-chinese-zh-cn"
|
||||
SAMPLES = 2
|
||||
|
||||
print("load model ...")
|
||||
processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
|
||||
# model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
|
||||
model = Wav2Vec2Model.from_pretrained(MODEL_ID)
|
||||
|
||||
# processor = Wav2Vec2Processor.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
|
||||
# model = Wav2Vec2ForCTC.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
|
||||
|
||||
# # Preprocessing the datasets.
|
||||
# # We need to read the audio files as arrays
|
||||
# def speech_file_to_array_fn(batch):
|
||||
# speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
|
||||
# batch["speech"] = speech_array
|
||||
# batch["sentence"] = batch["sentence"].upper()
|
||||
|
||||
# return batch
|
||||
|
||||
# print("load dataest ...")
|
||||
# test_dataset = load_dataset("common_voice", LANG_ID, split=f"test[:{SAMPLES}]")
|
||||
# test_dataset = test_dataset.map(speech_file_to_array_fn)
|
||||
# inputs = processor(test_dataset["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
||||
|
||||
|
||||
librispeech_samples_ds = load_dataset("patrickvonplaten/librispeech_asr_dummy", "clean", split="validation")
|
||||
path_file = librispeech_samples_ds[0]["file"]
|
||||
# path_file = '/yrfs/lsen/DATA/LuoXData/processing2/2021-P01/audio/audio_trim.wav'
|
||||
# print("file = ", path_file)
|
||||
|
||||
|
||||
# load audio
|
||||
# audio_input, sample_rate = sf.read(path_file)
|
||||
audio_input, sample_rate = torchaudio.load(path_file)
|
||||
if len(audio_input.shape) == 2:
|
||||
audio_input = audio_input[0]
|
||||
|
||||
print(f"audio_input shape = {audio_input.shape}, sr = {sample_rate}, duration = {(len(audio_input)/sample_rate)}")
|
||||
|
||||
if sample_rate != 16000:
|
||||
resampler = torchaudio.transforms.Resample(sample_rate, 16_000)
|
||||
audio_input = resampler(audio_input).squeeze().numpy()
|
||||
print(f"audio_input shape = {audio_input.shape}, sr = {16000}, duration = {(len(audio_input)/16000)}")
|
||||
|
||||
|
||||
# Preprocessing the datasets.
|
||||
# We need to read the aduio files as arrays
|
||||
|
||||
# pad input values and return pt tensor
|
||||
inputs = processor(audio_input, sampling_rate=16000, return_tensors="pt")
|
||||
|
||||
|
||||
print("predict ...")
|
||||
with torch.no_grad():
|
||||
logits = model(inputs.input_values, attention_mask=inputs.attention_mask)
|
||||
|
||||
|
||||
print(f"logits shape = {logits.shape}")
|
||||
|
||||
logits = F.softmax(logits, dim=-1)
|
||||
print(f"logits reshape = {logits.shape}")
|
||||
|
||||
# padding last vector by last (0,0), model (0,1)
|
||||
logits = F.pad(logits, (0,0,0,1), mode='replicate')
|
||||
logits = torch.squeeze(logits)
|
||||
print(f"pad reshape = {logits.shape}")
|
||||
|
||||
# convert to numpy
|
||||
logits = logits.cpu().numpy()
|
||||
path_save = Path(path_file).parent.joinpath(f"{Path(path_file).stem}_wav2vec2.npy")
|
||||
np.save(path_save, logits)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import torch
|
||||
import torchaudio
|
||||
from datasets import load_dataset
|
||||
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
||||
|
||||
test_dataset = load_dataset("common_voice", "zh-CN", split="test")
|
||||
|
||||
processor = Wav2Vec2Processor.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
|
||||
model = Wav2Vec2ForCTC.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
|
||||
|
||||
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
||||
|
||||
# Preprocessing the datasets.
|
||||
# We need to read the aduio files as arrays
|
||||
def speech_file_to_array_fn(batch):
|
||||
ipath = batch["path"]
|
||||
print(f"ipath speech = {ipath}")
|
||||
speech_array, sampling_rate = torchaudio.load(ipath)
|
||||
|
||||
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
||||
return batch
|
||||
|
||||
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
||||
inputs = processor(test_dataset[:2]["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
||||
|
||||
with torch.no_grad():
|
||||
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
||||
print(f"logits shape = {logits.shape}")
|
||||
|
||||
predicted_ids = torch.argmax(logits, dim=-1)
|
||||
|
||||
print("Prediction:", processor.batch_decode(predicted_ids))
|
||||
print("Reference:", test_dataset[:2]["sentence"])
|
||||
@@ -0,0 +1,94 @@
|
||||
import torch
|
||||
import torchaudio
|
||||
import torch.nn.functional as F
|
||||
import soundfile as sf
|
||||
import librosa
|
||||
from datasets import load_dataset
|
||||
from transformers import Wav2Vec2FeatureExtractor, Wav2Vec2Model
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
|
||||
|
||||
LANG_ID = "zh-CN"
|
||||
MODEL_ID = "jonatasgrosman/wav2vec2-large-xlsr-53-chinese-zh-cn"
|
||||
SAMPLES = 2
|
||||
|
||||
print("load model ...")
|
||||
processor = Wav2Vec2FeatureExtractor.from_pretrained(MODEL_ID)
|
||||
# model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
|
||||
model = Wav2Vec2Model.from_pretrained(MODEL_ID)
|
||||
|
||||
|
||||
# processor = Wav2Vec2Processor.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
|
||||
# model = Wav2Vec2ForCTC.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
|
||||
|
||||
# # Preprocessing the datasets.
|
||||
# # We need to read the audio files as arrays
|
||||
# def speech_file_to_array_fn(batch):
|
||||
# speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
|
||||
# batch["speech"] = speech_array
|
||||
# batch["sentence"] = batch["sentence"].upper()
|
||||
|
||||
# return batch
|
||||
|
||||
# print("load dataest ...")
|
||||
# test_dataset = load_dataset("common_voice", LANG_ID, split=f"test[:{SAMPLES}]")
|
||||
# test_dataset = test_dataset.map(speech_file_to_array_fn)
|
||||
# inputs = processor(test_dataset["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
||||
|
||||
|
||||
librispeech_samples_ds = load_dataset("patrickvonplaten/librispeech_asr_dummy", "clean", split="validation")
|
||||
path_file = librispeech_samples_ds[0]["file"]
|
||||
# path_file = '/yrfs/lsen/DATA/LuoXData/processing2/2021-P01/audio/audio_trim.wav'
|
||||
# print("file = ", path_file)
|
||||
|
||||
|
||||
# load audio
|
||||
# audio_input, sample_rate = sf.read(path_file)
|
||||
audio_input, sample_rate = torchaudio.load(path_file)
|
||||
if len(audio_input.shape) == 2:
|
||||
audio_input = audio_input[0]
|
||||
|
||||
print(f"audio_input shape = {audio_input.shape}, sr = {sample_rate}, duration = {(len(audio_input)/sample_rate)}")
|
||||
|
||||
if sample_rate != 16000:
|
||||
resampler = torchaudio.transforms.Resample(sample_rate, 16_000)
|
||||
audio_input = resampler(audio_input).squeeze().numpy()
|
||||
print(f"audio_input shape = {audio_input.shape}, sr = {16000}, duration = {(len(audio_input)/16000)}")
|
||||
|
||||
|
||||
# Preprocessing the datasets.
|
||||
# We need to read the aduio files as arrays
|
||||
print(f"audio_input shap={audio_input.shape}, dtype={audio_input.dtype}")
|
||||
# pad input values and return pt tensor
|
||||
inputs = processor(audio_input, sampling_rate=16000, return_tensors="pt")
|
||||
|
||||
|
||||
print("predict ...")
|
||||
with torch.no_grad():
|
||||
output = model(**inputs)
|
||||
|
||||
|
||||
# print(f"hidden_states = {output.hidden_states}")
|
||||
print(f"extract_features = {output.extract_features.shape}")
|
||||
print(f"last_hidden_state = {output.last_hidden_state.shape}")
|
||||
# print(f"attentions = {output.attentions.shape}")
|
||||
|
||||
|
||||
# print(f"logits shape = {logits.shape}")
|
||||
|
||||
# logits = F.softmax(logits, dim=-1)
|
||||
# print(f"logits reshape = {logits.shape}")
|
||||
|
||||
# # padding last vector by last (0,0), model (0,1)
|
||||
# logits = F.pad(logits, (0,0,0,1), mode='replicate')
|
||||
# logits = torch.squeeze(logits)
|
||||
# print(f"pad reshape = {logits.shape}")
|
||||
|
||||
# # convert to numpy
|
||||
# logits = logits.cpu().numpy()
|
||||
# path_save = Path(path_file).parent.joinpath(f"{Path(path_file).stem}_wav2vec2.npy")
|
||||
# np.save(path_save, logits)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user