Ejemplo n.º 1
0
void train (const ssi_char_t *dir, const ssi_char_t *model) {

	// load samples
	StringList files;
	FileTools::ReadFilesFromDir (files, dir, "*.wav");
	SampleList samples;	
	samples.addUserName ("user");

	for (ssi_size_t i = 0; i < files.size (); i++) {
		ssi_stream_t *stream = new ssi_stream_t;
		ssi_sample_t *sample = new ssi_sample_t;
		const ssi_char_t *filename = files.get (i);
	
		// parse class name
		FilePath fp (files.get(i));
		ssi_char_t *class_name = ssi_strcpy (fp.getName ());
		for (ssi_size_t j = 0; j < strlen (class_name); j++) {
			if (class_name[j] == '_') {
				class_name[j] = '\0';
				break;
			}
		}
		ssi_size_t class_id = samples.addClassName (class_name);
		delete[] class_name;

		// read wave file
		WavTools::ReadWavFile (filename, *stream);

		// create sample
		sample->class_id = class_id;
		sample->num = 1;
		sample->score = 1.0f;
		sample->streams = new ssi_stream_t *[1];
		sample->streams[0] = stream;
		sample->time = 0;
		sample->user_id = 0;				

		// add sample
		samples.addSample (sample);
	}

	// extract features
	SampleList samples_t;
	EmoVoiceFeat *ev_feat = ssi_create (EmoVoiceFeat, "ev_feat", true);
	ModelTools::TransformSampleList (samples, samples_t, *ev_feat);
	
	// create model
	IModel *bayes = ssi_create (NaiveBayes, "bayes", true);
	Trainer trainer (bayes);

	// evalulation
	Evaluation eval;
	eval.evalKFold (&trainer, samples_t, 10);
	eval.print ();

	// train & save
	trainer.train (samples_t);
	trainer.save (model);
}
Ejemplo n.º 2
0
bool Rank::train (ISamples &samples,
	ssi_size_t stream_index) {

	if (!_model) {
		ssi_wrn ("a model has not been set yet");
		return false;
	}

	release ();

	_n_scores = samples.getStream (stream_index).dim;	
	_scores = new score[_n_scores];

	Evaluation eval;
	Trainer trainer (_model, stream_index);
	
	SSI_DBG (SSI_LOG_LEVEL_DEBUG, "evaluate dimensions:");
	for (ssi_size_t ndim = 0; ndim < _n_scores; ndim++) {
		ISSelectDim samples_s (&samples);
		samples_s.setSelection (stream_index, 1, &ndim);
		if (_options.loo) {
			eval.evalLOO (&trainer, samples_s);
		} else if (_options.louo) {
			eval.evalLOUO (&trainer, samples_s);
		} else {
			eval.evalKFold (&trainer, samples_s, _options.kfold);
		}
		_scores[ndim].index = ndim;
		_scores[ndim].value = eval.get_classwise_prob ();
		SSI_DBG (SSI_LOG_LEVEL_DEBUG, "  #%02u -> %.2f", _scores[ndim].index, _scores[ndim].value);
	} 

	trainer.release ();

	return true;
}
Ejemplo n.º 3
0
bool ex_eval(void *arg) {

	ssi_size_t n_classes = 2;
	ssi_size_t n_samples = 20;
	ssi_size_t n_streams = 1;
	ssi_real_t train_distr[][3] = { 0.3f, 0.3f, 0.2f, 0.3f, 0.6f, 0.2f, 0.6f, 0.3f, 0.2f, 0.6f, 0.6f, 0.2f };
	ssi_real_t test_distr[][3] = { 0.5f, 0.5f, 0.5f };
	SampleList samples;		
	ModelTools::CreateTestSamples (samples, n_classes, n_samples, n_streams, train_distr);	
	ssi_char_t string[SSI_MAX_CHAR];	
	for (ssi_size_t n_class = 1; n_class < n_classes; n_class++) {
		ssi_sprint (string, "class%02d", n_class);
		samples.addClassName (string);
	}

	Evaluation eval;
	NaiveBayes *model = ssi_create (NaiveBayes, 0, true);
	Trainer trainer (model);
	trainer.train (samples);

	Evaluation2Latex e2latex;
	e2latex.open ("eval.tex");
	
	ssi_print_off ("devel set:\n");
	eval.eval (&trainer, samples);
	eval.print (ssiout);
	eval.print_result_vec ();

	e2latex.writeHead (eval, "caption", "label");
	e2latex.writeText ("results with different evaluation strategies", true);
	e2latex.writeEval ("devel", eval);
	
	ssi_print_off("k-fold:\n");
	eval.evalKFold (&trainer, samples, 3); 
	eval.print ();
	eval.print_result_vec ();

	e2latex.writeEval ("k-fold", eval);

	ssi_print_off("split:\n");
	eval.evalSplit (&trainer, samples, 0.5f); 
	eval.print ();
	eval.print_result_vec ();

	e2latex.writeEval ("split", eval);

	ssi_print_off("loo:\n");
	eval.evalLOO (&trainer, samples); 
	eval.print ();
	eval.print_result_vec ();

	e2latex.writeEval ("loo", eval);
	
	e2latex.writeTail ();
	e2latex.close ();

	FILE *fp = fopen("eval.csv", "w");
	eval.print(fp, Evaluation::PRINT::CSV_EX);
	fclose(fp);

	return true;
}