TFLite 无法将输入数组从形状 (96,96) 广播到形状 (96,96,1)

问题描述 投票:0回答:1

我使用 Google 的 Teachable Machine 使用 3 组 96x96px 灰度 jpg 构建了一个 TensorFlow lite 模型,然后以 tflite 格式导出该模型。当我尝试对新的 96x96px 灰度图像运行预测时,出现错误:

ValueError:无法将输入数组从形状 (96,96) 广播到 形状 (96,96,1)

我在 Raspberry Pi 3B+、Python 3.9.2、TFLite v2.5.0.post1 上运行此程序。使用 Imagemagick 将所有图像转换为灰度格式

convert infile.png -fx '(r+g+b)/3' -colorspace Gray outfile.jpg
输入和输出文件具有完全相同的色彩空间(灰色)和大小。这是预测的代码:

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), "%.")

添加以下行来扩展图像数据维度可以完成预测,但始终导致 0% 准确度。

image = np.expand_dims(image, axis=2)
python tensor tensorflow-lite tflite teachable-machine
1个回答
0
投票

在可教机器中训练时如何确定总体准确性?

© www.soinside.com 2019 - 2024. All rights reserved.