Tensorflow Lite - ValueError: Cannot set tensor: Dimension mismatch

拜拜、爱过 提交于 2021-02-10 20:47:46

问题


This is probably going to be a stupid question but I am new to machine learning and Tensorflow. I am trying to run object detection API on Raspberry Pi using Tensorflow Lite. I am trying to modify my code with the help of this example

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

This piece of code will detect object from a image. But instead of a image I want to detect object on real time through Pi camera. I tried to modified this code to read input from camera instead of image. Here is my piece of code -

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

but I am getting this error -

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

Can anyone please tell me how to fix this error or suggest a tutorial for the same?


回答1:


Many image-based machine learning models are trained with fixed-sized inputs. The original image may come with different dimensions, but get resized to a fixed size (e.g. 224x224x3).

Therefore, you need to resize the image before feeding the input to the model. It may work well because the fact that the training data are also resized from various sizes.

As the comment above already pointed out, cv.resize can do the trick.



来源:https://stackoverflow.com/questions/52012010/tensorflow-lite-valueerror-cannot-set-tensor-dimension-mismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!