Joseph Redmon
2015-02-11 0f645836f193e75c4c3b718369e6fab15b5d19c5
src/data.c
@@ -6,6 +6,20 @@
#include <stdlib.h>
#include <string.h>
struct load_args{
    char **paths;
    int n;
    int m;
    char **labels;
    int k;
    int h;
    int w;
    int nh;
    int nw;
    int jitter;
    data *d;
};
list *get_paths(char *filename)
{
    char *path;
@@ -19,16 +33,18 @@
    return lines;
}
void fill_truth_detection(char *path, float *truth, int height, int width, int num_height, int num_width, float scale, int dx, int dy)
void fill_truth_detection(char *path, float *truth, int height, int width, int num_height, int num_width, int dy, int dx, int jitter)
{
    int box_height = height/num_height;
    int box_width = width/num_width;
    char *labelpath = find_replace(path, "imgs", "det");
    char *labelpath = find_replace(path, "imgs", "det/train");
    labelpath = find_replace(labelpath, ".JPEG", ".txt");
    FILE *file = fopen(labelpath, "r");
    if(!file) file_error(labelpath);
    int x, y, h, w;
    while(fscanf(file, "%d %d %d %d", &x, &y, &w, &h) == 4){
    float x, y, h, w;
    while(fscanf(file, "%f %f %f %f", &x, &y, &w, &h) == 4){
        x *= width + jitter;
        y *= height + jitter;
        x -= dx;
        y -= dy;
        int i = x/box_width;
@@ -39,17 +55,15 @@
        if(j < 0) j = 0;
        if(j >= num_height) j = num_height-1;
        
        float dw = (float)(x%box_width)/box_height;
        float dh = (float)(y%box_width)/box_width;
        float sh = h/scale;
        float sw = w/scale;
        float dw = (x - i*box_width)/box_width;
        float dh = (y - j*box_height)/box_height;
        //printf("%d %d %f %f\n", i, j, dh, dw);
        int index = (i+j*num_width)*5;
        truth[index++] = 1;
        truth[index++] = dh;
        truth[index++] = dw;
        truth[index++] = sh;
        truth[index++] = sw;
        truth[index++] = h*(height+jitter)/height;
        truth[index++] = w*(width+jitter)/width;
    }
    fclose(file);
}
@@ -58,11 +72,14 @@
{
    int i;
    memset(truth, 0, k*sizeof(float));
    int count = 0;
    for(i = 0; i < k; ++i){
        if(strstr(path, labels[i])){
            truth[i] = 1;
            ++count;
        }
    }
    if(count != 1) printf("%d, %s\n", count, path);
}
matrix load_image_paths(char **paths, int n, int h, int w)
@@ -97,19 +114,19 @@
{
    matrix y = make_matrix(n, k);
    int i;
    for(i = 0; i < n; ++i){
    for(i = 0; i < n && labels; ++i){
        fill_truth(paths[i], labels, k, y.vals[i]);
    }
    return y;
}
matrix load_labels_detection(char **paths, int n, int height, int width, int num_height, int num_width, float scale)
matrix load_labels_detection(char **paths, int n, int height, int width, int num_height, int num_width)
{
    int k = num_height*num_width*5;
    matrix y = make_matrix(n, k);
    int i;
    for(i = 0; i < n; ++i){
        fill_truth_detection(paths[i], y.vals[i], height, width, num_height, num_width, scale,0,0);
        fill_truth_detection(paths[i], y.vals[i], height, width, num_height, num_width, 0, 0, 0);
    }
    return y;
}
@@ -148,7 +165,7 @@
    }
}
data load_data_detection_jitter_random(int n, char **paths, int m, int h, int w, int nh, int nw, float scale)
data load_data_detection_jitter_random(int n, char **paths, int m, int h, int w, int nh, int nw, int jitter)
{
    char **random_paths = get_random_paths(paths, n, m);
    int i;
@@ -158,26 +175,53 @@
    int k = nh*nw*5;
    d.y = make_matrix(n, k);
    for(i = 0; i < n; ++i){
        int dx = rand()%32;
        int dy = rand()%32;
        fill_truth_detection(random_paths[i], d.y.vals[i], 224, 224, nh, nw, scale, dx, dy);
        int dx = rand()%jitter;
        int dy = rand()%jitter;
        fill_truth_detection(random_paths[i], d.y.vals[i], h-jitter, w-jitter, nh, nw, dy, dx, jitter);
        image a = float_to_image(h, w, 3, d.X.vals[i]);
        jitter_image(a,224,224,dy,dx);
        jitter_image(a,h-jitter,w-jitter,dy,dx);
    }
    d.X.cols = 224*224*3;
   // print_matrix(d.y);
    d.X.cols = (h-jitter)*(w-jitter)*3;
    free(random_paths);
    return d;
}
void *load_detection_thread(void *ptr)
{
    struct load_args a = *(struct load_args*)ptr;
    *a.d = load_data_detection_jitter_random(a.n, a.paths, a.m, a.h, a.w, a.nh, a.nw, a.jitter);
    translate_data_rows(*a.d, -128);
    scale_data_rows(*a.d, 1./128);
    free(ptr);
    return 0;
}
data load_data_detection_random(int n, char **paths, int m, int h, int w, int nh, int nw, float scale)
pthread_t load_data_detection_thread(int n, char **paths, int m, int h, int w, int nh, int nw, int jitter, data *d)
{
    pthread_t thread;
    struct load_args *args = calloc(1, sizeof(struct load_args));
    args->n = n;
    args->paths = paths;
    args->m = m;
    args->h = h;
    args->w = w;
    args->nh = nh;
    args->nw = nw;
    args->jitter = jitter;
    args->d = d;
    if(pthread_create(&thread, 0, load_detection_thread, args)) {
        error("Thread creation failed");
    }
    return thread;
}
data load_data_detection_random(int n, char **paths, int m, int h, int w, int nh, int nw)
{
    char **random_paths = get_random_paths(paths, n, m);
    data d;
    d.shallow = 0;
    d.X = load_image_paths(random_paths, n, h, w);
    d.y = load_labels_detection(random_paths, n, h, w, nh, nw, scale);
    d.y = load_labels_detection(random_paths, n, h, w, nh, nw);
    free(random_paths);
    return d;
}
@@ -193,21 +237,13 @@
    return d;
}
struct load_args{
    char **paths;
    int n;
    int m;
    char **labels;
    int k;
    int h;
    int w;
    data *d;
};
void *load_in_thread(void *ptr)
{
    struct load_args a = *(struct load_args*)ptr;
    *a.d = load_data(a.paths, a.n, a.m, a.labels, a.k, a.h, a.w);
    translate_data_rows(*a.d, -128);
    scale_data_rows(*a.d, 1./128);
    free(ptr);
    return 0;
}
@@ -267,9 +303,9 @@
            X.vals[i][j] = (double)bytes[j+1];
        }
    }
   translate_data_rows(d, -144);
   scale_data_rows(d, 1./128);
   //normalize_data_rows(d);
    translate_data_rows(d, -144);
    scale_data_rows(d, 1./128);
    //normalize_data_rows(d);
    fclose(fp);
    return d;
}