【问题标题】:Passing image data to Tensorflow lite (micro)将图像数据传递给 Tensorflow lite (micro)
【发布时间】:2021-01-30 01:17:03
【问题描述】:

我有一个 tensorflow 模型,它几乎完全按照教程在

https://www.tensorflow.org/tutorials/images/classification

我正在模型之外进行标准化,所以我得到了

normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)
normalized_train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
normalized_val_ds = val_ds.map(lambda x, y: (normalization_layer(x), y))
model = Sequential([
  layers.Conv2D(16, 3, padding='same', activation='relu', input_shape=(img_height, img_width, 3)),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(num_classes)
])

这一切似乎都奏效了,以高验证精度等通过了 epoch

然后我通过

导出到 tensorflow lite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]

我正在将其加载到 ESP32 中,经过大量跟踪和错误后,它运行正常。 但是我没有得到关于 ESP32 的预测。

(其中framebuffer 绝对是一个红、蓝、绿字节数组(0 - 255))

float *runPrediction(uint8_t *framebuffer, size_t length)
{
  TfLiteStatus tflite_status;
  static float prediction[NUMBER_OF_OUTPUTS] = {0};

  for (size_t i = 0; i < length; i++)
  {
    model_input->data.f[i] = (1.0f / 255) * framebuffer[i];
  }

  tflite_status = interpreter->Invoke();

  // display model_output etc
}

我正在尝试在传入浮点数之前重新进行规范化/转换,但我认为我可能会出错?

我没有收到任何错误消息/崩溃并且模型正在运行,只是输出的预测都没有超过 0.00。

我最初在 jpeg 上训练了模型,但是在 QQVGA 分辨率 (160x120) 下,JPEG 噪声很差,所以我一直在使用来自原位传感器的原始 .bmp 图像来增强训练数据 - 但这并没有真正帮助所以希望它是代码中的东西。

我确定输入帧缓冲区是一个 R、G、B 字节数组,因为我使用它输出调试视图,并将其转换为 RGB565。

我看到之前的模型返回值高于 0.01,但我认为这只是噪音。

据说输入形状是[ 1 120 160 3] & float32,我猜我需要在c++代码中以某种方式匹配它?

【问题讨论】:

    标签: tensorflow deep-learning tensorflow-lite esp32


    【解决方案1】:

    看起来这是将图像传递到微控制器上的 Tensorflow lite 的正确方法。

    我使用非标准化数据作为 representative_dataset 值做错了什么,因此模型被量化为预期 0-255 的浮点数,即使它只能处理 0-1。

    使用标准化数据作为参考,我现在让模型返回了合理的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 2018-05-10
      • 1970-01-01
      相关资源
      最近更新 更多