Skip to content
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

Keras featurespace error in environment without TensoFlow #21009

Open
sibyjackgrove opened this issue Mar 10, 2025 · 10 comments
Open

Keras featurespace error in environment without TensoFlow #21009

sibyjackgrove opened this issue Mar 10, 2025 · 10 comments
Assignees
Labels

Comments

@sibyjackgrove
Copy link

I am trying to use a Keras feature space during inference to create a data window.

input_window= input_featurespace({'temp': [0, 0, 0, 0, 0, 0, 0, 0], 'proc': [0, 0, 0, 0, 0, 0, 0, 0], 'dsp_temp': [0, 0, 0, 0, 0, 0, 0, 0]}).

However, I am getting the following error:

File "/usr/local/lib/python3.12/site-packages/keras/src/layers/preprocessing/feature_space.py", line 709, in __call__
  data = {key: self._convert_input(value) for key, value in data.items()}
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/keras/src/layers/preprocessing/feature_space.py", line 693, in _convert_input
  if not isinstance(x, (tf.Tensor, tf.SparseTensor, tf.RaggedTensor)):
                        ^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/keras/src/utils/module_utils.py", line 35, in __getattr__
  self.initialize()
File "/usr/local/lib/python3.12/site-packages/keras/src/utils/module_utils.py", line 29, in initialize
  raise ImportError(self.import_error_msg)
ImportError: This requires the tensorflow module. You can install it via `pip install tensorflow`

I understand that this is because Tensorflow has not been installed. However, since the inference device has storage constraints, I don't want to use Tensorflow in my inference environment. Is there any way to get feature space to work with TensorFlow?

@mehtamansi29
Copy link
Collaborator

Hi @sibyjackgrove -

Thanks for reporting the issue. As you are trying to use a Keras feature space during inference as feature space, feature space define all the preprocessing once and re-use it at different stages of our system.

Here you can find more details about FeatureSpace using a Keras preprocessing layer

Using keras can structure the input like this:

import keras
import tensorflow as tf

input_featurespace = keras.Input(shape=(8,), dtype=tf.int32, name='temp')
input_featurespace_proc = keras.Input(shape=(8,), dtype=tf.int32, name='proc')
input_featurespace_dsp_temp = keras.Input(shape=(8,), dtype=tf.int32, name='dsp_temp')

input_dict = {
    'temp': input_featurespace,
    'proc': input_featurespace_proc,
    'dsp_temp': input_featurespace_dsp_temp
}

inputs = input_dict
inputs

For more help can you share your reproducible code here?

@sibyjackgrove
Copy link
Author

I am trying to reload a Keras preprocessor layer. The Preprocessor layer was created with TensorFLow backend. But I am trying to reload it for inference with Numpy backend in an environment without TensorFlow.

Please find the code below. I am unable to attack the model here.

import os
os.environ['KERAS_BACKEND'] = 'numpy'
import keras

input_featurespace= keras.models.load_model(filepath="saved_models/input_featurespace_w-8_f-4_o-2_v0.keras")
input_window= input_featurespace({'temp1': [0, 0, 0, 0, 0, 0, 0, 0],'temp2': [0, 0, 0, 0, 0, 0, 0, 0], 'proc_temp': [0, 0, 0, 0, 0, 0, 0, 0], 'dsp_temp': [0, 0, 0, 0, 0, 0, 0, 0]})

@mehtamansi29
Copy link
Collaborator

Hi @sibyjackgrove -

Thanks for reproducible code. But can you share sample model file or reproducible code generate the model ?

@sibyjackgrove
Copy link
Author

@mehtamansi29 please use the .keras file in the zipped folder below. Note that the feature space was created with Tensorflow backed.

input_featurespace_w-8_f-4_o-2_v0.zip

@mehtamansi29
Copy link
Collaborator

Hi @sibyjackgrove -

Thanks for the code and model.I tried with the same and input_window is giving me result for dsp_temp,proc_temp,temp1, temp2.
Attached gist here for reference.
If you face any other issue then let me know?

@sibyjackgrove
Copy link
Author

sibyjackgrove commented Mar 13, 2025

Thank you, @mehtamansi29 Yes, it works if TensorFlow is installed. However, it seems TensorFlow is still a dependency even if backend is numpy.
Please see the error in the attached Jupyter notebook after I uninstalled TensorFlow from the Jupyter notebook environment.
https://colab.research.google.com/gist/sibyjackgrove/21a2cac0149719f87f7ca755d989f022/21009-keras-featurespace-error-in-environment-without-tensoflow.ipynb

Is there any workaround for TensorFlow dependency?

@mehtamansi29
Copy link
Collaborator

Hi @sibyjackgrove -

When you install TensorFlow 2.16 or above then it will by default. And if you can also use numpy ops API without explicitly calling numpy backend. More details you can find here.

@sibyjackgrove
Copy link
Author

I'm sorry. Could you clarify again? I just want to know if I can call a Keras preprocessor without TensorFlow as a dependency. Could you let me know the section in the Keras 3 documentation that clarifies this?

@sibyjackgrove
Copy link
Author

@mehtamansi29 could you please provide any update on this issue?

@mehtamansi29
Copy link
Collaborator

Hi @sibyjackgrove -

Here you can find the Keras preprocessing layer documentation.And here you can find preprocessor class documentation.

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

No branches or pull requests

3 participants