【问题标题】:python opencv DTree model: train() terminates with std::length_errorpython opencv DTree 模型:train() 以 std::length_error 终止
【发布时间】:2020-04-10 02:56:40
【问题描述】:

我正在使用 opencv-python 4.2.0.32 和 Python 3.7.4。

当我在 DTree 模型上调用 train() 时,我的程序因错误而终止:

terminate called after throwing an instance of 'std::length_error' what(): vector::reserve Aborted (core dumped)

当我使用 KNearest 模型而不是 DTree 时,该代码有效。 这看起来有点像here 描述的行为,但我使用的是更新版本的 OpenCV,所以可能还有其他事情发生?

重现行为的代码示例:

import numpy as np
import cv2

samples = np.ndarray((5, 2), np.float32)
labels = np.zeros((5, 1), dtype=np.float32)

# This works
model_knn = cv2.ml.KNearest_create()
model_knn.train(samples=samples, layout=cv2.ml.ROW_SAMPLE, responses=labels)

# This terminates with an error
model_dtree = cv2.ml.DTrees_create()
model_dtree.train(samples=samples, layout=cv2.ml.ROW_SAMPLE, responses=labels)

【问题讨论】:

    标签: python opencv cv2


    【解决方案1】:

    看起来 DTrees 需要显式配置 - 如果您这样做 print(model_dtree.getMaxDepth()),它会返回 2147483647,这是 32 位有符号整数 (https://en.wikipedia.org/wiki/2,147,483,647) 的最大值。当您显式设置深度时,脚本运行成功:

    import numpy as np
    import cv2
    
    samples = np.ndarray((5, 2), np.float32)
    labels = np.zeros((5, 1), dtype=np.float32)
    
    print(samples)
    # This works
    model_knn = cv2.ml.KNearest_create()
    model_knn.train(samples=samples, layout=cv2.ml.ROW_SAMPLE, responses=labels)
    
    # This terminates with an error
    model_dtree = cv2.ml.DTrees_create()
    print(model_dtree.getMaxDepth()) # should be 2147483647, which we don't want
    model_dtree.setMaxDepth(10) # set it to something reasonable
    model_dtree.train(samples=samples, layout=cv2.ml.ROW_SAMPLE, responses=labels)
    print('finished training!')
    

    【讨论】:

    • 感谢您的回复!设置最大深度是一个好点,但即使这样,train() 仍然会在我的系统上导致核心转储。可以肯定的是,我在两个系统上进行了尝试,一个使用 opencv-python 4.2.0.32 和 Python 3.7.4,另一个使用 opencv 4.0.0 和 python 3.5 的树莓派。我自己在pi上编译了opencv。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多