Tensorflow int8 conversion error for segmentation U-Net
@david507huang,
 In the mentioned comment I did not see the inclusion of Select tf ops.
Could you please try to add the below snippet in your code and let us know the outcome. 

```
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS_INT8, # enable TensorFlow Lite ops.
  tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
tflite_model = converter.convert()
```
Also kindly note that all ops are not available right now in INT8. Thank you! Still the same errors. I tried converting the model to frozen graph and then to tflite. But the final tflite model outputs incorrect output and runs very slowly.  Hi @david507huang 

Sorry for the delayed response.

The conversion error is not provided. If it is successfully converted, can you provide more information regarding the results obtained and on which platform inference is done.

Also, if possible can you share a reproducible gist as the code provided is not reproducible since it has other dependencies.

Thanks. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59879">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59879">No</a>

### 1. System information

- OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
Windows 10 
- TensorFlow installation (pip package or built from source):
pip package
- TensorFlow library (version, if pip package or github SHA, if built from source):
- TensorFlow 2.11.0, pip package


### 2. Code

Provide code to help us reproduce your issues using one of the following options:

#### Option B: Paste your code here

import cv2
import numpy as np
from matplotlib import pyplot as plt
import tensorflow as tf
from tensorflow import keras
from mss import mss
from PIL import Image
import os

def U_Net (o_w, o_h, o_c, t_w, t_h, t_c, num_layers, initial_ch, deep_sup):
    global model
    def encoder(e_in, filters, kernel_size=(3, 3), padding="same", strides=1):
        e = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation="relu")(e_in)
        skip_out = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation="relu")(e)
        e_out = keras.layers.MaxPool2D((2, 2), (2, 2))(skip_out)
        return e_out, skip_out

    def bridge(bridge_in, filters, kernel_size=(3, 3), padding="same", strides=1):
        b = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation="relu")(bridge_in)
        bridge_out = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation="relu")(b)
        return bridge_out

    def decoder(d_in, skip_in, filters, kernel_size=(3, 3), padding="same", strides=1):
        up_sampled = keras.layers.UpSampling2D((2, 2))(d_in)
        up_sampled_reshaped = tf.keras.layers.experimental.preprocessing.Resizing(int((skip_in.shape[1])*t_h/o_h), int((skip_in.shape[2])*t_w/o_w), interpolation="bilinear", name=None)(up_sampled)
        skip_in_reshaped = tf.keras.layers.experimental.preprocessing.Resizing(up_sampled_reshaped.shape[1], up_sampled_reshaped.shape[2], interpolation="bilinear", name=None)(skip_in)
        concatenated = keras.layers.Concatenate()([up_sampled_reshaped, skip_in_reshaped])
        d = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation="relu")(concatenated)
        d_out = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation="relu")(d)
        return d_out

    inputs = keras.layers.Input((o_h, o_w, o_c))
    dict = {}

    num_layers_str = str(num_layers-1)
    num_layers_minus2_str = str(num_layers-3)

    e_out_end = "e_out"+num_layers_str
    d_out_end = "d_out"+num_layers_str
    d_out_end_minus2 = "d_out"+num_layers_minus2_str

    dict['e_out0'] = inputs
    for i in range(1,(num_layers)):
        dict['e_out%s'%i], dict['skip_out%s'%i] = encoder((dict['e_out%s'%(i-1)]), (0.5*initial_ch*(2**i)))


    bridge_out = bridge((dict[e_out_end]), (0.5*initial_ch*(2**num_layers)))

    dict['d_out0'] = bridge_out

    for n in range(1,(num_layers)):
        dict['d_out%s'%n] = decoder((dict['d_out%s'%(n-1)]), (dict['skip_out%s'%(num_layers-n)]), (0.5*initial_ch*(2**(num_layers-n))))

    outputs = keras.layers.Conv2D(t_c, (1, 1), padding="same", activation="sigmoid")(dict[d_out_end])

    if deep_sup == 1:
        ds = keras.layers.UpSampling2D((4, 4))(dict[d_out_end_minus2])
        ds = keras.layers.Conv2D(t_c, (1, 1), padding="same", activation="sigmoid")(ds)
        outputsconcat = tf.keras.layers.concatenate([outputs, ds], axis=1)
        model = keras.models.Model(inputs, outputsconcat)

    if deep_sup == 0:
        model = keras.models.Model(inputs, outputs)

    return model


def modelcreator(o_w, o_h, o_c, t_w, t_h, t_c, model_type, num_layers, initial_ch, deep_sup):
    global current_model
    # For U-Net:
    if model_type == "U-Net":
        current_model = U_Net (o_w, o_h, o_c, t_w, t_h, t_c, num_layers, initial_ch, deep_sup)
    return current_model

#################
(o_w, o_h, o_c, t_w, t_h, t_c, model_type, num_layers, initial_ch, deep_sup, gan_onoff) = (500,500,3,500,500,3,"U-Net",5,16,1,0)

current_model = modelcreator(o_w, o_h, o_c, t_w, t_h, t_c, model_type, num_layers, initial_ch, deep_sup, gan_onoff)

current_model.load_weights(Model_directory)

current_model.summary()

############

image_path = 
target_path = 

image_array = []
target_array = []

for i in os.listdir(image_path):
    

    ## Reading Image
    image = cv2.imread(image_path + i)
    image = cv2.resize(image, (o_w, o_h))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = image / 255.0
    
    image_array.append(image)
    
for i in os.listdir(target_path):
    
    ## Reading Mask
    target = cv2.imread(target_path + i)
    target = cv2.resize(target, (o_w, o_h))
    target = cv2.cvtColor(target, cv2.COLOR_BGR2RGB)

    ## Normalizaing
    target = target / 255.0
    
    target = np.array(target)
    target2 = np.array(target)
    targetconcat = np.concatenate((target, target2), axis=1)
    target = targetconcat
    
    target_array.append(target)

representative_data = image_array, target_array

################

def representative_data_gen():
    for i in range(len(image_array)):
        # Preprocess a single image
        img = image_array[i]
        img = np.asarray(img)[None, ...]  # Add batch dimension
        img = img.astype(np.float32)  # Convert to float32
        yield [img]  # Yield the preprocessed image as a list

#################

converter = tf.lite.TFLiteConverter.from_keras_model(current_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.inference_input_type = tf.int8  
converter.inference_output_type = tf.int8 
tflite_quant_model = converter.convert()

#################


### 3. Failure after conversion
If the conversion is successful, but the generated model is wrong, then state what is wrong:

Resulting model yields bad results and takes more than 5 min to process one image. 


### 4. (optional) RNN conversion support
If converting TF RNN to TFLite fused RNN ops, please prefix [RNN] in the title.

### 5. (optional) Any other info / logs
Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.

WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 5 of 20). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: C:\Users\David\AppData\Local\Temp\tmpl1ifyrjo\assets
INFO:tensorflow:Assets written to: C:\Users\David\AppData\Local\Temp\tmpl1ifyrjo\assets
C:\Users\David\AppData\Local\miniconda3\lib\site-packages\tensorflow\lite\python\convert.py:765: UserWarning: Statistics for quantized inputs were expected, but not specified; continuing anyway.
  warnings.warn("Statistics for quantized inputs were expected, but not "
