tensorflow / models

Models and examples built with TensorFlow
Other
76.99k stars 45.79k forks source link

Cannot initialise YAMNet using TF 2.16.1 #11212

Closed hzhongresearch closed 5 days ago

hzhongresearch commented 4 months ago

Prerequisites

Please answer the following questions for yourself before submitting an issue.

1. The entire URL of the file you are using

https://github.com/tensorflow/models/blob/8da4857396fcedb1abd19a08cd4de40d16c7bc50/research/audioset/yamnet/yamnet.py https://github.com/tensorflow/models/blob/8da4857396fcedb1abd19a08cd4de40d16c7bc50/research/audioset/yamnet/params.py

2. Describe the bug

@plakal @dpwe Cannot initialise YAMNet using TF 2.16.1.

3. Steps to reproduce

Steps to reproduce the behavior.

import yamnet
import params
mymodel = yamnet.yamnet_frames_model(params=params.Params)

4. Expected behavior

A clear and concise description of what you expected to happen.

The code should return a YAMNet model.

5. Additional context

Include any logs that would be helpful to diagnose the problem.

The following is the error message.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/henry/models/research/audioset/yamnet/yamnet.py", line 121, in yamnet_frames_model
    waveform_padded = features_lib.pad_waveform(waveform, params)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/henry/models/research/audioset/yamnet/features.py", line 91, in pad_waveform
    num_samples = tf.shape(waveform)[0]
                  ^^^^^^^^^^^^^^^^^^
  File "/home/henry/env_yamnet/lib/python3.11/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/henry/env_yamnet/lib/python3.11/site-packages/keras/src/backend/common/keras_tensor.py", line 91, in __tf_tensor__
    raise ValueError(
ValueError: A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.operations`). You are likely doing something like:

x = Input(...)
...
tf_fn(x)  # Invalid.

What you should do instead is wrap `tf_fn` in a layer:

class MyLayer(Layer):
    def call(self, x):
        return tf_fn(x)

x = MyLayer()(x)

6. System information

russnes commented 6 days ago

Same issue here using tensorflow-cpu 2.17.0. Works using tensorflow 2.11, but had to downgrade to python 3.7

plakal commented 6 days ago

Sorry for the late response. Neither me nor @dpwe were assigned this issue so we didn't notice until now. Looking into this.

plakal commented 6 days ago

TF 2.16.0 included an upgrade to Keras 3 by default, which includes many breaking changes. I've confirmed that our code still works if we stick with Keras 2 (which requires also installing tf_keras). I'll send a PR to switch us to Keras 2.

google-ml-butler[bot] commented 5 days ago

Are you satisfied with the resolution of your issue? Yes No