void Evaluation::eval (IModel &model, ISamples &samples, ssi_size_t stream_index) { // init confussion matrix _trainer = 0; destroy_conf_mat (); init_conf_mat (samples); ssi_size_t n_classes = samples.getClassSize (); ssi_real_t *probs = new ssi_real_t[n_classes]; _n_total = samples.getSize (); _result_vec = new ssi_size_t[2*_n_total]; _result_vec_ptr = _result_vec; samples.reset (); const ssi_sample_t *sample = 0; while (sample = samples.next ()) { ssi_size_t real_index = sample->class_id; *_result_vec_ptr++ = real_index; if (model.forward (*sample->streams[stream_index], n_classes, probs)) { ssi_size_t max_ind = 0; ssi_real_t max_val = probs[0]; for (ssi_size_t i = 1; i < n_classes; i++) { if (probs[i] > max_val) { max_val = probs[i]; max_ind = i; } } *_result_vec_ptr++ = max_ind; _conf_mat_ptr[real_index][max_ind]++; _n_classified++; } else if (!_allow_unclassified) { ssi_size_t max_ind = _default_class_id; *_result_vec_ptr++ = max_ind; _conf_mat_ptr[real_index][max_ind]++; _n_classified++; } else { *_result_vec_ptr++ = SSI_ISAMPLES_GARBAGE_CLASS_ID; _n_unclassified++; } } delete[] probs; }
bool MajorityVoting::forward (ssi_size_t n_models, IModel **models, ssi_size_t n_streams, ssi_stream_t *streams[], ssi_size_t n_probs, ssi_real_t *probs) { if (n_streams != _n_streams) { ssi_wrn ("#streams (%u) differs from #streams (%u)", n_streams, _n_streams); return false; } if (_n_streams != n_models) { ssi_wrn ("#models (%u) differs from #streams (%u)", n_models, _n_streams); return false; } if (_n_classes != n_probs) { ssi_wrn ("#probs (%u) differs from #classes (%u)", n_probs ,_n_classes); return false; } bool found_data = false; IModel *model = 0; ssi_stream_t *stream = 0; //calculate actual models ssi_size_t miss_counter = 0; ssi_size_t *available = new ssi_size_t[n_models]; for (ssi_size_t n_model = 0; n_model < n_models; n_model++) { stream = streams[n_model]; if (stream->num > 0) { found_data = true; available[n_model] = 1; } else{ miss_counter++; available[n_model] = 0; } } ssi_size_t counter = 0; ssi_size_t *models_actual = new ssi_size_t[(n_models - miss_counter)]; for (ssi_size_t n_model = 0; n_model < n_models; n_model++) { if(available[n_model] == 1){ models_actual[counter] = n_model; counter++; } } if(found_data){ ssi_size_t *votes = new ssi_size_t[(n_models - miss_counter)]; for (ssi_size_t n_model = 0; n_model < (n_models - miss_counter); n_model++) { model = models[models_actual[n_model]]; stream = streams[models_actual[n_model]]; model->forward (*stream, n_probs, probs); ssi_size_t max_ind = 0; ssi_real_t max_val = probs[0]; for (ssi_size_t i = 1; i < n_probs; i++) { if (probs[i] > max_val) { max_val = probs[i]; max_ind = i; } } votes[n_model] = max_ind; if (ssi_log_level >= SSI_LOG_LEVEL_DEBUG) { for (ssi_size_t num_probs = 0; num_probs < n_probs; num_probs++){ ssi_print("%f ", probs[num_probs]); }ssi_print("- vote: %d\n", max_ind); } } if (ssi_log_level >= SSI_LOG_LEVEL_DEBUG) { ssi_print("\n"); } //clear probs for (ssi_size_t num_probs = 0; num_probs < n_probs; num_probs++){ probs[num_probs] = 0; } //fill probs with votes for(ssi_size_t n_model = 0; n_model < (n_models - miss_counter); n_model++){ probs[votes[n_model]]++; } if (ssi_log_level >= SSI_LOG_LEVEL_DEBUG) { for (ssi_size_t num_probs = 0; num_probs < n_probs; num_probs++){ ssi_print("%f ", probs[num_probs]); }ssi_print("\n\n"); } if(votes){ delete[] votes; votes = 0; } if(available){ delete [] available; available = 0; } if(models_actual){ delete [] models_actual; models_actual = 0; } } /// is there a draw ? /// ssi_size_t max_ind = 0; ssi_size_t max_ind_draw = 0; ssi_real_t max_val = probs[0]; bool draw = false; for (ssi_size_t i = 1; i < n_probs; i++) { if (probs[i] >= max_val) { if(probs[i] == max_val){ draw = true; max_ind_draw = i; } max_val = probs[i]; max_ind = i; } } if(draw && (max_ind == max_ind_draw)){ return false; }else{ return found_data; } }
bool FeatureFusion::forward (ssi_size_t n_models, IModel **models, ssi_size_t n_streams, ssi_stream_t *streams[], ssi_size_t n_probs, ssi_real_t *probs) { if (!isTrained ()) { ssi_wrn ("not trained"); return false; } if (n_streams != _n_streams) { ssi_wrn ("#streams (%u) differs from #streams (%u)", n_streams, _n_streams); return false; } if (_n_models != n_models) { ssi_wrn ("#models (%u) differs from #models (%u)", n_models, _n_models); return false; } if (_n_classes != n_probs) { ssi_wrn ("#probs (%u) differs from #classes (%u)", n_probs ,_n_classes); return false; } //No Missing Data: if(!_handle_md){ IModel *model = 0; ssi_stream_t *stream = 0; model = models[0]; ssi_stream_t *fusion_stream = new ssi_stream_t; ssi_size_t fusion_stream_dim = 0; for(ssi_size_t nstrm = 0; nstrm < _n_streams; nstrm++){ fusion_stream_dim += streams[nstrm]->dim; } //create aligned streams ssi_stream_init (*fusion_stream, 1, fusion_stream_dim, streams[0]->byte, streams[0]->type, streams[0]->sr); ssi_byte_t *ptr = fusion_stream->ptr; for(ssi_size_t i = 0; i < _n_streams; i++){ memcpy(ptr, streams[i]->ptr, ( streams[i]->byte * streams[i]->dim ) ); ptr += ( streams[i]->byte * streams[i]->dim ); } //clear probs for (ssi_size_t num_probs = 0; num_probs < n_probs; num_probs++){ probs[num_probs] = 0.0f; } model->forward (*fusion_stream, n_probs, probs); ssi_stream_destroy(*fusion_stream); delete fusion_stream; fusion_stream = 0; ///// is there a draw ? /// ssi_size_t max_ind = 0; ssi_size_t max_ind_draw = 0; ssi_real_t max_val = probs[0]; bool draw = false; for (ssi_size_t i = 1; i < n_probs; i++) { if (probs[i] >= max_val) { if(probs[i] == max_val){ draw = true; max_ind_draw = i; } max_val = probs[i]; max_ind = i; } } if(draw && (max_ind == max_ind_draw)){ return false; }else{ return true; } }//No Missing Data //Missing Data: bool found_data = false; IModel *model = 0; ssi_stream_t *stream = 0; //calculate actual models ssi_size_t miss_counter = 0; ssi_size_t *available = new ssi_size_t[n_models]; available[0] = 1; for (ssi_size_t n_model = 1; n_model < _n_models; n_model++) { stream = streams[n_model - 1]; if (stream->num > 0) { found_data = true; available[n_model] = 1; } else{ miss_counter++; available[n_model] = 0; if(available[0] == 1){ available[0] = 0; miss_counter++; } } } ssi_size_t counter = 0; ssi_size_t *models_actual = new ssi_size_t[(n_models - miss_counter)]; for (ssi_size_t n_model = 0; n_model < n_models; n_model++) { if(available[n_model] == 1){ models_actual[counter] = n_model; counter++; } } if (ssi_log_level >= SSI_LOG_LEVEL_DEBUG) { ssi_print("\n\n-----------------------------\navailable models:\n"); for(ssi_size_t i = 0; i < (n_models - miss_counter); i++){ ssi_print("%d ", models_actual[i]); }ssi_print("\n"); } if(found_data){ if(available[0] == 1){ //feature fusion possible if (ssi_log_level >= SSI_LOG_LEVEL_DEBUG) { ssi_print("\nfeature fusion possible\n"); } model = models[0]; stream = 0; ssi_stream_t *fusion_stream = new ssi_stream_t; ssi_size_t fusion_stream_dim = 0; for(ssi_size_t nstrm = 0; nstrm < _n_streams; nstrm++){ fusion_stream_dim += streams[nstrm]->dim; } //create aligned streams ssi_stream_init (*fusion_stream, 1, fusion_stream_dim, streams[0]->byte, streams[0]->type, streams[0]->sr); ssi_byte_t *ptr = fusion_stream->ptr; for(ssi_size_t i = 0; i < _n_streams; i++){ memcpy(ptr, streams[i]->ptr, ( streams[i]->byte * streams[i]->dim ) ); ptr += ( streams[i]->byte * streams[i]->dim ); } //clear probs for (ssi_size_t num_probs = 0; num_probs < n_probs; num_probs++){ probs[num_probs] = 0.0f; } model->forward (*fusion_stream, n_probs, probs); ssi_stream_destroy(*fusion_stream); delete fusion_stream; fusion_stream = 0; if(available){ delete [] available; available = 0; } if(models_actual){ delete [] models_actual; models_actual = 0; } return true; }else{ //feature fusion not possible, choose filler ... if (ssi_log_level >= SSI_LOG_LEVEL_DEBUG) { ssi_print("\nfeature fusion not possible: filler needed\n"); ssi_print("\nfiller:\n"); for (ssi_size_t n_model = 0; n_model < _n_streams; n_model++) { ssi_print("%d ", _filler[n_model]); }ssi_print("\n"); } bool model_available = false; ssi_size_t model_id = 0; for(ssi_size_t h = 0; h < _n_streams; h++){ model_id = _filler[h]; for(ssi_size_t i = 0; i < (n_models - miss_counter); i++){ if(model_id == models_actual[i]){ model_available = true; break; } } if(model_available == true){ model = models[model_id]; if (ssi_log_level >= SSI_LOG_LEVEL_DEBUG) { ssi_print("\nSelected Model: %d", model_id); } break; } } model->forward(*streams[model_id - 1], n_probs, probs); } } if(available){ delete [] available; available = 0; } if(models_actual){ delete [] models_actual; models_actual = 0; } /// is there a draw ? /// ssi_size_t max_ind = 0; ssi_size_t max_ind_draw = 0; ssi_real_t max_val = probs[0]; bool draw = false; for (ssi_size_t i = 1; i < n_probs; i++) { if (probs[i] >= max_val) { if(probs[i] == max_val){ draw = true; max_ind_draw = i; } max_val = probs[i]; max_ind = i; } } if(draw && (max_ind == max_ind_draw)){ return false; }else{ return found_data; } }