ValueError:调用层“transformer”时遇到异常(变压器类型)

发布于 2025-01-13 16:47:15 字数 4065 浏览 1 评论 0原文

因此,我编写了一个作为 ASR 工作的 Transformers 神经网络,它工作正常,训练良好并将模型保存为...

model.save("savedmodel.model")

问题是,当我想要预测时,我这样做...

speech_model = load_model('D:\DOT\Speechrecognition\speechrecognitionE.model')

path = "D:\DOT\Speechrecognition\Data\LJSpeech-1.1\wavs\LJ001-0001.wav"

def path_to_audio(path):
    # spectrogram using stft
    audio = tf.io.read_file(path)
    audio, _ = tf.audio.decode_wav(audio, 1)
    audio = tf.squeeze(audio, axis=-1)
    stfts = tf.signal.stft(audio, frame_length=200, frame_step=80, fft_length=256)
    x = tf.math.pow(tf.abs(stfts), 0.5)
    # normalisation
    means = tf.math.reduce_mean(x, 1, keepdims=True)
    stddevs = tf.math.reduce_std(x, 1, keepdims=True)
    x = (x - means) / stddevs
    audio_len = tf.shape(x)[0]
    # padding to 10 seconds
    pad_len = 2754
    paddings = tf.constant([[0, pad_len], [0, 0]])
    x = tf.pad(x, paddings, "CONSTANT")[:pad_len, :]
    return x

x = path_to_audio(path)
#print(x)
speech_model.predict(x)

音频函数的路径,将频谱图的音频路径,在训练模型中它接收音频频谱图作为输入,但它显示此错误。

    Traceback (most recent call last):
File "C:\Users\berna\Desktop\Programming\AI_ML_DL\Projects\DOT\DOT-alpha.py", line 72, in <module>
    speech_model.predict(x)
File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1621, in predict_function  *
        return step_function(self, iterator)
    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1611, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1604, in run_step  **
        outputs = model.predict_step(data)
    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1572, in predict_step
        return self(x, training=False)
    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None

    ValueError: Exception encountered when calling layer "transformer" (type Transformer).

    Could not find matching concrete function to call loaded from the SavedModel. Got:
    Positional arguments (2 total):
        * Tensor("inputs:0", shape=(None, 129), dtype=float32)
        * False
    Keyword arguments: {}

    Expected these arguments to match one of the following 4 option(s):

    Option 1:
    Positional arguments (2 total):
        * [TensorSpec(shape=(None, None, 129), dtype=tf.float32, name='inputs/0'), TensorSpec(shape=(None, 199), dtype=tf.int32, name='inputs/1')]
        * False
    Keyword arguments: {}

    Option 2:
    Positional arguments (2 total):
        * [TensorSpec(shape=(None, None, 129), dtype=tf.float32, name='inputs/0'), TensorSpec(shape=(None, 199), dtype=tf.int32, name='inputs/1')]
        * True
    Keyword arguments: {}

    Option 3:
    Positional arguments (2 total):
        * [TensorSpec(shape=(None, None, 129), dtype=tf.float32, name='input_1'), TensorSpec(shape=(None, 199), dtype=tf.int32, name='input_2')]
        * False
    Keyword arguments: {}

    Option 4:
    Positional arguments (2 total):
        * [TensorSpec(shape=(None, None, 129), dtype=tf.float32, name='input_1'), TensorSpec(shape=(None, 199), dtype=tf.int32, name='input_2')]
        * True
    Keyword arguments: {}

    Call arguments received:
    • args=('tf.Tensor(shape=(None, 129), dtype=float32)',)
    • kwargs={'training': 'False'}

这意味着什么?预测有什么问题?

So I code a Transformers neural network that works as an ASR, it works, it trains good and saved the model as...

model.save("savedmodel.model")

The problem is that when I want to predict, I do this..

speech_model = load_model('D:\DOT\Speechrecognition\speechrecognitionE.model')

path = "D:\DOT\Speechrecognition\Data\LJSpeech-1.1\wavs\LJ001-0001.wav"

def path_to_audio(path):
    # spectrogram using stft
    audio = tf.io.read_file(path)
    audio, _ = tf.audio.decode_wav(audio, 1)
    audio = tf.squeeze(audio, axis=-1)
    stfts = tf.signal.stft(audio, frame_length=200, frame_step=80, fft_length=256)
    x = tf.math.pow(tf.abs(stfts), 0.5)
    # normalisation
    means = tf.math.reduce_mean(x, 1, keepdims=True)
    stddevs = tf.math.reduce_std(x, 1, keepdims=True)
    x = (x - means) / stddevs
    audio_len = tf.shape(x)[0]
    # padding to 10 seconds
    pad_len = 2754
    paddings = tf.constant([[0, pad_len], [0, 0]])
    x = tf.pad(x, paddings, "CONSTANT")[:pad_len, :]
    return x

x = path_to_audio(path)
#print(x)
speech_model.predict(x)

The path to audio function, converts the audio path to an spectrogram, in the training model it receive audio spectrograms as inputs, but it show this error..

    Traceback (most recent call last):
File "C:\Users\berna\Desktop\Programming\AI_ML_DL\Projects\DOT\DOT-alpha.py", line 72, in <module>
    speech_model.predict(x)
File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1621, in predict_function  *
        return step_function(self, iterator)
    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1611, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1604, in run_step  **
        outputs = model.predict_step(data)
    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1572, in predict_step
        return self(x, training=False)
    File "C:\Users\berna\AppData\Roaming\Python\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None

    ValueError: Exception encountered when calling layer "transformer" (type Transformer).

    Could not find matching concrete function to call loaded from the SavedModel. Got:
    Positional arguments (2 total):
        * Tensor("inputs:0", shape=(None, 129), dtype=float32)
        * False
    Keyword arguments: {}

    Expected these arguments to match one of the following 4 option(s):

    Option 1:
    Positional arguments (2 total):
        * [TensorSpec(shape=(None, None, 129), dtype=tf.float32, name='inputs/0'), TensorSpec(shape=(None, 199), dtype=tf.int32, name='inputs/1')]
        * False
    Keyword arguments: {}

    Option 2:
    Positional arguments (2 total):
        * [TensorSpec(shape=(None, None, 129), dtype=tf.float32, name='inputs/0'), TensorSpec(shape=(None, 199), dtype=tf.int32, name='inputs/1')]
        * True
    Keyword arguments: {}

    Option 3:
    Positional arguments (2 total):
        * [TensorSpec(shape=(None, None, 129), dtype=tf.float32, name='input_1'), TensorSpec(shape=(None, 199), dtype=tf.int32, name='input_2')]
        * False
    Keyword arguments: {}

    Option 4:
    Positional arguments (2 total):
        * [TensorSpec(shape=(None, None, 129), dtype=tf.float32, name='input_1'), TensorSpec(shape=(None, 199), dtype=tf.int32, name='input_2')]
        * True
    Keyword arguments: {}

    Call arguments received:
    • args=('tf.Tensor(shape=(None, 129), dtype=float32)',)
    • kwargs={'training': 'False'}

What does that means? what is wrong with the prediction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文