<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Input Scale of Quantized TFLite Model in i.MX Processors</title>
    <link>https://community.nxp.com/t5/i-MX-Processors/Input-Scale-of-Quantized-TFLite-Model/m-p/2028304#M233020</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I am trying to quantize VGG19 to run it on i.MX 8M Plus NPU and I need multiple convolutional outputs. When I try to quantize it with the following code, input scale is like 3.9e-9 and the responses for different inputs are the same.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def create_vgg19_c(batch_normalization=False, required_layers=None):
    base_model = VGG19(include_top=False, weights='imagenet', input_shape=(256, 256, 3))
    base_model.trainable = True

    if required_layers is None:
        required_layers = [2, 5, 8, 13, 18, 19]
    
    outputs = [base_model.layers[i].output for i in required_layers]
    model = Model(inputs=base_model.input, outputs=outputs)
    return model


model = create_vgg19_c()

quant_aware_model = tfmot.quantization.keras.quantize_model(model)

def representative_data_gen():
    _, h, w, _ = quant_aware_model.input_shape
    samples = 100
    for i in range(samples):

        img_in = np.random.uniform(0, 255, (1, h, w, 3)).astype('float32')

        img_in = tf.keras.applications.vgg19.preprocess_input(img_in)

        yield [img_in]

converter = tf.lite.TFLiteConverter.from_keras_model(quant_aware_model)

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.experimental_new_converter = True

converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8

quantized_tflite_model = converter.convert()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I tried to normalize the values between [-128, 127] for int8 and between [-1,1] for float32. I changed the parameters of the converter, I used just optimizer but scale and same results didn't changed. Also I changed the preprocess function but nothing changed. I want to quantize pretrained VGG19 backbone with specified outputs.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Jan 2025 13:11:33 GMT</pubDate>
    <dc:creator>merkan</dc:creator>
    <dc:date>2025-01-15T13:11:33Z</dc:date>
    <item>
      <title>Input Scale of Quantized TFLite Model</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/Input-Scale-of-Quantized-TFLite-Model/m-p/2028304#M233020</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I am trying to quantize VGG19 to run it on i.MX 8M Plus NPU and I need multiple convolutional outputs. When I try to quantize it with the following code, input scale is like 3.9e-9 and the responses for different inputs are the same.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def create_vgg19_c(batch_normalization=False, required_layers=None):
    base_model = VGG19(include_top=False, weights='imagenet', input_shape=(256, 256, 3))
    base_model.trainable = True

    if required_layers is None:
        required_layers = [2, 5, 8, 13, 18, 19]
    
    outputs = [base_model.layers[i].output for i in required_layers]
    model = Model(inputs=base_model.input, outputs=outputs)
    return model


model = create_vgg19_c()

quant_aware_model = tfmot.quantization.keras.quantize_model(model)

def representative_data_gen():
    _, h, w, _ = quant_aware_model.input_shape
    samples = 100
    for i in range(samples):

        img_in = np.random.uniform(0, 255, (1, h, w, 3)).astype('float32')

        img_in = tf.keras.applications.vgg19.preprocess_input(img_in)

        yield [img_in]

converter = tf.lite.TFLiteConverter.from_keras_model(quant_aware_model)

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.experimental_new_converter = True

converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8

quantized_tflite_model = converter.convert()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I tried to normalize the values between [-128, 127] for int8 and between [-1,1] for float32. I changed the parameters of the converter, I used just optimizer but scale and same results didn't changed. Also I changed the preprocess function but nothing changed. I want to quantize pretrained VGG19 backbone with specified outputs.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 13:11:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/Input-Scale-of-Quantized-TFLite-Model/m-p/2028304#M233020</guid>
      <dc:creator>merkan</dc:creator>
      <dc:date>2025-01-15T13:11:33Z</dc:date>
    </item>
    <item>
      <title>Re: Input Scale of Quantized TFLite Model</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/Input-Scale-of-Quantized-TFLite-Model/m-p/2029076#M233058</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Try to use the latest BSP since some problems has ben encounter with VGG19 but is suppose to be fixed in latest BSP.&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 14:03:04 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/Input-Scale-of-Quantized-TFLite-Model/m-p/2029076#M233058</guid>
      <dc:creator>Bio_TICFSL</dc:creator>
      <dc:date>2025-01-16T14:03:04Z</dc:date>
    </item>
  </channel>
</rss>

