# Boost training issue: vector subscript out of range

**URL:** <https://forum.opencv.org/t/boost-training-issue-vector-subscript-out-of-range/3737>\
**Category:** C++\
**Tags:** ml\
**Created:** [June 7, 2021, 7:40pm UTC](https://forum.opencv.org/t/boost-training-issue-vector-subscript-out-of-range/3737 "2021-06-07T19:40:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dporto](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dporto](https://forum.opencv.org/u/dporto)\
**Post date:** [June 7, 2021, 7:40pm UTC](https://forum.opencv.org/t/boost-training-issue-vector-subscript-out-of-range/3737/1 "2021-06-07T19:40:48Z")

</div>

I am having trouble with training a Boosted trees model for regression. I am using the code below to train the model, but get a runtime error for “vector subscript out of range”

```auto
Ptr<TrainData> trainingData = TrainData::create(dataset_opencv.trainData, ROW_SAMPLE, dataset_opencv.trainLabels);
Ptr<Boost> Mdl = Boost::create();
Mdl->setWeakCount(200);
Mdl->setBoostType(cv::ml::Boost::GENTLE);
Mdl->setUseSurrogates(false);
Mdl->setMaxCategories(200);
Mdl->setRegressionAccuracy(0.05);
Mdl->train(trainingData);

```

My data and responses are cv::Mats with the exact same number of rows. The training data is 25 columns, the labels are 1 column, and both are CV\_32F. Using the same training data for training a Random Forest model is running with no issue. Is there something I’m doing wrong in setting the parameters for Boosted Trees model?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 7, 2021, 10:56pm UTC](https://forum.opencv.org/t/boost-training-issue-vector-subscript-out-of-range/3737/2 "2021-06-07T22:56:05Z")

</div>

that’s the exact and complete error message? there’s more info and it’s useful.

perhaps this helps; I don’t understand a thing: [Vector subscript out of range. On a boosting procedure. · Issue #8384 · opencv/opencv · GitHub](https://github.com/opencv/opencv/issues/8384)

---

<div class="post-metadata">

**Author:** ![dporto](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dporto](https://forum.opencv.org/u/dporto)\
**Post date:** [June 8, 2021, 8:24pm UTC](https://forum.opencv.org/t/boost-training-issue-vector-subscript-out-of-range/3737/3 "2021-06-08T20:24:09Z")

</div>

Yes, it appears I am having a similar issue to that post. However, I’m not sure how the issue was resolved here. Perhaps it what would be helpful to know is how to appropriately set the variable type to numerical in cv::ml::TrainData()?

The exact error message I am getting is the following:

Debug Assertion Failed!  
Program: …\_RegressionEngine\References\OpenCV\x64\bin\opencv\_ml412d.dll  
File: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.22.27905\include\vector  
Line: 1465  
Expression: vector subscript out of range

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 8, 2021, 10:44pm UTC](https://forum.opencv.org/t/boost-training-issue-vector-subscript-out-of-range/3737/4 "2021-06-08T22:44:21Z")

</div>

I see that you run OpenCV v4.1.2. you should update to latest or very close. chasing bugs in old versions is a waste of time. they might have been fixed a long time ago.

the assertion is fairly useless because it comes from within C++ STL and it doesn’t say where in user code (OpenCV) it happens.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 9, 2021, 6:34am UTC](https://forum.opencv.org/t/boost-training-issue-vector-subscript-out-of-range/3737/5 "2021-06-09T06:34:10Z")

</div>

> [@dporto](#):
>
> how to appropriately set the variable type to numerical in cv::ml::TrainData()?

if you use float labels/responses, it will try to perform regression, with integer ones a classification (that’s how it got “solved” in the mentioned issue above)

(unfortunately, if you _want_ to do a regression, CV\_32F is the correct type)

and btw, adding a few lines for synthetic data, like:

```auto
Mat d(100,25,CV_32F);
Mat l(100,1,CV_32F);
randu(d,0,1);
randu(d,0,1);
Ptr<TrainData> trainingData = TrainData::create(d, ROW_SAMPLE, l);

```

to your example i get:

```auto
OpenCV(4.5.2-dev) Error: Assertion failed (std::abs(w->ord_responses[si]) == 1) in updateWeightsAndTrim, file C:\p\opencv\modules\ml\src\boost.cpp, line 270

```

so, updating seems a good idea (at least, we’re on the same level, then…)

[edit]  
changing `ml::Boost::GENTLE` to `ml::Boost::DISCRETE` fixed the error here.

---

<div class="post-metadata">

**Author:** ![saltbayfour](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@saltbayfour](https://forum.opencv.org/u/saltbayfour)\
**Post date:** [June 18, 2021, 2:53pm UTC](https://forum.opencv.org/t/boost-training-issue-vector-subscript-out-of-range/3737/6 "2021-06-18T14:53:15Z")

</div>

You only get that error in debug builds because the Microsoft compiler only includes that check in debug builds. The problem still exists in a Release build.

If `world.rank()` is zero, `unsorted_dist_list` will be an empty vector when you use it in `unsorted_dist_list[j]` . At a minimum, you should add [.](https://jfi.cam/jiofilocalhtml/) [.](https://forpc.onl/)

```auto
unsorted_dist_list.resize(world.size());

```

before the `for (size_t j = 0;` loop.

If `world.rank()` is not zero, `unsorted_dist_list` will be empty when passed to `mpi::scatter` .
