dlxj commited on
Commit ·
6abeb51
1
Parent(s): d366ded
fix 数据加载问题
Browse files- examples/asr/asr_ctc/speech_to_text_ctc_bpe.py +1 -0
- patch_nemo.py +42 -23
- readme.txt +53 -0
examples/asr/asr_ctc/speech_to_text_ctc_bpe.py
CHANGED
|
@@ -107,6 +107,7 @@ if __name__ == '__main__':
|
|
| 107 |
'model.train_ds.tarred_audio_filepaths=data/common_voice_11_0/ja/train_tarred_1bk/audio__OP_0..1023_CL_.tar',
|
| 108 |
'model.train_ds.is_tarred=true',
|
| 109 |
'++model.train_ds.tarred_dataset_resolve_paths=false',
|
|
|
|
| 110 |
'model.train_ds.manifest_filepath=data/common_voice_11_0/ja/train_tarred_1bk/tarred_audio_manifest.json',
|
| 111 |
'model.validation_ds.manifest_filepath=data/common_voice_11_0/ja/validation/validation_common_voice_11_0_manifest.json',
|
| 112 |
'model.test_ds.manifest_filepath=data/common_voice_11_0/ja/test/test_common_voice_11_0_manifest.json',
|
|
|
|
| 107 |
'model.train_ds.tarred_audio_filepaths=data/common_voice_11_0/ja/train_tarred_1bk/audio__OP_0..1023_CL_.tar',
|
| 108 |
'model.train_ds.is_tarred=true',
|
| 109 |
'++model.train_ds.tarred_dataset_resolve_paths=false',
|
| 110 |
+
'++model.train_ds.is_tarred_audio=true',
|
| 111 |
'model.train_ds.manifest_filepath=data/common_voice_11_0/ja/train_tarred_1bk/tarred_audio_manifest.json',
|
| 112 |
'model.validation_ds.manifest_filepath=data/common_voice_11_0/ja/validation/validation_common_voice_11_0_manifest.json',
|
| 113 |
'model.test_ds.manifest_filepath=data/common_voice_11_0/ja/test/test_common_voice_11_0_manifest.json',
|
patch_nemo.py
CHANGED
|
@@ -1,28 +1,47 @@
|
|
| 1 |
-
|
| 2 |
-
修复 nemo-toolkit 中的 SIGKILL 问题
|
| 3 |
-
File "C:/Users/o/.conda/envs/NeMo/Lib/site-packages/nemo/utils/exp_manager.py", line 176, in FaultToleranceParams
|
| 4 |
-
rank_termination_signal: signal.Signals = signal.SIGKILL
|
| 5 |
-
^^^^^^^^^^^^^^
|
| 6 |
-
AttributeError: module 'signal' has no attribute 'SIGKILL'. Did you mean: 'SIGILL'? 是不是 signal 版本不对,conda activate NeMo 再安装正确版本?
|
| 7 |
-
"""
|
| 8 |
|
| 9 |
-
|
| 10 |
-
import re
|
| 11 |
-
|
| 12 |
-
file_path = r"C:\Users\o\.conda\envs\NeMo\Lib\site-packages\nemo\utils\exp_manager.py"
|
| 13 |
|
| 14 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 15 |
content = f.read()
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
file_path = r"C:\Users\o\.conda\envs\NeMo\Lib\site-packages\nemo\collections\asr\data\audio_to_text.py"
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 6 |
content = f.read()
|
| 7 |
|
| 8 |
+
old_str = """ def __next__(self):
|
| 9 |
+
if self.current_fn is None:
|
| 10 |
+
self.current_bytes, self.current_fn = next(self.iterator)
|
| 11 |
+
self.offset_id = 0
|
| 12 |
+
else:
|
| 13 |
+
offset_list = self.collection.mapping[self.current_fn]
|
| 14 |
+
if len(offset_list) == self.offset_id + 1:
|
| 15 |
+
self.current_bytes, self.current_fn = next(self.iterator)
|
| 16 |
+
self.offset_id = 0
|
| 17 |
+
else:
|
| 18 |
+
self.offset_id += 1
|
| 19 |
+
|
| 20 |
+
return self.current_bytes, self.current_fn, self.offset_id"""
|
| 21 |
+
|
| 22 |
+
new_str = """ def __next__(self):
|
| 23 |
+
if self.current_fn is None:
|
| 24 |
+
self.current_bytes, self.current_fn = next(self.iterator)
|
| 25 |
+
self.offset_id = 0
|
| 26 |
+
else:
|
| 27 |
+
import os
|
| 28 |
+
file_id, _ = os.path.splitext(os.path.basename(self.current_fn))
|
| 29 |
+
offset_list = self.collection.mapping[file_id]
|
| 30 |
+
if len(offset_list) == self.offset_id + 1:
|
| 31 |
+
self.current_bytes, self.current_fn = next(self.iterator)
|
| 32 |
+
self.offset_id = 0
|
| 33 |
+
else:
|
| 34 |
+
self.offset_id += 1
|
| 35 |
+
|
| 36 |
+
return self.current_bytes, self.current_fn, self.offset_id"""
|
| 37 |
+
|
| 38 |
+
if old_str in content:
|
| 39 |
+
content = content.replace(old_str, new_str)
|
| 40 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 41 |
+
f.write(content)
|
| 42 |
+
print("Successfully patched audio_to_text.py!")
|
| 43 |
+
else:
|
| 44 |
+
if new_str in content:
|
| 45 |
+
print("Already patched!")
|
| 46 |
+
else:
|
| 47 |
+
print("Could not find the target string to patch.")
|
readme.txt
CHANGED
|
@@ -6,6 +6,37 @@ see https://github.com/NVIDIA-NeMo/NeMo/blob/main/tutorials/asr/ASR_Example_Comm
|
|
| 6 |
从零开始训练或从已有模型微调
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
conda create -n NeMo python==3.12 pip
|
| 10 |
conda activate NeMo
|
| 11 |
pip install -r requirements.txt
|
|
@@ -82,3 +113,25 @@ python text_tokenizer.py
|
|
| 82 |
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
从零开始训练或从已有模型微调
|
| 7 |
|
| 8 |
|
| 9 |
+
|
| 10 |
+
这个 KeyError 报错是由于 WebDataset 解析 tar 包中的路径与 NeMo 的 Manifest 映射字典键值不匹配 导致的。
|
| 11 |
+
|
| 12 |
+
### 问题分析
|
| 13 |
+
在使用 Tarred Dataset 模式训练时:
|
| 14 |
+
|
| 15 |
+
1. tar 文件中音频文件的路径带了多级目录结构,例如 huggingface_echodict/NeMo/data/.../common_voice_ja_26015575.wav 。
|
| 16 |
+
2. 当 WebDataset 迭代解压时,它会将不带扩展名的完整路径(如 .../common_voice_ja_26015575 )作为字典的 __key__ 返回。
|
| 17 |
+
3. 然而,NeMo 的内部实现( ASRManifestProcessor )是通过获取 audio_filepath 的 os.path.basename (文件名本身) 作为映射的字典键。这导致字典里的键是 common_voice_ja_26015575 。
|
| 18 |
+
4. 随后,在 nemo.collections.asr.data.audio_to_text.py 的 _loop_offsets 方法中,代码直接使用了 WebDataset 返回的长路径作为键去 self.collection.mapping 里查找,因为没做 basename 处理,从而引发了 KeyError 报错。
|
| 19 |
+
### 修复过程
|
| 20 |
+
为了解决这个问题,我编写了一个脚本 patch_nemo.py 来直接修补你的 Conda 环境中 NeMo 安装包下的源码文件 nemo/collections/asr/data/audio_to_text.py 。
|
| 21 |
+
|
| 22 |
+
在代码中,将如下原本的错误逻辑:
|
| 23 |
+
|
| 24 |
+
```
|
| 25 |
+
offset_list = self.collection.mapping[self.current_fn]
|
| 26 |
+
```
|
| 27 |
+
替换为了带有 basename 提取的逻辑:
|
| 28 |
+
|
| 29 |
+
```
|
| 30 |
+
import os
|
| 31 |
+
file_id, _ = os.path.splitext(os.path.basename(self.current_fn))
|
| 32 |
+
offset_list = self.collection.mapping[file_id]
|
| 33 |
+
```
|
| 34 |
+
我已经自动执行了这个脚本,成功对你环境里的源码打上了补丁。这样 NeMo 就能正确地去掉冗长的目录路径,提取出对应的 file_id 去字典里查询索引。
|
| 35 |
+
|
| 36 |
+
你现在可以再次运行训练脚本, KeyError 应该已经完全解决了。
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
conda create -n NeMo python==3.12 pip
|
| 41 |
conda activate NeMo
|
| 42 |
pip install -r requirements.txt
|
|
|
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
+
TOKENIZER=data/common_voice_11_0/ja/tokenizers/tokenizer_spe_bpe_v2491
|
| 117 |
+
TRAIN_MANIFEST=data/common_voice_11_0/ja/train_tarred_1bk/tarred_audio_manifest.json
|
| 118 |
+
TARRED_AUDIO_FILEPATHS=data/common_voice_11_0/ja/train_tarred_1bk/audio_0..1023.tar # "_0..1023" is the range for the batch of files audio_0.tar, audio_1.tar, ..., audio_1023.tar
|
| 119 |
+
DEV_MANIFEST=data/common_voice_11_0/ja/validation/validation_common_voice_11_0_manifest.json
|
| 120 |
+
TEST_MANIFEST=data/common_voice_11_0/ja/test/test_common_voice_11_0_manifest.json
|
| 121 |
+
|
| 122 |
+
python examples/asr/asr_ctc/speech_to_text_ctc_bpe.py \
|
| 123 |
+
--config-path=../conf/conformer/ \
|
| 124 |
+
--config-name=conformer_ctc_bpe \
|
| 125 |
+
exp_manager.name="Name of our experiment" \
|
| 126 |
+
exp_manager.resume_if_exists=true \
|
| 127 |
+
exp_manager.resume_ignore_no_checkpoint=true \
|
| 128 |
+
exp_manager.exp_dir=results/ \
|
| 129 |
+
++model.encoder.conv_norm_type=layer_norm \
|
| 130 |
+
model.tokenizer.dir=$TOKENIZER \
|
| 131 |
+
model.train_ds.is_tarred=true \
|
| 132 |
+
model.train_ds.tarred_audio_filepaths=$TARRED_AUDIO_FILEPATHS \
|
| 133 |
+
model.train_ds.manifest_filepath=$TRAIN_MANIFEST \
|
| 134 |
+
model.validation_ds.manifest_filepath=$DEV_MANIFEST \
|
| 135 |
+
model.test_ds.manifest_filepath=$TEST_MANIFEST
|
| 136 |
+
|
| 137 |
+
|