【问题标题】:How to use a pretrained tensorflowlite model for input interpretation如何使用预训练的 tensorflowlite 模型进行输入解释
【发布时间】:2020-01-21 00:18:22
【问题描述】:

我正在使用一个预训练的 tensorflow 光照模型,使用我创建的一个简短程序来获取一些示例输出:

import cv2 as cv
import numpy as np
import os
import tensorflow as tf
import numpy as np

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_jpeg(img, channels=3)
  # Use `convert_image_dtype` to convert to floats in the [0,1] range.
  img = tf.image.convert_image_dtype(img, tf.float32)
  # resize the image to the desired size.
  return tf.reshape(tf.image.resize(img, [257, 257]), [1, 257, 257, 3])


model = tf.lite.Interpreter('models\posenet_mobilenet_v1_100_257x257_multi_kpt_stripped.tflite')
input_details = model.get_input_details()
output_details = model.get_output_details()

img = tf.io.read_file('photos\standing\\1.jpg')
input_data = decode_img(img)

print('input shape: {}'.format(input_data.shape))
model.set_tensor(input_details[0]['index'], input_data)
model.invoke()

output_data = model.get_tensor(output_details[0]['index'])
print('output: {}'.format(output_data))

输入形状打印到控制台后,没有其他反应,程序结束。应该打印输出的行永远不会执行。

输出:

C:\python imagetest.py 信息:初始化的 TensorFlow Lite 运行时。 2020-01-21 08:07:32.567619: I tensorflow/core/platform/cpu_feature_guard.cc:145] 这个 TensorFlow 二进制文件使用 Intel(R) MKL-DNN 进行了优化,以在性能关键操作中使用以下 CPU 指令:AVX AVX2 要在非 MKL-DNN 操作中启用它们,请使用适当的编译器标志重建 TensorFlow。 2020-01-21 08:07:32.578283: I tensorflow/core/common_runtime/process_util.cc:115] 使用默认互操作设置创建新线程池:8. 使用 inter_op_parallelism_threads 进行调整以获得最佳性能。 输入形状:(1, 257, 257, 3)

使用 Interpreter 类的预训练 tflite 模型的正确方法是什么?

【问题讨论】:

  • 该输出消息似乎并不相关,不是吗?你有minimal reproducible example吗?
  • 这是完整的程序,没有图片或下载的模型无法重现。这里的问题是没有与程序停止的原因相对应的输出。似乎程序突然停止了。我希望在使用 Interpreter 类方面更有经验的人能够通过检查程序来判断。
  • 如果您修复这些警告/信息消息会怎样?

标签: python tensorflow machine-learning tensorflow2.0 tensorflow-lite


【解决方案1】:

能够使用以下代码使其正常工作:

img = cv.imread('photos\standing\\3.jpg')
img = tf.reshape(tf.image.resize(img, [257,257]), [1,257,257,3])
model = tf.lite.Interpreter('models\posenet_mobilenet_v1_100_257x257_multi_kpt_stripped.tflite')
model.allocate_tensors()
input_details = model.get_input_details()
output_details = model.get_output_details()
floating_model = input_details[0]['dtype'] == np.float32

if floating_model:
    img = (np.float32(img) - 127.5) / 127.5

model.set_tensor(input_details[0]['index'], img)
model.invoke()

output_data =  model.get_tensor(output_details[0]['index'])
offset_data = model.get_tensor(output_details[1]['index'])

我注意到的唯一区别是对allocate_tensors() 的初始调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-25
    • 2017-08-19
    • 2021-11-14
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多