dlxj commited on
Commit ·
0cd4b22
1
Parent(s): 8a3de9a
生成词表
Browse files- readme.txt +17 -2
- text_tokenizer.py +65 -0
readme.txt
CHANGED
|
@@ -8,7 +8,7 @@ see https://github.com/NVIDIA-NeMo/NeMo/blob/main/tutorials/asr/ASR_Example_Comm
|
|
| 8 |
|
| 9 |
conda create -n NeMo python==3.12 pip
|
| 10 |
conda activate NeMo
|
| 11 |
-
pip install
|
| 12 |
|
| 13 |
python scripts/speech_recognition/convert_hf_dataset_to_nemo.py \
|
| 14 |
output_dir=${OUTPUT_DIR} \
|
|
@@ -60,4 +60,19 @@ python scripts/speech_recognition/convert_to_tarred_audio_dataset.py \
|
|
| 60 |
python patch_nemo.py
|
| 61 |
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
conda create -n NeMo python==3.12 pip
|
| 10 |
conda activate NeMo
|
| 11 |
+
pip install -r requirements.txt
|
| 12 |
|
| 13 |
python scripts/speech_recognition/convert_hf_dataset_to_nemo.py \
|
| 14 |
output_dir=${OUTPUT_DIR} \
|
|
|
|
| 60 |
python patch_nemo.py
|
| 61 |
|
| 62 |
|
| 63 |
+
|
| 64 |
+
VOCAB_SIZE=128
|
| 65 |
+
python scripts/tokenizers/process_asr_text_tokenizer.py \
|
| 66 |
+
--manifest=data/common_voice_11_0/ja/train_tarred_1bk/tarred_audio_manifest.json \
|
| 67 |
+
--vocab_size=${VOCAB_SIZE} \
|
| 68 |
+
--data_root=data/common_voice_11_0/ja/tokenizers \
|
| 69 |
+
--tokenizer="spe" \
|
| 70 |
+
--spe_type=bpe
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
生成词表
|
| 75 |
+
python text_tokenizer.py
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
text_tokenizer.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# 将 scripts/tokenizers 添加到 sys.path,以便导入 process_asr_text_tokenizer
|
| 6 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "scripts", "tokenizers")))
|
| 7 |
+
|
| 8 |
+
# 由于 process_asr_text_tokenizer 内部使用了双下划线的函数,并且在 import 的时候定义了 parser,
|
| 9 |
+
# 为了避免冲突,我们直接调用其内部使用的函数,或者使用 os.system 来调用脚本
|
| 10 |
+
# 考虑到动态计算 VOCAB_SIZE,我们首先读取 manifest,收集所有的字符来计算 VOCAB_SIZE
|
| 11 |
+
|
| 12 |
+
def calculate_vocab_size(manifest_path):
|
| 13 |
+
unique_chars = set()
|
| 14 |
+
with open(manifest_path, 'r', encoding='utf-8') as f:
|
| 15 |
+
for line in f:
|
| 16 |
+
item = json.loads(line)
|
| 17 |
+
text = item.get('text', '')
|
| 18 |
+
for char in text:
|
| 19 |
+
unique_chars.add(char)
|
| 20 |
+
|
| 21 |
+
# 词汇表大小 = 独立字符数 + 预留特殊 token (如 <s>, </s>, <pad>, <unk> 等) 的数量
|
| 22 |
+
# 通常预留 10-20 个位置给特殊 token 和未见过的字符,这里我们加 20
|
| 23 |
+
vocab_size = len(unique_chars) + 20
|
| 24 |
+
# 为了后续的计算,通常建议 vocab_size 为 2 的幂或特定倍数,这里直接返回计算结果
|
| 25 |
+
return vocab_size
|
| 26 |
+
|
| 27 |
+
def main():
|
| 28 |
+
manifest_path = "data/common_voice_11_0/ja/train_tarred_1bk/tarred_audio_manifest.json"
|
| 29 |
+
data_root = "data/common_voice_11_0/ja/tokenizers"
|
| 30 |
+
|
| 31 |
+
print("Calculating dynamic VOCAB_SIZE...")
|
| 32 |
+
vocab_size = calculate_vocab_size(manifest_path)
|
| 33 |
+
print(f"Calculated VOCAB_SIZE: {vocab_size}")
|
| 34 |
+
|
| 35 |
+
# 调用 process_asr_text_tokenizer
|
| 36 |
+
# 由于 process_asr_text_tokenizer 在导入时会解析命令行参数,所以我们采用 subprocess 的方式调用
|
| 37 |
+
# 这样可以避免 argparse 报错,同时为了让脚本找到 nemo,我们将当前目录加入 PYTHONPATH
|
| 38 |
+
import subprocess
|
| 39 |
+
env = os.environ.copy()
|
| 40 |
+
env["PYTHONPATH"] = os.path.abspath(os.path.dirname(__file__)) + os.pathsep + env.get("PYTHONPATH", "")
|
| 41 |
+
|
| 42 |
+
command = [
|
| 43 |
+
"python",
|
| 44 |
+
"scripts/tokenizers/process_asr_text_tokenizer.py",
|
| 45 |
+
f"--manifest={manifest_path}",
|
| 46 |
+
f"--vocab_size={vocab_size}",
|
| 47 |
+
f"--data_root={data_root}",
|
| 48 |
+
"--tokenizer=spe",
|
| 49 |
+
"--spe_type=bpe"
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
# 尝试在 conda 的 NeMo 环境中执行
|
| 53 |
+
command = ["conda", "run", "-n", "NeMo"] + command
|
| 54 |
+
|
| 55 |
+
print(f"Running command: \n{' '.join(command)}")
|
| 56 |
+
result = subprocess.run(command, env=env)
|
| 57 |
+
exit_code = result.returncode
|
| 58 |
+
if exit_code != 0:
|
| 59 |
+
print(f"Error executing tokenizer script, exit code: {exit_code}")
|
| 60 |
+
sys.exit(1)
|
| 61 |
+
else:
|
| 62 |
+
print("Tokenizer processing completed successfully.")
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|