⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datafile.c

📁 face recognition test source code
💻 C
📖 第 1 页 / 共 2 页
字号:
  entry = init_entry(entr, entry);
  if (entry == NULL)
    return NULL; 

  maskcnt = 0;
  toke = strtok(line, SEPARATOR_CHARS);
  /* Read the first vector value */
  if (strcmp(toke, masked_string) == 0)
    {
      mask = set_mask(mask, dim, 0);
      if (mask == NULL)
	{
	  if (entry_is_new)
	    free_entry(entry);
	  return NULL;
	}
	  
      maskcnt++;
      ent = 0.0;
    }
  else
    if (sscanf(toke, "%f", &ent) <= 0) {
      fprintf(stderr, "Can't read entry on line %d, component 0\n", row);
      if (entry_is_new)
	free_entry(entry);

      ERROR(ERR_FILEFORMAT);
      return NULL;
    }
  entry->points[0] = ent;


  /* Read the other vector values */
  for (i = 1; i < dim; i++) {
    toke = strtok(NULL, SEPARATOR_CHARS);
    if (toke == NULL) {
      fprintf(stderr, "load_entry: can't read entry in file %s on line %d, component %d\n",
	      fi->name, row, i);
      if (entry_is_new)
	free_entry(entry);
      ERROR(ERR_FILEFORMAT);
      return NULL;
    }

    if (strcmp(toke, masked_string) == 0)
      {
	mask = set_mask(mask, dim, i);
	if (mask == NULL)
	  {
	    if (entry_is_new)
	      free_entry(entry);
	    return NULL;
	  }
	maskcnt++;
	ent = 0.0;
      }
    else
      if (sscanf(toke, "%f", &ent) <= 0) {
	fprintf(stderr, "load_entry: can't read entry in file %s on line %d, component %d\n",
	      fi->name, row, i);
	if (entry_is_new)
	  free_entry(entry);
	ERROR(ERR_FILEFORMAT);
	return NULL;
      }
    entry->points[i] = ent;
  }

  /* Entries with all components masked off are normally discarded but
     they are loaded if the skip_empty-flag is set in the flags of the 
     file. */
    
  if (maskcnt == dim)
    {
      if (entr->flags.skip_empty)
	{
	  ifverbose(3)
	    fprintf(stderr, "load_entry: skipping line %d of file %s, all components are masked off\n", row, fi->name);
	  free(mask);
	  mask = NULL;
	  goto read_next_line; /* load next line */
	}
      else
	ifverbose(3)
	  fprintf(stderr, "load_entry: loading line %d of file %s, all components are masked off\n", row, fi->name);
    }

  if (mask)
    {
      entry->mask = mask;
      mask = NULL;
    }

  /* Now the following tokens (if any) are label,
     weight term and fixed point description.
     Sometimes label is not needed. Other terms are never
     needed */

  label_found = 0;

  while ((toke = strtok(NULL, SEPARATOR_CHARS)) != NULL) 
    {
      if (strncmp(toke, "weight=", 7) == 0) 
	entry->weight = get_weight(toke);
      else if (strncmp(toke, "fixed=", 6) == 0) 
	{
	  if ((entry->fixed = get_fixed(toke)) == NULL)
	    {
	      fprintf(stderr, "bad fixed point, line %d of file %s\n", 
		      row, fi->name);
	      if (entry_is_new)
		free_entry(entry);
	      ERROR(ERR_FILEFORMAT);
	      return NULL;
	    }
	}
      else 
	{
	  if (sscanf(toke, "%s", lab) <= 0) {
	    fprintf(stderr, "Can't read entry label on line %dof file %s\n", 
		    row, fi->name);
	    if (entry_is_new)
	      free_entry(entry);
	    ERROR(ERR_FILEFORMAT);
	    return NULL;
	  }

	  add_entry_label(entry, find_conv_to_ind(lab));
	  label_found++;
	}
    }
    
  if ((entr->flags.labels_needed) && (!label_found))
    {
      fprintf(stderr, "Required label missing on line %d of file %s\n", 
	      row, fi->name);
      if (entry_is_new)
	free_entry(entry);
      ERROR(ERR_FILEFORMAT);
      return NULL;
    }

  return(entry);
}

/* next_entry - Get next entry from the entries table. Returns NULL when 
   at end of table or end of file is encountered. If loadmode is buffered,
   loads more data from file when needed. */

struct data_entry *next_entry(eptr *ptr)
{
  struct entries *entries = ptr->parent;
  struct data_entry *next, *current;

  current = ptr->current;

  if (current == NULL)
    return NULL;
  
  next = current->next;

  if (next == NULL)
    if (entries->flags.loadmode == LOADMODE_BUFFER)
      {
	if (entries->fi->flags.eof)
	  next = NULL; /* end of file, no more lines */
	else
	  {
	    /* load more lines */
	    if (read_entries(entries) == NULL)
	      next = NULL;
	    else
	      next = entries->entries;
	  }
      }      
  ptr->current = next;
  if (next) 
    ptr->index++;
  return next;
}

/* rewind_entries - go to the first entry in entries list. Returns pointer
   to first data_entry. Loads data from file if it hasn't been loaded yet and
   rewinds file if we are using buffered reading. */

struct data_entry *rewind_entries(struct entries *entries, eptr *ptr)
{
  struct data_entry *current = entries->entries;
  struct file_info *fi;

  ptr->parent = entries;

  if (entries->flags.loadmode == LOADMODE_BUFFER)
    {

      /* buffered loading */
      fi = entries->fi;
      if ((fi->flags.eof) || (current != NULL))
	{
	  /* if we are at the end of file, need to rewind the file */
	  if (rewind_file(fi))
	    {
	      fprintf(stderr, "error rewinding file\n");
	      return NULL;
	    }
	}
      
      if (!read_entries(entries))
	{
	  fprintf(stderr, "rewind_entries failed\n");
	  return NULL;
	}
      current = entries->entries;

    }
  else
    {
      /* not buffered */
      if ((current == NULL) && (!entries->flags.totlen_known))
	{
	  /* file not loaded into memory */
	  if (!read_entries(entries))
	    {
	      fprintf(stderr, "rewind_entries failed\n");
	      return NULL;
	    }
	  current = entries->entries;
	}
    }

  ptr->current = current;
  ptr->index = 0;
  entries->lap++;

  return current;
}

/* free_entrys - Free a list of entries */

void free_entrys(struct data_entry *data)
{
  struct data_entry *tmp;

  for (;data != NULL; data = tmp)
    {
      tmp = data->next;
      free_entry(data);
    }
}

/* copy_entry - Copy one entry (next==NULL) */

struct data_entry *copy_entry(struct entries *entries, struct data_entry *data)
{
  int i;
  struct data_entry *tmp;
  
  clear_err();
  /* allocate memory for the copy */
  tmp = alloc_entry(entries);
  if (tmp == NULL)
    return NULL;

  /* copy data vector */
  for (i = 0; i < entries->dimension; i++)
    tmp->points[i] = data->points[i];

  /* copy labels */
  copy_entry_labels(tmp, data);

  /* copy mask */
  if (data->mask)
    {
      if ((tmp->mask = malloc(entries->dimension)) == NULL)
	{
	  fprintf(stderr, "Can't allocate memory for mask\n");
	  free_entry(tmp);
	  ERROR(ERR_NOMEM);
	  return NULL;
	}
      memcpy(tmp->mask, data->mask, entries->dimension);
    }

  /* copy other stuff */
  tmp->weight = data->weight;

  /* copy fixed point */
  if (data->fixed)
    {
      if ((tmp->fixed = malloc(sizeof(struct fixpoint))) == NULL)
	{
	  free_entry(tmp);
	  ERROR(ERR_NOMEM);
	  return NULL;
	}
      memcpy(tmp->fixed, data->fixed, sizeof(struct fixpoint));
    }
  tmp->next = NULL;

  return(tmp);
}


/* some routines for getting values from datafile headers */

int get_weight(char *str)
{
  return(atoi(&(str[7])));
}

struct fixpoint *get_fixed(char *str)
{
  char *dup, *sav;
  struct fixpoint *tmp;

  tmp = malloc(sizeof(struct fixpoint));
  if (tmp == NULL)
    return NULL;

  dup = ostrdup(str);
  if (dup == NULL)
    {
      free(tmp);
      return NULL;
    }

  tmp->xfix = atoi(&(dup[6]));
  sav = strchr(dup, ',');
  tmp->yfix = atoi(&(sav[1]));

  free(dup);


  if ((tmp->xfix < 0) || (tmp->yfix < 0)) {
    fprintf(stderr, "Fixed point incorrect");
    free(tmp);
    tmp = NULL;
  }

  return(tmp);
}

int get_topol(char *str)
{
  int ret = TOPOL_UNKNOWN;
  char *dup;
  char *tok;

  dup = ostrdup(str);
  strtok(dup, " ");
  tok = strtok(NULL, " ");

  if (tok != NULL) {
    ret = topol_type(tok);
  }

  ofree(dup);
  return(ret);
}

int get_neigh(char *str)
{
  int ret = NEIGH_UNKNOWN;
  char *dup;
  char *tok;

  dup = ostrdup(str);
  strtok(dup, " ");
  strtok(NULL, " ");
  strtok(NULL, " ");
  strtok(NULL, " ");
  tok = strtok(NULL, " ");

  if (tok != NULL) {
    ret = neigh_type(tok);
  }

  ofree(dup);
  return(ret);
}

int get_xdim(char *str)
{
  int ret = 0;
  char *dup;
  char *tok;

  dup = ostrdup(str);
  strtok(dup, " ");
  strtok(NULL, " ");
  tok = strtok(NULL, " ");

  if (tok != NULL) {
    ret = atoi(tok);
  }

  ofree(dup);
  return(ret);
}

int get_ydim(char *str)
{
  int ret = 0;
  char *dup;
  char *tok;

  dup = ostrdup(str);
  strtok(dup, " ");
  strtok(NULL, " ");
  strtok(NULL, " ");
  tok = strtok(NULL, " ");

  if (tok != NULL) {
    ret = atoi(tok);
  }

  ofree(dup);
  return(ret);
}


/******************************************************************* 
 * Routines to handle files that contain the learning rate values  *
 *******************************************************************/

int alpha_read(float *alpha, long noc, char *infile)
{
  long i;
  char basename[STR_LNG];
  FILE *fp;
  struct file_info *fi;

  strcpy(basename, infile);
  strtok(basename, ".");
  strcat(basename, ".lra");

  fi = open_file(basename, "r");
  if (fi == NULL) {
    ifverbose(1) {
      fprintf(stderr, "Can't open alpha file %s", basename);
    }
    return(0);
  }

  fp = fi2fp(fi);

  for (i = 0; i < noc; i++) {
    if (fscanf(fp, "%g\n", &(alpha[i])) < 0)
      return(0);
  }

  close_file(fi);

  return(1);
}

int alpha_write(float *alpha, long noc, char *outfile)
{
  long i;
  char basename[STR_LNG];
  FILE *fp;
  struct file_info *fi;

  strcpy(basename, outfile);
  strtok(basename, ".");
  strcat(basename, ".lra");

  fi = open_file(basename, "w+");
  if (fi == NULL) {
    fprintf(stderr, "Can't open alpha file %s for writing", basename);
    return 0;
  }

  fp = fi2fp(fi);

  for (i = 0; i < noc; i++) {
    fprintf(fp, "%g\n", alpha[i]);
  }

  close_file(fi);
  return 0;
}

void invalidate_alphafile(char *outfile)
{
  int i;
  char basename[STR_LNG];
  FILE *fp;

  strcpy(basename, outfile);
  strtok(basename, ".");
  strcat(basename, ".lra");

  fp = fopen(basename, "r");
  if (fp != NULL) {
    ifverbose(1)
      fprintf(stdout, "Removing the learning rate file %s\n", basename);
    fclose(fp);
    i = remove(basename);
    if (i) {
      fprintf(stderr, "Can not remove %s", basename);
    }
  }
}


/******************************************************************* 
 * Other routines                                                  *
 *******************************************************************/

/* randomize_entry_order - arrange a list of entrys to random order. */

struct data_entry *randomize_entry_order(struct data_entry *entry)
{
  long i, nol;
  struct data_entry *temp, fake, newlist, *prev;

  if (entry == NULL)
    return(NULL);

  temp = entry;

  for (nol = 0; temp != NULL; temp = temp->next) 
    nol++; 
  
  fake.next = entry;
  newlist.next = NULL;
  prev = &newlist;

  for (;nol; nol--)
    {
      temp = &fake;
      for (i = orand() % nol; i > 0; i--)
	temp = temp->next;
      prev->next = temp->next;
      prev = prev->next;
      temp->next = temp->next->next;
      prev->next= NULL;
    }

  return(newlist.next);
}

/* set_buffer - sets the buffer size of an entries-file or turns on
   LOADMODE_ALL if buffer == 0. */

void set_buffer(struct entries *ent, long buffer)
{

  /* set buffered input */
  if (buffer > 0)
    {
      ent->flags.loadmode = LOADMODE_BUFFER;
      ent->buffer = buffer;
    }
  else
    ent->flags.loadmode = LOADMODE_ALL;
}



/* set_teach_params - sets values in teaching parameter structure based
   on values given in codebook and data files */

int set_teach_params(struct teach_params *params, struct entries *codes, 
		     struct entries *data, long dbuffer)
{
  int error = 0;
  
  /* set data buffer size */
  if (data != NULL)
    set_buffer(data, dbuffer);

  /* Load all codes to memory. This should be on for codebook files */
  set_buffer(codes, 0);

  params->topol = codes->topol;
  params->mapdist = NULL;
  params->neigh = codes->neigh;
  params->neigh_adapt = NULL;

  /* these two might change when using a different variation, for
     example, dot product */

  params->winner = find_winner_euc;
  params->dist = vector_dist_euc;
  params->vector_adapt = adapt_vector;

  if (codes) 
    params->codes = codes;
  if (data)
    params->data = data;
  params->snapshot = NULL;

  return error;
}

struct typelist topol_list[] = {
  {TOPOL_DATA, "data", NULL},
  {TOPOL_LVQ, "lvq", NULL},
  {TOPOL_HEXA, "hexa", NULL},
  {TOPOL_RECT, "rect", NULL},
  {TOPOL_UNKNOWN, NULL, NULL}};

/* neighborhood types */

struct typelist neigh_list[] = {
  {NEIGH_BUBBLE, "bubble", NULL},
  {NEIGH_GAUSSIAN, "gaussian", NULL},
  {NEIGH_UNKNOWN, NULL, NULL}};

/* snapshots */

struct typelist snapshot_list[] = {
  {SNAPSHOT_SAVEFILE, "file", NULL},
#ifndef NO_PIPED_COMMANDS
  {SNAPSHOT_EXEC_CMD, "command", NULL},
  {SNAPSHOT_EXEC_CMD_ASYNC, "command_async", NULL},
#endif /* NO_PIPED_COMMANDS */
  {SNAPSHOT_SAVEFILE, NULL, NULL}}; /* default */

int label_not_needed(int level)
{
  static int label_level = 0;

  if (level >= 0) {
    label_level = level;
  }

  return(label_level);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -