Reputation: 1900
I have built a tensorflow lite model using 3 sets of 96x96px grayscale jpgs using Google's Teachable Machine, then exported the model in tflite format. When I attempt to run a prediction on a new 96x96px grayscale image I get the error:
ValueError: could not broadcast input array from shape (96,96) into shape (96,96,1)
I am running this on a Raspberry Pi 3B+, Python 3.9.2, TFLite v2.5.0.post1. All images were converted to grayscale format using Imagemagick convert infile.png -fx '(r+g+b)/3' -colorspace Gray outfile.jpg
Input and output files are the exact same colorspace (Gray) and size. Here is the code for the prediction:
from tflite_runtime.interpreter import Interpreter
from PIL import Image, ImageOps
import numpy as np
import time
def load_labels(path): # Read the labels from the text file as a Python list.
with open(path, 'r') as f:
return [line.strip() for i, line in enumerate(f.readlines())]
def set_input_tensor(interpreter, image):
tensor_index = interpreter.get_input_details()[0]['index']
input_tensor = interpreter.tensor(tensor_index)()[0]
input_tensor[:, :] = image
def classify_image(interpreter, image, top_k=1):
set_input_tensor(interpreter, image)
interpreter.invoke()
output_details = interpreter.get_output_details()[0]
output = np.squeeze(interpreter.get_tensor(output_details['index']))
scale, zero_point = output_details['quantization']
output = scale * (output - zero_point)
ordered = np.argpartition(-output, 1)
return [(i, output[i]) for i in ordered[:top_k]][0]
data_folder = "/home/ben/detectClouds/"
model_path = data_folder + "model.tflite"
label_path = data_folder + "labels.txt"
interpreter = Interpreter(model_path)
print("Model Loaded Successfully.")
interpreter.allocate_tensors()
_, height, width, _ = interpreter.get_input_details()[0]['shape']
print("Image Shape (", width, ",", height, ")")
# Load an image to be classified.
image = Image.open(data_folder + "inputGray.png")
# Classify the image.
time1 = time.time()
label_id, prob = classify_image(interpreter, image)
time2 = time.time()
classification_time = np.round(time2-time1, 3)
print("Classificaiton Time =", classification_time, "seconds.")
# Read class labels.
labels = load_labels(label_path)
# Return the classification label of the image.
classification_label = labels[label_id]
#print(prob)
print("Image Label is :", classification_label, ", with Accuracy :", np.round(prob*100, 2), "%.")
Adding the following line to expand the image data dimensions allows the prediction to complete, but always results in 0% Accuracy.
image = np.expand_dims(image, axis=2)
Upvotes: 0
Views: 254