【发布时间】:2015-06-05 03:35:37
【问题描述】:
我已经成功训练了一个神经网络来识别图像中的数字,并将网络参数保存到一个 .xml 文件中。
但是,当针对新图像测试网络时,代码在 predict() 阶段失败并出现错误:
OpenCV 错误:CvANN_MLP::predict 文件中的错误参数(输入和输出都必须是相同类型且具有相同行数的浮点矩阵)........\opencv\modules \ml\src\ann_mlp.cpp,第 279 行。
ann_mlp.cpp 第 279 行是:
if( !CV_IS_MAT(_inputs) || !CV_IS_MAT(_outputs) ||
!CV_ARE_TYPES_EQ(_inputs,_outputs) ||
(CV_MAT_TYPE(_inputs->type) != CV_32FC1 &&
CV_MAT_TYPE(_inputs->type) != CV_64FC1) ||
_inputs->rows != _outputs->rows )
CV_Error( CV_StsBadArg, "Both input and output must be floating-point matrices "
"of the same type and have the same number of rows" );
我通过运行以下代码检查了输入行:
cv::Size s = newVec.size();
int rows = s.height;
int cols = s.width;
cout << "newVec dimensions: " << rows << " x " << cols << endl;
...它带有预期的 1 x 900 向量/矩阵。
我已根据错误对话框将输入和输出矩阵设置为 CV_32FC1,如下所示:
输入矩阵
cv::Mat newVec(1, 900, CV_32FC1);
newVec = crop_img.reshape(0, 1); //reshape / unroll image to vector
CvMat n = newVec;
newVec = cv::Mat(&n);
输出矩阵
cv::Mat classOut = cvCreateMatHeader(1, CLASSES, CV_32FC1);
我尝试像这样运行预测功能:
CvANN_MLP* nnetwork = new CvANN_MLP;
nnetwork->load("nnetwork.xml", "nnetwork");
int maxIndex = 0;
cv::Mat classOut = cvCreateMatHeader(1, CLASSES, CV_32FC1);
//prediction
nnetwork->predict(newVec, classOut);
float value;
float maxValue = classOut.at<float>(0, 0);
for (int index = 1; index<CLASSES; index++)
{
value = classOut.at<float>(0, index);
if (value>maxValue)
{
maxValue = value;
maxIndex = index;
}
}
有什么想法吗?非常感谢...
【问题讨论】:
-
非常类似于stackoverflow.com/questions/29370807/…,但输入不同。
标签: c++ opencv neural-network