Failed to apply the QAT function 'quantize_model' to the sequential model that is defined using tensorflow.keras
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- machine-learning, python
- Domain
- machine-learning
Research direction
Start by reproducing the failure with the TensorFlow 2.17.0, TensorFlow Model Optimization 0.8.0, and Python 3.10 setup shown in the issue. Inspect tensorflow_model_optimization/python/core/quantization/keras/quantize.py around quantize_model and its Sequential check, then verify the supported tensorflow.keras model path with a focused test. Done means the supplied Sequential model is accepted by quantize_model without the reported ValueError.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Python
- Stars
- 1.6k
- Forks
- 349
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tensorflow/model-optimization
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
tensorflow/model-optimization#1301 · 1 comment ·
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 48/100
tensorflow/model-optimization#1272 ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 50/100
tensorflow/model-optimization#1270 · 2 comments ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 48/100
tensorflow/model-optimization#1241 ·
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 35/100
tensorflow/model-optimization#1182 · 1 comment ·
All issues in tensorflow/model-optimization
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
syfoud/Simulated_Scepter#172 ·
-
A cancelled tests run makes the coverage comment workflow fail and reports it as a red check on main Openarea: ci bug perceived difficulty: 3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Nitjsefnie-Harness-Commons/daedalus#921 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
EleutherAI/lm-evaluation-harness#4207 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/clickhouse-connect#1057 ·