【问题标题】:How to use TensorFlow lite on a raspberry pi 4 without keras?如何在没有 keras 的树莓派 4 上使用 TensorFlow lite?
【发布时间】:2021-05-02 20:18:28
【问题描述】:

基本上我想将此代码 sn-p 转换为打开 tflite 模型且不使用 keras 的代码。我无法在我的树莓派 4 上安装 keras,因为它需要 Tensorflow 2+。

model = keras.models.load_model( saved_model_path )

image_url = tf.keras.utils.get_file('Court', origin='https://squashvideo.site/share/court3.jpg' )
img = tf.keras.preprocessing.image.load_img(image_url, target_size=( 224, 224 ) )
os.remove(image_url) # Remove the cached file

img_array = tf.keras.preprocessing.image.img_to_array(img)
prediction_scores = model.predict(np.expand_dims(img_array, axis=0)/255)

score = tf.nn.softmax(prediction_scores[0])

print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)

这是我尝试过的,它给出了以下错误:

from PIL import Image

def classify_image(interpreter, image, top_k=1):
  tensor_index = interpreter.get_input_details()[0]['index']
  input_tensor = interpreter.tensor(tensor_index)()[0]
  input_tensor[:, :] = 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, top_k)
  return [(i, output[i]) for i in ordered[:top_k]][0]

interpreter = Interpreter('/var/www/html/share/AI/court.tflite')
interpreter.allocate_tensors()
_, height, width, _ = interpreter.get_input_details()[0]['shape']
print("Image Shape (", width, ",", height, ")")

data_folder = "/var/www/html/share/"

image = Image.open(data_folder + "court1.jpg").convert('RGB').resize((width, height))
label_id, prob = classify_image(interpreter, image)

运行报错:

squash@court1:/var/www/html/share/AI $ python3 test.py
Image Shape ( 224 , 224 )
Traceback (most recent call last):
  File "test.py", line 44, in <module>
    label_id, prob = classify_image(interpreter, image)
  File "test.py", line 22, in classify_image
    interpreter.invoke()
  File "/home/squash/.local/lib/python3.7/site-packages/tflite_runtime/interpreter.py", line 539, in invoke
    self._ensure_safe()
  File "/home/squash/.local/lib/python3.7/site-packages/tflite_runtime/interpreter.py", line 287, in _ensure_safe
    data access.""")
RuntimeError: There is at least 1 reference to internal data
      in the interpreter in the form of a numpy array or slice. Be sure to
      only hold the function returned from tensor() if you are using raw
      data access.

【问题讨论】:

    标签: tensorflow tensorflow-lite raspberry-pi4


    【解决方案1】:

    错误在于您在此处向 tflite 解释器提供数据的方式:

    input_tensor = interpreter.tensor(tensor_index)()[0]
    input_tensor[:, :] = image
    

    Image.open 函数返回一个 Image 对象。在将其输入张量之前,您需要将其转换为二进制数据。你应该使用:

    interpreter.set_tensor(0, image_data)
    

    设置数据而不是上面的赋值。

    【讨论】:

    • 谢谢。我尝试注释掉这两行:# input_tensor = interpreter.tensor(tensor_index)()[0]# input_tensor[:, :] = image 并添加:interpreter.set_tensor(0, image),但现在它给出了错误:ValueError: Cannot set tensor: Got value of type UINT8 but expected type FLOAT32 for input 0, name: serving_default_input_1:0。我想我不清楚如何从 Image.open 命令中获取二进制图像:image = Image.open(data_folder + "court1.jpg").convert('RGB').resize((width, height))
    【解决方案2】:

    我认为我通过这样做修复了它:

    img = Image.open( image_url ).convert('RGB').resize((224, 224))
    img_array = np.array ( img, dtype=np.float32 )
    
    probs_lite = lite_model( np.expand_dims(img_array, axis=0)/255 )[0]
    
    print ( probs_lite )
    print (np.argmax(probs_lite))
    
    score = tf.nn.softmax(probs_lite)
    
    print(
      "This image most likely belongs to {} with a {:.2f} percent confidence."
      .format(class_names[np.argmax(score)], 100 * np.max(score))
    )
    
    

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2019-02-04
      • 2017-03-12
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多