How to input file into tflite model for inference (ValueError: Cannot set tensor: Got value of type FLOAT32 but expected type UINT8 for input 170)

Viewed 1071

I am having trouble understanding what I am supposed to do here. I am trying to run inference on this model. I have preprocessed my images to be of size 224x224, and scaled the values to be between [0,1] with normalization as seen in this code.


import numpy as np
from numpy import asarray
import tensorflow as tf
import os
import sys
from PIL import Image

def process_image(image_path):
    image = Image.open(image_path)
    new_image = image.resize((224,224))
    np_image = asarray(new_image)

    min = np_image.min()
    max = np_image.max()    

    # normalize to the range 0-1
    np_image = np_image.astype('float32')
    np_image -= min
    np_image /= (max - min)

    return [np_image]

Here is its output

[array([[[0.5411765 , 0.5372549 , 0.03529412],
        [0.24705882, 0.5372549 , 0.53333336],
        [0.03137255, 0.24705882, 0.52156866],
        ...,
        [0.24705882, 0.60784316, 0.6039216 ],
        [0.10196079, 0.24705882, 0.5686275 ],
        [0.5647059 , 0.0627451 , 0.24705882]],

I then am trying to put this result into the model and run inference. However, I keep getting this error.

ValueError: Cannot set tensor: Got value of type FLOAT32 but expected type UINT8 for input 170, name: module/hub_input/images_uint8

Any idea on how to fix it? I have tried to simply change the type back to uint8, but then all of the RGB values turn to 0 since everything is a decimal less than 1.

Here is the rest of the code

# Load interpreter
interpreter = tf.lite.Interpreter(model_path="aiy_vision_classifier_birds_V1_2.tflite")
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

print(input_details)
print(output_details)

interpreter.allocate_tensors()

# Inputting the image
image = process_image('Images/Processed/blue_Jay_processed.png') # This is the function shown above
print(image)
interpreter.set_tensor(input_details[0]['index'], image)

# run the inference
interpreter.invoke()

output_data = interpreter.get_tensor(input_details[0]['index'])

print("the output is {}".format(output_data))

In addition, here is the input and output details:

[{'name': 'module/hub_input/images_uint8', 'index': 170, 'shape': array([ 1, 224, 224, 3], dtype=int32), 'shape_signature': array([ 1, 224, 224, 3], dtype=int32), 'dtype': <class 'numpy.uint8'>, 'quantization': (0.0078125, 128), 'quantization_parameters': {'scales': array([0.0078125], dtype=float32), 'zero_points': array([128], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

[{'name': 'module/prediction', 'index': 171, 'shape': array([ 1, 965], dtype=int32), 'shape_signature': array([ 1, 965], dtype=int32), 'dtype': <class 'numpy.uint8'>, 'quantization': (0.00390625, 0), 'quantization_parameters': {'scales': array([0.00390625], dtype=float32), 'zero_points': array([0], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

0 Answers
Related