gpt4 book ai didi

java - OpenCV 错误 : Bad argument (The array of layer neuron counters must be an integer vector)

转载 作者:太空宇宙 更新时间:2023-11-03 21:54:24 25 4
gpt4 key购买 nike

此代码是关于使用 com.googlecode.javacv.cpp 训练神经网络当我制作 .create 方法时出现错误

static void train (CvMat TrainData, CvMat classes, int nlayers,int numOfClasses)
{
CvANN_MLP C=new CvANN_MLP();
CvMat layerSizes = cvCreateMat(1, 3, CV_32FC1);
layerSizes.put(0, 0, TrainData.cols());//put or rows or ???
layerSizes.put(0, 1, nlayers);
layerSizes.put(0, 2, numOfClasses);
double alpha=1.0;
double beta=1.0;

C.create(layerSizes, CvANN_MLP.SIGMOID_SYM, alpha,beta );//there is an error here

//OpenCV Error: Bad argument (The array of layer neuron counters must be an integer vector) in//CvANN_MLP::create, file ......\src\opencv\modules\ml\src\ann_mlp .cpp,第 230 行

    //Prepare trainClasses
//Create a mat with n trained data by m classes

CvMat trainClasses = null;
trainClasses.create(TrainData.rows(), numOfClasses, opencv_core.CV_32FC1);
for( int i = 0; i < trainClasses.rows(); i++ )
{
for( int k = 0; k < trainClasses.cols(); k++ )
{
int [] ClassesNumber =new int[1];
//If class of data i is same than a k class
classes.get(i, k );

if(k == ClassesNumber[0])
{
trainClasses.put(i, k);
}

else{
trainClasses.put(i, k);

}

}
}

CvMat weights = cvCreateMat( 1, TrainData.rows(), opencv_core.CV_32FC1 );
cvSet(weights, CvScalar.ONE);

//Scler 1111111111
//Learn classifier
C.train(TrainData, trainClasses, weights,null,new CvANN_MLP_TrainParams(), 0);

//trained=true;

}

最佳答案

您必须找到确切的问题,但错误是因为您试图将非整数数量的神经元放入层中。神经网络在每一层中使用离散数量的神经元。也许您不小心分配了 0 个神经元或负数的神经元?确保在调用方法或构造函数时以正确的顺序放置参数。

您告诉训练方法您有 0 个类要输出。因此,您试图将 0 个神经元放在一个层中。您需要确定您期望的输出数量,并且它必须大于零。

if(x>0)
C.train(TrainData, trainClasses, weights,null,new CvANN_MLP_TrainParams(), x);
else
//it won't work

此外,为什么要将 6 个参数传递给具有 4 个参数的方法?...您不能展示您的方法 - 或者您真的不知道这段代码在做什么。在这种情况下你不应该使用它。像下面这样使用它:

layerSizes.put(0, 0, #inputs);     
layerSizes.put(0, 1, #hidden neurons);
layerSizes.put(0, 2, #outputs);

关于java - OpenCV 错误 : Bad argument (The array of layer neuron counters must be an integer vector),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17276548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com