Search code examples
pythontensorflowoptimizationtensorflow.jsquantization

How can I quantize a keras model while converting it to a TensorflowJS Layers Model?


I created a tensorflow model and then converted it into tensorflow.js model using below code:

import tensorflowjs as tfjs
from tensorflow.keras.models import load_model

classifier = load_model("model")
tfjs.converters.save_keras_model(classifier, "js")

It works fine and now I would like to reduce the size of the model using quantization when converting it to a TensorflowJS Layers Model.


Solution

  • Yes it is possible you have four options for quantization:

    1. --quantization_bytes(depreceated)
    2. --quantize_float16
    3. --quantize_uint8
    4. --quantize_uint16

    Example conversion from keras format to tfjs_layers_model:

    tensorflowjs_converter \
       --input_format keras \
       --output_format tfjs_layers_model \
       --quantize_uint16 \
       original_model/model.json
       quantized_model/
    

    Or if you need more help with the entire process just type the following in your terminal

    tensorflowjs_wizard
    

    More info from Tensorflowjs Converter