如何在实现对象识别时解决“ValueError:Subshape must have connected start >= end because stride is negative”?

问题描述

我尝试实现以下代码

from imageai.Detection import ObjectDetection
detector = ObjectDetection()
model_path = "yolo-tiny.h5"
input_path = "passport_photo.png"
output_path = "new.jpg"
detector.setModelTypeAsTinyYOlov3()
detector.setModelPath(model_path)
detector.loadModel()
detection = detector.detectObjectsFromImage(input_image=input_path,output_image_path=output_path)
for eachItem in detection:
    print(eachItem["name"]," : ",eachItem["percentage_probability"])

但出现以下错误

ValueError: Subshape 必须计算 start >= end,因为 stride 是负数,但是是 0 和 2(从开始 0 和结束 9223372036854775807 计算,超过了 rank 2 和 stride-1 的形状)。

我尝试将 tensorflow v2.5.0 降级到 v2.4.0,因为我在某处读到它可能有帮助,但没有。 谁能给点建议?

解决方法

这似乎是因为 TensorFlow 2 对模型数据的类型做出了新的假设。我遇到了您遇到的确切问题,并按照 this comment 所说的做了,并且解决了问题,因此我至少可以继续前进。