Graph execution issue with converting NumPy function to TensorFlow ops

I’m trying to export my command recognition model for deployment on embedded devices, however, I’m facing trouble when trying to encapsulate the preprocessing function into my model, that way, when I export, inference becomes much easier.

Here’s my attempt at integrating the preprocessing within my model:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import tensorflow as tf
import numpy as np
import librosa
model = tf.saved_model.load("saved_model")
class_labels = ["background", "down", "go", "left", "no", "off", "on", "right", "stop", "up", "yes", "unknown"]
class ExportModel(tf.Module):
def __init__(self, model):
super().__init__()
self.model = model
self.__call__.get_concrete_function(
x=tf.TensorSpec(shape=(), dtype=tf.string))
self.__call__.get_concrete_function(
x=tf.TensorSpec(shape=[None, 16000], dtype=tf.float32))
@staticmethod
def preprocess_audio(audio_input, num_hops=98, sample_rate=16000, segment_duration=1,
hop_duration=0.010, num_bands=50):
if isinstance(audio_input, str):
audio_input = tf.io.read_file(audio_input)
audio_input, _ = tf.audio.decode_wav(audio_input, desired_channels=1, desired_samples=16000)
audio_input = tf.squeeze(audio_input, axis=-1)
segment_samples = int(segment_duration * sample_rate)
hop_samples = int(hop_duration * sample_rate)
audio_data = audio_input.numpy()
audio_data_padded = np.pad(audio_data, (0, max(0, segment_samples - len(audio_data))), mode='constant')
audio_data_normalized = librosa.util.normalize(audio_data_padded)
bark_spectrogram = librosa.feature.melspectrogram(y=audio_data_normalized, sr=sample_rate, n_fft=512,
hop_length=hop_samples, n_mels=num_bands)
log_bark_spectrogram = np.log10(bark_spectrogram + 1e-6)
if log_bark_spectrogram.shape[1] > num_hops:
log_bark_spectrogram = log_bark_spectrogram[:, :num_hops]
elif log_bark_spectrogram.shape[1] < num_hops:
pad_width = num_hops - log_bark_spectrogram.shape[1]
log_bark_spectrogram = np.pad(log_bark_spectrogram, ((0, 0), (0, pad_width)), mode='constant')
log_bark_spectrogram = np.transpose(log_bark_spectrogram)
log_bark_spectrogram = np.expand_dims(log_bark_spectrogram, axis=0)
log_bark_spectrogram = np.expand_dims(log_bark_spectrogram, axis=0)
log_bark_spectrogram = tf.convert_to_tensor(log_bark_spectrogram, dtype=tf.float32)
return log_bark_spectrogram
@tf.function
def __call__(self, x):
x = self.preprocess_audio(x)
result = self.model(x, training=False)
class_ids = tf.argmax(result, axis=-1)
class_names = tf.gather(class_labels, class_ids)
return {'predictions': result,
'class_ids': class_ids,
'class_names': class_names}
export = ExportModel(model)
tf.saved_model.save(export, "saved_model1")
</code>
<code>import tensorflow as tf import numpy as np import librosa model = tf.saved_model.load("saved_model") class_labels = ["background", "down", "go", "left", "no", "off", "on", "right", "stop", "up", "yes", "unknown"] class ExportModel(tf.Module): def __init__(self, model): super().__init__() self.model = model self.__call__.get_concrete_function( x=tf.TensorSpec(shape=(), dtype=tf.string)) self.__call__.get_concrete_function( x=tf.TensorSpec(shape=[None, 16000], dtype=tf.float32)) @staticmethod def preprocess_audio(audio_input, num_hops=98, sample_rate=16000, segment_duration=1, hop_duration=0.010, num_bands=50): if isinstance(audio_input, str): audio_input = tf.io.read_file(audio_input) audio_input, _ = tf.audio.decode_wav(audio_input, desired_channels=1, desired_samples=16000) audio_input = tf.squeeze(audio_input, axis=-1) segment_samples = int(segment_duration * sample_rate) hop_samples = int(hop_duration * sample_rate) audio_data = audio_input.numpy() audio_data_padded = np.pad(audio_data, (0, max(0, segment_samples - len(audio_data))), mode='constant') audio_data_normalized = librosa.util.normalize(audio_data_padded) bark_spectrogram = librosa.feature.melspectrogram(y=audio_data_normalized, sr=sample_rate, n_fft=512, hop_length=hop_samples, n_mels=num_bands) log_bark_spectrogram = np.log10(bark_spectrogram + 1e-6) if log_bark_spectrogram.shape[1] > num_hops: log_bark_spectrogram = log_bark_spectrogram[:, :num_hops] elif log_bark_spectrogram.shape[1] < num_hops: pad_width = num_hops - log_bark_spectrogram.shape[1] log_bark_spectrogram = np.pad(log_bark_spectrogram, ((0, 0), (0, pad_width)), mode='constant') log_bark_spectrogram = np.transpose(log_bark_spectrogram) log_bark_spectrogram = np.expand_dims(log_bark_spectrogram, axis=0) log_bark_spectrogram = np.expand_dims(log_bark_spectrogram, axis=0) log_bark_spectrogram = tf.convert_to_tensor(log_bark_spectrogram, dtype=tf.float32) return log_bark_spectrogram @tf.function def __call__(self, x): x = self.preprocess_audio(x) result = self.model(x, training=False) class_ids = tf.argmax(result, axis=-1) class_names = tf.gather(class_labels, class_ids) return {'predictions': result, 'class_ids': class_ids, 'class_names': class_names} export = ExportModel(model) tf.saved_model.save(export, "saved_model1") </code>
import tensorflow as tf
import numpy as np
import librosa

model = tf.saved_model.load("saved_model")
class_labels = ["background", "down", "go", "left", "no", "off", "on", "right", "stop", "up", "yes", "unknown"]


class ExportModel(tf.Module):
    def __init__(self, model):
        super().__init__()
        self.model = model
        self.__call__.get_concrete_function(
            x=tf.TensorSpec(shape=(), dtype=tf.string))
        self.__call__.get_concrete_function(
            x=tf.TensorSpec(shape=[None, 16000], dtype=tf.float32))

    @staticmethod
    def preprocess_audio(audio_input, num_hops=98, sample_rate=16000, segment_duration=1,
                         hop_duration=0.010, num_bands=50):
        if isinstance(audio_input, str):
            audio_input = tf.io.read_file(audio_input)
            audio_input, _ = tf.audio.decode_wav(audio_input, desired_channels=1, desired_samples=16000)
            audio_input = tf.squeeze(audio_input, axis=-1)

        segment_samples = int(segment_duration * sample_rate)
        hop_samples = int(hop_duration * sample_rate)

        audio_data = audio_input.numpy()

        audio_data_padded = np.pad(audio_data, (0, max(0, segment_samples - len(audio_data))), mode='constant')
        audio_data_normalized = librosa.util.normalize(audio_data_padded)

        bark_spectrogram = librosa.feature.melspectrogram(y=audio_data_normalized, sr=sample_rate, n_fft=512,
                                                          hop_length=hop_samples, n_mels=num_bands)

        log_bark_spectrogram = np.log10(bark_spectrogram + 1e-6)

        if log_bark_spectrogram.shape[1] > num_hops:
            log_bark_spectrogram = log_bark_spectrogram[:, :num_hops]
        elif log_bark_spectrogram.shape[1] < num_hops:
            pad_width = num_hops - log_bark_spectrogram.shape[1]
            log_bark_spectrogram = np.pad(log_bark_spectrogram, ((0, 0), (0, pad_width)), mode='constant')

        log_bark_spectrogram = np.transpose(log_bark_spectrogram)

        log_bark_spectrogram = np.expand_dims(log_bark_spectrogram, axis=0)
        log_bark_spectrogram = np.expand_dims(log_bark_spectrogram, axis=0)
        log_bark_spectrogram = tf.convert_to_tensor(log_bark_spectrogram, dtype=tf.float32)

        return log_bark_spectrogram

    @tf.function
    def __call__(self, x):
        x = self.preprocess_audio(x)
        result = self.model(x, training=False)

        class_ids = tf.argmax(result, axis=-1)
        class_names = tf.gather(class_labels, class_ids)
        return {'predictions': result,
                'class_ids': class_ids,
                'class_names': class_names}


export = ExportModel(model)
tf.saved_model.save(export, "saved_model1")

The code produces an AttributeError :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>File "C:UsersAamarPycharmProjectspythonProjectmain.py", line 55, in __call__ *
x = self.preprocess_audio(x)
File "C:UsersAamarPycharmProjectspythonProjectmain.py", line 29, in preprocess_audio *
audio_data = audio_input.numpy()
AttributeError: 'SymbolicTensor' object has no attribute 'numpy'`
</code>
<code>File "C:UsersAamarPycharmProjectspythonProjectmain.py", line 55, in __call__ * x = self.preprocess_audio(x) File "C:UsersAamarPycharmProjectspythonProjectmain.py", line 29, in preprocess_audio * audio_data = audio_input.numpy() AttributeError: 'SymbolicTensor' object has no attribute 'numpy'` </code>
File "C:UsersAamarPycharmProjectspythonProjectmain.py", line 55, in __call__  *
    x = self.preprocess_audio(x)
File "C:UsersAamarPycharmProjectspythonProjectmain.py", line 29, in preprocess_audio  *
    audio_data = audio_input.numpy()

AttributeError: 'SymbolicTensor' object has no attribute 'numpy'`

After some research and testing, I tried enabling run_functions_eagerly using tf.config.run_functions_eagerly(True) and I removed the two lines:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>self.__call__.get_concrete_function(
x=tf.TensorSpec(shape=(), dtype=tf.string))
self.__call__.get_concrete_function(
x=tf.TensorSpec(shape=[None, 16000], dtype=tf.float32))
</code>
<code>self.__call__.get_concrete_function( x=tf.TensorSpec(shape=(), dtype=tf.string)) self.__call__.get_concrete_function( x=tf.TensorSpec(shape=[None, 16000], dtype=tf.float32)) </code>
self.__call__.get_concrete_function(
            x=tf.TensorSpec(shape=(), dtype=tf.string))
self.__call__.get_concrete_function(
            x=tf.TensorSpec(shape=[None, 16000], dtype=tf.float32))

Which compiled the code successfully, however, when I tried inference on the exported model, it didn’t work as I expected since it threw a TypeError for string input, which means the preprocessing isn’t being integrated properly.

My suspesion is that tf.function executes its code as a graph execution, which behaves differently from eager execution, it’s more efficient and optimized.

In the previous three guides, you ran TensorFlow eagerly. This means TensorFlow operations are executed by Python, operation by operation, and return results back to Python.
While eager execution has several unique advantages, graph execution enables portability outside Python and tends to offer better performance. Graph execution means that tensor computations are executed as a TensorFlow graph, sometimes referred to as a tf.Graph or simply a “graph.”
Graphs are data structures that contain a set of tf.Operation objects, which represent units of computation; and tf.Tensor objects, which represent the units of data that flow between operations. They are defined in a tf.Graph context. Since these graphs are data structures, they can be saved, run, and restored all without the original Python code.

The solution I believe is to remove NumPy array operations and instead replace them with TensorFlow Operations to avoid any graph execution problems, I tried to change the preprocessing logic yet my attempts were futile.

P.S: Just let me know if you need the standalone model or any other necessary code to furthur understand the problem

New contributor

Aamar_Alberm3768 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật