当前位置: 首页>>代码示例>>C++>>正文


C++ CvANN_MLP类代码示例

本文整理汇总了C++中CvANN_MLP的典型用法代码示例。如果您正苦于以下问题:C++ CvANN_MLP类的具体用法?C++ CvANN_MLP怎么用?C++ CvANN_MLP使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了CvANN_MLP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: mlp

void mlp ( cv :: Mat & trainingData , cv :: Mat & trainingClasses , cv :: Mat & testData , cv :: Mat &testClasses ) {
    cv :: Mat layers = cv :: Mat (4 , 1 , CV_32SC1 ) ;
    layers . row (0) = cv :: Scalar (2) ;
    layers . row (1) = cv :: Scalar (10) ;
    layers . row (2) = cv :: Scalar (15) ;
    layers . row (3) = cv :: Scalar (1) ;

    CvANN_MLP mlp ;
    CvANN_MLP_TrainParams params;
    CvTermCriteria criteria ;
    criteria.max_iter = 100;
    criteria.epsilon = 0.00001f ;
    criteria.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS ;
    params.train_method = CvANN_MLP_TrainParams :: BACKPROP ;
    params.bp_dw_scale = 0.05f;
    params.bp_moment_scale = 0.05f;
    params.term_crit = criteria;
    mlp.create ( layers ) ;
    // train
    mlp.train ( trainingData , trainingClasses , cv :: Mat () , cv :: Mat () , params ) ;
    cv::Mat response (1 , 1 , CV_32FC1 ) ;
    cv::Mat predicted ( testClasses . rows , 1 , CV_32F ) ;
    for ( int i = 0; i < testData . rows ; i ++) {
        cv :: Mat response (1 , 1 , CV_32FC1 ) ;
        cv :: Mat sample = testData . row ( i ) ;
        mlp . predict ( sample , response ) ;
        predicted . at < float >( i ,0) = response . at < float >(0 ,0) ;
    }
    cout << " Accuracy_ { MLP } = " << evaluate ( predicted , testClasses ) << endl ;
    plot_binary ( testData , predicted , " Predictions Backpropagation " ) ;
}
开发者ID:yangfei0914,项目名称:road-sign-recognition-system,代码行数:31,代码来源:main.cpp

示例2: Predict_mlp

void Model::Predict_mlp( const SampleSet& samples, SampleSet& outError )
{
	
	int true_resp = 0;
	CvANN_MLP *model = (CvANN_MLP*)m_pModel;
	cv::Mat result;
	float temp[40];

	model->predict(samples.Samples(), result);

	for (int i = 0; i < samples.N(); i++)
	{
		float maxcol = -1;
		int index = -1;
		for (int j = 0; j < result.cols; j++)
		{
			if (result.at<float>(i,j) > maxcol)
			{
				maxcol = result.at<float>(i,j);
				index = j;
			}
 		}
		float label = samples.Classes()[index];
		if (label != samples.GetLabelAt(i))
		{
			outError.Add(samples.GetSampleAt(i), samples.GetLabelAt(i));
		}
		else
		{
			true_resp++;
		}
	}
	printf("%d %d",samples.N(), true_resp);
}
开发者ID:ElmerNing,项目名称:OpencvML,代码行数:34,代码来源:Model.cpp

示例3: train

void train(Mat TrainData, Mat classes, int nlayers){
    Mat layers(1,3,CV_32SC1);
    layers.at<int>(0)= TrainData.cols;
    layers.at<int>(1)= nlayers;
    layers.at<int>(2)= numberCharacters;
    ann.create(layers, CvANN_MLP::SIGMOID_SYM, 1, 1);
    
    //Prepare trainClases
    //Create a mat with n trained data by m classes
    Mat trainClasses;
    trainClasses.create( TrainData.rows, numberCharacters, CV_32FC1 );
    for( int i = 0; i <  trainClasses.rows; i++ )
    {
        for( int k = 0; k < trainClasses.cols; k++ )
        {
            //If class of data i is same than a k class
            if( k == classes.at<int>(i) )
                trainClasses.at<float>(i,k) = 1;
            else
                trainClasses.at<float>(i,k) = 0;
        }
    }
    Mat weights( 1, TrainData.rows, CV_32FC1, Scalar::all(1) );
    
    //Learn classifier
    ann.train( TrainData, trainClasses, weights );
}
开发者ID:DuLiCS,项目名称:Eye-gaze-ann-classify,代码行数:27,代码来源:main.cpp

示例4: Sample

void CImageProcess::ANN_Region()
{
	char path[512] = {0};
	float obj[MAX_TRAIN_COLS] = {0};
	Sample("./sources/0 (1).bmp", obj, MAX_TRAIN_COLS);

	CvANN_MLP bpANN;

	CvANN_MLP_TrainParams param;
	param.term_crit = cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,5000,0.01);
	param.train_method = CvANN_MLP_TrainParams::BACKPROP;
	param.bp_dw_scale = 0.1;
	param.bp_moment_scale = 0.1;

	Mat	layerSize = (Mat_<int>(1,3)<<MAX_TRAIN_COLS ,MAX_OBJ_COLS,MAX_OBJ_COLS);
	bpANN.create(layerSize, CvANN_MLP::SIGMOID_SYM);


	//m_bpANN.load("./sources/mlp.xml");

	Mat input(1, MAX_TRAIN_COLS, CV_32FC1, obj);
	float _obj[MAX_OBJ_COLS] ={0};
	Mat out(1, MAX_OBJ_COLS, CV_32FC1, _obj);
	//Mat out;
	bpANN.predict(input,out);

	int i=0;
	i+=i;

}
开发者ID:shengdewu,项目名称:repository,代码行数:30,代码来源:ImageProcess.cpp

示例5: predict

void predict(int nbSamples, int size)
{
	CvANN_MLP network;
	CvFileStorage* storage = cvOpenFileStorage( "data/neural_model.xml", 0, CV_STORAGE_READ);
	CvFileNode *n = cvGetFileNodeByName(storage, 0, "neural_model");
	network.read(storage, n);

	Mat toPredict(nbSamples, size * size, CV_32F);

	int label;
	float pixel;
	FILE *file = fopen("data/predict.txt", "r");

	for(int i=0; i < nbSamples; i++){

			for(int j=0; j < size * size; j++){

					// WHILE ITS PIXEL VALUE
					if(j < size * size){


						fscanf(file, "%f,", &pixel);
						toPredict.at<float>(i,j) = pixel;
					}
			}
	}
	fclose(file);

	Mat classOut(nbSamples, 62,CV_32F);

	network.predict(toPredict,classOut);

	float value;
	int maxIndex = 0;
	float maxValue;

	for(int k = 0; k < nbSamples; k++)
	{
		maxIndex = 0;
		maxValue = classOut.at<float>(0,0);
		for(int index=1;index<62;index++){

			value = classOut.at<float>(0,index);
			if(value>maxValue){

				maxValue = value;
				maxIndex = index;
			}
		}
	}

	cout<<"Index predicted : " << maxIndex + 1 << endl;

	cvReleaseFileStorage(&storage);
}
开发者ID:moktar84,项目名称:BigReco,代码行数:55,代码来源:predict.cpp

示例6: CheckCircle

int CheckCircle( Mat src)//Matとcircleの組を渡すこと
{
     //XMLを読み込んでニューラルネットワークの構築
    CvANN_MLP nnetwork;
    CvFileStorage* storage = cvOpenFileStorage( "param.xml", 0, CV_STORAGE_READ );
    CvFileNode *n = cvGetFileNodeByName(storage,0,"DigitOCR");
    nnetwork.read(storage,n);
    cvReleaseFileStorage(&storage);

        //特徴ベクトルの生成
        int index;
        float train[64];
        for(int i=0; i<64; i++) train[i] = 0;
        Mat norm(src.size(), src.type());
        Mat sample(src.size(), src.type());
        normalize(src, norm, 0, 255, NORM_MINMAX, CV_8UC3);
        
        for(int y=0; y<sample.rows; y++){
            for(int x=0; x<sample.cols; x++){
                index = y*sample.step+x*sample.elemSize();
                int color = (norm.data[index+0]/64)+
                    (norm.data[index+1]/64)*4+
                    (norm.data[index+2]/64)*16;
                train[color]+=1;
            }
        }
        int pixel = sample.cols * sample.rows;
        for(int i=0; i<64; i++){
            train[i] /= pixel;
        }

        //分類の実行
        Mat data(1, ATTRIBUTES, CV_32F);
        for(int col=0; col<ATTRIBUTES; col++){
            data.at<float>(0,col) = train[col];
        }
        int maxIndex = 0;
        Mat classOut(1,CLASSES,CV_32F);
        nnetwork.predict(data, 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;
            }
		}
		return maxIndex;
}
开发者ID:ZenkiKawakami,项目名称:KozeniChecker,代码行数:50,代码来源:main.cpp

示例7: classify_emotion

int classify_emotion(Mat& face, const char* ann_file, int tagonimg)
{
    int ret = 0;
    Mat output(1, OUTPUT_SIZE, CV_64FC1);
    Mat data(1, nn_input_size, CV_64FC1);
    CvANN_MLP nnetwork;
    nnetwork.load(ann_file, "facial_ann");

    vector<Point_<double> > points;
    vector<double> distances;
    if(!get_facial_points(face, points)) {
        return -1;
    }

    get_euler_distance_sets(points, distances);
    int j = 0;
    while(!distances.empty()) {
        data.at<double>(0,j) = distances.back();
        distances.pop_back();
        j++;
    }

    nnetwork.predict(data, output);

    /* Find the biggest value in the output vector, that is what we want. */
    double b = 0;
    int k = 1;
    for (j = 0; j < OUTPUT_SIZE; j++) {
        cout<<output.at<double>(0, j)<<" ";
        if (b < output.at<double>(0, j)) {
            b = output.at<double>(0, j);
            k = j + 1;
        }
    }

    /* Print the result on the image. */
    if (tagonimg) {
        putText(face, get_emotion(k), Point(30, 30), FONT_HERSHEY_SIMPLEX,
                0.7, Scalar(0, 255, 0), 2);
        draw_distance(face, points);
    }

    return k;
}
开发者ID:schue,项目名称:face-analysis-sdk,代码行数:44,代码来源:main.cpp

示例8: annTrain

void annTrain(Mat TrainData, Mat classes, int nNeruns) {
  ann.clear();
  Mat layers(1, 3, CV_32SC1);
  layers.at<int>(0) = TrainData.cols;
  layers.at<int>(1) = nNeruns;
  layers.at<int>(2) = numAll;
  ann.create(layers, CvANN_MLP::SIGMOID_SYM, 1, 1);

  // Prepare trainClases
  // Create a mat with n trained data by m classes
  Mat trainClasses;
  trainClasses.create(TrainData.rows, numAll, CV_32FC1);
  for (int i = 0; i < trainClasses.rows; i++) {
    for (int k = 0; k < trainClasses.cols; k++) {
      // If class of data i is same than a k class
      if (k == classes.at<int>(i))
        trainClasses.at<float>(i, k) = 1;
      else
        trainClasses.at<float>(i, k) = 0;
    }
  }
  Mat weights(1, TrainData.rows, CV_32FC1, Scalar::all(1));

  // Learn classifier
  // ann.train( TrainData, trainClasses, weights );

  // Setup the BPNetwork

  // Set up BPNetwork's parameters
  CvANN_MLP_TrainParams params;
  params.train_method = CvANN_MLP_TrainParams::BACKPROP;
  params.bp_dw_scale = 0.1;
  params.bp_moment_scale = 0.1;

  // params.train_method=CvANN_MLP_TrainParams::RPROP;
  // params.rp_dw0 = 0.1;
  // params.rp_dw_plus = 1.2;
  // params.rp_dw_minus = 0.5;
  // params.rp_dw_min = FLT_EPSILON;
  // params.rp_dw_max = 50.;

  ann.train(TrainData, trainClasses, Mat(), Mat(), params);
}
开发者ID:KingBing,项目名称:EasyPR,代码行数:43,代码来源:ann_train.cpp

示例9: Train_mlp

void Model::Train_mlp( const SampleSet& samples )
{
	CvANN_MLP* model = (CvANN_MLP*)m_pModel;
	CvANN_MLP_TrainParams* para = (CvANN_MLP_TrainParams*)m_trainPara;
	
	int dim = samples.Dim();
	vector<float> classes = samples.Classes();
	cv::Mat layerSize = (cv::Mat_<int>(1, 3) << dim, 100, classes.size());
	model->create(layerSize);

	cv::Mat newLaybels = cv::Mat::zeros(samples.N(), classes.size(), CV_32F);
	for (int n=0; n<samples.N(); n++)
	{
		int label = samples.GetLabelAt(n);
		for (int c=0; c<classes.size(); c++)
		{
			if (label == classes[c])
				newLaybels.at<float>(n, c) = 1.0f;
		}
	}
	model->train(samples.Samples(), newLaybels, cv::Mat::ones(samples.N(), 1, CV_32F), cv::Mat(), *para);
}
开发者ID:ElmerNing,项目名称:OpencvML,代码行数:22,代码来源:Model.cpp

示例10: testModel

void Training::testModel(string testPath,CvANN_MLP &neural_network){

	int success(0),fail(0);
		
	loadDataSet(testPath, testSet, testClassification, NB_TEST_SAMPLES);
	cout<<"Test set loaded"<<endl;
	cv::Mat classificationResult(1, CLASSES, CV_32F);
	Mat testSample;

	for(int i=0; i < NB_TEST_SAMPLES;i++){

		testSample = testSet.row(i);

		//predict
		neural_network.predict(testSample, classificationResult);

		int maxIndex = 0;
		float value = 0.0f;
		float maxValue = classificationResult.at<float>(0,0);
		for(int j=1; j<CLASSES;j++){
		
				value=classificationResult.at<float>(0,j);
				if(value>maxValue){
		
					maxValue = value;
					maxIndex = j;
				}
		}

		if(testClassification.at<float>(i,maxIndex)!=1.0f)
			fail++;
		else
			success++;

					
	}
			
		cout<<"Successfully classified : "<<success<<endl;
		cout<<"Wrongly classified ! "<<fail<<endl;
		cout<<"Succes % : "<<success * 100 / NB_TEST_SAMPLES<<endl;

}
开发者ID:moktar84,项目名称:BigReco,代码行数:42,代码来源:training.cpp

示例11: Parameters

//---------------------------------------------------------
bool COpenCV_NNet::On_Execute(void)
{
	//-------------------------------------------------
	bool					b_updateWeights, b_noInputScale, b_noOutputScale, b_NoData;
	int						i_matType, i_layers, i_maxIter, i_neurons, i_areasClassId, i_trainFeatTotalCount, *i_outputFeatureIdxs, i_outputFeatureCount, i_Grid, x, y, i_evalOut, i_winner;
	double					d_alpha, d_beta, d_eps;
	DATA_TYPE				e_dataType;
	TRAINING_METHOD			e_trainMet;
	ACTIVATION_FUNCTION		e_actFunc;
	CSG_Table				*t_Weights, *t_Indices, *t_TrainInput, *t_EvalInput, *t_EvalOutput;
	CSG_Parameter_Grid_List	*gl_TrainInputs;
	CSG_Grid				*g_EvalOutput, *g_EvalOutputCert;
	CSG_Shapes				*s_TrainInputAreas;
	CSG_Parameters			*p_TrainFeatures;
	TSG_Point				p;
	CvMat					*mat_Weights, *mat_Indices, **mat_data, *mat_neuralLayers, mat_layerSizesSub, *mat_EvalInput, *mat_EvalOutput;	// todo: mat_indices to respect input indices, mat_weights for initialization
	CvANN_MLP_TrainParams	tp_trainParams;
	CvANN_MLP				model;

	b_updateWeights		= Parameters("UPDATE_WEIGHTS"							)->asBool();
	b_noInputScale		= Parameters("NO_INPUT_SCALE"							)->asBool();
	b_noOutputScale		= Parameters("NO_OUTPUT_SCALE"							)->asBool();
	i_layers			= Parameters("NNET_LAYER"								)->asInt();
	i_neurons			= Parameters("NNET_NEURONS"								)->asInt();
	i_maxIter			= Parameters("MAX_ITER"									)->asInt();
	i_areasClassId		= Parameters("TRAIN_INPUT_AREAS_CLASS_FIELD"			)->asInt();
	e_dataType			= (DATA_TYPE)Parameters("DATA_TYPE"						)->asInt();
	e_trainMet			= (TRAINING_METHOD)Parameters("TRAINING_METHOD"			)->asInt();
	e_actFunc			= (ACTIVATION_FUNCTION)Parameters("ACTIVATION_FUNCTION"	)->asInt();
	d_alpha				= Parameters("ALPHA"									)->asDouble();
	d_beta				= Parameters("BETA"										)->asDouble();
	d_eps				= Parameters("EPSILON"									)->asDouble();
	t_Weights			= Parameters("WEIGHTS"									)->asTable();
	t_Indices			= Parameters("INDICES"									)->asTable();
	t_TrainInput		= Parameters("TRAIN_INPUT_TABLE"						)->asTable();
	t_EvalInput			= Parameters("EVAL_INPUT_TABLE"							)->asTable();
	t_EvalOutput		= Parameters("EVAL_OUTPUT_TABLE"						)->asTable();
	p_TrainFeatures		= Parameters("TRAIN_FEATURES_TABLE"						)->asParameters();
	gl_TrainInputs		= Parameters("TRAIN_INPUT_GRIDS"						)->asGridList();
	g_EvalOutput		= Parameters("EVAL_OUTPUT_GRID_CLASSES"					)->asGrid();
	g_EvalOutputCert	= Parameters("EVAL_OUTPUT_GRID_CERTAINTY"				)->asGrid();
	s_TrainInputAreas	= Parameters("TRAIN_INPUT_AREAS"						)->asShapes();

	// Fixed matrix type (TODO: Analyze what to do for other types of data (i.e. images))
	i_matType = CV_32FC1;

	//-------------------------------------------------
	if (e_dataType == TABLE)
	{	
		// We are working with TABLE data
		if( t_TrainInput->Get_Count() == 0 || p_TrainFeatures->Get_Count() == 0 )
		{
			Error_Set(_TL("Select an input table and at least one output feature!"));
			return( false );
		}

		// Count the total number of available features
		i_trainFeatTotalCount = t_TrainInput->Get_Field_Count();

		// Count the number of selected output features
		i_outputFeatureIdxs = (int *)SG_Calloc(i_trainFeatTotalCount, sizeof(int));
		i_outputFeatureCount = 0;
	
		for(int i=0; i<p_TrainFeatures->Get_Count(); i++)
		{
			if( p_TrainFeatures->Get_Parameter(i)->asBool() )
			{
				i_outputFeatureIdxs[i_outputFeatureCount++] = CSG_String(p_TrainFeatures->Get_Parameter(i)->Get_Identifier()).asInt();
			}
		}

		// Update the number of training features
		i_trainFeatTotalCount = i_trainFeatTotalCount-i_outputFeatureCount;

		if( i_outputFeatureCount <= 0 )
		{
			Error_Set(_TL("Select at least one output feature!"));
			return( false );
		}

		// Now convert the input and output training data into a OpenCV matrix objects
		mat_data = GetTrainAndOutputMatrix(t_TrainInput, i_matType, i_outputFeatureIdxs, i_outputFeatureCount);
	}
	else
	{
		// TODO: Add some grid validation logic
		i_trainFeatTotalCount = gl_TrainInputs->Get_Count();
		i_outputFeatureCount = s_TrainInputAreas->Get_Count();

		// Convert the data from the grid into the matrix from
		mat_data = GetTrainAndOutputMatrix(gl_TrainInputs, i_matType, s_TrainInputAreas, i_areasClassId, g_EvalOutput, g_EvalOutputCert);
	}

	//-------------------------------------------------
	// Add two additional layer to the network topology (0-th layer for input and the last as the output)
	i_layers = i_layers + 2;
	mat_neuralLayers = cvCreateMat(i_layers, 1, CV_32SC1);
	cvGetRows(mat_neuralLayers, &mat_layerSizesSub, 0, i_layers);
	
//.........这里部分代码省略.........
开发者ID:johanvdw,项目名称:saga-debian,代码行数:101,代码来源:opencv_nnet.cpp

示例12: trainMachine

// Read the training data and train the network.
void trainMachine()
{ int i; //The number of training samples. 
    int train_sample_count;

    //The training data matrix. 
    //Note that we are limiting the number of training data samples to 1000 here.
    //The data sample consists of two inputs and an output. That's why 3.
    float td[10000][3];

    //Read the training file
    /*
       A sample file contents(say we are training the network for generating 
       the mean given two numbers) would be:

       5
       12 16 14
       10 5  7.5
       8  10 9
       5  4  4.5
       12 6  9

     */
    FILE *fin;
    fin = fopen("train.txt", "r");

    //Get the number of samples.
    fscanf(fin, "%d", &train_sample_count);
    printf("Found training file with %d samples...\n", train_sample_count);

    //Create the matrices

    //Input data samples. Matrix of order (train_sample_count x 2)
    CvMat* trainData = cvCreateMat(train_sample_count, 2, CV_32FC1);

    //Output data samples. Matrix of order (train_sample_count x 1)
    CvMat* trainClasses = cvCreateMat(train_sample_count, 1, CV_32FC1);

    //The weight of each training data sample. We'll later set all to equal weights.
    CvMat* sampleWts = cvCreateMat(train_sample_count, 1, CV_32FC1);

    //The matrix representation of our ANN. We'll have four layers.
    CvMat* neuralLayers = cvCreateMat(4, 1, CV_32SC1);

    CvMat trainData1, trainClasses1, neuralLayers1, sampleWts1;

    cvGetRows(trainData, &trainData1, 0, train_sample_count);
    cvGetRows(trainClasses, &trainClasses1, 0, train_sample_count);
    cvGetRows(trainClasses, &trainClasses1, 0, train_sample_count);
    cvGetRows(sampleWts, &sampleWts1, 0, train_sample_count);
    cvGetRows(neuralLayers, &neuralLayers1, 0, 4);

    //Setting the number of neurons on each layer of the ANN
    /* 
       We have in Layer 1: 2 neurons (2 inputs)
       Layer 2: 3 neurons (hidden layer)
       Layer 3: 3 neurons (hidden layer)
       Layer 4: 1 neurons (1 output)
     */
    cvSet1D(&neuralLayers1, 0, cvScalar(2));
    cvSet1D(&neuralLayers1, 1, cvScalar(3));
    cvSet1D(&neuralLayers1, 2, cvScalar(3));
    cvSet1D(&neuralLayers1, 3, cvScalar(1));

    //Read and populate the samples.
    for (i=0;i<train_sample_count;i++)
        fscanf(fin,"%f %f %f",&td[i][0],&td[i][1],&td[i][2]);

    fclose(fin);

    //Assemble the ML training data.
    for (i=0; i<train_sample_count; i++)
    {
        //Input 1
        cvSetReal2D(&trainData1, i, 0, td[i][0]);
        //Input 2
        cvSetReal2D(&trainData1, i, 1, td[i][1]);
        //Output
        cvSet1D(&trainClasses1, i, cvScalar(td[i][2]));
        //Weight (setting everything to 1)
        cvSet1D(&sampleWts1, i, cvScalar(1));
    }

    //Create our ANN.
    machineBrain.create(neuralLayers);//sigmoid 0 0(激活函数的两个参数)

    //Train it with our data.   
    machineBrain.train(
        trainData,//输入
        trainClasses,//输出
        sampleWts,//输入项的权值
        0,
        CvANN_MLP_TrainParams(
            cvTermCriteria(
                CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,///类型 CV_TERMCRIT_ITER 和CV_TERMCRIT_EPS二值之一,或者二者的组合
                10000000,//最大迭代次数
                0.00000001//结果的精确性 两次迭代间权值变化量
                ),
            CvANN_MLP_TrainParams::BACKPROP,//BP算法
            0.01,//几个可显式调整的参数 学习速率 阿尔法
//.........这里部分代码省略.........
开发者ID:richard-liang,项目名称:machine-learning,代码行数:101,代码来源:ann.cpp

示例13: trainMachine

// Read the training data and train the network.
void trainMachine()
{
    int i;
    //The number of training samples. 
    int train_sample_count = 130;
    
    //The training data matrix. 
    float td[130][61];
    
    //Read the training file
    FILE *fin;
    fin = fopen("data/sonar_train.csv", "r");
    
    //Create the matrices    
    //Input data samples. Matrix of order (train_sample_count x 60)
    CvMat* trainData = cvCreateMat(train_sample_count, 60, CV_32FC1);
    
    //Output data samples. Matrix of order (train_sample_count x 1)
    CvMat* trainClasses = cvCreateMat(train_sample_count, 1, CV_32FC1);
    
    //The weight of each training data sample. We'll later set all to equal weights.
    CvMat* sampleWts = cvCreateMat(train_sample_count, 1, CV_32FC1);
    
    //The matrix representation of our ANN. We'll have four layers.
    CvMat* neuralLayers = cvCreateMat(4, 1, CV_32SC1);
    
    //Setting the number of neurons on each layer of the ANN
    /* 
     We have in Layer 1: 60 neurons (60 inputs)
     Layer 2: 150 neurons (hidden layer)
     Layer 3: 225 neurons (hidden layer)
     Layer 4: 1 neurons (1 output)
     */
    cvSet1D(neuralLayers, 0, cvScalar(60));
    cvSet1D(neuralLayers, 1, cvScalar(150));
    cvSet1D(neuralLayers, 2, cvScalar(225));
    cvSet1D(neuralLayers, 3, cvScalar(1));
    
    //Read and populate the samples.
    for (i=0;i<train_sample_count;i++)
        fscanf(fin,"%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f",
               &td[i][0],&td[i][1],&td[i][2],&td[i][3],&td[i][4],&td[i][5],&td[i][6],&td[i][7],&td[i][8],&td[i][9],&td[i][10],&td[i][11],&td[i][12],&td[i][13],&td[i][14],&td[i][15],&td[i][16],&td[i][17],&td[i][18],&td[i][19],&td[i][20],&td[i][21],&td[i][22],&td[i][23],&td[i][24],&td[i][25],&td[i][26],&td[i][27],&td[i][28],&td[i][29],&td[i][30],&td[i][31],&td[i][32],&td[i][33],&td[i][34],&td[i][35],&td[i][36],&td[i][37],&td[i][38],&td[i][39],&td[i][40],&td[i][41],&td[i][42],&td[i][43],&td[i][44],&td[i][45],&td[i][46],&td[i][47],&td[i][48],&td[i][49],&td[i][50],&td[i][51],&td[i][52],&td[i][53],&td[i][54],&td[i][55],&td[i][56],&td[i][57],&td[i][58],&td[i][59],&td[i][60]);
    
    //we are done reading the file, so close it
    fclose(fin);
    
    //Assemble the ML training data.
    for (i=0; i<train_sample_count; i++)
    {
        //inputs
        for (int j = 0; j < 60; j++) 
            cvSetReal2D(trainData, i, j, td[i][j]);
    
        //Output
        cvSet1D(trainClasses, i, cvScalar(td[i][60]));
        //Weight (setting everything to 1)
        cvSet1D(sampleWts, i, cvScalar(1));
    }
    
    //Create our ANN.
    ann.create(neuralLayers);
    cout << "training...\n";
    //Train it with our data.
    ann.train(
        trainData,
        trainClasses,
        sampleWts,
        0,
        CvANN_MLP_TrainParams(
            cvTermCriteria(
                CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,
                100000,
                0.000001),
            CvANN_MLP_TrainParams::BACKPROP,
            0.01,
            0.05));
}
开发者ID:micahlmartin,项目名称:Machine-Learning-Presentation,代码行数:78,代码来源:main.cpp

示例14: main

int main()
{
	const int sampleTypeCount = 7;				//共有几种字体
	const int sampleCount = 50;					//每种字体的样本数
	const int sampleAllCount = sampleCount*sampleTypeCount;
	const int featureCount = 256;				//特征维数
	CvANN_MLP bp;// = CvANN_MLP(layerSizes,CvANN_MLP::SIGMOID_SYM,1,1);


	string str_dir[sampleTypeCount];
	str_dir[0] = "A水滴渍";
	str_dir[1] = "B水纹";
	str_dir[2] = "C指纹";
	str_dir[3] = "D釉面凹凸";
	str_dir[4] = "X凹点";
	str_dir[5] = "Y杂质";
	str_dir[6] = "Z划痕";

	float trainingData[sampleAllCount][featureCount] = { 0 };
	float outputData[sampleAllCount][sampleTypeCount] = { 0 };

	int itemIndex = 0;
	for (int index = 0; index < 7; index++)
	{
		for (int i = 1; i <= 50; i++)
		{
			outputData[itemIndex][index] = 1;

			cout << str_dir[index] << "_" << i << endl;
			stringstream ss;
			char num[4];
			sprintf(num, "%03d", i);
			ss << "特征样本库\\" << str_dir[index] << "\\" << num << ".jpg";
			string path;
			ss >> path;
			//读取灰度图像以便计算灰度直方图
			cv::Mat f = cv::imread(path, 0);


			cv::Mat grayHist;

			// 设定bin数目,也就是灰度级别,这里选择的是0-255灰度
			int histSize = 256;


			//cv::equalizeHist(f, f);
			cv::normalize(f, f, histSize, 0, cv::NORM_MINMAX);
			//cv::bitwise_xor(f, cv::Scalar(255), f);//反相

			FeatureMaker::GetGrayHist(f, grayHist, histSize);

			for (int j = 0; j < 256; j++)
			{
				trainingData[itemIndex][j] = grayHist.ptr<float>(j)[0];
			}
			itemIndex++;
		}
	}

	//创建一个网络
	cv::Mat layerSizes = (cv::Mat_<int>(1, 3) << featureCount, 25, sampleTypeCount);//创建一个featureCount输入  IDC_EDIT_YinCangCount隐藏  sampleTypeCount输出的三层网络


	CvANN_MLP_TrainParams param;
	param.term_crit = cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 50000, 0.002);
	param.train_method = CvANN_MLP_TrainParams::BACKPROP;
	param.bp_dw_scale = 0.01;//权值更新率
	param.bp_moment_scale = 0.03;//权值更新冲量

	cv::Mat inputs(sampleAllCount, featureCount, CV_32FC1, trainingData);//样品总数,特征维数,储存的数据类型
	cv::Mat outputs(sampleAllCount, sampleTypeCount, CV_32FC1, outputData);

	bp.create(layerSizes, CvANN_MLP::SIGMOID_SYM);
	bp.train(inputs, outputs, cv::Mat(), cv::Mat(), param);
	bp.save("ANN_mlp.xml");

	itemIndex = 0;
	int zhengque = 0;
	for (int index = 0; index < 7; index++)
	{
		for (int i = 1; i <= 50; i++)
		{
			cv::Mat sampleMat(1, featureCount, CV_32FC1, trainingData[itemIndex]);//样品总数,特征维数,储存的数据类型
			cv::Mat nearest(1, sampleTypeCount, CV_32FC1, cv::Scalar(0));
			bp.predict(sampleMat, nearest);
			float possibility = -1;
			int outindex = 0;
			for (int i = 0; i < nearest.size().width; i++){
				float x = nearest.at<float>(0, i);
				if (x>possibility){
					possibility = x;
					outindex = i;
				}
			}
			if (outindex == index)
				zhengque++;
			cout << str_dir[index] << "_" << i << ":" << outindex << "->" << possibility << "->" << str_dir[outindex] << endl;
			itemIndex++;
		}
	}
//.........这里部分代码省略.........
开发者ID:harme199497,项目名称:cameraTest,代码行数:101,代码来源:FeatureFinder人工神经网络+直方图特征=0.977.cpp

示例15: cvTermCriteria

// Train
void *train(Mat &trainData, Mat &response)
{
	if(conf.classifier == "SVM")
 	{
	 	std::cout<<"--->SVM Training ..."<<std::endl;	
		CvSVMParams params;
		params.kernel_type = CvSVM::LINEAR;
		params.svm_type = CvSVM::C_SVC;
		params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,1000,1e-6);
		
		CvSVM *classifier = new CvSVM;
		classifier->train(trainData,response,Mat(),Mat(),params);

		int c = classifier->get_support_vector_count();
	 	printf("    %d support vectors founded.\n",c);
	 	return (void *)classifier;
	}
	else if(conf.classifier == "BP")
	{
		std::cout<<"--->BP Training ..."<<std::endl;
		
	  	// Data transforming.
		int numClass = (int)conf.classes.size();
	   	cv::Mat labelMat = Mat::zeros(response.rows, numClass, CV_32F);
		for(int i = 0;i<response.rows;i++)
		{
			int k = response.ptr<int>()[i];
			labelMat.ptr<float>(i)[k-1] = 1.0;
		}
			
		// Set up BP network parameters
		CvANN_MLP_TrainParams params;
		params.term_crit = cvTermCriteria( CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 1000, 0.01 ),
		params.train_method = CvANN_MLP_TrainParams::BACKPROP;  
		params.bp_dw_scale = 0.1;  
		params.bp_moment_scale = 0.1;  
		
	   	CvANN_MLP *classifier = new CvANN_MLP;
	 
	   	// Set up the topology structure of BP network.
	   	int inputNodeNum = trainData.cols;
		float ratio = 2;
	   	int hiddenNodeNum = static_cast<float>(inputNodeNum) * ratio;
	   	int outputNodeNum = numClass;
	   	int maxHiddenLayersNum = 1;
		
	   	// It could be better! esp. Output node amount
	   	printf("    %d input nodes, %d output nodes.\n",inputNodeNum, outputNodeNum);
	   	Mat layerSizes;
	   	layerSizes.push_back(inputNodeNum);
	   	printf("    Hidden layers nodes' amount:");
		layerSizes.push_back(hiddenNodeNum);
		
	   	for(int i = 0 ;i<maxHiddenLayersNum;i++)
	   	{
	   		if(hiddenNodeNum > outputNodeNum * ratio + 1 )
			{
				hiddenNodeNum = static_cast<float>(hiddenNodeNum) / ratio;
				layerSizes.push_back(hiddenNodeNum);
				printf(" %d ",hiddenNodeNum);
			}
			else 
				break;
	   	}
	   	printf("\n");
	   	layerSizes.push_back(outputNodeNum);
	   	
	   	classifier->create(layerSizes, CvANN_MLP::SIGMOID_SYM);
		classifier->train(trainData, labelMat, Mat(),Mat(), params); 
		return (void *)classifier;
	}
	else{
		std::cout<<"--->Error: wrong classifier."<<std::endl;
		return NULL;
	}	
}
开发者ID:Ernesto-73,项目名称:BOW_with_bounding_boxes,代码行数:77,代码来源:bow.cpp


注:本文中的CvANN_MLP类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。