bool ex_model(void *args) { ssi_size_t n_classes = 2; ssi_size_t n_samples = 5; ssi_size_t n_streams = 3; ssi_real_t distr[][3] = { 0.3f, 0.3f, 0.2f, 0.3f, 0.6f, 0.2f }; SampleList samples; ModelTools::CreateTestSamples(samples, n_classes, n_samples, n_streams, distr, "user"); ModelTools::PrintInfo(samples); { PythonModel *model = ssi_create(PythonModel, 0, true); model->getOptions()->setScript("ssi_model"); model->train(samples, 0); model->save("path/to/model"); } { PythonModel *model = ssi_create(PythonModel, 0, true); model->getOptions()->setScript("ssi_model"); model->load("path/to/model"); ssi_size_t nProbs = n_classes; ssi_real_t* probs = new ssi_real_t[n_classes]; for (ssi_size_t i = 0; i < nProbs; i++) { probs[i] = 1.0f; } model->forward(*samples.get(0)->streams[0], nProbs, probs); } return true; }
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); }
bool ex_fusion(void *arg) { ssi_tic (); ssi_size_t n_classes = 4; ssi_size_t n_samples = 50; ssi_size_t n_streams = 3; ssi_real_t train_distr[][3] = { 0.25f, 0.25f, 0.1f, 0.25f, 0.75f, 0.1f, 0.75f, 0.75f, 0.1f, 0.75f, 0.75f, 0.1f }; ssi_real_t test_distr[][3] = { 0.5f, 0.5f, 0.5f }; SampleList strain; SampleList sdevel; SampleList stest; ModelTools::CreateTestSamples (strain, n_classes, n_samples, n_streams, train_distr, "user"); ModelTools::CreateTestSamples (sdevel, n_classes, n_samples, n_streams, train_distr, "user"); ModelTools::CreateTestSamples (stest, 1, n_samples * n_classes, n_streams, test_distr, "user"); 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); stest.addClassName (string); } ssi_char_t *name = "fusion"; // strain { IModel **models = new IModel *[n_streams]; ssi_char_t string[SSI_MAX_CHAR]; for (ssi_size_t n_stream = 0; n_stream < n_streams; n_stream++) { ssi_sprint (string, "%s.%02d", name, n_stream); models[n_stream] = ssi_create(SimpleKNN, string, true); } SimpleFusion *fusion = ssi_create (SimpleFusion, name, true); Trainer trainer (n_streams, models, fusion); trainer.train (strain); trainer.save ("fusion"); delete[] models; } // evaluation { Trainer trainer; Trainer::Load (trainer, "fusion"); Evaluation eval; eval.eval (&trainer, sdevel); eval.print (); } ssi_print_off(""); ssi_toc_print (); ssi_print("\n"); return true; }
void Evaluation::evalSplit (Trainer *trainer, ISamples &samples, ssi_real_t split) { if (split <= 0 || split >= 1) { ssi_err ("split must be a value between 0 and 1"); } _trainer = trainer; destroy_conf_mat (); init_conf_mat (samples); ssi_size_t *indices = new ssi_size_t[samples.getSize ()]; ssi_size_t *indices_count_lab = new ssi_size_t[samples.getClassSize ()]; ssi_size_t indices_count_all; indices_count_all = 0; for (ssi_size_t j = 0; j < samples.getClassSize (); j++) { indices_count_lab[j] = 0; } ssi_size_t label; ssi_size_t label_size; for (ssi_size_t j = 0; j < samples.getSize (); j++) { label = samples.get (j)->class_id; label_size = samples.getSize (label); if (++indices_count_lab[label] <= ssi_cast (ssi_size_t, label_size * split + 0.5f)) { indices[indices_count_all++] = j; } } SampleList strain; SampleList stest; // split off samples ModelTools::SelectSampleList (samples, strain, stest, indices_count_all, indices); _n_total = stest.getSize (); _result_vec = new ssi_size_t[2*_n_total]; _result_vec_ptr = _result_vec; // train with remaining samples _trainer->release (); if (_preproc_mode) { _trainer->setPreprocMode (_preproc_mode, _n_streams_refs, _stream_refs); } else if (_fselmethod) { _trainer->setSelection (strain, _fselmethod, _pre_fselmethod, _n_pre_feature); } _trainer->train (strain); // test with remaining samples eval_h (stest); delete[] indices; delete[] indices_count_lab; }
bool SDLSoundSingleton::play(const UPlayer* owner, const string& file) { if(!openDevice()) return false; SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; if (SDL_LoadWAV(file.c_str(), &wave, &data, &dlen) == NULL) { cerr << "Couldn't load " << file << ": " << SDL_GetError() << endl; return false; } SDL_AudioCVT cvt; SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 44100); cvt.buf = new Uint8[dlen * cvt.len_mult]; memcpy(cvt.buf, data, dlen); cvt.len = dlen; SDL_ConvertAudio(&cvt); SDL_FreeWAV(data); stop(owner); SDL_LockAudio(); Uint8Pointer sampleData(cvt.buf); SamplePointer samplePointer(new Sample(owner, sampleData, cvt.len_cvt)); mSampleList.push_back(samplePointer); SDL_PauseAudio(0); SDL_UnlockAudio(); return true; }
bool ex_model_norm(void *arg) { Trainer::SetLogLevel(SSI_LOG_LEVEL_DEBUG); ssi_size_t n_classes = 4; ssi_size_t n_samples = 50; ssi_size_t n_streams = 1; ssi_real_t train_distr[][3] = { 0.25f, 0.25f, 0.1f, 0.25f, 0.75f, 0.1f, 0.75f, 0.75f, 0.1f, 0.75f, 0.75f, 0.1f }; ssi_real_t test_distr[][3] = { 0.5f, 0.5f, 0.5f }; SampleList strain; SampleList sdevel; SampleList stest; ModelTools::CreateTestSamples(strain, n_classes, n_samples, n_streams, train_distr, "user"); ModelTools::CreateTestSamples(sdevel, n_classes, n_samples, n_streams, train_distr, "user"); ModelTools::CreateTestSamples(stest, 1, n_samples * n_classes, n_streams, test_distr, "user"); 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); stest.addClassName(string); } // train svm { SVM *model = ssi_create(SVM, 0, true); model->getOptions()->seed = 1234; Trainer trainer(model); ISNorm::Params params; ISNorm::ZeroParams(params, ISNorm::METHOD::ZSCORE); trainer.setNormalization(¶ms); trainer.train(strain); trainer.save("svm+norm"); } // evaluation { Trainer trainer; Trainer::Load(trainer, "svm+norm"); Evaluation eval; eval.eval(&trainer, sdevel); eval.print(); trainer.cluster(stest); ModelTools::PlotSamples(stest, "svm (external normalization)", ssi_rect(650,0,400,400)); } return true; }
void CloudTestB::execute(const double parameters[], const SampleList& samples, bool estimates[]) const { const int32_t count = cofIasint.surfaceTypeCount_; copy(parameters, parameters + 3 * count, cofIasint.thresholdValues_); copy(parameters + 3 * count, parameters + 10 * count, cofIasint.coefficientsA1_); copy(parameters + 10 * count, parameters + 17 * count, cofIasint.coefficientsA2_); copy(parameters + 17 * count, parameters + 19 * count, cofIasint.coefficientsA3_); for (size_t i = 0; i < samples.size(); ++i) { estimates[i] = cloudTestImpl( samples[i]->getBrightnessTemperatures(TEST_B), samples[i]->getFractionalLandCover(), cofIasint); } }
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; } } }
bool ex_hierarchical(void *arg) { Trainer::SetLogLevel(SSI_LOG_LEVEL_DEBUG); ssi_size_t n_classes = 6; ssi_size_t n_samples = 50; ssi_size_t n_streams = 1; ssi_real_t train_distr[][3] = { 0.2f, 0.2f, 0.1f, 0.2f, 0.5f, 0.1f, 0.2f, 0.8f, 0.1f, 0.8f, 0.8f, 0.1f, 0.8f, 0.5f, 0.1f, 0.8f, 0.2f, 0.1f, }; ssi_real_t test_distr[][3] = { 0.5f, 0.5f, 0.5f }; SampleList strain; SampleList sdevel; SampleList stest; ModelTools::CreateTestSamples(strain, n_classes, n_samples, n_streams, train_distr, "user"); ModelTools::CreateTestSamples(sdevel, n_classes, n_samples, n_streams, train_distr, "user"); ModelTools::CreateTestSamples(stest, 1, n_samples * n_classes, n_streams, test_distr, "user"); 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); stest.addClassName(string); } ModelTools::PlotSamples(strain, "train", ssi_rect(650, 0, 400, 400)); // non-hierarchical { SVM *model = ssi_create(SVM, 0, true); model->getOptions()->params.kernel_type = LINEAR; model->getOptions()->balance = SVM::BALANCE::OFF; Trainer trainer(model); trainer.train(strain); trainer.eval(sdevel); trainer.cluster(stest); ModelTools::PlotSamples(stest, "svm", ssi_rect(650, 0, 400, 400)); OptionList::SaveXML("svm", model->getOptions()); } // hierarchical { HierarchicalModel *hmodel = ssi_create(HierarchicalModel, 0, true); /* hmodel->initTree(5); hmodel->addNode(0, 0, ssi_create(SVM, "svm", true), "0, 1, 2, 3, 4, 5"); hmodel->addNode(1, 0, ssi_create(SVM, "svm", true), "0, 1, 2, 3, 4"); hmodel->addNode(1, 1, ssi_create(SVM, "svm", true), "5"); hmodel->addNode(2, 0, ssi_create(SVM, "svm", true), "0, 1, 2"); hmodel->addNode(2, 1, ssi_create(SVM, "svm", true), "3, 4"); hmodel->addNode(3, 0, ssi_create(SVM, "svm", true), "0, 1"); hmodel->addNode(3, 1, ssi_create(SVM, "svm", true), "2"); */ hmodel->initTree(2); hmodel->addNode(0, 0, ssi_create(SVM, "svm", true), "0, 1, 2, 3, 4, 5"); hmodel->addNode(1, 0, ssi_create(SVM, "svm", true), "0, 1, 2", "1"); hmodel->addNode(1, 1, ssi_create(SVM, "svm", true), "3, 4, 5", "1"); hmodel->getTree()->print(HierarchicalModel::ToString); Trainer trainer(hmodel); trainer.train(strain); trainer.eval(sdevel); trainer.save("hierarchical"); } { Trainer trainer; Trainer::Load(trainer, "hierarchical"); trainer.eval(sdevel); trainer.cluster(stest); ModelTools::PlotSamples(stest, "hierarchical svm", ssi_rect(650, 0, 400, 400)); } return true; }
bool ex_model(void *arg) { Trainer::SetLogLevel (SSI_LOG_LEVEL_DEBUG); ssi_size_t n_classes = 4; ssi_size_t n_samples = 50; ssi_size_t n_streams = 1; ssi_real_t train_distr[][3] = { 0.25f, 0.25f, 0.1f, 0.25f, 0.75f, 0.1f, 0.75f, 0.75f, 0.1f, 0.75f, 0.75f, 0.1f }; ssi_real_t test_distr[][3] = { 0.5f, 0.5f, 0.5f }; SampleList strain; SampleList sdevel; SampleList stest; ModelTools::CreateTestSamples (strain, n_classes, n_samples, n_streams, train_distr, "user"); ModelTools::CreateTestSamples (sdevel, n_classes, n_samples, n_streams, train_distr, "user"); ModelTools::CreateTestSamples (stest, 1, n_samples * n_classes, n_streams, test_distr, "user"); 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); stest.addClassName (string); } // train svm { SVM *model = ssi_create(SVM, 0, true); model->getOptions()->seed = 1234; Trainer trainer(model); trainer.train(strain); trainer.save("svm"); } // evaluation { Trainer trainer; Trainer::Load(trainer, "svm"); Evaluation eval; eval.eval(&trainer, sdevel); eval.print(); trainer.cluster(stest); ModelTools::PlotSamples(stest, "svm (internal normalization)", ssi_rect(650, 0, 400, 400)); } // train knn { KNearestNeighbors *model = ssi_create(KNearestNeighbors, 0, true); model->getOptions()->k = 5; //model->getOptions()->distsum = true; Trainer trainer (model); trainer.train (strain); trainer.save ("knn"); } // evaluation { Trainer trainer; Trainer::Load (trainer, "knn"); Evaluation eval; eval.eval (&trainer, sdevel); eval.print (); trainer.cluster (stest); ModelTools::PlotSamples(stest, "knn", ssi_rect(650, 0, 400, 400)); } // train naive bayes { NaiveBayes *model = ssi_create(NaiveBayes, 0, true); model->getOptions()->log = true; Trainer trainer (model); trainer.train (strain); trainer.save ("bayes"); } // evaluation { Trainer trainer; Trainer::Load (trainer, "bayes"); Evaluation eval; eval.eval (&trainer, sdevel); eval.print (); trainer.cluster (stest); ModelTools::PlotSamples(stest, "bayes", ssi_rect(650, 0, 400, 400)); } // training { LDA *model = ssi_create(LDA, "lda", true); Trainer trainer (model); trainer.train (strain); model->print(); trainer.save ("lda"); } // evaluation { Trainer trainer; Trainer::Load (trainer, "lda"); Evaluation eval; eval.eval (&trainer, sdevel); eval.print (); trainer.cluster (stest); ModelTools::PlotSamples(stest, "lda", ssi_rect(650, 0, 400, 400)); } ssi_print ("\n\n\tpress a key to contiue\n"); getchar (); return true; }
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; }
bool SDLSoundSingleton::isPlaying(const UPlayer* owner) { return find_if(mSampleList.begin(), mSampleList.end(), SampleBelongsTo(owner)) != mSampleList.end(); }
inline void SDLSoundSingleton::stop(const UPlayer* owner) { SDL_LockAudio(); mSampleList.remove_if(SampleBelongsTo(owner)); SDL_UnlockAudio(); }