0

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)
UltrasoundJelly
  • 1,845
  • 2
  • 18
  • 32
  • You can use `.reshape` to convert a `(96,96)` array to `(96,96,1)`, and it costs nothing. – Tim Roberts May 19 '23 at 04:53
  • Thanks. When I run `image = np.reshape(image,(96,96,1))` the code runs, but I still get 0% accuracy every time. – UltrasoundJelly May 19 '23 at 11:10
  • That suggests that the images used to create the model were processed slightly differently from the images in the prediction, but we don't see the training here. You're just loading a packaged model. – Tim Roberts May 19 '23 at 18:51
  • If I use one of the images used to create the model itself I'm getting the same results. [0. 0. 0.] in the prediction. The images are all from the same source, processed in the same batch exactly the same way. – UltrasoundJelly May 19 '23 at 21:48
  • Well, the fact that one comes out `(96,96)` and one comes out `(96,96,1)` suggests they were not processed identically. – Tim Roberts May 20 '23 at 00:14

0 Answers0