簡體   English   中英

如何使用預訓練的 tensorflowlite 模型進行輸入解釋

[英]How to use a pretrained tensorflowlite model for input interpretation

我正在使用預先訓練的 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 模型的正確方法是什么?

能夠使用以下代碼使其正常工作:

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()的初始調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM