TFLite:`ValueError:模型输入未量化`

2024-05-15 18:52:33 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

我尝试将^{}操作转换为TFLite。我正在使用下面的脚本。但是转换失败,并引发错误ValueError: Model input is not quantized.。 有人知道吗?我没有找到任何关于之前相关问题的相关信息

代码片段

import tensorflow as tf

IMG_SIZE = 128
NUM_BOXES = 100
CROP_SIZE = 28
NB_DATA_SAMPLES = 32
BATCH_SIZE = 1


def create_model():
    images_ = tf.keras.Input(shape=(IMG_SIZE, IMG_SIZE, 3), batch_size=BATCH_SIZE, dtype=tf.float32)
    boxes_ = tf.keras.Input(shape=(NUM_BOXES, 4), batch_size=BATCH_SIZE, dtype=tf.float32)

    box_indices = tf.reshape(
        tf.repeat(
            tf.expand_dims(tf.range(BATCH_SIZE, dtype=tf.int32), axis=-1),
            NUM_BOXES,
            axis=-1
        ),
        shape=(-1,)
    )

    cropped_images = tf.image.crop_and_resize(
        image=images_,
        boxes=tf.reshape(boxes_, (-1, 4)),
        box_indices=box_indices,
        crop_size=(CROP_SIZE, CROP_SIZE))

    model = tf.keras.models.Model(inputs=[images_, boxes_], outputs=cropped_images)
    model.summary(line_length=200)
    return model


model = create_model()

def representative_dataset_generator():
    for _ in range(NB_DATA_SAMPLES):
        image_ = tf.random.normal(shape=(1, IMG_SIZE, IMG_SIZE, 3))
        bboxes_ = tf.random.uniform((1, NUM_BOXES, 4), maxval=1)
        yield [image_, bboxes_]

# Converter
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]

converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS_INT8,
    tf.lite.OpsSet.SELECT_TF_OPS
]
converter.target_spec.supported_types = [tf.int8]

converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8

converter.representative_dataset = representative_dataset_generator
quant_model = converter.convert()

谢谢


Tags: cropimageimgsizemodeltfbatchlite