tensorflow / tensorflow/model-optimization

Quantize naive !!!

Open
#956 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi all,
I am working on quantization. I have a .h5 model. I want to convert the weights from float 32 to int8 or float16. It seem to be " post training quantization". How can I do that without converting to tflite ( still save as .h5 model) ?

I tried:
import os, argparse, json, cv2

Import necessary items from Keras

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dropout, UpSampling2D
from tensorflow.keras.layers import Conv2D, Conv2DTranspose, MaxPooling2D
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint

Import local packages

import tensorflow_model_optimization as tfmot
import tensorflow as tf
from tensorflow.keras.models import model_from_json
DEBUG = False

LastValueQuantizer = tfmot.quantization.keras.quantizers.LastValueQuantizer
MovingAverageQuantizer = tfmot.quantization.keras.quantizers.MovingAverageQuantizer

def apply_quantization(skip_layers):
def wrapper(layer):
if type(layer) in skip_layers:
print(layer.name)
return layer
else:
return tfmot.quantization.keras.quantize_annotate_layer(layer)
return wrapper

# def wrapper(layer):
#     if type(layer) in skip_layers:
#         print(layer.name)
#         return tfmot.quantization.keras.quantize_annotate_layer(layer)
#     else:
#         return layer
# return wrapper

LastValueQuantizer = tfmot.quantization.keras.quantizers.LastValueQuantizer
MovingAverageQuantizer = tfmot.quantization.keras.quantizers.MovingAverageQuantizer

class DefaultQuantizeConfig(tfmot.quantization.keras.QuantizeConfig):
def get_weights_and_quantizers(self, layer):
return [(layer.kernel, LastValueQuantizer(num_bits=4, symmetric=True, narrow_range=False, per_axis=False))]

def get_activations_and_quantizers(self, layer):
    return [(layer.activation, MovingAverageQuantizer(num_bits=4, symmetric=False, narrow_range=False, per_axis=False))]

def set_quantize_weights(self, layer, quantize_weights):
    layer.kernel = quantize_weights[0]
def set_quantize_activations(self, layer, quantize_activations):
    layer.activation = quantize_activations[0]
def get_output_quantizers(self, layer):
    return [tfmot.quantization.keras.quantizers.MovingAverageQuantizer(
    num_bits=4, per_axis=False, symmetric=False, narrow_range=False)]
def get_config(self):
    return {}

"""
# Configure how to quantize weights.
def get_weights_and_quantizers(self, layer):
    return [(layer.kernel, LastValueQuantizer(num_bits=8, symmetric=True, narrow_range=False, per_axis=False))]

# Configure how to quantize activations.
def get_activations_and_quantizers(self, layer):
    return [
        (layer.activation, MovingAverageQuantizer(num_bits=8, symmetric=False, narrow_range=False, per_axis=False))]

def set_quantize_weights(self, layer, quantize_weights):
    # Add this line for each item returned in `get_weights_and_quantizers`
    # , in the same order
    layer.kernel = quantize_weights[0]

def set_quantize_activations(self, layer, quantize_activations):
    # Add this line for each item returned in `get_activations_and_quantizers`
    # , in the same order.
    layer.activation = quantize_activations[0]

# Configure how to quantize outputs (may be equivalent to activations).
def get_output_quantizers(self, layer):
    return []

def get_config(self):
    return {}
"""

if name == 'main':

input_shape = (320,320,3)

with open("path/to/1_model_quantize.json") as f:
    json_model = f.read()
model = model_from_json(json_model)
model.load_weights("path to h5")
model.summary()

# Quantize
quantize_model = tfmot.quantization.keras.quantize_model
# q_aware stands for for quantization aware.
# q_aware_model = quantize_model(model)
q_aware_model = tf.keras.models.clone_model(model, clone_function=apply_quantization(skip_layers=[BatchNormalization]))
with tfmot.quantization.keras.quantize_scope({'DefaultQuantizeConfig': DefaultQuantizeConfig,
                                              }):
    quant_aware_model = tfmot.quantization.keras.quantize_apply(q_aware_model)

# `quantize_model` requires a recompile.
quant_aware_model.compile(optimizer='Adam', loss='mean_squared_error', metrics=['mean_squared_error', 'accuracy'])
quantize_file = "save quantize .h5"
quant_aware_model.summary()
tf.keras.models.save_model(quant_aware_model, quantize_file, include_optimizer=False)

the result is all layer still in float32
Thank you so much.

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 with the main flow that builds the model through tfmot.quantization.keras.quantize_apply and then saves it with tf.keras.models.save_model. Review the reported .h5 output and the quantization configuration to determine whether the saved layers are expected to remain float32. Done means documenting or reproducing the supported result for the requested .h5 workflow.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.