Error trying to train a cv::ml::SVMSGD: (-215:Assertion failed) responses.type() == CV_32F || responses.type() == CV_32S in function 'setData'

Hello.
I am trying to create a train Model but i am getting this error:
(-215:Assertion failed) responses.type() == CV_32F || responses.type() == CV_32S in function ‘setData’

#include <opencv2/highgui.hpp>
#include <string>
#include <opencv2/opencv.hpp>

#include "cv_idx.hpp"

std::string LABELS = "static/t10k-labels.idx1-ubyte";
std::string IMAGES = "static/t10k-images.idx3-ubyte";

std::string CHECK_LABELS = "static/train-labels.idx1-ubyte";
std::string CHECK_IMAGES = "static/train-images.idx3-ubyte";
/**
 * Notes:
 *	[ML Intro](https://docs.opencv.org/master/dc/dd6/ml_intro.html)
 *	[Example SVM](https://docs.opencv.org/4.5.3/d1/d73/tutorial_introduction_to_svm.html)
 */

int main(void)
{
	// Cargar las etiquetas
	CV_idx idx_labels(LABELS);
	// Carga las imágenes
	CV_idx images{IMAGES};

	// Pasarlos a espacio CV
	cv::Mat labelsMat((int)idx_labels.number_of_elements, 1, CV_8UC1, static_cast<uint8_t*>(idx_labels));
	auto imagesMat = static_cast<std::vector<cv::Mat> >(images);


	// Train the Stochastic Gradient Descent SVM
	cv::Mat trainModelData;
	trainModelData.convertTo(trainModelData, CV_32F);
	for(auto image: imagesMat) {
		cv::Mat normal;
		image.convertTo(normal, CV_32F, 1.0/255.0);
		cv::Mat image_row((int)normal.total(), 1,  CV_32F);
		if(!normal.isContinuous()){assert(0);}
		image_row.data = normal.data;
		trainModelData.push_back(image_row);
	}

	cv::Ptr<cv::ml::SVMSGD> svmsgd = cv::ml::SVMSGD::create();
	svmsgd->train(trainModelData, cv::ml::ROW_SAMPLE, labelsMat);

	// Print the images
	/*CV_idx check_labels{LABELS};
	CV_idx check_images{IMAGES};
	cv::Mat check_labelsMat((int)idx_labels.number_of_elements, 1, CV_8UC1, static_cast<uint8_t*>(check_labels));
	auto check_imagesMat = static_cast<std::vector<cv::Mat> >(check_images);
	namedWindow("Display digit", cv::WINDOW_AUTOSIZE);
	for(auto image: check_imagesMat) {
		cv::Mat normal;
		image.convertTo(normal, CV_32F, 1/255.0);
		normal.reshape(1, normal.total());
		std::cout << svmsgd->predict(normal, responses) << std::endl;
		imshow("Display digit", image);
		cv::waitKey(0);
	}*/

}

i already checked that the images are correct and can be displayed with imshow
and i have a row for every image in the dataset ¿someone can help me to understand why this happen?
Best regards

wrong data type.

make sure it satisfies the assertion.

wth is CV_idx ?

SVMSGD expects integer(classification) or float(regression) labels,
yours are uchar, that’s the error.

are you aware, that SVMSGD is strictly 2-class / binary ?
(data path looks like mnist chars, so that would not work as-is
if you really want to classify mnist digits, try with a plain cv::ml::SVM first)

1 Like

Hi, that was the error, incorrect label type thanks
Probably you have reason with your advice but for now I am following a book “hand-on machine learning with etc.” and use that in the 2nd chapter i am simply trying to move from python to C++ the chapters.
Do you know some good book for that topic?
Best regards