char *l = parser->get_association("loss-type");
if(strcmp(l, "exponential") == 0)
loss_type = LOSS_EXPONENTIAL;
- else if(strcmp(l, "ev-regularized") == 0)
- loss_type = LOSS_EV_REGULARIZED;
else if(strcmp(l, "hinge") == 0)
loss_type = LOSS_HINGE;
else if(strcmp(l, "logistic") == 0)
}
break;
- case LOSS_EV_REGULARIZED:
- {
- scalar_t sum_pos = 0, sum_sq_pos = 0, nb_pos = 0, m_pos, v_pos;
- scalar_t sum_neg = 0, sum_sq_neg = 0, nb_neg = 0, m_neg, v_neg;
-
- for(int n = 0; n < samples->nb_samples(); n++) {
- if(samples->label(n) > 0) {
- sum_pos += responses[n];
- sum_sq_pos += sq(responses[n]);
- nb_pos += 1.0;
- } else if(samples->label(n) < 0) {
- sum_neg += responses[n];
- sum_sq_neg += sq(responses[n]);
- nb_neg += 1.0;
- }
- }
-
- l = 0;
-
- if(nb_pos > 0) {
- m_pos = sum_pos / nb_pos;
- v_pos = sum_sq_pos/(nb_pos - 1) - sq(sum_pos)/(nb_pos * (nb_pos - 1));
- l += nb_pos * exp(v_pos/2 - m_pos);
- }
-
- if(nb_neg > 0) {
- m_neg = sum_neg / nb_neg;
- v_neg = sum_sq_neg/(nb_neg - 1) - sq(sum_neg)/(nb_neg * (nb_neg - 1));
- l += nb_neg * exp(v_neg/2 + m_neg);
- }
-
- }
- break;
-
case LOSS_HINGE:
{
for(int n = 0; n < samples->nb_samples(); n++) {
case LOSS_EXPONENTIAL:
{
scalar_t num = 0, den = 0, z;
+
for(int n = 0; n < sample_set->nb_samples(); n++) {
z = sample_set->label(n) * weak_learner_responses[n];
if(z > 0) {
}
break;
- case LOSS_EV_REGULARIZED:
- {
-
- scalar_t u = 0, du = -0.1;
- scalar_t *responses = new scalar_t[sample_set->nb_samples()];
-
- scalar_t l, prev_l = -1;
-
- const scalar_t minimum_delta_for_optimization = 1e-5;
-
- scalar_t shift = 0;
-
- {
- scalar_t sum_pos = 0, sum_sq_pos = 0, nb_pos = 0, m_pos, v_pos;
- scalar_t sum_neg = 0, sum_sq_neg = 0, nb_neg = 0, m_neg, v_neg;
-
- for(int n = 0; n < sample_set->nb_samples(); n++) {
- if(sample_set->label(n) > 0) {
- sum_pos += responses[n];
- sum_sq_pos += sq(responses[n]);
- nb_pos += 1.0;
- } else if(sample_set->label(n) < 0) {
- sum_neg += responses[n];
- sum_sq_neg += sq(responses[n]);
- nb_neg += 1.0;
- }
- }
-
- if(nb_pos > 0) {
- m_pos = sum_pos / nb_pos;
- v_pos = sum_sq_pos/(nb_pos - 1) - sq(sum_pos)/(nb_pos * (nb_pos - 1));
- shift = max(shift, v_pos/2 - m_pos);
- }
-
- if(nb_neg > 0) {
- m_neg = sum_neg / nb_neg;
- v_neg = sum_sq_neg/(nb_neg - 1) - sq(sum_neg)/(nb_neg * (nb_neg - 1));
- shift = max(shift, v_neg/2 + m_neg);
- }
-
-// (*global.log_stream) << "nb_pos = " << nb_pos << " nb_neg = " << nb_neg << endl;
-
- }
-
- int nb = 0;
-
- while(nb < 100 && abs(du) > minimum_delta_for_optimization) {
- nb++;
-
-// (*global.log_stream) << "l = " << l << " u = " << u << " du = " << du << endl;
-
- u += du;
- for(int s = 0; s < sample_set->nb_samples(); s++) {
- responses[s] = current_responses[s] + u * weak_learner_responses[s] ;
- }
-
- {
- scalar_t sum_pos = 0, sum_sq_pos = 0, nb_pos = 0, m_pos, v_pos;
- scalar_t sum_neg = 0, sum_sq_neg = 0, nb_neg = 0, m_neg, v_neg;
-
- for(int n = 0; n < sample_set->nb_samples(); n++) {
- if(sample_set->label(n) > 0) {
- sum_pos += responses[n];
- sum_sq_pos += sq(responses[n]);
- nb_pos += 1.0;
- } else if(sample_set->label(n) < 0) {
- sum_neg += responses[n];
- sum_sq_neg += sq(responses[n]);
- nb_neg += 1.0;
- }
- }
-
- l = 0;
-
- if(nb_pos > 0) {
- m_pos = sum_pos / nb_pos;
- v_pos = sum_sq_pos/(nb_pos - 1) - sq(sum_pos)/(nb_pos * (nb_pos - 1));
- l += nb_pos * exp(v_pos/2 - m_pos - shift);
- }
-
- if(nb_neg > 0) {
- m_neg = sum_neg / nb_neg;
- v_neg = sum_sq_neg/(nb_neg - 1) - sq(sum_neg)/(nb_neg * (nb_neg - 1));
- l += nb_neg * exp(v_neg/2 + m_neg - shift);
- }
-
- }
-
- if(l > prev_l) du = du * -0.25;
- prev_l = l;
- }
-
- delete[] responses;
-
- return u;
- }
-
case LOSS_HINGE:
case LOSS_LOGISTIC:
{
}
} while(nb_sampled < nb_to_sample);
- (*global.log_stream) << "nb_sampled = " << nb_sampled << " nb_to_sample = " << nb_to_sample << endl;
-
(*global.log_stream) << "Done." << endl;
delete[] sampled_indexes;
deallocate();
}
-void RGBImage::write_ppm(const char *filename) {
- FILE *outfile;
-
- if ((outfile = fopen (filename, "wb")) == 0) {
- fprintf (stderr, "Can't open %s for reading\n", filename);
- exit(1);
- }
-
- fprintf(outfile, "P6\n%d %d\n255\n", _width, _height);
-
- char *raw = new char[_width * _height * 3];
-
- int k = 0;
- for(int y = 0; y < _height; y++) for(int x = 0; x < _width; x++) {
- raw[k++] = _bit_map[x + _width * (y + _height * RED)];
- raw[k++] = _bit_map[x + _width * (y + _height * GREEN)];
- raw[k++] = _bit_map[x + _width * (y + _height * BLUE)];
- }
-
- fwrite((void *) raw, sizeof(unsigned char), _width * _height * 3, outfile);
- fclose(outfile);
-
- delete[] raw;
-}
-
-void RGBImage::read_ppm(const char *filename) {
- const int buffer_size = 1024;
- FILE *infile;
- char buffer[buffer_size];
- int max;
-
- deallocate();
-
- if((infile = fopen (filename, "r")) == 0) {
- fprintf (stderr, "Can't open %s for reading\n", filename);
- exit(1);
- }
-
- fgets(buffer, buffer_size, infile);
-
- if(strncmp(buffer, "P6", 2) == 0) {
-
- do {
- fgets(buffer, buffer_size, infile);
- } while((buffer[0] < '0') || (buffer[0] > '9'));
- sscanf(buffer, "%d %d", &_width, &_height);
- fgets(buffer, buffer_size, infile);
- sscanf(buffer, "%d", &max);
-
- allocate();
-
- unsigned char *raw = new unsigned char[_width * _height * RGB_DEPTH];
- fread(raw, sizeof(unsigned char), _width * _height * RGB_DEPTH, infile);
-
- int k = 0;
- for(int y = 0; y < _height; y++) for(int x = 0; x < _width; x++) {
- _bit_plans[RED][y][x] = raw[k++];
- _bit_plans[GREEN][y][x] = raw[k++];
- _bit_plans[BLUE][y][x] = raw[k++];
- }
-
- delete[] raw;
-
- } else if(strncmp(buffer, "P5", 2) == 0) {
-
- do {
- fgets(buffer, buffer_size, infile);
- } while((buffer[0] < '0') || (buffer[0] > '9'));
- sscanf(buffer, "%d %d", &_width, &_height);
- fgets(buffer, buffer_size, infile);
- sscanf(buffer, "%d", &max);
-
- allocate();
-
- unsigned char *pixbuf = new unsigned char[_width * _height];
- fread(buffer, sizeof(unsigned char), _width * _height, infile);
-
- int k = 0, l = 0;
- for(int y = 0; y < _height; y++) for(int x = 0; x < _width; x++) {
- unsigned char c = pixbuf[k++];
- _bit_map[l++] = c;
- _bit_map[l++] = c;
- _bit_map[l++] = c;
- }
-
- delete[] pixbuf;
-
- } else {
- cerr << "Can not read ppm of type [" << buffer << "] from " << filename << ".\n";
- exit(1);
- }
-}
-
void RGBImage::read_png(const char *name) {
// This is the number of bytes the read_png routine will read to
// decide if the file is a PNG or not. According to the png
return _bit_plans[d][y][x];
}
- virtual void read_ppm(const char *filename);
- virtual void write_ppm(const char *filename);
-
virtual void read_png(const char *filename);
virtual void write_png(const char *filename);
RGBImageSubpixel::~RGBImageSubpixel() { }
-void RGBImageSubpixel::read_ppm(const char *filename) {
- abort();
-}
-
-void RGBImageSubpixel::write_ppm(const char *filename) {
- abort();
-}
-
-
void RGBImageSubpixel::read_png(const char *filename) {
abort();
}
inline int width() const { return _width / _scale; }
inline int height() const { return _height / _scale; }
- virtual void read_ppm(const char *filename);
- virtual void write_ppm(const char *filename);
-
virtual void read_png(const char *filename);
virtual void write_png(const char *filename);