static inline void free_pattern(struct simple_pattern *m) {
    if(!m) return;

    free_pattern(m->child);
    free_pattern(m->next);
    freez((void *)m->match);
    freez(m);
}
Beispiel #2
0
static void
free_selector(ks_selector_t *selector)
{
	(void) free_pattern(&selector->ks_class);
	(void) free_pattern(&selector->ks_module);
	(void) free_pattern(&selector->ks_instance);
	(void) free_pattern(&selector->ks_name);
	(void) free_pattern(&selector->ks_statistic);
	free(selector);
}
Beispiel #3
0
void        free_struct_sample(SAMPLE s)
{
  /* Frees the memory of sample s. */
  int i;
  for(i=0;i<s.n;i++) {
    free_pattern(s.examples[i].x);
    free_label(s.examples[i].y);
  }
  free(s.examples);
}
void free_struct_sample(SAMPLE s) {
  int i;
  for (i=0;i<s.n;i++) {
    free_pattern(s.examples[i].x);
    free_label(s.examples[i].y);
    free_latent_var(s.examples[i].h);
  }
  free(s.examples);

}
void free_struct_sample(SAMPLE s) {
/*
  Free the whole training sample. 
*/
  int i;
  for (i=0;i<s.n;i++) {
    free_pattern(s.examples[i].x);
    free_label(s.examples[i].y);
  }
  free(s.examples);

}
/* select training samples for aspectlet */
void select_examples_aspectlet(char *trainfile, CAD **cads, int cad_index)
{
  int i, j, n, count, count_pos, part_num, view_label;
  int *flag;
  char *pname;
  float azimuth, elevation, distance;
  FILE *fp;
  LABEL y;
  EXAMPLE  example, *examples_aspectlet;

  /* find corresponding parts between two cads */
  part_num = cads[cad_index]->part_num;
  flag = (int*)my_malloc(sizeof(int)*part_num);
  for(i = 0; i < part_num; i++)
  {
    pname = cads[cad_index]->part_names[i];
    for(j = 0; j < cads[0]->part_num; j++)
    {
      if(strcmp(pname, cads[0]->part_names[j]) == 0)
      {
        flag[i] = j;
        break;
      }
    }
  }

  examples_aspectlet = NULL;

  /* select positive samples */
  /* open data file */
  if((fp = fopen(trainfile, "r")) == NULL)
  {
    printf("Can not open data file %s\n", trainfile);
    exit(1);
  }
  fscanf(fp, "%d", &n);

  count = 0;
  for(i = 0; i < n; i++)
  {
    /* read example */
    example = read_one_example(fp, cads);
    y = example.y;
    if(y.object_label == 1)
    {
      azimuth = cads[0]->objects2d[y.view_label]->azimuth;
      elevation = cads[0]->objects2d[y.view_label]->elevation;
      distance = cads[0]->objects2d[y.view_label]->distance;
      /* find the view label */
      view_label = -1;
      for(j = 0; j < cads[cad_index]->view_num; j++)
      {
        if(cads[cad_index]->objects2d[j]->azimuth == azimuth && cads[cad_index]->objects2d[j]->elevation == elevation && cads[cad_index]->objects2d[j]->distance == distance)
        {
          view_label = j;
          break;
        }
      }

      /* select the sample */
      if(view_label != -1)
      {
        examples_aspectlet = (EXAMPLE*)realloc(examples_aspectlet, sizeof(EXAMPLE)*(count+1));
        if(examples_aspectlet == NULL)
        {
          printf("out of memory\n");
          exit(1);
        }

        /* construct the sample */
        examples_aspectlet[count].y.object_label = y.object_label;
        examples_aspectlet[count].y.cad_label = y.cad_label;
        examples_aspectlet[count].y.view_label = view_label;
        examples_aspectlet[count].y.part_num = part_num;
        examples_aspectlet[count].y.part_label = (float*)my_malloc(sizeof(float)*2*part_num);
        /* get part labels */
        for(j = 0; j < part_num; j++)
        {
          if(cads[cad_index]->roots[j] != -1)  /* not root template */
          {
            examples_aspectlet[count].y.part_label[j] = y.part_label[flag[j]];
            examples_aspectlet[count].y.part_label[j+part_num] = y.part_label[flag[j]+cads[0]->part_num];
          }
          else /* is root template */
          {
            if(y.part_label[flag[j]] == 0 && y.part_label[flag[j]+cads[0]->part_num] == 0)  /* root not visible */
            {
              examples_aspectlet[count].y.part_label[j] = 0;
              examples_aspectlet[count].y.part_label[j+part_num] = 0;
            }
            else  /* compute root location and bounding box */
              compute_bbox_root(&(examples_aspectlet[count].y), j, cads[cad_index]);
          }
        }
        /* get occlusion label */
        examples_aspectlet[count].y.occlusion = (int*)my_malloc(sizeof(int)*part_num);
        for(j = 0; j < part_num; j++)
          examples_aspectlet[count].y.occlusion[j] = y.occlusion[flag[j]];
        examples_aspectlet[count].y.energy = 0;
        /* copy the image */
        examples_aspectlet[count].x.image = copy_cumatrix(example.x.image);
        count++;
      }  /* end if view_label != -1 */
      free_pattern(example.x);
      free_label(example.y);
    }  /* end if y.object_label == 1 */
    else
      break;
  }

  /* select negative samples */
  count_pos = count;
  for(; i < n; i++)
  {
    if(y.object_label == -1)
    {
      examples_aspectlet = (EXAMPLE*)realloc(examples_aspectlet, sizeof(EXAMPLE)*(count+1));
      if(examples_aspectlet == NULL)
      {
        printf("out of memory\n");
        exit(1);
      }

      examples_aspectlet[count].y.object_label = -1;
      examples_aspectlet[count].y.cad_label = -1;
      examples_aspectlet[count].y.view_label = -1;
      examples_aspectlet[count].y.part_num = -1;
      examples_aspectlet[count].y.part_label = NULL;
      examples_aspectlet[count].y.occlusion = NULL;
      for(j = 0; j < 4; j++)
        examples_aspectlet[count].y.bbox[j] = 0;
      examples_aspectlet[count].y.energy = 0;
      /* copy the image */
      examples_aspectlet[count].x.image = copy_cumatrix(example.x.image);
      count++;
      if(count >= 2*count_pos)
        break;
    }
    free_pattern(example.x);
    free_label(example.y);
    example = read_one_example(fp, cads);
    y = example.y;
  }

  free_pattern(example.x);
  free_label(example.y);
  free(flag);
  fclose(fp);

  /* write examples */
  printf("Writing data to temp_part.dat: %d positive samples, %d negative samples...\n", count_pos, count-count_pos);
  fp = fopen("temp_part.dat", "w");
  fprintf(fp, "%d\n", count);
  for(i = 0; i < count; i++)
  {
    /* write object label */
    fprintf(fp, "%d ", examples_aspectlet[i].y.object_label);
    if(examples_aspectlet[i].y.object_label == 1)
    {
      /* write cad label */
      fprintf(fp, "%d ", examples_aspectlet[i].y.cad_label);
      /* write view label */
      fprintf(fp, "%d ", examples_aspectlet[i].y.view_label);
      /* write part label */
      for(j = 0; j < 2*part_num; j++)
        fprintf(fp, "%f ", examples_aspectlet[i].y.part_label[j]);
      /* write occlusion label */
      for(j = 0; j < part_num; j++)
        fprintf(fp, "%d ", examples_aspectlet[i].y.occlusion[j]);
      /* write bounding box */
      for(j = 0; j < 4; j++)
        fprintf(fp, "%f ", examples_aspectlet[i].y.bbox[j]);
    }
    /* write image size */
    fprintf(fp, "%d ", examples_aspectlet[i].x.image.dims_num);
    for(j = 0; j < examples_aspectlet[i].x.image.dims_num; j++)
      fprintf(fp, "%d ", examples_aspectlet[i].x.image.dims[j]);
    /* write image pixel */
    for(j = 0; j < examples_aspectlet[i].x.image.length; j++)
      fprintf(fp, "%u ", (unsigned int)examples_aspectlet[i].x.image.data[j]);
    fprintf(fp, "\n");
  }
  fclose(fp);
  printf("Done\n");

  /* clean up */
  for(i = 0; i < count; i++)
  {
    free_pattern(examples_aspectlet[i].x);
    free_label(examples_aspectlet[i].y);
  }
  free(examples_aspectlet);
}
void simple_pattern_free(SIMPLE_PATTERN *list) {
    if(!list) return;

    free_pattern(((struct simple_pattern *)list));
}