Exemplo n.º 1
0
void ISNorm::norm (ISamples &samples) {

	samples.reset ();
	ssi_sample_t *sample = 0;
	while (sample = samples.next ()) {
		norm (*sample);
	}
}
Exemplo n.º 2
0
bool FileSamplesOut::write (ISamples &data) {

	data.reset ();
	ssi_sample_t *sample = 0;
	while (sample = data.next ()) {
		write (*sample);
	}

	return true;
}
Exemplo n.º 3
0
void Evaluation::eval (IFusion &fusion, ssi_size_t n_models, IModel **models, ISamples &samples) {

	// 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 (fusion.forward (n_models, models, sample->num, sample->streams, 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;
}
Exemplo n.º 4
0
bool MyModel::train (ISamples &samples,
	ssi_size_t stream_index) {

	if (samples.getSize () == 0) {
		ssi_wrn ("empty sample list");
		return false;
	}

	if (isTrained ()) {
		ssi_wrn ("already trained");
		return false;
	}

	_n_classes = samples.getClassSize ();
	_n_features = samples.getStream (stream_index).dim;
	_centers = new ssi_real_t *[_n_classes];
	for (ssi_size_t i = 0; i < _n_classes; i++) {
		_centers[i] = new ssi_real_t[_n_features];
		for (ssi_size_t j = 0; j < _n_features; j++) {
			_centers[i][j] = 0;
		}
	}

	ssi_sample_t *sample;	
	samples.reset ();
	ssi_real_t *ptr = 0;
	while (sample = samples.next ()) {				
		ssi_size_t id = sample->class_id;	
		ptr = ssi_pcast (ssi_real_t, sample->streams[stream_index]->ptr);
		for (ssi_size_t j = 0; j < _n_features; j++) {
			_centers[id][j] += ptr[j];
		}
	}	 

	for (ssi_size_t i = 0; i < _n_classes; i++) {
		ssi_size_t num = samples.getSize (i);
		for (ssi_size_t j = 0; j < _n_features; j++) {
			_centers[i][j] /= num;
		}
	}

	return true;
}
Exemplo n.º 5
0
bool SimpleKNN::train (ISamples &samples,
	ssi_size_t stream_index) {

	if (samples.getSize () == 0) {
		ssi_wrn ("empty sample list");
		return false;
	}

	if (samples.getSize () < _options.k) {
		ssi_wrn ("sample list has less than '%u' entries", _options.k);
		return false;
	}

	if (isTrained ()) {
		ssi_wrn ("already trained");
		return false;
	}

	_n_classes = samples.getClassSize ();
	_n_samples = samples.getSize ();	
	_n_features = samples.getStream (stream_index).dim;
	_data = new ssi_real_t[_n_features*_n_samples];
	_classes = new ssi_size_t[_n_samples];

	ssi_sample_t *sample;	
	samples.reset ();
	ssi_real_t *data_ptr = _data;
	ssi_size_t *class_ptr = _classes;
	ssi_stream_t *stream_ptr = 0;
	ssi_size_t bytes_to_copy = _n_features * sizeof (ssi_real_t);
	while (sample = samples.next ()) {				
		memcpy (data_ptr, sample->streams[stream_index]->ptr, bytes_to_copy);
		*class_ptr++ = sample->class_id;
		data_ptr += _n_features;
	}	 

	return true;
}
Exemplo n.º 6
0
void Evaluation::eval_h (ISamples &samples) {	

	// walk through sample list and test trainer against each sample
	samples.reset ();
	const ssi_sample_t *sample = 0;
	ssi_size_t index, real_index;
	while (sample = samples.next ()) {
		real_index = sample->class_id;
		*_result_vec_ptr++ = real_index;
		if (_trainer->forward (sample->num, sample->streams, index)) {
			*_result_vec_ptr++ = index;
			_conf_mat_ptr[real_index][index]++;
			_n_classified++;
		} else if (!_allow_unclassified) {
			index = _default_class_id;
			*_result_vec_ptr++ = index;
			_conf_mat_ptr[real_index][index]++;
			_n_classified++;
		} else {
			*_result_vec_ptr++ = SSI_ISAMPLES_GARBAGE_CLASS_ID;
			_n_unclassified++;
		}
	}
}