前言:

参考文献依然是放前面:https://blog.csdn.net/caicaiatnbu/category_9096319.html

但我主要是以这个版本的darknet【 https://github.com/AlexeyAB/darknet 】,与原始的版本还是有一点区别的。

步入正题,utils主要是进行一些读写操作。

读代码好长。。抽空读,中间有两次突然断电,注释都没保存,后面也懒得补了,我会坚持的!努力学C++

1. utils.h

#ifndef UTILS_H
#define UTILS_H
#include "darknet.h"
#include "list.h"

#include <stdio.h>
#include <time.h>

#ifndef M_PI
#define M_PI       3.14159265358979323846   // pi
#endif

#ifdef __cplusplus
extern "C" {  //这个在list的解析里提到过,要以C的方式链接
#endif

LIB_API void free_ptrs(void **ptrs, int n);
LIB_API void top_k(float *a, int n, int k, int *index);

void *xmalloc(size_t size);
void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *ptr, size_t size);


double what_time_is_it_now();//获得当前的时间
int *read_map(char *filename);//
void shuffle(void *arr, size_t n, size_t size);//对数组进行洗牌操作
void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections);
char *basecfg(char *cfgfile);
int alphanum_to_int(char c);
char int_to_alphanum(int i);
int read_int(int fd);
void write_int(int fd, int n);
void read_all(int fd, char *buffer, size_t bytes);
void write_all(int fd, char *buffer, size_t bytes);
int read_all_fail(int fd, char *buffer, size_t bytes);
int write_all_fail(int fd, char *buffer, size_t bytes);
LIB_API void find_replace(const char* str, char* orig, char* rep, char* output);
void replace_image_to_label(const char* input_path, char* output_path);
void error(const char *s);
void malloc_error();
void calloc_error();
void realloc_error();
void file_error(char *s);
void strip(char *s);
void strip_args(char *s);
void strip_char(char *s, char bad);
list *split_str(char *s, char delim);
char *fgetl(FILE *fp);
list *parse_csv_line(char *line);
char *copy_string(char *s);
int count_fields(char *line);
float *parse_fields(char *line, int n);
void normalize_array(float *a, int n);
void scale_array(float *a, int n, float s);
void translate_array(float *a, int n, float s);
int max_index(float *a, int n);
int top_max_index(float *a, int n, int k);
float constrain(float min, float max, float a);
int constrain_int(int a, int min, int max);
float mse_array(float *a, int n);
float rand_normal();
size_t rand_size_t();
float rand_uniform(float min, float max);
float rand_scale(float s);
int rand_int(int min, int max);
float sum_array(float *a, int n);
float mean_array(float *a, int n);
void mean_arrays(float **a, int n, int els, float *avg);
float variance_array(float *a, int n);
float mag_array(float *a, int n);
float mag_array_skip(float *a, int n, int * indices_to_skip);
float dist_array(float *a, float *b, int n, int sub);
float **one_hot_encode(float *a, int n, int k);
float sec(clock_t clocks);
int find_int_arg(int argc, char **argv, char *arg, int def);
float find_float_arg(int argc, char **argv, char *arg, float def);
int find_arg(int argc, char* argv[], char *arg);
char *find_char_arg(int argc, char **argv, char *arg, char *def);
int sample_array(float *a, int n);
int sample_array_custom(float *a, int n);
void print_statistics(float *a, int n);
unsigned int random_gen_fast(void);
float random_float_fast();
int rand_int_fast(int min, int max);
unsigned int random_gen();
float random_float();
float rand_uniform_strong(float min, float max);
float rand_precalc_random(float min, float max, float random_part);
double double_rand(void);
unsigned int uint_rand(unsigned int less_than);
int check_array_is_nan(float *arr, int size);
int check_array_is_inf(float *arr, int size);
int int_index(int *a, int val, int n);
int *random_index_order(int min, int max);
int max_int_index(int *a, int n);
boxabs box_to_boxabs(const box* b, const int img_w, const int img_h, const int bounds_check);
int make_directory(char *path, int mode);

#define max_val_cmp(a,b) (((a) > (b)) ? (a) : (b))
#define min_val_cmp(a,b) (((a) < (b)) ? (a) : (b))

#ifdef __cplusplus
}
#endif

#endif

2. utils.c

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#include <assert.h>
#include <float.h>
#include <limits.h>
#include "darkunistd.h"
#ifdef WIN32
#include "gettimeofday.h"
#else
#include <sys/time.h>
#include <sys/stat.h>
#endif


#ifndef USE_CMAKE_LIBS
#pragma warning(disable: 4996)
#endif

//malloc构建size大小的存储空间
void *xmalloc(size_t size) {
    void *ptr=malloc(size);
    if(!ptr) {
        malloc_error();
    }
    return ptr;
}


//calloc构建size大小的存储空间
void *xcalloc(size_t nmemb, size_t size) {
    void *ptr=calloc(nmemb,size);
    if(!ptr) {
        calloc_error();
    }
    return ptr;
}

//realloc扩大存储空间
void *xrealloc(void *ptr, size_t size) {
    ptr=realloc(ptr,size);
    if(!ptr) {
        realloc_error();
    }
    return ptr;
}

// 获得当前时间,到秒的时间戳,并对微秒进行四舍五入。
double what_time_is_it_now()
{
    /*
    struct timeval
    {
        __time_t tv_sec;        /* Seconds. */
        __suseconds_t tv_usec;  /* Microseconds. */
     };
    tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。即当前时间距Epoch时间tv_sec秒,tv_usec微秒。
    */

    struct timeval time; 
    //int gettimeofday(struct  timeval*tv,struct  timezone *tz )
    //gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
    //在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。
    //获取时间
    if (gettimeofday(&time, NULL)) {
        return 0;
    }
    // tv_usec 获取到微秒,需要10^-6转换为秒
    return (double)time.tv_sec + (double)time.tv_usec * .000001;
}

// 读取指定文件中,每一行的最开始的整数,保存到一维数组中,数组长度为文件的有效行数。
int *read_map(char *filename)
{
    int n = 0;
    int *map = 0;
    char *str;
    FILE *file = fopen(filename, "r");//打开文件
    if(!file) file_error(filename);//判断是否成功打开
    //fgetl:从文件中读取一行数据,并去掉行末的换行符
    while((str=fgetl(file))){//逐行获得数据
        ++n;
        map = (int*)xrealloc(map, n * sizeof(int));//realloc扩大数据空间,size为数据的长度
        map[n-1] = atoi(str);//将字符串转换为整型数据【跳过空格,tab】,存放到map末尾中
        free(str);
    }
    if (file) fclose(file);
    return map;
}

// 对每一个sections进行洗牌操作
//在64位系统中为long long unsigned int,非64位系统中为long unsigned int
//在某些情况下,使用size_t类型是更为有效,比习惯性使用无符号类型的程序会更安全。
void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections)
{
    size_t i;
    for(i = 0; i < sections; ++i){
        size_t start = n*i/sections;
        size_t end = n*(i+1)/sections;
        size_t num = end-start;
        shuffle((char*)arr+(start*size), num, size);// shuffle对数组进行洗牌操作
    }
}

// 对数组进行洗牌操作
void shuffle(void *arr, size_t n, size_t size)
{
    size_t i;
    void* swp = (void*)xcalloc(1, size); //分配一个1个长度为size的连续空间
    for(i = 0; i < n-1; ++i){
        size_t j = i + random_gen()/(RAND_MAX / (n-i)+1);//进行洗牌操作【?】
        memcpy(swp,            (char*)arr+(j*size), size);
        memcpy((char*)arr+(j*size), (char*)arr+(i*size), size);
        memcpy((char*)arr+(i*size), swp,          size);
    }
    free(swp);
}

// 就是删除,从第index+1开始,到argc结束,将argv元素向左移动,最后一个元素置null
void del_arg(int argc, char **argv, int index)
{
    int i;
    for(i = index; i < argc-1; ++i) argv[i] = argv[i+1];
    argv[i] = 0;
}

// 删除指定字符串 arg,其后面的字符串往左移动
int find_arg(int argc, char* argv[], char *arg)
{
    int i;
    for(i = 0; i < argc; ++i) {
        if(!argv[i]) continue;
        if(0==strcmp(argv[i], arg)) {//如果有arg相同的字符
            del_arg(argc, argv, i);//删除【这里应该只能删除第一个碰到的相同字符吧?】
            return 1;
        }
    }
    return 0;
}

//查找字符串arg, 后面的参数往左一定两个位置(删除)
int find_int_arg(int argc, char **argv, char *arg, int def)
{
    int i;
    for(i = 0; i < argc-1; ++i){//在这个范围内
        if(!argv[i]) continue;
        if(0==strcmp(argv[i], arg)){//找到了目标值
            def = atoi(argv[i+1]);目标值后一位转为int【?】
            del_arg(argc, argv, i);
            del_arg(argc, argv, i);//这里删除目标值和目标值后一位【?感觉应该是读cfg文件时要用到的,要继续往后看】
            break;
        }
    }
    return def;//返回目标值
}

//同上int 
float find_float_arg(int argc, char **argv, char *arg, float def)
{
    int i;
    for(i = 0; i < argc-1; ++i){
        if(!argv[i]) continue;
        if(0==strcmp(argv[i], arg)){
            def = atof(argv[i+1]);
            del_arg(argc, argv, i);
            del_arg(argc, argv, i);
            break;
        }
    }
    return def;
}

//同上int
char *find_char_arg(int argc, char **argv, char *arg, char *def)
{
    int i;
    for(i = 0; i < argc-1; ++i){
        if(!argv[i]) continue;
        if(0==strcmp(argv[i], arg)){
            def = argv[i+1];
            del_arg(argc, argv, i);
            del_arg(argc, argv, i);
            break;
        }
    }
    return def;
}


// 提取cfg文件的文件名,
//比如cfg/yolo-tiny.cfg-->yolo-tiny.cfg
//cfg\\yolo-tiny.cfg-->yolo-tiny.cfg
char *basecfg(char *cfgfile)
{
    char *c = cfgfile;
    char *next;
    // 查找到最后一个 '/', 只要最后一个'/'后面的内容
    while((next = strchr(c, '/')))
    {
        c = next+1;
    }
    //查找到最后一个 '\\', 只要最后一个'\\'后面的内容
    if(!next) while ((next = strchr(c, '\\'))) { c = next + 1; }
    c = copy_string(c);//将文件名给c
    next = strchr(c, '.');//yolo-tiny.cfg-->next=.cfg
    if (next) *next = 0;//next 部分置0
    return c;
}

// 字符转整型
int alphanum_to_int(char c)
{
    return (c < 58) ? c - 48 : c-87;
}

// 整型转字符
char int_to_alphanum(int i)
{
    if (i == 36) return '.';
    return (i < 10) ? i + 48 : i + 87;
}

// 可视化float数组A,M×N列
void pm(int M, int N, float *A)
{
    int i,j;
    for(i =0 ; i < M; ++i){
        printf("%d ", i+1);
        for(j = 0; j < N; ++j){
            printf("%2.4f, ", A[i*N+j]);
        }
        printf("\n");
    }
    printf("\n");
}

// 判断str中是否出现子串orig,若出现则进行替代;
void find_replace(const char* str, char* orig, char* rep, char* output)
{
    char* buffer = (char*)calloc(8192, sizeof(char));
    char *p;

    sprintf(buffer, "%s", str);
    // 判断orig是否是buffer的子串,若是返回orig在buffer的起始地址,否则返回null
    if (!(p = strstr(buffer, orig))) {  // Is 'orig' even in 'str'?
        sprintf(output, "%s", buffer);
        free(buffer);
        return;
    }

    *p = '\0';// sprintf拼接必须是'\0'结尾的字符数组;
    //将origin用rep代替
    sprintf(output, "%s%s%s", buffer, rep, p + strlen(orig));
    free(buffer);
}

//将读取的字符串进行精简,删掉最开始和最末尾可能存在的' '和'\t'
//比如' \t   yolo-tiny   \t'-->'yolo-tiny'
void trim(char *str)
{
    char* buffer = (char*)xcalloc(8192, sizeof(char));
    sprintf(buffer, "%s", str);

    char *p = buffer;
    while (*p == ' ' || *p == '\t') ++p;//判断str中开头部分是否有' '和'\t'如果有就跳过

    char *end = p + strlen(p) - 1;//排除开头的空格部分后,找到p的最末尾
    while (*end == ' ' || *end == '\t') { //判断str中,从末尾开始是否有' '和'\t'如果有就截掉
        *end = '\0';
        --end;//重新设定末尾位置
    }
    sprintf(str, "%s", p);

    free(buffer);
}


void find_replace_extension(char *str, char *orig, char *rep, char *output)
{
    char* buffer = (char*)calloc(8192, sizeof(char));//缓冲空间,中转站

    sprintf(buffer, "%s", str);
    char *p = strstr(buffer, orig);// 判断orig是否是buffer的子串,若是返回orig在buffer的起始地址,否则返回null给p
    int offset = (p - buffer);//获得子串的位移长度,子串起始地址到buffer的起始地址
    int chars_from_end = strlen(buffer) - offset;//获得从子串起始地址到末尾的长度
    //如果p==null或者获得从子串起始地址到末尾的长度不等于子串的长度,则返回orig在buffer的起始地址,否则返回null
    if (!p || chars_from_end != strlen(orig)) {  // Is 'orig' even in 'str' AND is 'orig' found at the end of 'str'?
        sprintf(output, "%s", buffer);
        free(buffer);
        return;
    }

    *p = '\0';

    //将origin用rep代替
    sprintf(output, "%s%s%s", buffer, rep, p + strlen(orig));
    free(buffer);
}

void replace_image_to_label(const char* input_path, char* output_path)
{
    find_replace(input_path, "/images/train2014/", "/labels/train2014/", output_path);    // COCO
    find_replace(output_path, "/images/val2014/", "/labels/val2014/", output_path);        // COCO
    find_replace(output_path, "/JPEGImages/", "/labels/", output_path);    // PascalVOC
    find_replace(output_path, "\\images\\train2014\\", "\\labels\\train2014\\", output_path);    // COCO
    find_replace(output_path, "\\images\\val2014\\", "\\labels\\val2014\\", output_path);        // COCO
    find_replace(output_path, "\\JPEGImages\\", "\\labels\\", output_path);    // PascalVOC
    //find_replace(output_path, "/images/", "/labels/", output_path);    // COCO
    //find_replace(output_path, "/VOC2007/JPEGImages/", "/VOC2007/labels/", output_path);        // PascalVOC
    //find_replace(output_path, "/VOC2012/JPEGImages/", "/VOC2012/labels/", output_path);        // PascalVOC

    //find_replace(output_path, "/raw/", "/labels/", output_path);
    trim(output_path);

    // replace only ext of files
    find_replace_extension(output_path, ".jpg", ".txt", output_path);
    find_replace_extension(output_path, ".JPG", ".txt", output_path); // error
    find_replace_extension(output_path, ".jpeg", ".txt", output_path);
    find_replace_extension(output_path, ".JPEG", ".txt", output_path);
    find_replace_extension(output_path, ".png", ".txt", output_path);
    find_replace_extension(output_path, ".PNG", ".txt", output_path);
    find_replace_extension(output_path, ".bmp", ".txt", output_path);
    find_replace_extension(output_path, ".BMP", ".txt", output_path);
    find_replace_extension(output_path, ".ppm", ".txt", output_path);
    find_replace_extension(output_path, ".PPM", ".txt", output_path);
    find_replace_extension(output_path, ".tiff", ".txt", output_path);
    find_replace_extension(output_path, ".TIFF", ".txt", output_path);

    // Check file ends with txt:
    if(strlen(output_path) > 4) {
        char *output_path_ext = output_path + strlen(output_path) - 4;
        if( strcmp(".txt", output_path_ext) != 0){
            fprintf(stderr, "Failed to infer label file name (check image extension is supported): %s \n", output_path);
        }
    }else{
        fprintf(stderr, "Label file name is too short: %s \n", output_path);
    }
}

float sec(clock_t clocks)
{
    return (float)clocks/CLOCKS_PER_SEC;
}

void top_k(float *a, int n, int k, int *index)
{
    int i,j;
    for(j = 0; j < k; ++j) index[j] = -1;
    for(i = 0; i < n; ++i){
        int curr = i;
        for(j = 0; j < k; ++j){
            if((index[j] < 0) || a[curr] > a[index[j]]){
                int swap = curr;
                curr = index[j];
                index[j] = swap;
            }
        }
    }
}

void error(const char *s)
{
    perror(s);
    assert(0);
    exit(EXIT_FAILURE);
}

void malloc_error()
{
    fprintf(stderr, "xMalloc error\n");
    exit(EXIT_FAILURE);
}

void calloc_error()
{
    fprintf(stderr, "Calloc error\n");
    exit(EXIT_FAILURE);
}

void realloc_error()
{
    fprintf(stderr, "Realloc error\n");
    exit(EXIT_FAILURE);
}

void file_error(char *s)
{
    fprintf(stderr, "Couldn't open file: %s\n", s);
    exit(EXIT_FAILURE);
}

list *split_str(char *s, char delim)
{
    size_t i;
    size_t len = strlen(s);
    list *l = make_list();
    list_insert(l, s);
    for(i = 0; i < len; ++i){
        if(s[i] == delim){
            s[i] = '\0';
            list_insert(l, &(s[i+1]));
        }
    }
    return l;
}

void strip(char *s)
{
    size_t i;
    size_t len = strlen(s);
    size_t offset = 0;
    for(i = 0; i < len; ++i){
        char c = s[i];
        if(c==' '||c=='\t'||c=='\n'||c =='\r'||c==0x0d||c==0x0a) ++offset;
        else s[i-offset] = c;
    }
    s[len-offset] = '\0';
}


void strip_args(char *s)
{
    size_t i;
    size_t len = strlen(s);
    size_t offset = 0;
    for (i = 0; i < len; ++i) {
        char c = s[i];
        if (c == '\t' || c == '\n' || c == '\r' || c == 0x0d || c == 0x0a) ++offset;
        else s[i - offset] = c;
    }
    s[len - offset] = '\0';
}

void strip_char(char *s, char bad)
{
    size_t i;
    size_t len = strlen(s);
    size_t offset = 0;
    for(i = 0; i < len; ++i){
        char c = s[i];
        if(c==bad) ++offset;
        else s[i-offset] = c;
    }
    s[len-offset] = '\0';
}

void free_ptrs(void **ptrs, int n)
{
    int i;
    for(i = 0; i < n; ++i) free(ptrs[i]);
    free(ptrs);
}

// 读取指定文件中的一行字符
char *fgetl(FILE *fp)
{
    // feof()检测流文件指针是否已到文件结尾。文件读取结束,返回非0,否则返回0.
    if(feof(fp)) return 0;
    size_t size = 512;// 申请512个字符空间
    char* line = (char*)xmalloc(size * sizeof(char));

    //fgets 读取文件中一行的数据,存放到line中.读取失败或读取到换行符或读取到文件末尾返回,读取fgets失败返回null
    if(!fgets(line, size, fp)){
        free(line);
        return 0;
    }

    size_t curr = strlen(line);//获得当前读取数据的curr_size

    //读取未满一行时,最后一个字符不为'\n'且文件流文件指针没有到结尾
    while((line[curr-1] != '\n') && !feof(fp)){
        //如果curr_size的大小等于之前构建的字符空间,那么进行扩容
        if(curr == size-1){
            size *= 2;
            line = (char*)xrealloc(line, size * sizeof(char));
        }
        size_t readsize = size-curr;//可读size获得
        if(readsize > INT_MAX) readsize = INT_MAX-1; //判断界限,不能超过INTMAX,上溢
        fgets(&line[curr], readsize, fp);//继续读取剩余部分的数据
        curr = strlen(line);//获得新的数据size
    }
    // 手动字符串补0
    //index n-1处为'\n'表示读完了一行,将其修改为'\0',因为字符数组默认结果为'\0',需手动补上
    if(curr >= 2)
        if(line[curr-2] == 0x0d) line[curr-2] = 0x00;
    if(curr >= 1)
        if(line[curr-1] == 0x0a) line[curr-1] = 0x00;

    return line;
}

//读整型数据
int read_int(int fd)
{
    int n = 0;
    // next为成功读取的字节数,如果为-1,则表示读取失败,若调read之前已经到达文件末尾,则这次read返回0.
    int next = read(fd, &n, sizeof(int));
    if(next <= 0) return -1;
    return n;
}

// 向文件fd中写入一个整数
void write_int(int fd, int n)
{
    int next = write(fd, &n, sizeof(int));
    if(next <= 0) error("read failed");
}

// 从fd文件中,读取bytes个字节数据到内存中buffer起始,直到读完
int read_all_fail(int fd, char *buffer, size_t bytes)
{
    size_t n = 0;
    while(n < bytes){
        int next = read(fd, buffer + n, bytes-n);
        if(next <= 0) return 1;
        n += next;
    }
    return 0;
}

// 将内存中buffer起始的bytes个字节数据写入到fd文件中,直到写完
int write_all_fail(int fd, char *buffer, size_t bytes)
{
    size_t n = 0;
    while(n < bytes){
        // 将buffer+n 所指向的内存写入bytes-n个字节到fd文件中
        size_t next = write(fd, buffer + n, bytes-n);
        //如果写入成功,会返回写入的字节数,即返回 bytes,否则,返回-1【写入失败】
        if(next <= 0) return 1;
        n += next;//更新读取的字节数直到满足 bytes的要求
    }
    return 0;
}

// 将内存中buffer起始的bytes个字节数据写入到fd文件中,一次性操作,若写不完,则报错
void read_all(int fd, char *buffer, size_t bytes)
{
    size_t n = 0;
    while(n < bytes){
        int next = read(fd, buffer + n, bytes-n);
        if(next <= 0) error("read failed");
        n += next;
    }
}

// 从fd文件中,读取bytes个字节数据到内存中buffer起始,一次性操作,若读不完,则报错
void write_all(int fd, char *buffer, size_t bytes)
{
    size_t n = 0;
    while(n < bytes){
        size_t next = write(fd, buffer + n, bytes-n);
        if(next <= 0) error("write failed");
        n += next;
    }
}

// 字符数组的复制操作
char *copy_string(char *s)
{
    if(!s) {
        return NULL;
    }
    char* copy = (char*)xmalloc(strlen(s) + 1);//空间
    //将以s地址开始的前strlen(s) + 1 个字节复制到copy所指向的数组中
    strncpy(copy, s, strlen(s)+1);
    return copy;
}

// 解析某种特定格式,如: "******, "222,"32242, "342423
list *parse_csv_line(char *line)
{
    list *l = make_list();// 链表初始化
    char *c, *p;
    int in = 0;
    // 遍历字符数组line
    for(c = line, p = line; *c != '\0'; ++c){
        if(*c == '"') in = !in;//如果有",则in=1
        else if(*c == ',' && !in){//如果有,且in=0-->遇到','截断
            *c = '\0';//将该位置截止符
            //用list插入函数将得到的字符串,放入l中
            list_insert(l, copy_string(p));
            p = c+1;//更新循环信息
        }
    }
    list_insert(l, copy_string(p));//插入最后的剩余部分
    return l;
}

// 统计字符数组中有多少','字符以及'\0'
int count_fields(char *line)
{
    int count = 0;
    int done = 0;
    char *c;
    for(c = line; !done; ++c){
        done = (*c == '\0');//判断是否结尾【?】
        if(*c == ',' || done) ++count;
    }
    return count;
}

// 解析字符数组中的float实数
float *parse_fields(char *line, int n)
{
    float* field = (float*)xcalloc(n, sizeof(float));//申请空间
    char *c, *p, *end;
    int count = 0;
    int done = 0;
    for(c = line, p = line; !done; ++c){
        done = (*c == '\0');//判断是否到末尾
        if(*c == ',' || done){//如果到末尾或者遇到','字符,进入循环,如果没有碰到则将c指针加一,获取下一字符,这时候p是保持不变的;
            *c = '\0';//将字符截断
//strtod将字符串转换为float类型 ,会跳过前面的空格字符,直到遇到数字或者+/- 才开始转换,到出现非数字或者字符串结束符'\0'结束;end,
            field[count] = strtod(p, &end); //将截断的字符中可转换的字符转换为float型

// 如果转换数字开始位置指针p 和 工作指针c指向同一地址,表明,一开始就遇到','或者'\0',肯定不合理,使用nan函数
            if(p == c) field[count] = nan("");


            if(end != c && (end != c-1 || *end != '\r')) field[count] = nan(""); //DOS file formats!【?这里不太懂】
            p = c+1;//这一段转换结束后,将p起始位置放到与c一样的时候
            ++count;//并且统计截断字符的次数
        }
    }
    return field;
}

// 对float一维数组求和,比较好理解
float sum_array(float *a, int n)
{
    int i;
    float sum = 0;
    for(i = 0; i < n; ++i) sum += a[i];
    return sum;
}

//对float一维数组计算均值
float mean_array(float *a, int n)
{
    return sum_array(a,n)/n;
}

// 求二维float数组,每一列的平均值,保存在avg一维float数组中
void mean_arrays(float **a, int n, int els, float *avg)
{
    int i;
    int j;
    memset(avg, 0, els*sizeof(float));
    for(j = 0; j < n; ++j){
        for(i = 0; i < els; ++i){
            avg[i] += a[j][i];
        }
    }
    for(i = 0; i < els; ++i){
        avg[i] /= n;
    }
}

// 计算向量a的mean和var,并输出
void print_statistics(float *a, int n)
{
    float m = mean_array(a, n);
    float v = variance_array(a, n);
    printf("MSE: %.6f, Mean: %.6f, Variance: %.6f\n", mse_array(a, n), m, v);
}

// 对float一维数组求方差
float variance_array(float *a, int n)
{
    int i;
    float sum = 0;
    float mean = mean_array(a, n);
    for(i = 0; i < n; ++i) sum += (a[i] - mean)*(a[i]-mean);//计算方差
    float variance = sum/n;
    return variance;
}

// 判断整数a 与 区间整数[min, max]的关系,超过区间返回区间上下限
int constrain_int(int a, int min, int max)
{
    if (a < min) return min;
    if (a > max) return max;
    return a;
}

// 判断小数a 与 区间小数[min, max]的关系,超过区间返回区间上下限
float constrain(float min, float max, float a)
{
    if (a < min) return min;
    if (a > max) return max;
    return a;
}

// 当sub为1的时候,其实就是计算两个一维数组的欧式距离
float dist_array(float *a, float *b, int n, int sub)
{
    int i;
    float sum = 0;
    for(i = 0; i < n; i += sub) sum += pow(a[i]-b[i], 2);
    return sqrt(sum);
}
// 向量的模长除以N
float mse_array(float *a, int n)
{
    int i;
    float sum = 0;
    for(i = 0; i < n; ++i) sum += a[i]*a[i];
    return sqrt(sum/n);
}

// 归一化操作
void normalize_array(float *a, int n)
{
    int i;
    float mu = mean_array(a,n);//均值
    float sigma = sqrt(variance_array(a,n));//方差
    for(i = 0; i < n; ++i){
        a[i] = (a[i] - mu)/sigma;//归一化
    }
    mu = mean_array(a,n);//重新得到均值
    sigma = sqrt(variance_array(a,n));//重新得到方差
}

//向量平移
void translate_array(float *a, int n, float s)
{
    int i;
    for(i = 0; i < n; ++i){
        a[i] += s;
    }
}

//向量取模
float mag_array(float *a, int n)
{
    int i;
    float sum = 0;
    for(i = 0; i < n; ++i){
        sum += a[i]*a[i];
    }
    return sqrt(sum);
}

//这里就是取需要的数据向量的模
// indicies to skip is a bit array
float mag_array_skip(float *a, int n, int * indices_to_skip)
{
    int i;
    float sum = 0;
    for (i = 0; i < n; ++i) {
        if (indices_to_skip[i] != 1) {
            sum += a[i] * a[i];
        }
    }
    return sqrt(sum);
}


// 向量的与实数的乘法
void scale_array(float *a, int n, float s)
{
    int i;
    for(i = 0; i < n; ++i){
        a[i] *= s;
    }
}


// 从向量中采用一个数,返回其index
int sample_array(float *a, int n)
{
    float sum = sum_array(a, n);
    scale_array(a, n, 1. / sum);
    float r = rand_uniform(0, 1);//随机生成0-1的数
    int i;
    for (i = 0; i < n; ++i) {
        r = r - a[i];//满足条件计算
        if (r <= 0) return i;//若满足则返回值的位置
    }
    return n - 1;
}

//与上面函数是一个功能,只是需要满足的条件不同,
int sample_array_custom(float *a, int n)
{
    float sum = sum_array(a, n);
    scale_array(a, n, 1./sum);
    float r = rand_uniform(0, 1);
    int start_index = rand_int(0, 0);
    int i;
    for(i = 0; i < n; ++i){
        r = r - a[(i + start_index) % n];
        if (r <= 0) return i;
    }
    return n-1;
}
// 一维int数组找最大值操作,若找到最大值,返回index,否则,返回-1;
int max_index(float *a, int n)
{
    if(n <= 0) return -1;
    int i, max_i = 0;
    float max = a[0];
    for(i = 1; i < n; ++i){
        if(a[i] > max){
            max = a[i];
            max_i = i;
        }
    }
    return max_i;
}


int top_max_index(float *a, int n, int k)
{
    if (n <= 0) return -1;
    float *values = (float*)xcalloc(k, sizeof(float));
    int *indexes = (int*)xcalloc(k, sizeof(int));//构建临时空间
    int i, j;
    for (i = 0; i < n; ++i) {
        for (j = 0; j < k; ++j) {
            if (a[i] > values[j]) {//局部最大值【?】
                values[j] = a[i];
                indexes[j] = i;
                break;
            }
        }
    }
    int count = 0;
    for (j = 0; j < k; ++j) if (values[j] > 0) count++;//判断局部最大值的个数
    int get_index = rand_int(0, count-1);// 返回一个区间[0, count-1]内的一个整数
    int val = indexes[get_index];//获得该位置的值,并返回
    free(indexes);
    free(values);
    return val;
}

// 一维数组进行查找操作,若查找指定值,返回index,否则,返回-1;
int int_index(int *a, int val, int n)
{
    int i;
    for (i = 0; i < n; ++i) {
        if (a[i] == val) return i;
    }
    return -1;
}


// 返回一个区间[min, max]内的一个整数
int rand_int(int min, int max)
{
    if (max < min){
        int s = min;
        min = max;
        max = s;
    }
    int r = (random_gen()%(max - min + 1)) + min;
    return r;
}

// From http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
// 标准正太分布
float rand_normal()
{
    static int haveSpare = 0;
    static double rand1, rand2;

    if(haveSpare)
    {
        haveSpare = 0;
        return sqrt(rand1) * sin(rand2);
    }

    haveSpare = 1;
    // 获取区间[0,1]内的一个随机数
    rand1 = random_gen() / ((double) RAND_MAX);
    if(rand1 < 1e-100) rand1 = 1e-100;
    rand1 = -2 * log(rand1);
    rand2 = (random_gen() / ((double)RAND_MAX)) * 2.0 * M_PI;

    return sqrt(rand1) * cos(rand2);
}

/*
   float rand_normal()
   {
   int n = 12;
   int i;
   float sum= 0;
   for(i = 0; i < n; ++i) sum += (float)random_gen()/RAND_MAX;
   return sum-n/2.;
   }
 */

size_t rand_size_t()
{
    return  ((size_t)(random_gen()&0xff) << 56) |
            ((size_t)(random_gen()&0xff) << 48) |
            ((size_t)(random_gen()&0xff) << 40) |
            ((size_t)(random_gen()&0xff) << 32) |
            ((size_t)(random_gen()&0xff) << 24) |
            ((size_t)(random_gen()&0xff) << 16) |
            ((size_t)(random_gen()&0xff) << 8) |
            ((size_t)(random_gen()&0xff) << 0);
}

// 进行随机采样操作,从区间[min, max]随机返回一个实数
float rand_uniform(float min, float max)
{
    if(max < min){
        float swap = min;
        min = max;
        max = swap;
    }

//局限范围有效
#if (RAND_MAX < 65536)
        int rnd = rand()*(RAND_MAX + 1) + rand();
        return ((float)rnd / (RAND_MAX*RAND_MAX) * (max - min)) + min;
#else
        return ((float)rand() / RAND_MAX * (max - min)) + min;
#endif
    //return (random_float() * (max - min)) + min;
}


// 随机采用的基础上,50%的概率返回S,50%的概率返回1/s
float rand_scale(float s)
{
    float scale = rand_uniform_strong(1, s);
    if(random_gen()%2) return scale;
    return 1./scale;
}

// 进行one_hot 编码,根据,float数组a中的元素进行编码
float **one_hot_encode(float *a, int n, int k)
{
    int i;
    float** t = (float**)xcalloc(n, sizeof(float*)); // 申请n个float * 指针存储空间
    for(i = 0; i < n; ++i){
        t[i] = (float*)xcalloc(k, sizeof(float)); // 每个t中的元素为一个float 指针,指向k个float
        int index = (int)a[i];// 丢掉小数部分
        t[i][index] = 1; //第index 置为1
    }
    return t;
}

static unsigned int x = 123456789, y = 362436069, z = 521288629;

// Marsaglia's xorshf96 generator: period 2^96-1
unsigned int random_gen_fast(void)
{
    unsigned int t;
    x ^= x << 16; //x*(2^16)  ^  x
    x ^= x >> 5;
    x ^= x << 1;

    t = x;
    x = y;
    y = z;
    z = t ^ x ^ y;

    return z;
}

float random_float_fast()
{
    return ((float)random_gen_fast() / (float)UINT_MAX);
}

int rand_int_fast(int min, int max)
{
    if (max < min) {
        int s = min;
        min = max;
        max = s;
    }
    int r = (random_gen_fast() % (max - min + 1)) + min;
    return r;
}

unsigned int random_gen()
{
    unsigned int rnd = 0;
#ifdef WIN32
    rand_s(&rnd);
#else   // WIN32
    rnd = rand();
#if (RAND_MAX < 65536)
        rnd = rand()*(RAND_MAX + 1) + rnd;
#endif  //(RAND_MAX < 65536)
#endif  // WIN32
    return rnd;
}


float random_float()
{
    unsigned int rnd = 0;
#ifdef WIN32
    rand_s(&rnd);
    return ((float)rnd / (float)UINT_MAX);
#else   // WIN32

    rnd = rand();
#if (RAND_MAX < 65536)
    rnd = rand()*(RAND_MAX + 1) + rnd;
    return((float)rnd / (float)(RAND_MAX*RAND_MAX));
#endif  //(RAND_MAX < 65536)
    return ((float)rnd / (float)RAND_MAX);

#endif  // WIN32
}

float rand_uniform_strong(float min, float max)
{
    if (max < min) {
        float swap = min;
        min = max;
        max = swap;
    }
    return (random_float() * (max - min)) + min;
}

float rand_precalc_random(float min, float max, float random_part)
{
    if (max < min) {
        float swap = min;
        min = max;
        max = swap;
    }
    return (random_part * (max - min)) + min;
}

#define RS_SCALE (1.0 / (1.0 + RAND_MAX))

double double_rand(void)
{
    double d;
    do {
        d = (((rand() * RS_SCALE) + rand()) * RS_SCALE + rand()) * RS_SCALE;
    } while (d >= 1); // Round off
    return d;
}

unsigned int uint_rand(unsigned int less_than)
{
    return (unsigned int)((less_than)* double_rand());
}

//确认数组中是否有值为nan
int check_array_is_nan(float *arr, int size)
{
    int i;
    for (i = 0; i < size; ++i) {
        if (isnan(arr[i])) return 1;
    }
    return 0;
}

//确认数组中是否有浮点数是无限大
int check_array_is_inf(float *arr, int size)
{
    int i;
    for (i = 0; i < size; ++i) {
        if (isinf(arr[i])) return 1;
    }
    return 0;
}

//随机生成无序的整数数组
int *random_index_order(int min, int max)
{
    int *inds = (int *)xcalloc(max - min, sizeof(int));//临时空间
    int i;
    for (i = min; i < max; ++i) {
        inds[i - min] = i;//给数组inds赋值
    }
    for (i = min; i < max - 1; ++i) {
        int swap = inds[i - min];
        int index = i + rand() % (max - i);
        inds[i - min] = inds[index - min];
        inds[index - min] = swap;//交换任意序号的两个值
    }
    return inds;
}

// 一维int数组找最大值操作,若找到最大值,返回index,否则,返回-1;
int max_int_index(int *a, int n)
{
    if (n <= 0) return -1;
    int i, max_i = 0;
    int max = a[0];
    for (i = 1; i < n; ++i) {
        if (a[i] > max) {
            max = a[i];
            max_i = i;
        }
    }
    return max_i;
}


// Absolute box from relative coordinate bounding box and image size

//boxabs的定义我还没找到耶
boxabs box_to_boxabs(const box* b, const int img_w, const int img_h, const int bounds_check)
{
    boxabs ba;
    ba.left = (b->x - b->w / 2.)*img_w;
    ba.right = (b->x + b->w / 2.)*img_w;
    ba.top = (b->y - b->h / 2.)*img_h;
    ba.bot = (b->y + b->h / 2.)*img_h;//获得图片上的box的相对应位置

    if (bounds_check) {//边界限定
        if (ba.left < 0) ba.left = 0;
        if (ba.right > img_w - 1) ba.right = img_w - 1;
        if (ba.top < 0) ba.top = 0;
        if (ba.bot > img_h - 1) ba.bot = img_h - 1;
    }

    return ba;//返回转换的box
}

//创建文件夹
int make_directory(char *path, int mode)
{
#ifdef WIN32
    return _mkdir(path);
#else
    return mkdir(path, mode);
#endif
}