Joseph Redmon
2015-04-11 390a0cf923cee683e5be300390c736a2ab9b7fd5
src/data.c
@@ -47,7 +47,7 @@
    return random_paths;
}
matrix load_image_paths(char **paths, int n, int h, int w)
matrix load_image_paths(char **paths, int n, int w, int h)
{
    int i;
    matrix X;
@@ -56,7 +56,9 @@
    X.cols = 0;
    for(i = 0; i < n; ++i){
        image im = load_image_color(paths[i], h, w);
        image im = load_image_color(paths[i], w, h);
        translate_image(im, -128);
        scale_image(im, 1./128);
        X.vals[i] = im.data;
        X.cols = im.h*im.w*im.c;
    }
@@ -108,8 +110,9 @@
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, "VOC2012/JPEGImages", "labels");
    char *labelpath = find_replace(path, "detection_images", "labels");
    labelpath = find_replace(labelpath, ".jpg", ".txt");
    labelpath = find_replace(labelpath, ".JPEG", ".txt");
    int count = 0;
    box *boxes = read_boxes(labelpath, &count);
    randomize_boxes(boxes, count);
@@ -206,12 +209,12 @@
    }
}
data load_data_captcha(char **paths, int n, int m, int k, int h, int w)
data load_data_captcha(char **paths, int n, int m, int k, int w, int h)
{
    if(m) paths = get_random_paths(paths, n, m);
    data d;
    d.shallow = 0;
    d.X = load_image_paths(paths, n, h, w);
    d.X = load_image_paths(paths, n, w, h);
    d.y = make_matrix(n, k*NUMCHARS);
    int i;
    for(i = 0; i < n; ++i){
@@ -221,12 +224,12 @@
    return d;
}
data load_data_captcha_encode(char **paths, int n, int m, int h, int w)
data load_data_captcha_encode(char **paths, int n, int m, int w, int h)
{
    if(m) paths = get_random_paths(paths, n, m);
    data d;
    d.shallow = 0;
    d.X = load_image_paths(paths, n, h, w);
    d.X = load_image_paths(paths, n, w, h);
    d.X.cols = 17100;
    d.y = d.X;
    if(m) free(paths);
@@ -257,21 +260,6 @@
    return y;
}
data load_data_image_pathfile(char *filename, char **labels, int k, int h, int w)
{
    list *plist = get_paths(filename);
    char **paths = (char **)list_to_array(plist);
    int n = plist->size;
    data d;
    d.shallow = 0;
    d.X = load_image_paths(paths, n, h, w);
    d.y = load_labels_paths(paths, n, labels, k);
    free_list_contents(plist);
    free_list(plist);
    free(paths);
    return d;
}
char **get_labels(char *filename)
{
    list *plist = get_paths(filename);
@@ -291,10 +279,8 @@
    }
}
data load_data_detection_jitter_random(int n, char **paths, int m, int classes, int h, int w, int num_boxes, int background)
data load_data_detection_jitter_random(int n, char **paths, int m, int classes, int w, int h, int num_boxes, int background)
{
    //float minscale = 0.85;
    //float maxscale = 1.15;
    char **random_paths = get_random_paths(paths, n, m);
    int i;
    data d;
@@ -310,10 +296,14 @@
        image orig = load_image_color(random_paths[i], 0, 0);
        int oh = orig.h;
        int ow = orig.w;
        int pleft  = (rand_uniform() * 64. - 32.);
        int pright = (rand_uniform() * 64. - 32.);
        int ptop   = (rand_uniform() * 64. - 32.);
        int pbot   = (rand_uniform() * 64. - 32.);
        int dw = ow/10;
        int dh = oh/10;
        int pleft  = (rand_uniform() * 2*dw - dw);
        int pright = (rand_uniform() * 2*dw - dw);
        int ptop   = (rand_uniform() * 2*dh - dh);
        int pbot   = (rand_uniform() * 2*dh - dh);
        int swidth =  ow - pleft - pright;
        int sheight = oh - ptop - pbot;
@@ -322,12 +312,12 @@
        float sy = (float)sheight / oh;
        int flip = rand()%2;
        image cropped = crop_image(orig, ptop, pleft, sheight, swidth);
        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, h, w);
        image sized = resize_image(cropped, w, h);
        free_image(cropped);
        if(flip) flip_image(sized);
        d.X.vals[i] = sized.data;
@@ -342,14 +332,14 @@
{
    printf("Loading data: %d\n", rand());
    struct load_args a = *(struct load_args*)ptr;
    *a.d = load_data_detection_jitter_random(a.n, a.paths, a.m, a.classes, a.h, a.w, a.num_boxes, a.background);
    *a.d = load_data_detection_jitter_random(a.n, a.paths, a.m, a.classes, a.w, a.h, a.num_boxes, a.background);
    translate_data_rows(*a.d, -128);
    scale_data_rows(*a.d, 1./128);
    free(ptr);
    return 0;
}
pthread_t load_data_detection_thread(int n, char **paths, int m, int classes, int h, int w, int nh, int nw, int background, data *d)
pthread_t load_data_detection_thread(int n, char **paths, int m, int classes, int w, int h, int nh, int nw, int background, data *d)
{
    pthread_t thread;
    struct load_args *args = calloc(1, sizeof(struct load_args));
@@ -370,12 +360,12 @@
    return thread;
}
data load_data(char **paths, int n, int m, char **labels, int k, int h, int w)
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);
    data d;
    d.shallow = 0;
    d.X = load_image_paths(paths, n, h, w);
    d.X = load_image_paths(paths, n, w, h);
    d.y = load_labels_paths(paths, n, labels, k);
    if(m) free(paths);
    return d;
@@ -384,14 +374,12 @@
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);
    *a.d = load_data(a.paths, a.n, a.m, a.labels, a.k, a.w, a.h);
    free(ptr);
    return 0;
}
pthread_t load_data_thread(char **paths, int n, int m, char **labels, int k, int h, int w, data *d)
pthread_t load_data_thread(char **paths, int n, int m, char **labels, int k, int w, int h, data *d)
{
    pthread_t thread;
    struct load_args *args = calloc(1, sizeof(struct load_args));