Skip to content

Why are LceQuantize and LceDequantize operators appearing consecutively? #906

@Oslomayor

Description

@Oslomayor

Hi everyone. I've built a classification model using the Larq framework. After training, I obtained an h5 format model file and converted it to tflite format using the Larq Compute Engine. I observed the model using Netron, with the h5 model on the left and the tflite model on the right.
larq_vww1_lcequant

I noticed that after the reshape operation in the tflite model, there are consecutive LceQuantize and LceDequantize operators. As marked by the red box, I think the consecutive use of these two operators seems meaningless. Why does this happen during the conversion process?
I am using Larq version 0.13.1, Larq Compute Engine version 0.11.1, TensorFlow version 2.3.0, and Python 3.8.

My model code is as follows:

kwargs = dict(input_quantizer="ste_sign",
              kernel_quantizer="ste_sign",
              kernel_constraint="weight_clip",
              use_bias=False)

vww_model = tf.keras.models.Sequential([
    # In the first layer we only quantize the weights and not the input
    lq.layers.QuantConv2D(32, 3,
                          kernel_quantizer="ste_sign",
                          kernel_constraint="weight_clip",
                          use_bias=False,
                          input_shape=(96, 96, 3),
                          strides=(2, 2),
                          padding="same",
                          pad_values=1),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantConv2D(32, 3, padding="same", pad_values=1, strides=(2, 2), **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantConv2D(32, 3, padding="same", pad_values=1, **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantConv2D(64, 3, padding="same", pad_values=1, strides=(2, 2), **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantConv2D(64, 3, padding="same", pad_values=1, **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantConv2D(128, 3, padding="same", pad_values=1, strides=(2, 2), **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantConv2D(128, 3, padding="same", pad_values=1, **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantConv2D(256, 3, padding="same", pad_values=1, strides=(2, 2), **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantConv2D(256, 3, padding="same", pad_values=1, **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),
    tf.keras.layers.MaxPool2D(pool_size=(3, 3), strides=(1, 1)),

    tf.keras.layers.Flatten(),

    lq.layers.QuantDense(128, **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantDense(64, **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),

    lq.layers.QuantDense(2, **kwargs),
    tf.keras.layers.BatchNormalization(momentum=0.999, scale=False),
    tf.keras.layers.Activation("softmax"),
])

Conversion code is as follows:

model_name_in = "vww1_full.h5"
model_name_out = "vww1_binary.tflite"

custom_objects = {
    "QuantConv2D": layers.QuantConv2D,
}

model = load_model(model_name_in, custom_objects=custom_objects)
lq.models.summary(model)

# Convert our Keras model to a TFLite flatbuffer file
with open(model_name_out, "wb") as flatbuffer_file:
    flatbuffer_bytes = lce.convert_keras_model(model)
    flatbuffer_file.write(flatbuffer_bytes)

I would greatly appreciate it if anyone could offer a solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions