model-optimization icon indicating copy to clipboard operation
model-optimization copied to clipboard

can not quantize SeparableConv2D layer for QAT

Open PonyYeh opened this issue 5 years ago • 5 comments

Describe the bug I load Xception model using tf.keras and I want to perform quantization aware training for SeparableConv2D layer, and then I got this error : File "/opt/conda/envs/tensorflow2_0/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/default_8bit/default_8bit_transforms.py", line 485, in replacement conv_weights['bias:0'] = sepconv_weights[2] IndexError: list index out of range How can I fix this problem? Thank you very much!

System information

TensorFlow version (installed from source or binary): 2.3.0

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

Python version: 3.6

Code to reproduce the issue

from tensorflow.keras import applications
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Flatten
import tensorflow_model_optimization as tfmot

def load_model(mode_weight_path):
    base_model = applications.xception.Xception(include_top=False, weights=None, input_shape=(80,80,3), pooling="avg")
    features = base_model.output
    pred_gender = Dense(units=2, activation="softmax", name="pred_gender")(features)
    pred_age = Dense(units=101, activation="softmax", name="pred_age")(features)
    model = Model(inputs=base_model.input, outputs=[pred_gender, pred_age])
    model.load_weights(mode_weight_path)
    return model

model = load_model('../test/checkpoint_weights.hdf5')
model.summary()

def apply_quantization_to_dense(layer):
    if isinstance(layer, tf.keras.layers.SeparableConv2D):
        return tfmot.quantization.keras.quantize_annotate_layer(layer)
    return layer

model = tf.keras.models.clone_model(
    model,
    clone_function=apply_quantization_to_dense,
)
qut_model = tfmot.quantization.keras.quantize_apply(model)
qut_model.summary()

PonyYeh avatar Apr 01 '21 03:04 PonyYeh

@PonyYeh, can you provide the checkpoint file so I can actually use the code to reproduce? @daverim can you take a look on this? Guess some of the SeparableConv2D doesn't have weights[2]: https://github.com/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/python/core/quantization/keras/default_8bit/default_8bit_transforms.py#L485

xhae avatar Apr 01 '21 16:04 xhae

Guess landing https://github.com/tensorflow/model-optimization/pull/607 would fix this, working in progress.

xhae avatar Apr 02 '21 04:04 xhae

Hi @xhae , I appreciate that you try to figure out this problem. Yes, here is the checkpoint file to download. Thanks!

PonyYeh avatar Apr 06 '21 01:04 PonyYeh

@xhae similar problem. could you give me a answer?

qiyangzhang0329 avatar Apr 29 '21 14:04 qiyangzhang0329

#607 is now merged, you can now try using pip install git+https://github.com/tensorflow/model-optimization.git

daverim avatar May 17 '21 01:05 daverim