繁体   English   中英

TFLite:`ValueError:Model 输入未量化。`

[英]TFLite: `ValueError: Model input is not quantized.`

问题

我尝试将crop_and_resize操作转换为 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()

谢谢

我可以成功执行你的代码。 但是,它只量化 Reshape 层而不量化裁剪层。 所以我想也许 tflite 无法量化他们不支持的操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM