TFLite 模型在 Android 中给出的输出与 Kivy App 中的输出不同

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

我面临着一个似乎无法解决的问题:当我在 Python 或 Android 上运行时,我的 tflite 模型为我提供了不同的数据,导致错误并使其在 Android 上崩溃。我正在开发一个带有 tflite 的 Kivy 应用程序,用于检测棋盘上的棋子。我将 YOLOv8 模型转换为 tflite 格式。在该方法中进行图像推理时出现问题:

def get_positions(model_path, img_name):
    print("Loading model...")
    interpreter = Interpreter(model_path=model_path)
    interpreter.allocate_tensors()

    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    print("Input details:", input_details)
    print("Output details:", output_details)

    print("Loading image...")
    image = cv2.imread(img_name)
    if image is None:
        raise ValueError(f"Could not load the image from {img_name}")

    print("Original image dimensions:", image.shape)
    image_height = input_details[0]['shape'][1]
    image_width = input_details[0]['shape'][2]
    resized_image = cv2.resize(image, (image_width, image_height))
    input_image = np.array(resized_image, dtype=np.float32) / 255.0
    input_image = input_image[np.newaxis, :]
    print("Resized image dimensions:", input_image.shape)

    interpreter.set_tensor(input_details[0]['index'], input_image)
    interpreter.invoke()

    output = interpreter.get_tensor(output_details[0]['index'])
    output = output[0]
    output = output.T
    print("Model output:", output)

    boxes_xywh = output[..., :4]
    scores = np.max(output[..., 4:], axis=1)
    print("Scores:", scores)
    classes = np.argmax(output[..., 4:], axis=1)

    indices = cv2.dnn.NMSBoxes(boxes_xywh.tolist(), scores.tolist(), confidence_threshold, iou_threshold)
    print("Indices after NMS:", indices)

    results = []
    for i in indices:
        if scores[i] >= confidence_threshold:
            x_center, y_center, width, height = boxes_xywh[i]
            x_center, width = x_center * image_width, width * image_width
            y_center, height = y_center * image_height, height * image_height
            x_center /= image_width
            y_center /= image_height
            width /= image_width
            height /= image_height

            result = {
                'class_id': classes[i],
                'class_name': CLASSES[classes[i]],
                'x_center': x_center,
                'y_center': y_center,
                'width': width,
                'height': height,
                'confidence': scores[i]
            }
            print("Result:", result)
            results.append(result)

    return results

在我的计算机上,它可以按预期工作,但是当我在 Android 上创建 APK 时,它会崩溃,因为未生成适当的数据。

这是我电脑上的调试日志(运行良好)

`正在加载模型...信息:为 CPU 创建了 TensorFlow Lite XNNPACK 委托。输入详细信息: [{'name': 'inputs_0', 'index': 0, 'shape': array([ 1, 640, 640, 3], dtype=int32), 'shape_signature': array([ 1, 640 , 640, 3], dtype=int32), 'dtype': , '量化': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}] 输出详细信息: [{'name': 'Identity', 'index': 409, 'shape': array( [ 1, 17, 8400], dtype=int32), 'shape_signature': array([ 1, 17, 8400], dtype=int32), 'dtype': , '量化': (0.0, 0), '量化参数': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}] 正在加载图像.. . 原始图像尺寸:(1181, 1690, 3) 调整后的图像尺寸:(1, 640, 640, 3) 模型输出:[[1.1006817e-02 3.4101062e-02 1.9614611e-02 ... 2.6802209e-05 3.2099266 e-05 2.3671286e-05] [3.3980943e-02 1.1495238e-02 5.1065966e-02 ... 3.0108062e-05 2.4680974e-05 3.4402736e-05] [4.6541955e-02 7.9 353973e-03 6.3635595e-02 ... 1.8403694e-05 4.7014169e-06 2.9310922e-05] ... [8.8734090e-01 9.7479749e-01 1.9226933e-01 ... 2.3383725e-06 1.9063839e-06 8.1505 220e-07] [9.3079507 e-01 9.7354954e-01 1.9993711e-01 ... 2.2026159e-06 2.1150520e-06 8.2314017e-07] [9.7550833e-01 9.7113740e-01 1.7806405e-01 ... 1.6 881496e-06 1.1987435e- 06 6.0122733e-07]] 分数:[4.7006957e-05 8.5894673e-05 4.0709678e-05 ... 3.7478815e-06 3.3213523e-06 2.8609916e-06] NMS 后的索引:[7601 78 53 7668 6563 7878 7088 7262 7287 6536 2125 6607 3547 6808 5907 7688 1336 6896 6972 7068 2867 4295 7497 5086 448 4266 5798 705 7077 6478 6483 7242]结果:{ 'class_id': 7, 'class_name': '白主教', 'x_center': 0.05852939561009407, ' y_center':0.773659348487854,'宽度':0.08957379311323166,'高度':0.14608508348464966,'置信度':0.92357063}结果:{'class_id':12,'class_name':'白车', 'x_center': 0.3321531414985657, 'y_center' : 0.8937365412712097, '宽度': 0.0860576331615448, '高度': 0.13431024551391602, '置信度': 0.9208999} 结果: {'class_id': 3, 'class_name': '黑骑士', ':0.7246953248977661,'y_center':0.7933675646781921 ,“宽度”:0.0967983603477478,“高度”:0.13826501369476318,“置信度”:0.9208062}........................................`

从我的 Android 应用程序(它崩溃了)`

Loading model... 05-03 16:37:36.598  6567 30025 I python  : Input details: [{'name': 'inputs_0', 'index': 0, 'shape': array([  1, 640, 640,   3], dtype=int32), 'shape_signature': array([  1, 640, 640,   3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}] 05-03 16:37:36.599  6567 30025 I python  : Output details: [{'name': 'Identity', 'index': 409, 'shape': array([   1,   17, 8400], dtype=int32), 'shape_signature': array([   1,   17, 8400], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}] 05-03 16:37:36.599  6567 30025 I python  : Loading image... 05-03 16:37:36.654  6567 30025 I python  : Original image dimensions: (1181, 1690, 3) 05-03 16:37:36.667  6567 30025 I python  : Resized image dimensions: (1, 640, 640, 3) 05-03 16:37:39.163  6567 30025 I python  : Model output: [[1.1006776e-02 3.4101110e-02 1.9614521e-02 ... 2.6802416e-05 05-03 16:37:39.163  6567 30025 I python  :   3.2099608e-05 2.3671355e-05] 05-03 16:37:39.163  6567 30025 I python  :  [3.3980943e-02 1.1495264e-02 5.1065952e-02 ... 3.0108265e-05 05-03 16:37:39.163  6567 30025 I python  :   2.4681047e-05 3.4402743e-05] 05-03 16:37:39.163  6567 30025 I python  :  [4.6541952e-02 7.9354066e-03 6.3635573e-02 ... 1.8403680e-05 05-03 16:37:39.163  6567 30025 I python  :   4.7014123e-06 2.9310897e-05] 05-03 16:37:39.163  6567 30025 I python  :  ... 05-03 16:37:39.163  6567 30025 I python  :  [8.8734090e-01 9.7479749e-01 1.9226938e-01 ... 2.3383748e-06 05-03 16:37:39.163  6567 30025 I python  :   1.9063839e-06 8.1505374e-07] 05-03 16:37:39.163  6567 30025 I python  :  [9.3079507e-01 9.7354960e-01 1.9993705e-01 ... 2.2026243e-06 05-03 16:37:39.163  6567 30025 I python  :   2.1150458e-06 8.2314176e-07] 05-03 16:37:39.163  6567 30025 I python  :  [9.7550833e-01 9.7113740e-01 1.7806411e-01 ... 1.6881464e-06 05-03 16:37:39.163  6567 30025 I python  :   1.1987436e-06 6.0122733e-07]] 05-03 16:37:39.164  6567 30025 I python  : Scores: [4.7007226e-05 8.5895088e-05 4.0709638e-05 ... 3.7478853e-06 3.3213526e-06 05-03 16:37:39.164  6567 30025 I python  :  2.8609834e-06] 05-03 16:37:39.180  6567 30025 I python  : Indices after NMS: [[7601] 05-03 16:37:39.180  6567 30025 I python  :  [7853] 05-03 16:37:39.180  6567 30025 I python  :  [7668] 05-03 16:37:39.180  6567 30025 I python  :  [6563] 05-03 16:37:39.180  6567 30025 I python  :  [7878] 05-03 16:37:39.180  6567 30025 I python  :  [7088] 05-03 16:37:39.180  6567 30025 I python  :  [7262] 05-03 16:37:39.180  6567 30025 I python  :  [7287] 05-03 16:37:39.180  6567 30025 I python  :  [6536] 05-03 16:37:39.180  6567 30025 I python  :  [2125] 05-03 16:37:39.180  6567 30025 I python  :  [6607] 05-03 16:37:39.180  6567 30025 I python  :  [3547] 05-03 16:37:39.180  6567 30025 I python  :  [6808] 05-03 16:37:39.180  6567 30025 I python  :  [5907] 05-03 16:37:39.180  6567 30025 I python  :  [7688] 05-03 16:37:39.180  6567 30025 I python  :  [1336] 05-03 16:37:39.180  6567 30025 I python  :  [6896] 05-03 16:37:39.180  6567 30025 I python  :  [6972] 05-03 16:37:39.180  6567 30025 I python  :  [7068] 05-03 16:37:39.180  6567 30025 I python  :  [2867] 05-03 16:37:39.180  6567 30025 I python  :  [4295] 05-03 16:37:39.180  6567 30025 I python  :  [7497] 05-03 16:37:39.180  6567 30025 I python  :  [5086] 05-03 16:37:39.180  6567 30025 I python  :  [ 448] 05-03 16:37:39.180  6567 30025 I python  :  [4266] 05-03 16:37:39.180  6567 30025 I python  :  [5798] 05-03 16:37:39.180  6567 30025 I python  :  [ 705] 05-03 16:37:39.180  6567 30025 I python  :  [7077] 05-03 16:37:39.180  6567 30025 I python  :  [6478] 05-03 16:37:39.180  6567 30025 I python  :  [6483] 05-03 16:37:39.180  6567 30025 I python  :  [7242]] 05-03 16:37:39.181  6567 30025 I python  : [INFO   ] [Base        ] Leaving application in progress... 05-03 16:37:39.181  6567 30025 I python  :  Traceback (most recent call last): 05-03 16:37:39.181  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/app/main.py", line 165, in <module> 05-03 16:37:39.181  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/app.py", line 956, in run 05-03 16:37:39.182  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/base.py", line 574, in runTouchApp 05-03 16:37:39.182  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/base.py", line 339, in mainloop 05-03 16:37:39.182  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/base.py", line 383, in idle 05-03 16:37:39.182  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/base.py", line 334, in dispatch_input 05-03 16:37:39.182  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/base.py", line 263, in post_dispatch_input 05-03 16:37:39.183  6567 30025 I python  :    File "kivy/_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch 05-03 16:37:39.183  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/core/window/__init__.py", line 1709, in on_motion 05-03 16:37:39.183  6567 30025 I python  :    File "kivy/_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch 05-03 16:37:39.183  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/core/window/__init__.py", line 1726, in on_touch_down 05-03 16:37:39.184  6567 30025 I python  :    File "kivy/_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch 05-03 16:37:39.184  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/uix/screenmanager.py", line 1210, in on_touch_down 05-03 16:37:39.184  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/uix/widget.py", line 589, in on_touch_down 05-03 16:37:39.184  6567 30025 I python  :    File "kivy/_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch 05-03 16:37:39.184  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/uix/relativelayout.py", line 306, in on_touch_down 05-03 16:37:39.185  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/uix/widget.py", line 589, in on_touch_down 05-03 16:37:39.185  6567 30025 I python  :    File "kivy/_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch 05-03 16:37:39.185  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/uix/widget.py", line 589, in on_touch_down 05-03 16:37:39.185  6567 30025 I python  :    File "kivy/_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch 05-03 16:37:39.185  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/platform/build-arm64-v8a/build/python-installs/myapp/arm64-v8a/kivy/uix/behaviors/button.py", line 151, in on_touch_down 05-03 16:37:39.186  6567 30025 I python  :    File "kivy/_event.pyx", line 727, in kivy._event.EventDispatcher.dispatch 05-03 16:37:39.186  6567 30025 I python  :    File "kivy/_event.pyx", line 1307, in kivy._event.EventObservers.dispatch 05-03 16:37:39.186  6567 30025 I python  :    File "kivy/_event.pyx", line 1231, in kivy._event.EventObservers._dispatch 05-03 16:37:39.186  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/app/main.py", line 139, in scan_and_analyze 05-03 16:37:39.186  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/app/main_model.py", line 13, in main 05-03 16:37:39.187  6567 30025 I python  :    File "/home/jr/Desktop/Kivy/main/.buildozer/android/app/model_tflite.py", line 71, in get_positions 05-03 16:37:39.187  6567 30025 I python  :  ValueError: not enough values to unpack (expected 4, got 1)

python android kivy yolo tflite
1个回答
0
投票

好的,我解决了。应该是这样的:

for i in indices.flatten():
    if scores[i] >= confidence_threshold:
        x_center, y_center, width, height = boxes_xywh[i]
        

问题在于数组是二维的,而不是一维的。至于为什么在VSCode中运行良好,我不知道......

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