简体   繁体   English

使用OpenCV在ANN中笨拙的CvMat *

[英]Unwieldy CvMat* in ANN using OpenCV

I'm trying to use OpenCV to train a neural network in C++. 我正在尝试使用OpenCV在C ++中训练神经网络。

I can't convert between cv::Mat* (or Mat*, if namespace cv is used) to CvMat*, and I would appreciate some help with this. 我无法在cv :: Mat *(或Mat *,如果使用命名空间cv的情况下)之间转换为CvMat *,在此我将提供一些帮助。


Let me elaborate: 让我详细说明:

I've got two data structures of cv::Mat* type. 我有两个cv :: Mat *类型的数据结构。 The first is the set of feature vectors and the second is the set of expected output. 第一个是特征向量集,第二个是期望输出集。

   cv::Mat *feat = new cv::Mat(3000, 100, CV_32F, featureData);
   cv::Mat *op = new cv::Mat(3000, 2, CV_32F, expectedOutput);

(These are 3000 data points of feature vector length = 100 and output state = 2) (这些是特征向量长度= 100且输出状态= 2的3000个数据点)

These two matrices had been populated with data of correct dimensions and seem to be working fine when sample data were printed on the console. 这两个矩阵已经填充了正确尺寸的数据,并且当在控制台上打印示例数据时,它们似乎工作正常。

The neural network has been initialized as: 神经网络已初始化为:

   int layers_array[] = {100,200,2};    //hidden layer nodes = 200

   CvMat* layer = cvCreateMatHeader(1, 3, CV_32SC1); 
   cvInitMatHeader(layer, 1,3,CV_32SC1, layers_array);

   CvANN_MLP nnetwork;
   nnetwork.create(layer, CvANN_MLP::SIGMOID_SYM, SIGMOID_ALPHA, SIGMOID_BETA);

Now, the train method of ANN is of the following template: 现在,ANN的训练方法具有以下模板:

   virtual int train( const CvMat* inputs, const CvMat* outputs,
                       const CvMat* sampleWeights, const CvMat* sampleIdx=0,
                       CvANN_MLP_TrainParams params = CvANN_MLP_TrainParams(),
                       int flags=0 );

I tried to convert between cv::Mat * and CvMat * using the following code: 我尝试使用以下代码在cv :: Mat *和CvMat *之间转换:

   CvMat featMat,opMat;

   (&featMat)->cols = feat->cols;
   (&featMat)->rows = feat->rows;
   (&featMat)->type = CV_32F;
   (&featMat)->data.fl = (float *)feat->data;


   (&opMat)->cols = op->cols;
   (&opMat)->rows = op->rows;
   (&opMat)->type = CV_32F;
   (&opMat)->data.fl = (float *)op->data;

   //setting up the ANN training parameters

   int iterations = network.train(&featMat, &opMat, NULL, NULL, trainingParams);

When I run this code, I get the following error message in my console: 当我运行此代码时,在控制台中收到以下错误消息:

**OpenCV Error: Bad argument (input training data should be a floating-point matrix withthe number of rows equal to the number of training samples and the number
of columns equal to the size of 0-th (input) layer) in CvANN_MLP::prepare_to_train, file ..\..\OpenCV-2.3.0-win-src\OpenCV-2.3.0\modules\ml\src\ann_mlp.cpp, 
line 694**

I understand the error message. 我了解错误消息。 However, to the best of my knowledge, I believe I haven't made a mess of the number of nodes in the input/output layer. 但是,据我所知,我相信我并没有弄乱输入/输出层中的节点数量。

Can you please help me understand what is going wrong? 您能帮我了解问题出在哪里吗?

please try to avoid pointers to cv::Mat as well as CvMat* in general. 请尽量避免使用指向cv :: Mat以及CvMat *的指针。

luckily, there's an overload to CvANN_MLP::train that takes cv::Mat as args, so use that instead: 幸运的是, CvANN_MLP :: train有一个重载 ,将cv :: Mat当作args,所以改用它:

   cv::Mat feat = cv::Mat(3000, 100, CV_32F, featureData);
   cv::Mat op = cv::Mat(3000, 2, CV_32F, expectedOutput);

   int layers_array[] = {100,200,2};    //hidden layer nodes = 200
   cv::Mat layers = cv::Mat (3, 1, CV_32SC1, layers_array );

   CvANN_MLP nnetwork;
   nnetwork.create(layers, CvANN_MLP::SIGMOID_SYM, SIGMOID_ALPHA, SIGMOID_BETA);

   int iterations = nnetwork.train(feat, op, cv::Mat(), cv::Mat(), CvANN_MLP_TrainParams());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM