tensorflow / tensorflow/model-optimization

ValueError: `to_annotate` can only be a `keras.layers.Layer` instance. You passed an instance of type: Dense.

Open
#1,180 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.6k
Forks
349
Avg merge
3d 2h
Merged PRs (30d)
1

Description

import tensorflow as tf
from tensorflow.keras import layers, models
import tensorflow_model_optimization as tfmot

Use quantize_annotate_layer to annotate that the Dense layer

should be quantized.

input_shape = (20,)
annotated_model = tf.keras.Sequential([
tfmot.quantization.keras.quantize_annotate_layer(tf.keras.layers.Dense(20, input_shape=input_shape)),
tf.keras.layers.Flatten()
])

Use quantize_apply to actually make the model quantization aware.

quant_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model)
quant_aware_model.summary()

when run the above code, error appears:"ValueError: to_annotate can only be a keras.layers.Layer instance. You passed an instance of type: Dense." Can anyone find the way to fix it? Thanks a lot

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided TensorFlow and Keras example with tensorflow_model_optimization and confirm the quantize_annotate_layer error. Trace the type check at quantize_annotate_layer and quantize_apply, then verify that the example accepts the Dense layer and produces the quantization-aware model summary without the ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
keras, python, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.