Joseph Redmon
2015-08-11 d0b9326a352ed2fbc3ae66fdef40b4533a2f211d
src/data.c
@@ -8,7 +8,7 @@
unsigned int data_seed;
struct load_args{
typedef struct load_args{
    char **paths;
    int n;
    int m;
@@ -22,7 +22,10 @@
    int classes;
    int background;
    data *d;
};
    char *path;
    image *im;
    image *resized;
} load_args;
list *get_paths(char *filename)
{
@@ -49,6 +52,33 @@
    return random_paths;
}
char **find_replace_paths(char **paths, int n, char *find, char *replace)
{
    char **replace_paths = calloc(n, sizeof(char*));
    int i;
    for(i = 0; i < n; ++i){
        char *replaced = find_replace(paths[i], find, replace);
        replace_paths[i] = copy_string(replaced);
    }
    return replace_paths;
}
matrix load_image_paths_gray(char **paths, int n, int w, int h)
{
    int i;
    matrix X;
    X.rows = n;
    X.vals = calloc(X.rows, sizeof(float*));
    X.cols = 0;
    for(i = 0; i < n; ++i){
        image im = load_image(paths[i], w, h, 1);
        X.vals[i] = im.data;
        X.cols = im.h*im.w*im.c;
    }
    return X;
}
matrix load_image_paths(char **paths, int n, int w, int h)
{
    int i;
@@ -110,7 +140,7 @@
void fill_truth_detection(char *path, float *truth, int classes, int num_boxes, int flip, int background, float dx, float dy, float sx, float sy)
{
    char *labelpath = find_replace(path, "detection_images", "labels");
    char *labelpath = find_replace(path, "JPEGImages", "labels");
    labelpath = find_replace(labelpath, ".jpg", ".txt");
    labelpath = find_replace(labelpath, ".JPEG", ".txt");
    int count = 0;
@@ -167,14 +197,12 @@
        h = constrain(0, 1, h);
        if (w < .01 || h < .01) continue;
        if(1){
            //w = sqrt(w);
            //h = sqrt(h);
            w = pow(w, 1./2.);
            h = pow(h, 1./2.);
        }
        int index = (i+j*num_boxes)*(4+classes+background);
        //if(truth[index+classes+background+2]) continue;
        if(truth[index+classes+background+2]) continue;
        if(background) truth[index++] = 0;
        truth[index+id] = 1;
        index += classes;
@@ -302,7 +330,7 @@
            ++count;
        }
    }
    if(count != 1) printf("%d, %s\n", count, path);
    if(count != 1) printf("Too many or too few labels: %d, %s\n", count, path);
}
matrix load_labels_paths(char **paths, int n, char **labels, int k)
@@ -426,21 +454,47 @@
        int flip = rand_r(&data_seed)%2;
        image cropped = crop_image(orig, pleft, ptop, swidth, sheight);
        float dx = ((float)pleft/ow)/sx;
        float dy = ((float)ptop /oh)/sy;
        free_image(orig);
        image sized = resize_image(cropped, w, h);
        free_image(cropped);
        if(flip) flip_image(sized);
        d.X.vals[i] = sized.data;
        fill_truth_detection(random_paths[i], d.y.vals[i], classes, num_boxes, flip, background, dx, dy, 1./sx, 1./sy);
        free_image(orig);
        free_image(cropped);
    }
    free(random_paths);
    return d;
}
void *load_image_in_thread(void *ptr)
{
    load_args a = *(load_args*)ptr;
    free(ptr);
    *(a.im) = load_image_color(a.path, 0, 0);
    *(a.resized) = resize_image(*(a.im), a.w, a.h);
    return 0;
}
pthread_t load_image_thread(char *path, image *im, image *resized, int w, int h)
{
    pthread_t thread;
    struct load_args *args = calloc(1, sizeof(struct load_args));
    args->path = path;
    args->w = w;
    args->h = h;
    args->im = im;
    args->resized = resized;
    if(pthread_create(&thread, 0, load_image_in_thread, args)) {
        error("Thread creation failed");
    }
    return thread;
}
void *load_localization_thread(void *ptr)
{
    printf("Loading data: %d\n", rand_r(&data_seed));
@@ -497,6 +551,21 @@
    return thread;
}
data load_data_writing(char **paths, int n, int m, int w, int h)
{
    if(m) paths = get_random_paths(paths, n, m);
    char **replace_paths = find_replace_paths(paths, n, ".png", "-label.png");
    data d;
    d.shallow = 0;
    d.X = load_image_paths(paths, n, w, h);
    d.y = load_image_paths_gray(replace_paths, n, w/8, h/8);
    if(m) free(paths);
    int i;
    for(i = 0; i < n; ++i) free(replace_paths[i]);
    free(replace_paths);
    return d;
}
data load_data(char **paths, int n, int m, char **labels, int k, int w, int h)
{
    if(m) paths = get_random_paths(paths, n, m);