0

I'm using opencv-python 4.2.0.32 and Python 3.7.4.

When I call train() on a DTree model, my program terminates with an error:

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

The code works when I use a KNearest model instead of DTree. This looks a bit like the behaviour described here but I use a more recent version of OpenCV so maybe there's something else going on?

Code sample to reproduce behaviour:

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)
DennisLi
  • 3,915
  • 6
  • 30
  • 66
Paschen
  • 36
  • 2

1 Answers1

0

Looks like DTrees requires explicit configuration - if you do print(model_dtree.getMaxDepth()), it returns 2147483647, the maximum value for a 32-bit signed integer (https://en.wikipedia.org/wiki/2,147,483,647). When you explicitly set the depth, the script runs successfully:

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!')
danielcahall
  • 2,672
  • 8
  • 14
  • Thanks for your reply! Setting the max depth is a good point, but even with this, train() still causes a core dump on my systems. Just to be sure, I tried it on two systems, one using opencv-python 4.2.0.32 and Python 3.7.4, the other a raspberry pi with opencv 4.0.0 and python 3.5. I compiled opencv myself on the pi. – Paschen Apr 15 '20 at 11:56