Reputation: 1038
I am using a pre-trained tensorflow light model to get some sample output using a short program I created:
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))
After the input shape is printed to the console, nothing else happens, the program ends. The line that supposedly should print the output never executes.
output:
C:\python imagetest.py INFO: Initialized TensorFlow Lite runtime. 2020-01-21 08:07:32.567619: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations: AVX AVX2 To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags. 2020-01-21 08:07:32.578283: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 8. Tune using inter_op_parallelism_threads for best performance. input shape: (1, 257, 257, 3)
What is the correct way to use a pretrained tflite model using the Interpreter class?
Upvotes: 0
Views: 739
Reputation: 1038
Was able to get it to working using the following code:
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'])
The only difference I notice is an initial call to allocate_tensors()
.
Upvotes: 1