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
void ElanTools::LoadSampleList (SampleList &samples,
	ssi_size_t num,
	ssi_stream_t *streams[],
	ElanTier &elanTier,
	const ssi_char_t *user_name,
	bool useTierNameAsLabel) {

	// add user name
	ssi_size_t user_id = samples.addUserName (user_name);

	// add labels
	ssi_size_t class_id;
	if (useTierNameAsLabel) {		
		class_id = samples.addClassName (elanTier.name ());
	} 

	// add samples
	ElanTier::iterator anno;		
	for (anno = elanTier.begin (); anno != elanTier.end (); anno++) {
	
		ssi_sample_t *sample = new ssi_sample_t;
		ssi_stream_t **chops = new ssi_stream_t *[num];

		bool success = false;
		for (ssi_size_t j = 0; j < num; j++) {	

			// calculate start and stop index
			ssi_size_t start_index = ssi_cast (ssi_size_t, (anno->from / 1000.0) * streams[j]->sr + 0.5);
			ssi_size_t stop_index = ssi_cast (ssi_size_t, (anno->to / 1000.0) * streams[j]->sr + 0.5);

			if (! (start_index <= stop_index && stop_index < streams[j]->num)) {
				ssi_wrn ("invalid interval [%lf..%lf]s", anno->from/1000.0, anno->to/1000.0);
				continue;
			}

			// extract sample			
			chops[j] = new ssi_stream_t;
 			ssi_stream_copy (*streams[j], *chops[j], start_index, stop_index);

			success = true;
		}

		if (success) {

			// create and add new sample
			if (useTierNameAsLabel) {
				sample->class_id = class_id;
			} else {
				sample->class_id = samples.addClassName (anno->value.str ());
			}
			sample->num = num;
			sample->prob = 1.0f;
			sample->streams = chops;
			sample->time = anno->from / 1000.0;
			sample->user_id = user_id;
			samples.addSample (sample);

		} else {
			delete sample;
			delete[] chops;
		}

	}
}