python opencv DTree模型:train()以std :: length_error终止

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

我正在使用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个回答
0
投票

看起来像DTree,需要显式配置-如果执行print(model_dtree.getMaxDepth()),它将返回2147483647。当您明确设置深度时,脚本将成功运行:

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 some very high, nonsensical value
model_dtree.setMaxDepth(10) # set it to something reasonable
model_dtree.train(samples=samples, layout=cv2.ml.ROW_SAMPLE, responses=labels)
print('finished')
© www.soinside.com 2019 - 2024. All rights reserved.