• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C++ TrainingSet类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: TEST

TEST(NaiveBayesTest, test2) {
   enum Bird {
    SMALL,
    MIDDLE,
    BIG
  }; 
  TrainingSet<Bird, 2> trainingSet;
  
  // weight, height
  std::array<double, 2> s1 = {2, 10};
  std::array<double, 2> s2 = {2.3, 12};
  trainingSet.add(SMALL, s1);
  trainingSet.add(SMALL, s2);
  
  std::array<double, 2> m1 = {4, 15};
  std::array<double, 2> m2 = {4.7, 17.2};
  trainingSet.add(MIDDLE, m1);
  trainingSet.add(MIDDLE, m2);
  
  std::array<double, 2> b1 = {7, 23};
  std::array<double, 2> b2 = {8.5, 22.5};
  trainingSet.add(BIG, b1);
  trainingSet.add(BIG, b2);
  
  NaiveBayesClasifier<Bird, 2> clasifier;
  EXPECT_TRUE(clasifier.train(trainingSet));
  
  std::array<double, 2> sample1 = {1.5, 9};
  std::array<double, 2> sample2 = {4.9, 16};
  std::array<double, 2> sample3 = {9, 20};
  
  EXPECT_EQ(clasifier.clasify(sample1), SMALL);
  EXPECT_EQ(clasifier.clasify(sample2), MIDDLE);
  EXPECT_EQ(clasifier.clasify(sample3), BIG);
}
开发者ID:BogdanCojocar,项目名称:Naive-Bayes-Clasifier,代码行数:35,代码来源:TestNaiveBayes.cpp


示例2:

bool TrainingSet::operator!=(const TrainingSet &trset)
{
	if(trset.getInputs() == inputs && trset.getTargets() == targets){
		return false;
	}
	return true;
}
开发者ID:marlncpe,项目名称:INSYDE,代码行数:7,代码来源:trainingset.cpp


示例3: left_set

void	ClassificationTree::print_train_log(const TreeNode::PtrSplitNodeBase split, const TrainingSet &train_set) const
{
	MatType				ltype	=	train_set.get_label_type();
	MatType				ftype	=	train_set.get_feature_type();
	int					rows	=	(int)ltype.total();
	cv::Mat_<double>	left_tmp;
	cv::Mat_<double>	right_tmp;
	TrainingSet			left_set(ftype, ltype);
	TrainingSet			right_set(ftype, ltype);

	split->operator()(train_set, left_set, right_set);

	
	left_set.compute_target_mean(left_tmp);
	right_set.compute_target_mean(right_tmp);

	cv::Mat_<double>	left_dist(rows, 1, (double*)left_tmp.data);
	cv::Mat_<double>	right_dist(rows, 1, (double*)right_tmp.data);


	printf("left dist\n");
	for (unsigned ii = 0; ii < left_dist.total(); ++ii) {
		printf("\tlabel%d:%f\n", ii, left_dist.at<double>(ii) / left_set.size());
	}

	printf("right dist\n");
	for (unsigned ii = 0; ii < right_dist.total(); ++ii) {
		printf("\tlabel%d:%f\n", ii, right_dist.at<double>(ii) / right_set.size());
	}

}
开发者ID:mrthat,项目名称:cvpr,代码行数:31,代码来源:ClassificationTree.cpp


示例4: CalculateBP

float CalculateBP(const char * reffn, const char * trainfn, int refNum, int ngram, int bleuType)
{
    TrainingSet * ts = new TrainingSet();
    if( bleuType < 3 )
        ts->LoadRefData(reffn, refNum, ngram, MAX_SENT_NUM);
    ts->LoadTrainingData(trainfn, false);
    float BP = trainer->GetBP(ts, ngram, (BLEU_TYPE)bleuType, 0);
    delete ts;
    return BP;
}
开发者ID:initial-d,项目名称:smt_server,代码行数:10,代码来源:Interface.cpp


示例5: positives

LogLikelihood::LogLikelihood(TrainingSet &positives,TrainingSet &negatives)
  : positives(positives),
    negatives(negatives),
    numPos(positives.numCases()),
    numNeg(negatives.numCases()),
    numAttributes(positives.getSchema().numAttributes()),
    func(positives.getSchema().numAttributes()),
    ok(true)
{
}
开发者ID:bmajoros,项目名称:BioMaLL,代码行数:10,代码来源:LogLikelihood.C


示例6: MERTraining

bool MERTraining(const char * reffn, const char * trainfn, const char * configfn, int ngram, int bleuType)
{
    TrainingSet * ts = new TrainingSet();
    if( bleuType < 3 )
        ts->LoadRefData(reffn, 4, ngram, MAX_SENT_NUM);
    ts->LoadTrainingData(trainfn, false);
    trainer->LoadPara(configfn);
    trainer->OptimzieWeightsWithMERT(ts, ngram, (BLEU_TYPE)bleuType, 0);
    delete ts;
    return true;
}
开发者ID:initial-d,项目名称:smt_server,代码行数:11,代码来源:Interface.cpp


示例7:

void	ClassificationTree::print_train_log(const TreeNode::PtrLeafNodeBase leaf, const TrainingSet &train_set) const
{
	cv::Mat_<double>	label_dist;

	train_set.compute_target_mean(label_dist);

	printf("leaf dist\n");

	for (unsigned ii = 0; ii < label_dist.total(); ++ii) {
		printf("\tlabel%d:%f\n", ii, label_dist.at<double>(ii) / std::max<double>((double)train_set.size(), 1.0));
	}
}
开发者ID:mrthat,项目名称:cvpr,代码行数:12,代码来源:ClassificationTree.cpp


示例8: _initTrain

void CLTreeTrainer<ImgType, nChannels, FeatType, FeatDim, nClasses>::train(
  Tree<FeatType, FeatDim, nClasses> &tree,
  const TrainingSet<ImgType, nChannels> &trainingSet,
  const TreeTrainerParameters<FeatType, FeatDim> &params,
  unsigned int startDepth, unsigned int endDepth)
{
  /** \todo support a starting depth different from 1 */
  if (startDepth!=1) throw "Starting depth must be equal to 1";

  _initTrain(tree, trainingSet, params, startDepth, endDepth);
  

  for (unsigned int currDepth=startDepth; currDepth<endDepth; currDepth++)
  {
    boost::chrono::steady_clock::time_point perLevelTrainStart = 
      boost::chrono::steady_clock::now(); 

    unsigned int frontierSize = _initFrontier(tree, params, currDepth);
    unsigned int nSlices = _initHistogram(params);

    
    if (nSlices>1)
    {
      BOOST_LOG_TRIVIAL(info) << "Maximum allowed global histogram size reached: split in "
			      << nSlices << " slices";
    }
    

    // Flag all images as to-be-skipped: the flag will be set to false if at least one
    // image pixel is processed
    std::fill_n(m_toSkipTsImg, trainingSet.getImages().size(), true);

    for (unsigned int i=0; i<nSlices; i++)
    {
      _traverseTrainingSet(trainingSet, params, currDepth, i);
      _learnBestFeatThr(tree, params, currDepth, i);
    }

    // Update skipped images flags
    std::copy(m_toSkipTsImg, m_toSkipTsImg+trainingSet.getImages().size(), m_skippedTsImg);
  

    boost::chrono::duration<double> perLevelTrainTime =
      boost::chrono::duration_cast<boost::chrono::duration<double> >(boost::chrono::steady_clock::now() - 
								   perLevelTrainStart);
    
    BOOST_LOG_TRIVIAL(info) << "Depth " << currDepth << " trained in "
			    << perLevelTrainTime.count() << " seconds";
    
  }

  _cleanTrain();
}
开发者ID:mUogoro,项目名称:padenti,代码行数:53,代码来源:cl_tree_trainer_impl.hpp


示例9: InitFoldSets

void DataSet::InitFoldSets(TrainingSet &ts, ValidationSet &vs, int fold)
{
	int vStart = nSamples*(fold - 1) / nFolds;
	int vEnd = nSamples*fold / nFolds;
	ts.Init(nSamples - (vEnd - vStart), nFeatures);
	vs.Init((vEnd - vStart), nFeatures);
	for (int i = 0; i < nSamples; i++)
	{
		if (i >= vStart&&i<vEnd)
			vs.PushSample(X[i], Y[i]);
		else
			ts.PushSample(X[i], Y[i]);
	}
}
开发者ID:felipesfaria,项目名称:FariaTcc,代码行数:14,代码来源:DataSet.cpp


示例10: entrenamiento

void PruebaPantalla::on_DotMatrixRepresentationButton_clicked()
{
	QString openDir = QFileDialog::getOpenFileName(this, //widget
												  "Abrir conjunto de entrenamiento", //caption
												  "/home/edixon/programacion/INSYDE//samples/TrainingSets", //dir
												  "Conjunto de entrenamiento (*.tsf)", //filter
												  new QString("Conjunto de entrenamiento (*.tsf)"));

	if(openDir == "") return;

	TrainingSet *ts = new TrainingSet(openDir);

	DotMatrixRepresentation *dmr = new DotMatrixRepresentation(ts->getInputs()[0]);

	dmr->show();
}
开发者ID:,项目名称:,代码行数:16,代码来源:


示例11: build

void Apta::build(TrainingSet trainingSet, bool useWhiteNodes)
{
    if (useWhiteNodes) {
        this->_useWhiteNodes = true;
    }

    // Start with the root of APTA colored red
    this->_addNode(true, this->_rootId, "", "", '\0');

    for (pair<string, bool> sample : trainingSet.get()) {
        this->_addPath(this->_rootId, sample.first,
            sample.second ? Apta::ACCEPTED : Apta::REJECTED);
    }
}
开发者ID:grammarhoard,项目名称:2014-rosu-inference-cpp,代码行数:14,代码来源:Apta.cpp


示例12: main

int main(int argc, char *argv[]) {
	QApplication a(argc, argv);

	TrainingSet input;
	input.AddInput(red);
	input.AddInput(green);
	input.AddInput(dk_green);
	input.AddInput(blue);
	input.AddInput(dk_blue);
	input.AddInput(yellow);
	input.AddInput(orange);
	input.AddInput(purple);
	input.AddInput(black);
	input.AddInput(white);

	std::vector<float> vCol(3);
	int w1 = 40;
	int w2 = 4;

	SOMNetGPU gpu;
	gpu.CreateSOM(3, 1, w1,w1);
	gpu.SetTrainingSet(input);
	
	SetFcn(&ownFn);
	gpu.SetDistFunction(ownFn);
	// or just: SetFcn(gpu.GetDistFunction() );

	gpu.Training(1000);

	SOMReader w(w1, w1, w2);
	for(int x = 0; x < w1*w1; x++) {
		SOMNeuron *pNeur = (SOMNeuron*)((SOMLayer*)gpu.GetOPLayer())->GetNeuron(x);
		vCol[0] = pNeur->GetConI(0)->GetValue();
		vCol[1] = pNeur->GetConI(1)->GetValue();
		vCol[2] = pNeur->GetConI(2)->GetValue();

		w.SetField(QPoint(pNeur->GetPosition()[0], pNeur->GetPosition()[1]), vCol );
	}
	w.Save("SimpFnExtByGPU.png");
	return 0;
}
开发者ID:AtnesNess,项目名称:annetgpgpu,代码行数:41,代码来源:SimpFnExtGPU.cpp


示例13: test

void NeuralNetwork::test(TrainingSet &testSet)
{
	vector<InputImage *>* data = testSet.getData();

	int numCorrect = 0;
	for (vector<InputImage *>::iterator testImage = data->begin(); testImage != data->end(); ++testImage)
	{
		Mat *trainingImageMat = (*testImage)->getImage();
		vector<int> *actualLabel = (*testImage)->getLabelVector();

		// Get V
		Mat V = parameters * (*trainingImageMat);

		// Compute prediction
		vector<float> predictions(LABEL_SIZE);
		predictHelper(V, predictions);

		// Find max for prediction
		float max = 0;
		int maxInd = 0;
		int count = 0;
		for (vector<float>::iterator it = predictions.begin(); it != predictions.end(); ++it)
		{
			if (*it > max)
			{
				max = *it;
				maxInd = count;
			}
			count++;
		}

		char predictedChar = InputImage::oneHotIndexToChar(maxInd);
		cout << "Predicted: " << predictedChar << " | Actual: " << (*testImage)->getCharLabel() << endl;
		if (tolower(predictedChar) == tolower((*testImage)->getCharLabel()))
		{
			numCorrect++;
		}
	}

	float percentCorrect = ((float)numCorrect / (float)data->size()) * 100;
	cout << "Percent correct: " << (int)percentCorrect << "%" << endl;
}
开发者ID:utat-uav,项目名称:cv16-interface,代码行数:42,代码来源:NeuralNetwork.cpp


示例14: train

void NeuralNetwork::train(TrainingSet &trainingSet)
{
	vector<InputImage *>* data = trainingSet.getData();

	vector<float> G;

	// Repeat until convergence
	bool hasConverged = false;
	int count = 0;
	float avgCrossEntropy = 100;
	time_t timer;
	time(&timer);
	int k = 0;
	while (!hasConverged)
	{
		if (count > MIN_TRAIN_TIME)
		{
			hasConverged = true;
			break;
		}
		count++;

		if (count % 5 == 0)
		{
			cout << count << "th cycle with " << avgCrossEntropy << " avg cross entropy" << endl;
			cout << difftime(time(0), timer) << " seconds elapsed" << endl;
		}

		// Reset average crossentropy
		avgCrossEntropy = 0;

		// Get predictions
		vector<vector<float>> allPredictions;
		vector<InputImage *> inputImages;
		for (int m = k; m < k + BATCH_SIZE; ++m)
		{
			int ind = m % data->size();

			Mat *trainingImageMat = data->at(ind)->getImage();
			vector<int> *actualLabel = data->at(ind)->getLabelVector();

			// Get V
			Mat V = parameters * (*trainingImageMat);

			// Compute prediction
			vector<float> predictions(LABEL_SIZE);
			predictHelper(V, predictions);

			avgCrossEntropy -= (logf(predictions[data->at(ind)->getLabelIndex()]));

			allPredictions.push_back(predictions);
			inputImages.push_back(data->at(ind));
		}

		// Update parameters
		for (int i = 0; i < parameters.rows; ++i)
		{
			for (int j = 0; j < parameters.cols; ++j)
			{
				float grad = 0;
#pragma omp parallel for reduction(+:grad)
				for (int p = 0; p < BATCH_SIZE; p++)
				{
					grad += inputImages.at(p)->getImage()->at<float>(j, 0) * (inputImages.at(p)->getLabelVector()->at(i) - allPredictions[p][i]);
				}

				parameters.at<float>(i, j) += TRAINING_STEP * grad;
			}
		}

		// Average the cross entropy
		avgCrossEntropy /= BATCH_SIZE;

		k += BATCH_SIZE;
	}

	// Save to file
	ofstream nnsave;
	nnsave.open("savednn.txt");
	for (int i = 0; i < parameters.rows; ++i)
	{
		for (int j = 0; j < parameters.cols; ++j)
		{
			nnsave << parameters.at<float>(i, j) << "\t";
		}
		nnsave << endl;
	}
	nnsave << endl;
	nnsave.close();

	//cout << parameters << endl;
}
开发者ID:utat-uav,项目名称:cv16-interface,代码行数:92,代码来源:NeuralNetwork.cpp


示例15: TrainNeuralNetwork

void BackPropagation::TrainNeuralNetwork(LayeredFeedForwardNeuralNet& networkToTrain, const TrainingSet& trainingSet) const
{
    long trainingIterations = 0;
    double cumulativeNetworkError = DBL_MAX;
    TrainingSet trainingSetCopy = trainingSet;
    
    // get activation derivative function for delta rule
    std::shared_ptr<IUnaryExpressionParser> pExpressionParser = UnaryExpressionParserFactory::CreateDerivativeParser();
    UnaryFunction activationDerivative = pExpressionParser->GetFunctionForExpression(networkToTrain.GetActivationFunction());
    
    while (cumulativeNetworkError > m_targetNetworkError && trainingIterations < m_iterationLimit)
    {
        std::cout << "Enet = " << cumulativeNetworkError << std::endl;
        
        // reset network error for new training set iteration.
        cumulativeNetworkError = 0.0;
        
        // begin a new training cycle: put exemplars in random order
        std::random_shuffle(trainingSetCopy.begin(), trainingSetCopy.end());
        
        for (const Exemplar& exemplar : trainingSetCopy)
        {
            // fire the neural network and record activations at each layer
            std::vector<VectorXd> layerActivations;
            layerActivations.push_back(exemplar.first);
            for (long layerIndex = 1; layerIndex < networkToTrain.GetLayerCount(); layerIndex++)
            {
                layerActivations.push_back(
                    networkToTrain.FireSingleLayer(layerActivations[layerIndex - 1], layerIndex)
                );
            }
            
            // deque of errors on each layer (so we can add in reverse order)
            std::deque<VectorXd> layerErrors;
            
            // iterate over the layers in reverse order (back propagating), calculating errors.
            // reverse order because error in below layers is dependent on error of above layers.
            for (long layerIndex = networkToTrain.GetLayerCount() - 1; layerIndex > 0; layerIndex--)
            {
                VectorXd currentLayerError; // what we're trying to calculate
                const VectorXd& currentLayerActivation = layerActivations[layerIndex];
                
                if (layerIndex == networkToTrain.GetLayerCount() - 1)
                {
                    // this is the output layer's error, which is calculated against the known exemplar expected output
                    // Eo = (Do - Yo)Yo([1_0..1_n] - Yo)    for sigmoid (we use generalised delta rule and derivative of activation fn)
                    const VectorXd& expectedOutputPattern = exemplar.second;
                    currentLayerError = (expectedOutputPattern - currentLayerActivation) * currentLayerActivation.unaryExpr(activationDerivative);
                } else {
                    // this is a hidden layer error vector, which is calculated against the above layer's error and input weights.
                    // Ehy = Yh(1 - Yh)Wi^T.Eo    for sigmoid (we use generalised delta rule and derivative of activation fn)
                    MatrixXd aboveLayerInputWeights = networkToTrain.GetLayerInputWeights(layerIndex + 1);
                    const VectorXd& aboveLayerError = layerErrors.front();
                    // when calculating hidden layer errors we don't care about bias weights for the input weights of the above layer.
                    // this is because the "error of the bias unit" in a hidden layer is not used to calculate changes in weights below. so get rid of these to simplify calculation.
                    MatrixXd aboveLayerInputWeightsMinusBias = aboveLayerInputWeights.leftCols(aboveLayerInputWeights.cols() - 1);
                    // note we use cwiseProduct because we want to multiply elements of weighted error vector against deriative of current layer activations.
                    currentLayerError = (aboveLayerInputWeightsMinusBias.transpose() * aboveLayerError).cwiseProduct(currentLayerActivation.unaryExpr(activationDerivative));
                }
                layerErrors.push_front(currentLayerError);
            }
            // push a dummy 0 error to error deque so error/activation stl vector elements line up.
            layerErrors.push_front(VectorXd::Zero(exemplar.first.size()));
            
            // next we need to iterate over errors for each layer (excluding dummy input layer), calculating change in input weights.
            for (long layerIndex = 1; layerIndex < networkToTrain.GetLayerCount(); layerIndex++)
            {
                // get weight matrix to adjust
                MatrixXd weightsToAdjust = networkToTrain.GetLayerInputWeights(layerIndex);
                
                // get previous layer's activations (plus bias value)
                VectorXd previousLayerActivationPlusBias(weightsToAdjust.cols());
                previousLayerActivationPlusBias << layerActivations[layerIndex - 1], VectorXd::Constant(1, -1.0);
                
                // calculate change in weights ΔW = η Yh^T . Eo (where . is outer product)
                MatrixXd layerInputWeightsDelta = (layerErrors[layerIndex] * previousLayerActivationPlusBias.transpose()) * m_learningRate;
                
                // update neural net weights
                weightsToAdjust += layerInputWeightsDelta;
                networkToTrain.SetLayerInputWeights(weightsToAdjust, layerIndex);
                
                std::cout << "Weights for layer " << layerIndex << " are now:" << std::endl;
                std::cout << weightsToAdjust << std::endl;
            }
            
            // ok now update the cumulative network error.
            // this is (expected - actual activations) normalised, squared and then halved.
            cumulativeNetworkError += (exemplar.second - layerActivations.back()).squaredNorm() / 2;
            
        } // end for training-set-iteration
        trainingIterations++;
    } // target reached (or iteration limit exceeded). end training.
    
    if (trainingIterations == m_iterationLimit)
    {
        std::cout << "Iteration limit reached - optimisation did not converge on a global minimum." << std::endl;
    } else {
        std::cout << "Target network error reached after " << trainingIterations << " training set iterations." << std::endl;
    }
}
开发者ID:philmccarthy24,项目名称:FFNeuralNet,代码行数:100,代码来源:BackPropagation.cpp


示例16: WinMain

//*******************************************************************
// WinMain - Neural main
//
// parameters:
//             hInstance     - The instance of this instance of this
//                             application.
//             hPrevInstance - The instance of the previous instance
//                             of this application. This will be 0
//                             if this is the first instance.
//             lpszCmdLine   - A long pointer to the command line that
//                             started this application.
//             cmdShow       - Indicates how the window is to be shown
//                             initially. ie. SW_SHOWNORMAL, SW_HIDE,
//                             SW_MIMIMIZE.
//
// returns:
//             wParam from last message.
//
//*******************************************************************
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
						 LPSTR lpszCmdLine, int cmdShow)
{


/*
	Declarations of local variables
*/

	int control_file_number = -1 ;           // Stack pointer for control files
	FILE *control_files[MAX_CONTROL_FILES] ; // This is the stack

	char *control_line ;    // User's commands here
	char *command, *rest ;  // Pointers to its command and parameter parts
	int n_command, n_rest ; // Lengths of those parts

	int net_model = -1 ;     // Network model (see NETMOD_? in CONST.H)
	int out_model = -1 ;     // Output model (see OUTMOD_? in CONST.H)
	int n_inputs = -1 ;      // Number of input neurons
	int n_outputs = -1 ;     // Number of output neurons
	int n_hidden1 = -1 ;     // Number of hidden layer one neurons
	int n_hidden2 = -1 ;     // Ditto layer 2 (0 if just one hidden layer)


	TrainingSet *tset = NULL ;            // Training set here
	Network *network = NULL ;             // Network here
	struct LearnParams learn_params ;     // General learning parameters
	struct AnnealParams anneal_params ;   // Simulated annealing parameters
	struct GenInitParams geninit_params ; // Genetic initialization parameters
	struct KohParams koh_params ;         // Kohonen parameters

	int classif_output = -1 ;  // Current class (0=reject) for classif training
	char out_file[80] = "" ;   // File for EXECUTE output
	float threshold ;         // CLASSIFY confusion reject cutoff
	char resp_file[80]="";     // file for initializing output neuron's name
	char train_file[80]="";
/*
	Miscellaneous variables
*/

	int i, n, m ;
	float p ;
	char *msg ;
	FILE *fp ;
	unsigned long me,mc;
	char *fname;
	char *control;

#if VERSION_16_BIT
	if (sizeof(int) > 2) {
		printf ( "\nRecompile with VERSION_16_BIT set to 0 in CONST.H" ) ;
		exit ( 1 ) ;
		}
#else
	if (sizeof(int) < 4) {
		printf ( "\nRecompile with VERSION_16_BIT set to 1 in CONST.H" ) ;
		exit ( 1 ) ;
		}
#endif


printf ( "\nNEURAL SYSTEM - Program to train and test neural networks" ) ;

if (argc>1)
{
  strcpy(fname,argv[1]);
}


/*
   Process command line parameters
*/

   mem_name[0] = 0 ;  // Default is no memory allocation file
 /*
   if (strlen ( mem_name )) {
      strcat ( mem_name , ":mem.log" ) ;
      fp = fopen ( mem_name , "wt" ) ;
      if (fp == NULL) {
	 printf ( "\nCannot open debugging file %s", mem_name ) ;
	 exit ( 1 ) ;
//.........这里部分代码省略.........
开发者ID:amirna2,项目名称:fingerprints,代码行数:101,代码来源:WINMAIN.CPP


示例17:

void CLTreeTrainer<ImgType, nChannels, FeatType, FeatDim, nClasses>::_initTrain(
  Tree<FeatType, FeatDim, nClasses> &tree,
  const TrainingSet<ImgType, nChannels> &trainingSet,
  const TreeTrainerParameters<FeatType, FeatDim> &params,
  unsigned int startDepth, unsigned int endDepth)
{
  unsigned int nNodes = (2<<(endDepth-1))-1;
  cl_int errCode;

  // Init OpenCL tree buffers and load corresponding data
  m_clTreeLeftChildBuff = cl::Buffer(m_clContext,
				     CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,
				     nNodes*sizeof(cl_uint),
				     (void*)tree.getLeftChildren());
  m_clTreeFeaturesBuff = cl::Buffer(m_clContext,
				    CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,
				    nNodes*sizeof(FeatType)*FeatDim,
				    (void*)tree.getFeatures());
  m_clTreeThrsBuff = cl::Buffer(m_clContext,
				CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,
				nNodes*sizeof(FeatType),
				(void*)tree.getThresholds());
  m_clTreePosteriorsBuff = cl::Buffer(m_clContext,
				      CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,
				      nNodes*sizeof(cl_float)*nClasses,
				      (void*)tree.getPosteriors());

  // Init per-node total and per-class number of samples
  m_perNodeTotSamples = new unsigned int[nNodes];
  m_perClassTotSamples = new unsigned int[nNodes*nClasses];
  std::fill_n(m_perNodeTotSamples, nNodes, 0);
  std::fill_n(m_perClassTotSamples, nNodes*nClasses, 0);

  // Init to-skip flags for training set images
  m_toSkipTsImg = new bool[trainingSet.getImages().size()];
  m_skippedTsImg = new bool[trainingSet.getImages().size()];
  std::fill_n(m_skippedTsImg, trainingSet.getImages().size(), false);

  // Init OpenCL training set image buffer:
  // - first of all, iterate through the training set and find the maximum
  //   image width/height
  m_maxTsImgWidth=0;
  m_maxTsImgHeight=0;
  m_maxTsImgSamples=0;
  m_perNodeTotSamples[0] = 0;
  const std::vector<TrainingSetImage<ImgType, nChannels> > &tsImages = trainingSet.getImages();
  for (typename std::vector<TrainingSetImage<ImgType, nChannels> >::const_iterator it=tsImages.begin();
       it!=tsImages.end(); ++it)
  {
    const TrainingSetImage<ImgType, nChannels> &currImage=*it;
    
    if (currImage.getWidth()>m_maxTsImgWidth) m_maxTsImgWidth=currImage.getWidth();
    if (currImage.getHeight()>m_maxTsImgHeight) m_maxTsImgHeight=currImage.getHeight();

    // Get maximum number of sampled pixels as well
    if (currImage.getNSamples()>m_maxTsImgSamples) m_maxTsImgSamples=currImage.getNSamples();

    /** \todo update here total number of pixel per class at root node */
    m_perNodeTotSamples[0]+=currImage.getNSamples();
  }

  // Make the maximum width and height a multiple of the, respectively, work-group x and y
  // dimension
  m_maxTsImgWidth += (m_maxTsImgWidth%WG_WIDTH) ? WG_WIDTH-(m_maxTsImgWidth%WG_WIDTH) : 0;
  m_maxTsImgHeight += (m_maxTsImgHeight%WG_HEIGHT) ? WG_HEIGHT-(m_maxTsImgHeight%WG_HEIGHT) : 0;

  // - initialize OpenCL images
  cl::size_t<3> origin, region;
  size_t rowPitch;
  origin[0]=0; origin[1]=0; origin[2]=0;
  region[0]=m_maxTsImgWidth; region[1]=m_maxTsImgHeight;
  region[2]= (nChannels<=4) ? 1 : nChannels;

  cl::ImageFormat clTsImgFormat;
  ImgTypeTrait<ImgType, nChannels>::toCLImgFmt(clTsImgFormat);
  if (nChannels<=4)
  {
    m_clTsImg1 = new cl::Image2D(m_clContext, CL_MEM_READ_ONLY, clTsImgFormat,
				 m_maxTsImgWidth, m_maxTsImgHeight);
    m_clTsImg2 = new cl::Image2D(m_clContext, CL_MEM_READ_ONLY, clTsImgFormat,
				 m_maxTsImgWidth, m_maxTsImgHeight);
  }
  else
  {
    m_clTsImg1 = new cl::Image3D(m_clContext, CL_MEM_READ_ONLY, clTsImgFormat,
				 m_maxTsImgWidth, m_maxTsImgHeight, nChannels);
    m_clTsImg2 = new cl::Image3D(m_clContext, CL_MEM_READ_ONLY, clTsImgFormat,
				 m_maxTsImgWidth, m_maxTsImgHeight, nChannels);
  }
  m_clTsImgPinn = cl::Buffer(m_clContext,
			     CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR,
			     m_maxTsImgWidth*m_maxTsImgHeight*nChannels*sizeof(ImgType)*2);
  m_clTsImgPinnPtr = 
    reinterpret_cast<ImgType*>(m_clQueue1.enqueueMapBuffer(m_clTsImgPinn, CL_TRUE,
							   CL_MAP_WRITE,
							   0, m_maxTsImgWidth*m_maxTsImgHeight*nChannels*sizeof(ImgType)*2));

  clTsImgFormat.image_channel_order = CL_R;
  clTsImgFormat.image_channel_data_type = CL_UNSIGNED_INT8;
  region[2] = 1;
//.........这里部分代码省略.........
开发者ID:Banus,项目名称:padenti,代码行数:101,代码来源:cl_tree_trainer_impl_init.hpp


示例18: TrainingSetFile

TrainingSetFile::TSFResult TrainingSetFile::fromFile(QFile &file)
{
	QString
			version,
			text;

	QStringRef name;

	QXmlStreamReader tsReadXML;

	QXmlStreamReader::TokenType tt;
	QStringList textElements;
	QXmlStreamAttributes attributes;

	TrainingSetFile *retTSF = new TrainingSetFile();
	TSFResult res = {retTSF, true, NoError, "", 0};

	TrainingSet *ts = retTSF->getTrainingSet();

	int
			lastPatternIndex = 0,
			sTextElements,
			pSize = 0,
			iSize = 0,
			tSize = 0;

	Normalization
			*inor = new Normalization(),
			*tnor = new Normalization();

	vector<vector<double> >
			inputs,
			targets;

	DataRepresentation
			*idr = ts->getInputsDataRepresentation(),
			*tdr = ts->getTargetsDataRepresentation();

	if(file.open(QIODevice::ReadOnly)){
		tsReadXML.setDevice(&file);
		while (!tsReadXML.atEnd()) {
			tt = tsReadXML.readNext();

			if(tsReadXML.hasError()){
				file.close();
				return {retTSF, false, toTSFError(tsReadXML.error()), tsReadXML.errorString(), tsReadXML.lineNumber()};
			}

			if(tt == QXmlStreamReader::StartDocument){
				continue;
			}else if(tt == QXmlStreamReader::StartElement){
				name = tsReadXML.name();
				if(name == STR_TRAININGSET){
					attributes = tsReadXML.attributes();
					if(attributes.hasAttribute(STR_PATTERNSIZE) &&
					   attributes.hasAttribute(STR_INPUTSSIZE) &&
					   attributes.hasAttribute(STR_TARGETSSIZE))
					{
						pSize = attributes.value(STR_PATTERNSIZE).toInt();
						iSize = attributes.value(STR_INPUTSSIZE).toInt();
						tSize = attributes.value(STR_TARGETSSIZE).toInt();

						inputs = vector<vector<double> >(pSize, vector<double>(iSize, 0));
						targets = vector<vector<double> >(pSize, vector<double>(tSize, 0));
					}else{
						file.close();
						return {
							retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_PATTERNSIZE + ", " + STR_INPUTSSIZE + ", " + STR_TARGETSSIZE + ") on tag " + STR_TRAININGSET, tsReadXML.lineNumber()
						};
					}
				}else if(name == STR_PROPERTIES){
					attributes = tsReadXML.attributes();
					if(attributes.hasAttribute(STR_VERSION)){
						version = attributes.value(STR_VERSION).toString();
					}else{
						file.close();
						return
						{
							retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_VERSION + ") on tag " + STR_PROPERTIES, tsReadXML.lineNumber()
						};
					}
				}else if(name == STR_INPUTSDATAREPRESENTATION){
					attributes = tsReadXML.attributes();
					if(attributes.hasAttribute(STR_NAME) &&
					   attributes.hasAttribute(STR_WIDTH) &&
					   attributes.hasAttribute(STR_HEIGHT) &&
					   attributes.hasAttribute(STR_FORMAT))
					{
						idr->setType(drFromStrToInt(attributes.value(STR_NAME).toString()));
						idr->setWidth(attributes.value(STR_WIDTH).toInt());
						idr->setHeight(attributes.value(STR_HEIGHT).toInt());
						idr->setImageFormat(fromStrToImgFormat(attributes.value(STR_FORMAT).toString()));
					}else{
						file.close();
						return
						{
							retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_NAME + ", " + STR_WIDTH + ", " + STR_HEIGHT + ", " + STR_FORMAT + ") on tag " + STR_INPUTSDATAREPRESENTATION, tsReadXML.lineNumber()
						};
					}
				}else if(name == STR_TARGETSDATAREPRESENTATION){
//.........这里部分代码省略.........
开发者ID:,项目名称:,代码行数:101,代码来源:


示例19: makeFaceRecognizer

FaceRecognizer::FaceRecognizer(const TrainingSet& set, const double avgThRangeScale, const double maxThreshold):
  imgNorm_( set.normalizer() ),
  faceRecognizer_( makeFaceRecognizer() )
{
  if( set.entries().size() < 2 )
    throw Util::Exception{ UTIL_LOCSTRM << "training samples set needs to have at least 2 elements" };

  int                  nextFreeLabel = 0;
  std::vector<cv::Mat> faces;
  std::vector<int>     labels;
  std::vector<cv::Mat> testSet;
  // prepare space for the destination data
  faces.reserve( set.samples() );
  labels.reserve( set.samples() );
  labMap_.reserve( set.samples() );
  testSet.reserve( set.samples() );

  // prepare data in the format used by the learning algorithm
  for( const auto& e: set.entries() )
  {
    const std::string& name = e.first;
    const cv::Mat&     face = e.second;
    // find/create id to assign
    int id = -1;
    // initially this vector is ordered by string names
    const LabelMap::value_type searchValue(-1/*whatever*/, name);
    const auto                 swo = [](const LabelMap::value_type& lhs, const LabelMap::value_type& rhs) -> bool { return lhs.second < rhs.second; };
    const auto                 it  = std::lower_bound( labMap_.begin(), labMap_.end(), searchValue, swo );
    if( it==labMap_.end() || it->second!=name )
    {
      // assign new label
      id = nextFreeLabel;
      ++nextFreeLabel;
      labMap_.insert( it, LabelMap::value_type{id, name} );
      // first image from every class keep as a test one
      testSet.push_back(face);
      continue;
    }
    else
    {
      // use already assigned id
      assert( it->second==name );
      id = it->first;
    }
    // add to the containers
    faces.push_back(face);
    labels.push_back(id);
  }

  // learn data
  assert( faceRecognizer_.get() != nullptr );
  assert( faces.size() == labels.size() );
  //for(size_t i=0; i<faces.size(); ++i)
  faceRecognizer_->train(faces, labels);

  // set threshold according to what has been learned
  double thMin = 99999999999999;
  double thMax = 0;
  for(const auto& face: testSet)
  {
    int    label = -1;
    double dist  = -1;
    faceRecognizer_->predict(face, label, dist);
    if(dist<thMin)
      thMin = dist;
    if(dist>thMax)
      thMax = dist;
  }
  const double diff = thMax - thMin;
  threshold_ = thMin + diff * avgThRangeScale;
  if( threshold_ > maxThreshold )
    threshold_ = maxThreshold;

  // final version needs to be sorted by int ids, for easier search
  {
    const auto swo = [](const LabelMap::value_type& lhs, const LabelMap::value_type& rhs) -> bool { return lhs.first < rhs.first; };
    std::sort( labMap_.begin(), labMap_.end(), swo );
  }
  assert( labMap_.size() == static_cast<size_t>(nextFreeLabel) );
}
开发者ID:el-bart,项目名称:TIGER,代码行数:80,代码来源:FaceRecognizer.cpp


示例20: main

int main (
   int argc ,    // Number of command line arguments (includes prog name)
   char *argv[]  // Arguments (prog name is argv[0])
   )

{

/*
   Declarations of local variables
*/

/*
   User's command control line related variables are here.
   Control_file_number and control_files permit nesting of 'CONTROL' commands.
   If control_file_number equals -1, control commands are read from stdin.
   Otherwise they are read from that file in FILE *control_files.
   Up to MAX_CONTROL_FILES can be stacked.
*/

   int control_file_number = -1 ;           // Stack pointer for control files
   FILE *control_files[MAX_CONTROL_FILES] ; // This is the stack

   char *control_line ;    // User's commands here
   char *command, *rest ;  // Pointers to its command and parameter parts
   int n_command, n_rest ; // Lengths of those parts

/*
   These are network parameters which may be set by the user via commands.
   They are initialized to defaults which indicate that the user has not
   yet set them.  As they are set, their current values are placed here.
   When learning is done for a network, their values are copied from here
   into the network object.  When a network is read, the object's values
   are copied from it to here.  Otherwise, these variables are not used;
   the values in the network object itself are used.  The only purpose of
   these variables is to keep track of current values.
*/

   int net_model = -1 ;     // Network model (see NETMOD_? in CONST.H)
   int out_model = -1 ;     // Output model (see OUTMOD_? in CONST.H)
   int n_inputs = -1 ;      // Number of input neurons
   int n_outputs = -1 ;     // Number of output neurons
   int n_hidden1 = -1 ;     // Number of hidden layer one neurons
   int n_hidden2 = -1 ;     // Ditto layer 2 (0 if just one hidden layer)


   TrainingSet *tset = NULL ;            // Training set here
   Network *network = NULL ;             // Network here
   struct LearnParams learn_params ;     // General learning parameters
   struct AnnealParams anneal_params ;   // Simulated annealing parameters
   struct GenInitParams geninit_params ; // Genetic initialization parameters
   struct KohParams koh_params ;         // Kohonen parameters

   int classif_output = -1 ;  // Current class (0=reject) for classif training
   char out_file[80] = "" ;   // File for EXECUTE output
   double threshold ;         // CLASSIFY confusion reject cutoff

/*
   Miscellaneous variables
*/

   int i, n, m ;
   double p ;
   char *msg ;
   FILE *fp ;

/*
--------------------------------------------------------------------------------

   Program starts here.

   Verify that a careless user didn't fail to set the integer size
   correctly when compiling.

--------------------------------------------------------------------------------
*/

#if VERSION_16_BIT
   if (sizeof(int) > 2) {
      printf ( "\nRecompile with VERSION_16_BIT set to 0 in CONST.H" ) ;
      exit ( 1 ) ;
      }
#else
   if (sizeof(int) < 4) {
      printf ( "\nRecompile with VERSION_16_BIT set to 1 in CONST.H" ) ;
      exit ( 1 ) ;
      }
#endif

printf ( "\nNEURAL - Program to train and test neural networks" ) ;
printf("\nCopyright (c) 1993 by Academic Press, Inc.");
printf("\nAll rights reserved.  Permission is hereby granted, until further notice,");
printf("\nto make copies of this diskette, which are not for resale, provided these");
printf("\ncopies are made from this master diskette only, and provided that the");
printf("\nfollowing copyright notice appears on the diskette label:");
printf("\n(c) 1993 by Academic Press, Inc.");
printf("\nExcept as previously stated, no part of the computer program embodied in");
printf("\nthis diskette may be reproduced or transmitted in any form or by any means,");
printf("\nelectronic or mechanical, including input into storage in any information");
printf("\nsystem for resale, without permission in writing from the publisher.");
printf("\nProduced in the United States of America.");
//.........这里部分代码省略.........
开发者ID:abishekahluwaila,项目名称:read,

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ Traits类代码示例发布时间:2022-05-31
下一篇:
C++ Train类代码示例发布时间:2022-05-31
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap