Tensorflow Graph已固化无法修改报错,手动重置无效如何解决?
问题描述
使用inaSpeechSegmenter进行音频分段时触发TensorFlow图已固化的错误,代码如下:
vocals_path = "output/song.wav" audio_path = vocals_path from inaSpeechSegmenter import Segmenter import tensorflow as tf # Initialize the segmenter seg = Segmenter() # Analyze the audio file segments = seg(vocals_path) # Print the segments for segment in segments: print(segment)
报错信息
File "/root/code/aimusic/essgender.py", line 138, in <module> seg = Segmenter() ^^^^^^^^^^^ File "/root/code/aimusic/venv/lib/python3.11/site-packages/inaSpeechSegmenter/segmenter.py", line 241, in __init__ self.vad = SpeechMusicNoise(batch_size) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/code/aimusic/venv/lib/python3.11/site-packages/inaSpeechSegmenter/segmenter.py", line 131, in __init__ self.nn = keras.models.load_model(model_path, compile=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/code/aimusic/venv/lib/python3.11/site-packages/keras/saving/saving_api.py", line 212, in load_model return legacy_sm_saving_lib.load_model( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/code/aimusic/venv/lib/python3.11/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "/root/code/aimusic/venv/lib/python3.11/site-packages/tensorflow/python/framework/ops.py", line 3375, in _check_not_finalized raise RuntimeError("Graph is finalized and cannot be modified.") RuntimeError: Graph is finalized and cannot be modified.
已尝试的无效方法
- 手动重置默认图:
tf.compat.v1.reset_default_graph()
- 重置图并创建交互式会话:
from tensorflow.python.framework import ops ops.reset_default_graph() sess = tf.InteractiveSession()
解决方案
- 调整导入顺序:先导入TensorFlow,再导入
inaSpeechSegmenter,避免图被提前初始化:
import tensorflow as tf from inaSpeechSegmenter import Segmenter vocals_path = "output/song.wav" audio_path = vocals_path seg = Segmenter() segments = seg(vocals_path) for segment in segments: print(segment)
- 确保版本兼容:
inaSpeechSegmenter与TensorFlow版本存在适配关系,若当前版本不兼容,降级到稳定组合:
pip uninstall tensorflow inaSpeechSegmenter pip install tensorflow==2.7 inaSpeechSegmenter==0.7.5
- 禁用TF1.x兼容模式:如果脚本中存在
tf.compat.v1相关调用,全部移除,使用TensorFlow 2.x原生的eager execution模式(默认开启),避免手动创建会话或操作图。 - 检查前置代码:确认脚本其他部分没有提前初始化TensorFlow会话、冻结图的操作,若有则将
Segmenter的初始化移至这些操作之前。
内容的提问来源于stack exchange,提问作者user2950593
相关产品推荐
相关产品推荐

