【问题标题】:tensorflow-lite - using tflite Interpreter to get an image in the outputtensorflow-lite - 使用 tflite 解释器在输出中获取图像
【发布时间】:2019-04-08 00:46:00
【问题描述】:

我正在尝试使用 Tensorflow-for-poets-2 TFLite 教程的工作流程,https://codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-tflite/#6

但是,我正在尝试进行样式转换,而不是图像分类。这意味着我的网络的输入和输出都是图像(与原始示例相比,输入是图像,输出是分数列表)。

我的许多问题之一是从 tflite 推理中获取输出处理后的图像:

加载 tflite 模型后,我有 tflite 解释器 tflite 。 使用这个解释器我运行推理:

tflite.run(imgData, Out_imgData);

在哪里

imgData, Out_imgData

是 ByteBuffers,创建方式与 Tensorflow-for-poets-2 TFLite 教程中相同,https://codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-tflite/#6

现在我的推理输出为 ByteBuffer

Out_imgData

当推理输出是图像时,我找不到示例。请帮我将浮动 ByteBuffer Out_imgData 转换为位图图像。或者给我举个例子。

视觉问题描述: 在 python 中使用 tflite Interpreter,我得到输出图像: enter image description here

【问题讨论】:

    标签: tensorflow-lite


    【解决方案1】:

    我在分割项目中遇到了类似的问题:在使用 tflite 文件进行推理期间,几乎所有输出像素都是 255 秒,但在使用导出模型进行推理期间一切正常。 对解决方案的长期搜索使我找到了this related issue。它说问题出在批量标准化层中。我删除了它们,我的输出变得正常,但是没有 bn 的神经网络质量急剧下降。我尝试用 tf.keras.layers.BatchNormalization 和 tf.contrib.layers.batch_norm 替换 tf.layer.batch_normalization,但都一样。最后我通过实现我自己的批量标准化解决了这个问题:

    def my_moments(input_tensor):
        mean = tf.reduce_mean(input_tensor, axis=[0, 1, 2])
        dev = input_tensor - mean
        dev = dev * dev
        dev = tf.reduce_mean(dev, axis=[0, 1, 2])
        return mean, dev
    
    def my_bn(input_tensor):
        mu = tf.Variable(tf.ones(input_tensor.shape[3]))
        beta = tf.Variable(tf.zeros(input_tensor.shape[3]))
        mean, dev = my_moments(input_tensor)
        return beta + mu * (input_tensor - mean) / (tf.sqrt(dev) + 0.001)
    

    请注意,这不是批量规范的字面实现(此处不使用移动平均),因为我的项目只需要训练模式。另请注意,我们不能使用 tf.nn.moments 来计算均值和偏差,因为 tflite 不支持它(因此我们需要实现自己的矩函数)。在用提供的函数替换批量标准化后,我能够训练我的网络,将其导出到 tflite 并在 tflite 推理期间正确使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多