Skip to content

Convert TF and Numpy ops in whisper_audio_convert.py to Keras Ops #2225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

LakshmiKalaKadali
Copy link

This PR is in continuation to the draft PR .

Converted Numpy and TF ops into Keras ops in whisper_audio_converter.py and in whisper_audio_converter_test.py

@LakshmiKalaKadali LakshmiKalaKadali marked this pull request as ready for review April 24, 2025 06:41
outputs = WhisperAudioConverter(**self.init_kwargs)(audio_tensor)

# Verify shape.
self.assertEqual(outputs.shape, (5, 80))
# Verify output.
expected = [1.1656, 1.0151, -0.8343, -0.8343, -0.8343]
self.assertAllClose(outputs[:, 0], expected, atol=0.01, rtol=0.01)
self.assertAllClose(outputs[:, 0], expected, atol=0.01, rtol=0.01)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep newlines at end of files.


# Convert the tensor to a Ragged Tensor.
if isinstance(audio, tf.Tensor):
audio = tf.RaggedTensor.from_tensor(audio)

# Pad audio.
audio_shape = audio.shape.as_list()
audio_shape = list(audio.shape)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe tf.shape cannot always be listified like this. Maybe call ops.shape?

@mattdangerw
Copy link
Member

@abheesht17 if you have time can you review this? You probably have the most insight here.

@mattdangerw mattdangerw requested a review from abheesht17 April 25, 2025 01:38
@@ -1,14 +1,10 @@
import numpy as np
import keras.ops as ops
import tensorflow as tf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can never do a bare import of tf like this. Check other files in the library.

@@ -214,21 +219,21 @@ def call(self, audio):

rank_1_input = audio.shape.rank == 1
if rank_1_input:
audio = tf.expand_dims(audio, 0)
audio = ops.expand_dims(audio, 0)

# Convert the tensor to a Ragged Tensor.
if isinstance(audio, tf.Tensor):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably switch to a @preprocessing_function annotation and remove this? Something to try at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants