【问题标题】:Tensorflow unwanted subgraphsTensorFlow 不需要的子图
【发布时间】:2021-08-02 11:09:32
【问题描述】:

我正在尝试构建一个 TFLite 模型,该模型由用于检测的高效 det 和用于识别的 keras-ocr(只是识别器模型)组成,我需要它在移动设备上运行得快一些。 我想完全量化模型,但在Netron 中检查模型时发现模型有一些子图,而 TFLite 不支持使用子图对模型进行量化。

List of subgraphs in model

这是我正在使用的功能模型定义以及我认为生成子图的层。

MAX_DETECTIONS = 10

img_inputs = tf.keras.Input(shape=(1080, 1920, 3), batch_size = 1)
boxes, scores, classes, _ = efficient_det_model_keras.model(img_inputs)
crops = CroppingLayer(MAX_DETECTIONS)(img_inputs, boxes, scores)
recognitions = RecognitionLayer(recognizer.prediction_model, MAX_DETECTIONS)(crops)

model = tf.keras.Model(inputs=img_inputs, outputs=(recognitions, scores), name="alpr")
class CroppingLayer(tf.keras.layers.Layer):
    def __init__(self, MAX_RECOGNITIONS):
        super(CroppingLayer, self).__init__()
        self.MAX_RECOGNITIONS = MAX_RECOGNITIONS
        self.threshold = 0.3
    
    def get_crop(self, input_image, ymin, xmin, height, width):
        cropped = tf.image.crop_to_bounding_box(input_image, ymin, xmin, height, width)
        cropped = tf.image.resize(cropped, (31, 200))

        cropped = tf.image.rgb_to_grayscale(cropped)/255
        
        return cropped
    
    
    def call(self, input_image, boxes, scores):
        res = []

        ymin = tf.cast(boxes[0, :, 0], tf.int32)
        xmin = tf.cast(boxes[0, :, 1], tf.int32)
        ymax = tf.cast(boxes[0, :, 2], tf.int32)
        xmax = tf.cast(boxes[0, :, 3], tf.int32)
        height = tf.math.abs(ymax - ymin)
        width = tf.math.abs(xmax - xmin)

        for i in range(self.MAX_RECOGNITIONS):
            cropped = self.get_crop(input_image[0], ymin[i], xmin[i], height[i], width[i])

            res.append(cropped)

        batch = [tf.stack(res, axis = 0)]

        return tf.stack(batch, axis = 0)

有什么办法可以摆脱这些子图来制作单个图模型?

【问题讨论】:

    标签: python tensorflow tensorflow-lite


    【解决方案1】:

    看起来 for 循环语句导致在 TensorFlow 图中创建了一个控制流操作。如果识别数可以固定,解开 for 循环语句可能有助于创建单个子图。

    【讨论】:

    • 将循环中的 self.MAX_RECOGNITIONS 更改为设置的整数并不能解决问题。在这一点上我唯一的提示是循环条件中的一个参数是“alpr/cropping_layer_5/crop_to_bounding_box/strided_slice1”。我认为这可能与crop_to_bounding_box 的实现方式有关,但我不确定。
    • 设置一个整数是不够的 解开for循环将有助于摆脱TF控制流操作依赖。例如cropped_0 = self.get_crop(input_image[0], ymin[0], xmin[0], height[0], width[0]) cropped_1 = self.get_crop(input_image[1], ymin[1], xmin[1], height[1], width[1]) ... res.append(cropped_0) res.append(cropped_1) ....
    • 我已经尝试过了,但仍然得到相同的子图:/ 我也尝试过使用 tf.image.crop_and_resize (pastebin.com/VRW20J47),但仍然无法正常工作。
    • 我猜裁剪层只是模型的一层。还有其他层创建控制操作。如果可能的话,最好分享可重现的模型代码。
    • 这是我的代码 (gist.github.com/bhuminiecki/0a555dd63f8f5066c150e42e4d438cc8) 和训练有素的效率模型 (drive.google.com/drive/folders/…) 的要点。 EfficientDet 使用来自 automl 的官方脚本进行训练,输入大小为 1920x1080,每张图像检测 10 次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2019-12-24
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多