Skip to content
New issue

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

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

Already on GitHub? # to your account

Failed to apply the QAT function 'quantize_model' to the sequential model that is defined using tensorflow.keras #1140

Open
wwwind opened this issue Jul 26, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@wwwind
Copy link
Contributor

wwwind commented Jul 26, 2024

Prior to filing: check that this should be a bug instead of a feature request. Everything supported, including the compatible versions of TensorFlow, is listed in the overview page of each technique. For example, the overview page of quantization-aware training is here. An issue for anything not supported should be a feature request.

Describe the bug
A clear and concise description of what the bug is.

System information

TensorFlow version (installed from source or binary):
2.17.0

TensorFlow Model Optimization version (installed from source or binary):
0.8.0

Python version:
3.10

Describe the expected behavior

Describe the current behavior
Failed with the error

  File "/home/jamesbond/work/venv/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 135, in quantize_model
    raise ValueError(
ValueError: `to_quantize` can only either be a keras Sequential or Functional model.

Code to reproduce the issue

import tensorflow as tf
print(tf.__version__)

import tensorflow_model_optimization as tfmot
print(tfmot.__version__)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

def create_model():

# Define a simple Sequential model
    model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    MaxPooling2D((2, 2)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
    ])

    return model

to_quantize_model = create_model()
print(f'Type of the model is {type(to_quantize_model)}')

# Check that the model is sequential
if not isinstance(to_quantize_model, Sequential):
    raise ValueError('not sequantial')

# Check the whole condition from https://github.com/tensorflow/model-optimization/blob/ed3f0176b561fe693a3cc55b53a3605b943b6bbf/tensorflow_model_optimization/python/core/quantization/keras/quantize.py#L135
if not isinstance(to_quantize_model, Sequential) and not (
      hasattr(to_quantize_model, '_is_graph_network')
      and to_quantize_model._is_graph_network
  ):  # pylint: disable=protected-access
    raise ValueError(
        'Condition FAILED: `to_quantize` can only either be a keras Sequential or '
        'Functional model.'
    )

# But now this API fails with the condition above
qat_model = tfmot.quantization.keras.quantize_model(to_quantize_model)

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
My model is defined using tensorflow.keras instead of tensorflow_model_optimization.python.core.keras.compat like in tutorials and this leads to this error as model is not recognized as Sequential, although it is Sequential.

@wwwind wwwind added the bug Something isn't working label Jul 26, 2024
@wwwind wwwind changed the title Failed to apply QAT function: quantize_model to the sequantial model that is defined using tensorflow.keras Failed to apply the QAT function 'quantize_model' to the sequential model that is defined using tensorflow.keras Jul 26, 2024
@hbellafkir
Copy link

I had the same issue. I just switched to tensorflow_model_optimization.python.core.keras.compat. The code didn’t require much modification.

@Litschi123
Copy link

TFMOT is using Keras version 2 which is only used by default until TensorFlow version 2.15.

  1. Install tf_keras pip install tf_keras
  2. Add os.environ['TF_USE_LEGACY_KERAS'] = "1" before importing TensorFlow in your script to make it use Keras v2.

For more details check: https://keras.io/getting_started/#tensorflow--keras-2-backwards-compatibility

@husainchhil
Copy link

tensorflow_model_optimization.python.core.keras.compat.

Can you please share your code? I am learning TinyML and am facing the same issue.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants