gpt4 book ai didi

python - Tensorflow Lite - ValueError : Cannot set tensor: Dimension mismatch

转载 作者:行者123 更新时间:2023-12-05 07:30:58 27 4
gpt4 key购买 nike

这可能是一个愚蠢的问题,但我是机器学习和 Tensorflow 的新手。我正在尝试使用 Tensorflow Lite 在 Raspberry Pi 上运行对象检测 API。我正在尝试借助此示例修改我的代码

https://github.com/freedomtan/tensorflow/blob/deeplab_tflite_python/tensorflow/contrib/lite/examples/python/object_detection.py

这段代码将从图像中检测对象。但是我想通过 Pi 相机实时检测物体而不是图像。我试图修改此代码以从相机而不是图像读取输入。这是我的一段代码-

import numpy as np
from tensorflow.contrib.lite.python import interpreter as interpreter_wrapper
import cv2

cap = cv2.VideoCapture(0)
ret, image_np = cap.read()

PATH_TO_MODEL = "ssd_mobilenet_v1_coco.tflite"

interpreter = tf.contrib.lite.Interpreter(model_path=PATH_TO_MODEL)
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

while True:
# NxHxWxC, H:1, W:2
height = input_details[0]['shape'][1]
width = input_details[0]['shape'][2]
ret, image_np = cap.read()

image_np_expanded = np.expand_dims(image_np, axis=0)
#if floating_model:
image_np_expanded = (np.float32(image_np_expanded) - input_mean) / input_std

#HERE I AM GETTING ERROR
interpreter.set_tensor(input_details[0]['index'], image_np_expanded)

if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break

但是我收到了这个错误-

Traceback (most recent call last):
File "New_object_detection.py", line 257, in <module>
interpreter.set_tensor(input_details[0]['index'], image_np_expanded)
File "/home/saurabh/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/interpreter.py", line 151, in set_tensor
self._interpreter.SetTensor(tensor_index, value)
File "/home/saurabh/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py", line 133, in SetTensor
return _tensorflow_wrap_interpreter_wrapper.InterpreterWrapper_SetTensor(self, i, value)
ValueError: Cannot set tensor: Dimension mismatch

谁能告诉我如何解决这个错误或建议一个教程吗?

最佳答案

许多基于图像的机器学习模型都使用固定大小的输入进行训练。原始图像可能具有不同的尺寸,但会调整为固定尺寸(例如 224x224x3)。

因此,您需要在将输入提供给模型之前调整图像的大小。它可能工作得很好,因为训练数据也根据不同的大小进行了调整。

正如上面的评论所指出的,cv.resize 可以解决问题。

关于python - Tensorflow Lite - ValueError : Cannot set tensor: Dimension mismatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52012010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com