init
Browse files- encodec_audio_tokenizer.py +3 -8
- tokenize_dataset_s2s.py +6 -9
encodec_audio_tokenizer.py
CHANGED
@@ -34,7 +34,7 @@ class BaseEncodecTokenizer:
|
|
34 |
@torch.no_grad()
|
35 |
def wav_to_tokens(self,
|
36 |
wav: torch.Tensor,
|
37 |
-
sample_rate:
|
38 |
cpu_offload: bool = True,
|
39 |
chunk_length: Optional[int] = None,
|
40 |
stride: Optional[int] = None,
|
@@ -54,13 +54,8 @@ class BaseEncodecTokenizer:
|
|
54 |
raise ValueError(f"wav should be (batch, channel, time): {wav.ndim} dims")
|
55 |
original_device = wav.device
|
56 |
# sampling audio
|
57 |
-
|
58 |
-
|
59 |
-
for sr, single_wav in zip(sample_rate, wav):
|
60 |
-
if sr != self.sample_rate:
|
61 |
-
single_wav = julius.resample_frac(single_wav, sr, self.sample_rate)
|
62 |
-
new_wav.append(single_wav)
|
63 |
-
wav = torch.concat(new_wav)
|
64 |
batch_size, channels, input_length = wav.shape
|
65 |
if channels > 1:
|
66 |
logging.warning("Audio has more than one channel but encoder takes the first channel only.")
|
|
|
34 |
@torch.no_grad()
|
35 |
def wav_to_tokens(self,
|
36 |
wav: torch.Tensor,
|
37 |
+
sample_rate: int,
|
38 |
cpu_offload: bool = True,
|
39 |
chunk_length: Optional[int] = None,
|
40 |
stride: Optional[int] = None,
|
|
|
54 |
raise ValueError(f"wav should be (batch, channel, time): {wav.ndim} dims")
|
55 |
original_device = wav.device
|
56 |
# sampling audio
|
57 |
+
if sample_rate != self.sample_rate:
|
58 |
+
wav = julius.resample_frac(wav, sample_rate, self.sample_rate)
|
|
|
|
|
|
|
|
|
|
|
59 |
batch_size, channels, input_length = wav.shape
|
60 |
if channels > 1:
|
61 |
logging.warning("Audio has more than one channel but encoder takes the first channel only.")
|
tokenize_dataset_s2s.py
CHANGED
@@ -17,21 +17,18 @@ dataset = load_dataset(f"{hf_org}/{hf_dataset}", f"subset_{dataset_id}", split="
|
|
17 |
tokenizer = EncodecTokenizer.from_pretrained()
|
18 |
|
19 |
|
20 |
-
def tokenize(
|
21 |
for side in sides:
|
22 |
-
wav = torch.as_tensor(
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
return batch
|
28 |
|
29 |
|
30 |
dataset = dataset.map(
|
31 |
function=tokenize,
|
32 |
remove_columns=[f"{s}.audio" for s in sides] + [f"{s}.url" for s in sides] + [f"{s}.duration_start" for s in sides] + [f"{s}.duration_end" for s in sides],
|
33 |
-
batched=True,
|
34 |
-
batch_size=batch_size,
|
35 |
num_proc=num_proc,
|
36 |
desc="tokenize dataset"
|
37 |
)
|
|
|
17 |
tokenizer = EncodecTokenizer.from_pretrained()
|
18 |
|
19 |
|
20 |
+
def tokenize(example):
|
21 |
for side in sides:
|
22 |
+
wav = torch.as_tensor(example[f"{side}.audio"]["array"].reshape(1, 1, -1))
|
23 |
+
example[f"{side}.audio.tokens"] = tokenizer.wav_to_tokens(
|
24 |
+
wav=wav, sample_rate=[example[f"{side}.audio"]["sampling_rate"]]
|
25 |
+
).numpy().tolist()
|
26 |
+
return example
|
|
|
27 |
|
28 |
|
29 |
dataset = dataset.map(
|
30 |
function=tokenize,
|
31 |
remove_columns=[f"{s}.audio" for s in sides] + [f"{s}.url" for s in sides] + [f"{s}.duration_start" for s in sides] + [f"{s}.duration_end" for s in sides],
|
|
|
|
|
32 |
num_proc=num_proc,
|
33 |
desc="tokenize dataset"
|
34 |
)
|