data.c
Go to the documentation of this file.
00001 #include "data.h"
00002 #include "utils.h"
00003 #include "image.h"
00004 #include "cuda.h"
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 
00010 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
00011 
00012 list *get_paths(char *filename)
00013 {
00014     char *path;
00015     FILE *file = fopen(filename, "r");
00016     if(!file) file_error(filename);
00017     list *lines = make_list();
00018     while((path=fgetl(file))){
00019         list_insert(lines, path);
00020     }
00021     fclose(file);
00022     return lines;
00023 }
00024 
00025 /*
00026 char **get_random_paths_indexes(char **paths, int n, int m, int *indexes)
00027 {
00028     char **random_paths = calloc(n, sizeof(char*));
00029     int i;
00030     pthread_mutex_lock(&mutex);
00031     for(i = 0; i < n; ++i){
00032         int index = rand()%m;
00033         indexes[i] = index;
00034         random_paths[i] = paths[index];
00035         if(i == 0) printf("%s\n", paths[index]);
00036     }
00037     pthread_mutex_unlock(&mutex);
00038     return random_paths;
00039 }
00040 */
00041 
00042 char **get_random_paths(char **paths, int n, int m)
00043 {
00044     char **random_paths = calloc(n, sizeof(char*));
00045     int i;
00046     pthread_mutex_lock(&mutex);
00047     for(i = 0; i < n; ++i){
00048         int index = rand()%m;
00049         random_paths[i] = paths[index];
00050         //if(i == 0) printf("%s\n", paths[index]);
00051     }
00052     pthread_mutex_unlock(&mutex);
00053     return random_paths;
00054 }
00055 
00056 char **find_replace_paths(char **paths, int n, char *find, char *replace)
00057 {
00058     char **replace_paths = calloc(n, sizeof(char*));
00059     int i;
00060     for(i = 0; i < n; ++i){
00061         char replaced[4096];
00062         find_replace(paths[i], find, replace, replaced);
00063         replace_paths[i] = copy_string(replaced);
00064     }
00065     return replace_paths;
00066 }
00067 
00068 matrix load_image_paths_gray(char **paths, int n, int w, int h)
00069 {
00070     int i;
00071     matrix X;
00072     X.rows = n;
00073     X.vals = calloc(X.rows, sizeof(float*));
00074     X.cols = 0;
00075 
00076     for(i = 0; i < n; ++i){
00077         image im = load_image(paths[i], w, h, 3);
00078 
00079         image gray = grayscale_image(im);
00080         free_image(im);
00081         im = gray;
00082 
00083         X.vals[i] = im.data;
00084         X.cols = im.h*im.w*im.c;
00085     }
00086     return X;
00087 }
00088 
00089 matrix load_image_paths(char **paths, int n, int w, int h)
00090 {
00091     int i;
00092     matrix X;
00093     X.rows = n;
00094     X.vals = calloc(X.rows, sizeof(float*));
00095     X.cols = 0;
00096 
00097     for(i = 0; i < n; ++i){
00098         image im = load_image_color(paths[i], w, h);
00099         X.vals[i] = im.data;
00100         X.cols = im.h*im.w*im.c;
00101     }
00102     return X;
00103 }
00104 
00105 matrix load_image_augment_paths(char **paths, int n, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure)
00106 {
00107     int i;
00108     matrix X;
00109     X.rows = n;
00110     X.vals = calloc(X.rows, sizeof(float*));
00111     X.cols = 0;
00112 
00113     for(i = 0; i < n; ++i){
00114         image im = load_image_color(paths[i], 0, 0);
00115         image crop = random_augment_image(im, angle, aspect, min, max, size);
00116         int flip = rand()%2;
00117         if (flip) flip_image(crop);
00118         random_distort_image(crop, hue, saturation, exposure);
00119 
00120         /*
00121         show_image(im, "orig");
00122         show_image(crop, "crop");
00123         cvWaitKey(0);
00124         */
00125         free_image(im);
00126         X.vals[i] = crop.data;
00127         X.cols = crop.h*crop.w*crop.c;
00128     }
00129     return X;
00130 }
00131 
00132 
00133 box_label *read_boxes(char *filename, int *n)
00134 {
00135     box_label *boxes = calloc(1, sizeof(box_label));
00136     FILE *file = fopen(filename, "r");
00137     if(!file) file_error(filename);
00138     float x, y, h, w;
00139     int id;
00140     int count = 0;
00141     while(fscanf(file, "%d %f %f %f %f", &id, &x, &y, &w, &h) == 5){
00142         boxes = realloc(boxes, (count+1)*sizeof(box_label));
00143         boxes[count].id = id;
00144         boxes[count].x = x;
00145         boxes[count].y = y;
00146         boxes[count].h = h;
00147         boxes[count].w = w;
00148         boxes[count].left   = x - w/2;
00149         boxes[count].right  = x + w/2;
00150         boxes[count].top    = y - h/2;
00151         boxes[count].bottom = y + h/2;
00152         ++count;
00153     }
00154     fclose(file);
00155     *n = count;
00156     return boxes;
00157 }
00158 
00159 void randomize_boxes(box_label *b, int n)
00160 {
00161     int i;
00162     for(i = 0; i < n; ++i){
00163         box_label swap = b[i];
00164         int index = rand()%n;
00165         b[i] = b[index];
00166         b[index] = swap;
00167     }
00168 }
00169 
00170 void correct_boxes(box_label *boxes, int n, float dx, float dy, float sx, float sy, int flip)
00171 {
00172     int i;
00173     for(i = 0; i < n; ++i){
00174         if(boxes[i].x == 0 && boxes[i].y == 0) {
00175             boxes[i].x = 999999;
00176             boxes[i].y = 999999;
00177             boxes[i].w = 999999;
00178             boxes[i].h = 999999;
00179             continue;
00180         }
00181         boxes[i].left   = boxes[i].left  * sx - dx;
00182         boxes[i].right  = boxes[i].right * sx - dx;
00183         boxes[i].top    = boxes[i].top   * sy - dy;
00184         boxes[i].bottom = boxes[i].bottom* sy - dy;
00185 
00186         if(flip){
00187             float swap = boxes[i].left;
00188             boxes[i].left = 1. - boxes[i].right;
00189             boxes[i].right = 1. - swap;
00190         }
00191 
00192         boxes[i].left =  constrain(0, 1, boxes[i].left);
00193         boxes[i].right = constrain(0, 1, boxes[i].right);
00194         boxes[i].top =   constrain(0, 1, boxes[i].top);
00195         boxes[i].bottom =   constrain(0, 1, boxes[i].bottom);
00196 
00197         boxes[i].x = (boxes[i].left+boxes[i].right)/2;
00198         boxes[i].y = (boxes[i].top+boxes[i].bottom)/2;
00199         boxes[i].w = (boxes[i].right - boxes[i].left);
00200         boxes[i].h = (boxes[i].bottom - boxes[i].top);
00201 
00202         boxes[i].w = constrain(0, 1, boxes[i].w);
00203         boxes[i].h = constrain(0, 1, boxes[i].h);
00204     }
00205 }
00206 
00207 void fill_truth_swag(char *path, float *truth, int classes, int flip, float dx, float dy, float sx, float sy)
00208 {
00209     char labelpath[4096];
00210     find_replace(path, "images", "labels", labelpath);
00211     find_replace(labelpath, "JPEGImages", "labels", labelpath);
00212     find_replace(labelpath, ".jpg", ".txt", labelpath);
00213     find_replace(labelpath, ".JPG", ".txt", labelpath);
00214     find_replace(labelpath, ".JPEG", ".txt", labelpath);
00215 
00216     int count = 0;
00217     box_label *boxes = read_boxes(labelpath, &count);
00218     randomize_boxes(boxes, count);
00219     correct_boxes(boxes, count, dx, dy, sx, sy, flip);
00220     float x,y,w,h;
00221     int id;
00222     int i;
00223 
00224     for (i = 0; i < count && i < 30; ++i) {
00225         x =  boxes[i].x;
00226         y =  boxes[i].y;
00227         w =  boxes[i].w;
00228         h =  boxes[i].h;
00229         id = boxes[i].id;
00230 
00231         if (w < .0 || h < .0) continue;
00232 
00233         int index = (4+classes) * i;
00234 
00235         truth[index++] = x;
00236         truth[index++] = y;
00237         truth[index++] = w;
00238         truth[index++] = h;
00239 
00240         if (id < classes) truth[index+id] = 1;
00241     }
00242     free(boxes);
00243 }
00244 
00245 void fill_truth_region(char *path, float *truth, int classes, int num_boxes, int flip, float dx, float dy, float sx, float sy)
00246 {
00247     char labelpath[4096];
00248     find_replace(path, "images", "labels", labelpath);
00249     find_replace(labelpath, "JPEGImages", "labels", labelpath);
00250 
00251     find_replace(labelpath, ".jpg", ".txt", labelpath);
00252     find_replace(labelpath, ".png", ".txt", labelpath);
00253     find_replace(labelpath, ".JPG", ".txt", labelpath);
00254     find_replace(labelpath, ".JPEG", ".txt", labelpath);
00255     int count = 0;
00256     box_label *boxes = read_boxes(labelpath, &count);
00257     randomize_boxes(boxes, count);
00258     correct_boxes(boxes, count, dx, dy, sx, sy, flip);
00259     float x,y,w,h;
00260     int id;
00261     int i;
00262 
00263     for (i = 0; i < count; ++i) {
00264         x =  boxes[i].x;
00265         y =  boxes[i].y;
00266         w =  boxes[i].w;
00267         h =  boxes[i].h;
00268         id = boxes[i].id;
00269 
00270         if (w < .01 || h < .01) continue;
00271 
00272         int col = (int)(x*num_boxes);
00273         int row = (int)(y*num_boxes);
00274 
00275         x = x*num_boxes - col;
00276         y = y*num_boxes - row;
00277 
00278         int index = (col+row*num_boxes)*(5+classes);
00279         if (truth[index]) continue;
00280         truth[index++] = 1;
00281 
00282         if (id < classes) truth[index+id] = 1;
00283         index += classes;
00284 
00285         truth[index++] = x;
00286         truth[index++] = y;
00287         truth[index++] = w;
00288         truth[index++] = h;
00289     }
00290     free(boxes);
00291 }
00292 
00293 void fill_truth_detection(char *path, int num_boxes, float *truth, int classes, int flip, float dx, float dy, float sx, float sy)
00294 {
00295     char labelpath[4096];
00296     find_replace(path, "images", "labels", labelpath);
00297     find_replace(labelpath, "JPEGImages", "labels", labelpath);
00298 
00299     find_replace(labelpath, "raw", "labels", labelpath);
00300     find_replace(labelpath, ".jpg", ".txt", labelpath);
00301     find_replace(labelpath, ".png", ".txt", labelpath);
00302     find_replace(labelpath, ".JPG", ".txt", labelpath);
00303     find_replace(labelpath, ".JPEG", ".txt", labelpath);
00304     int count = 0;
00305     box_label *boxes = read_boxes(labelpath, &count);
00306     randomize_boxes(boxes, count);
00307     correct_boxes(boxes, count, dx, dy, sx, sy, flip);
00308     if(count > num_boxes) count = num_boxes;
00309     float x,y,w,h;
00310     int id;
00311     int i;
00312 
00313     for (i = 0; i < count; ++i) {
00314         x =  boxes[i].x;
00315         y =  boxes[i].y;
00316         w =  boxes[i].w;
00317         h =  boxes[i].h;
00318         id = boxes[i].id;
00319 
00320         if ((w < .01 || h < .01)) continue;
00321 
00322         truth[i*5+0] = x;
00323         truth[i*5+1] = y;
00324         truth[i*5+2] = w;
00325         truth[i*5+3] = h;
00326         truth[i*5+4] = id;
00327     }
00328     free(boxes);
00329 }
00330 
00331 #define NUMCHARS 37
00332 
00333 void print_letters(float *pred, int n)
00334 {
00335     int i;
00336     for(i = 0; i < n; ++i){
00337         int index = max_index(pred+i*NUMCHARS, NUMCHARS);
00338         printf("%c", int_to_alphanum(index));
00339     }
00340     printf("\n");
00341 }
00342 
00343 void fill_truth_captcha(char *path, int n, float *truth)
00344 {
00345     char *begin = strrchr(path, '/');
00346     ++begin;
00347     int i;
00348     for(i = 0; i < strlen(begin) && i < n && begin[i] != '.'; ++i){
00349         int index = alphanum_to_int(begin[i]);
00350         if(index > 35) printf("Bad %c\n", begin[i]);
00351         truth[i*NUMCHARS+index] = 1;
00352     }
00353     for(;i < n; ++i){
00354         truth[i*NUMCHARS + NUMCHARS-1] = 1;
00355     }
00356 }
00357 
00358 data load_data_captcha(char **paths, int n, int m, int k, int w, int h)
00359 {
00360     if(m) paths = get_random_paths(paths, n, m);
00361     data d = {0};
00362     d.shallow = 0;
00363     d.X = load_image_paths(paths, n, w, h);
00364     d.y = make_matrix(n, k*NUMCHARS);
00365     int i;
00366     for(i = 0; i < n; ++i){
00367         fill_truth_captcha(paths[i], k, d.y.vals[i]);
00368     }
00369     if(m) free(paths);
00370     return d;
00371 }
00372 
00373 data load_data_captcha_encode(char **paths, int n, int m, int w, int h)
00374 {
00375     if(m) paths = get_random_paths(paths, n, m);
00376     data d = {0};
00377     d.shallow = 0;
00378     d.X = load_image_paths(paths, n, w, h);
00379     d.X.cols = 17100;
00380     d.y = d.X;
00381     if(m) free(paths);
00382     return d;
00383 }
00384 
00385 void fill_truth(char *path, char **labels, int k, float *truth)
00386 {
00387     int i;
00388     memset(truth, 0, k*sizeof(float));
00389     int count = 0;
00390     for(i = 0; i < k; ++i){
00391         if(strstr(path, labels[i])){
00392             truth[i] = 1;
00393             ++count;
00394         }
00395     }
00396     if(count != 1) printf("Too many or too few labels: %d, %s\n", count, path);
00397 }
00398 
00399 void fill_hierarchy(float *truth, int k, tree *hierarchy)
00400 {
00401     int j;
00402     for(j = 0; j < k; ++j){
00403         if(truth[j]){
00404             int parent = hierarchy->parent[j];
00405             while(parent >= 0){
00406                 truth[parent] = 1;
00407                 parent = hierarchy->parent[parent];
00408             }
00409         }
00410     }
00411     int i;
00412     int count = 0;
00413     for(j = 0; j < hierarchy->groups; ++j){
00414         //printf("%d\n", count);
00415         int mask = 1;
00416         for(i = 0; i < hierarchy->group_size[j]; ++i){
00417             if(truth[count + i]){
00418                 mask = 0;
00419                 break;
00420             }
00421         }
00422         if (mask) {
00423             for(i = 0; i < hierarchy->group_size[j]; ++i){
00424                 truth[count + i] = SECRET_NUM;
00425             }
00426         }
00427         count += hierarchy->group_size[j];
00428     }
00429 }
00430 
00431 matrix load_labels_paths(char **paths, int n, char **labels, int k, tree *hierarchy)
00432 {
00433     matrix y = make_matrix(n, k);
00434     int i;
00435     for(i = 0; i < n && labels; ++i){
00436         fill_truth(paths[i], labels, k, y.vals[i]);
00437         if(hierarchy){
00438             fill_hierarchy(y.vals[i], k, hierarchy);
00439         }
00440     }
00441     return y;
00442 }
00443 
00444 matrix load_tags_paths(char **paths, int n, int k)
00445 {
00446     matrix y = make_matrix(n, k);
00447     int i;
00448     int count = 0;
00449     for(i = 0; i < n; ++i){
00450         char label[4096];
00451         find_replace(paths[i], "imgs", "labels", label);
00452         find_replace(label, "_iconl.jpeg", ".txt", label);
00453         FILE *file = fopen(label, "r");
00454         if(!file){
00455             find_replace(label, "labels", "labels2", label);
00456             file = fopen(label, "r");
00457             if(!file) continue;
00458         }
00459         ++count;
00460         int tag;
00461         while(fscanf(file, "%d", &tag) == 1){
00462             if(tag < k){
00463                 y.vals[i][tag] = 1;
00464             }
00465         }
00466         fclose(file);
00467     }
00468     printf("%d/%d\n", count, n);
00469     return y;
00470 }
00471 
00472 char **get_labels(char *filename)
00473 {
00474     list *plist = get_paths(filename);
00475     char **labels = (char **)list_to_array(plist);
00476     free_list(plist);
00477     return labels;
00478 }
00479 
00480 void free_data(data d)
00481 {
00482     if(!d.shallow){
00483         free_matrix(d.X);
00484         free_matrix(d.y);
00485     }else{
00486         free(d.X.vals);
00487         free(d.y.vals);
00488     }
00489 }
00490 
00491 data load_data_region(int n, char **paths, int m, int w, int h, int size, int classes, float jitter, float hue, float saturation, float exposure)
00492 {
00493     char **random_paths = get_random_paths(paths, n, m);
00494     int i;
00495     data d = {0};
00496     d.shallow = 0;
00497 
00498     d.X.rows = n;
00499     d.X.vals = calloc(d.X.rows, sizeof(float*));
00500     d.X.cols = h*w*3;
00501 
00502 
00503     int k = size*size*(5+classes);
00504     d.y = make_matrix(n, k);
00505     for(i = 0; i < n; ++i){
00506         image orig = load_image_color(random_paths[i], 0, 0);
00507 
00508         int oh = orig.h;
00509         int ow = orig.w;
00510 
00511         int dw = (ow*jitter);
00512         int dh = (oh*jitter);
00513 
00514         int pleft  = rand_uniform(-dw, dw);
00515         int pright = rand_uniform(-dw, dw);
00516         int ptop   = rand_uniform(-dh, dh);
00517         int pbot   = rand_uniform(-dh, dh);
00518 
00519         int swidth =  ow - pleft - pright;
00520         int sheight = oh - ptop - pbot;
00521 
00522         float sx = (float)swidth  / ow;
00523         float sy = (float)sheight / oh;
00524 
00525         int flip = rand()%2;
00526         image cropped = crop_image(orig, pleft, ptop, swidth, sheight);
00527 
00528         float dx = ((float)pleft/ow)/sx;
00529         float dy = ((float)ptop /oh)/sy;
00530 
00531         image sized = resize_image(cropped, w, h);
00532         if(flip) flip_image(sized);
00533         random_distort_image(sized, hue, saturation, exposure);
00534         d.X.vals[i] = sized.data;
00535 
00536         fill_truth_region(random_paths[i], d.y.vals[i], classes, size, flip, dx, dy, 1./sx, 1./sy);
00537 
00538         free_image(orig);
00539         free_image(cropped);
00540     }
00541     free(random_paths);
00542     return d;
00543 }
00544 
00545 data load_data_compare(int n, char **paths, int m, int classes, int w, int h)
00546 {
00547     if(m) paths = get_random_paths(paths, 2*n, m);
00548     int i,j;
00549     data d = {0};
00550     d.shallow = 0;
00551 
00552     d.X.rows = n;
00553     d.X.vals = calloc(d.X.rows, sizeof(float*));
00554     d.X.cols = h*w*6;
00555 
00556     int k = 2*(classes);
00557     d.y = make_matrix(n, k);
00558     for(i = 0; i < n; ++i){
00559         image im1 = load_image_color(paths[i*2],   w, h);
00560         image im2 = load_image_color(paths[i*2+1], w, h);
00561 
00562         d.X.vals[i] = calloc(d.X.cols, sizeof(float));
00563         memcpy(d.X.vals[i],         im1.data, h*w*3*sizeof(float));
00564         memcpy(d.X.vals[i] + h*w*3, im2.data, h*w*3*sizeof(float));
00565 
00566         int id;
00567         float iou;
00568 
00569         char imlabel1[4096];
00570         char imlabel2[4096];
00571         find_replace(paths[i*2],   "imgs", "labels", imlabel1);
00572         find_replace(imlabel1, "jpg", "txt", imlabel1);
00573         FILE *fp1 = fopen(imlabel1, "r");
00574 
00575         while(fscanf(fp1, "%d %f", &id, &iou) == 2){
00576             if (d.y.vals[i][2*id] < iou) d.y.vals[i][2*id] = iou;
00577         }
00578 
00579         find_replace(paths[i*2+1], "imgs", "labels", imlabel2);
00580         find_replace(imlabel2, "jpg", "txt", imlabel2);
00581         FILE *fp2 = fopen(imlabel2, "r");
00582 
00583         while(fscanf(fp2, "%d %f", &id, &iou) == 2){
00584             if (d.y.vals[i][2*id + 1] < iou) d.y.vals[i][2*id + 1] = iou;
00585         }
00586 
00587         for (j = 0; j < classes; ++j){
00588             if (d.y.vals[i][2*j] > .5 &&  d.y.vals[i][2*j+1] < .5){
00589                 d.y.vals[i][2*j] = 1;
00590                 d.y.vals[i][2*j+1] = 0;
00591             } else if (d.y.vals[i][2*j] < .5 &&  d.y.vals[i][2*j+1] > .5){
00592                 d.y.vals[i][2*j] = 0;
00593                 d.y.vals[i][2*j+1] = 1;
00594             } else {
00595                 d.y.vals[i][2*j]   = SECRET_NUM;
00596                 d.y.vals[i][2*j+1] = SECRET_NUM;
00597             }
00598         }
00599         fclose(fp1);
00600         fclose(fp2);
00601 
00602         free_image(im1);
00603         free_image(im2);
00604     }
00605     if(m) free(paths);
00606     return d;
00607 }
00608 
00609 data load_data_swag(char **paths, int n, int classes, float jitter)
00610 {
00611     int index = rand()%n;
00612     char *random_path = paths[index];
00613 
00614     image orig = load_image_color(random_path, 0, 0);
00615     int h = orig.h;
00616     int w = orig.w;
00617 
00618     data d = {0};
00619     d.shallow = 0;
00620     d.w = w;
00621     d.h = h;
00622 
00623     d.X.rows = 1;
00624     d.X.vals = calloc(d.X.rows, sizeof(float*));
00625     d.X.cols = h*w*3;
00626 
00627     int k = (4+classes)*30;
00628     d.y = make_matrix(1, k);
00629 
00630     int dw = w*jitter;
00631     int dh = h*jitter;
00632 
00633     int pleft  = rand_uniform(-dw, dw);
00634     int pright = rand_uniform(-dw, dw);
00635     int ptop   = rand_uniform(-dh, dh);
00636     int pbot   = rand_uniform(-dh, dh);
00637 
00638     int swidth =  w - pleft - pright;
00639     int sheight = h - ptop - pbot;
00640 
00641     float sx = (float)swidth  / w;
00642     float sy = (float)sheight / h;
00643 
00644     int flip = rand()%2;
00645     image cropped = crop_image(orig, pleft, ptop, swidth, sheight);
00646 
00647     float dx = ((float)pleft/w)/sx;
00648     float dy = ((float)ptop /h)/sy;
00649 
00650     image sized = resize_image(cropped, w, h);
00651     if(flip) flip_image(sized);
00652     d.X.vals[0] = sized.data;
00653 
00654     fill_truth_swag(random_path, d.y.vals[0], classes, flip, dx, dy, 1./sx, 1./sy);
00655 
00656     free_image(orig);
00657     free_image(cropped);
00658 
00659     return d;
00660 }
00661 
00662 data load_data_detection(int n, char **paths, int m, int w, int h, int boxes, int classes, float jitter, float hue, float saturation, float exposure)
00663 {
00664     char **random_paths = get_random_paths(paths, n, m);
00665     int i;
00666     data d = {0};
00667     d.shallow = 0;
00668 
00669     d.X.rows = n;
00670     d.X.vals = calloc(d.X.rows, sizeof(float*));
00671     d.X.cols = h*w*3;
00672 
00673     d.y = make_matrix(n, 5*boxes);
00674     for(i = 0; i < n; ++i){
00675         image orig = load_image_color(random_paths[i], 0, 0);
00676 
00677         int oh = orig.h;
00678         int ow = orig.w;
00679 
00680         int dw = (ow*jitter);
00681         int dh = (oh*jitter);
00682 
00683         int pleft  = rand_uniform(-dw, dw);
00684         int pright = rand_uniform(-dw, dw);
00685         int ptop   = rand_uniform(-dh, dh);
00686         int pbot   = rand_uniform(-dh, dh);
00687 
00688         int swidth =  ow - pleft - pright;
00689         int sheight = oh - ptop - pbot;
00690 
00691         float sx = (float)swidth  / ow;
00692         float sy = (float)sheight / oh;
00693 
00694         int flip = rand()%2;
00695         image cropped = crop_image(orig, pleft, ptop, swidth, sheight);
00696 
00697         float dx = ((float)pleft/ow)/sx;
00698         float dy = ((float)ptop /oh)/sy;
00699 
00700         image sized = resize_image(cropped, w, h);
00701         if(flip) flip_image(sized);
00702         random_distort_image(sized, hue, saturation, exposure);
00703         d.X.vals[i] = sized.data;
00704 
00705         fill_truth_detection(random_paths[i], boxes, d.y.vals[i], classes, flip, dx, dy, 1./sx, 1./sy);
00706 
00707         free_image(orig);
00708         free_image(cropped);
00709     }
00710     free(random_paths);
00711     return d;
00712 }
00713 
00714 
00715 void *load_thread(void *ptr)
00716 {
00717     //printf("Loading data: %d\n", rand());
00718     load_args a = *(struct load_args*)ptr;
00719     if(a.exposure == 0) a.exposure = 1;
00720     if(a.saturation == 0) a.saturation = 1;
00721     if(a.aspect == 0) a.aspect = 1;
00722 
00723     if (a.type == OLD_CLASSIFICATION_DATA){
00724         *a.d = load_data_old(a.paths, a.n, a.m, a.labels, a.classes, a.w, a.h);
00725     } else if (a.type == CLASSIFICATION_DATA){
00726         *a.d = load_data_augment(a.paths, a.n, a.m, a.labels, a.classes, a.hierarchy, a.min, a.max, a.size, a.angle, a.aspect, a.hue, a.saturation, a.exposure);
00727     } else if (a.type == SUPER_DATA){
00728         *a.d = load_data_super(a.paths, a.n, a.m, a.w, a.h, a.scale);
00729     } else if (a.type == WRITING_DATA){
00730         *a.d = load_data_writing(a.paths, a.n, a.m, a.w, a.h, a.out_w, a.out_h);
00731     } else if (a.type == REGION_DATA){
00732         *a.d = load_data_region(a.n, a.paths, a.m, a.w, a.h, a.num_boxes, a.classes, a.jitter, a.hue, a.saturation, a.exposure);
00733     } else if (a.type == DETECTION_DATA){
00734         *a.d = load_data_detection(a.n, a.paths, a.m, a.w, a.h, a.num_boxes, a.classes, a.jitter, a.hue, a.saturation, a.exposure);
00735     } else if (a.type == SWAG_DATA){
00736         *a.d = load_data_swag(a.paths, a.n, a.classes, a.jitter);
00737     } else if (a.type == COMPARE_DATA){
00738         *a.d = load_data_compare(a.n, a.paths, a.m, a.classes, a.w, a.h);
00739     } else if (a.type == IMAGE_DATA){
00740         *(a.im) = load_image_color(a.path, 0, 0);
00741         *(a.resized) = resize_image(*(a.im), a.w, a.h);
00742     } else if (a.type == TAG_DATA){
00743         *a.d = load_data_tag(a.paths, a.n, a.m, a.classes, a.min, a.max, a.size, a.angle, a.aspect, a.hue, a.saturation, a.exposure);
00744     }
00745     free(ptr);
00746     return 0;
00747 }
00748 
00749 pthread_t load_data_in_thread(load_args args)
00750 {
00751     pthread_t thread;
00752     struct load_args *ptr = calloc(1, sizeof(struct load_args));
00753     *ptr = args;
00754     if(pthread_create(&thread, 0, load_thread, ptr)) error("Thread creation failed");
00755     return thread;
00756 }
00757 
00758 void *load_threads(void *ptr)
00759 {
00760     int i;
00761     load_args args = *(load_args *)ptr;
00762     if (args.threads == 0) args.threads = 1;
00763     data *out = args.d;
00764     int total = args.n;
00765     free(ptr);
00766     data *buffers = calloc(args.threads, sizeof(data));
00767     pthread_t *threads = calloc(args.threads, sizeof(pthread_t));
00768     for(i = 0; i < args.threads; ++i){
00769         args.d = buffers + i;
00770         args.n = (i+1) * total/args.threads - i * total/args.threads;
00771         threads[i] = load_data_in_thread(args);
00772     }
00773     for(i = 0; i < args.threads; ++i){
00774         pthread_join(threads[i], 0);
00775     }
00776     *out = concat_datas(buffers, args.threads);
00777     out->shallow = 0;
00778     for(i = 0; i < args.threads; ++i){
00779         buffers[i].shallow = 1;
00780         free_data(buffers[i]);
00781     }
00782     free(buffers);
00783     free(threads);
00784     return 0;
00785 }
00786 
00787 pthread_t load_data(load_args args)
00788 {
00789     pthread_t thread;
00790     struct load_args *ptr = calloc(1, sizeof(struct load_args));
00791     *ptr = args;
00792     if(pthread_create(&thread, 0, load_threads, ptr)) error("Thread creation failed");
00793     return thread;
00794 }
00795 
00796 data load_data_writing(char **paths, int n, int m, int w, int h, int out_w, int out_h)
00797 {
00798     if(m) paths = get_random_paths(paths, n, m);
00799     char **replace_paths = find_replace_paths(paths, n, ".png", "-label.png");
00800     data d = {0};
00801     d.shallow = 0;
00802     d.X = load_image_paths(paths, n, w, h);
00803     d.y = load_image_paths_gray(replace_paths, n, out_w, out_h);
00804     if(m) free(paths);
00805     int i;
00806     for(i = 0; i < n; ++i) free(replace_paths[i]);
00807     free(replace_paths);
00808     return d;
00809 }
00810 
00811 data load_data_old(char **paths, int n, int m, char **labels, int k, int w, int h)
00812 {
00813     if(m) paths = get_random_paths(paths, n, m);
00814     data d = {0};
00815     d.shallow = 0;
00816     d.X = load_image_paths(paths, n, w, h);
00817     d.y = load_labels_paths(paths, n, labels, k, 0);
00818     if(m) free(paths);
00819     return d;
00820 }
00821 
00822 /*
00823    data load_data_study(char **paths, int n, int m, char **labels, int k, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure)
00824    {
00825    data d = {0};
00826    d.indexes = calloc(n, sizeof(int));
00827    if(m) paths = get_random_paths_indexes(paths, n, m, d.indexes);
00828    d.shallow = 0;
00829    d.X = load_image_augment_paths(paths, n, min, max, size, angle, aspect, hue, saturation, exposure);
00830    d.y = load_labels_paths(paths, n, labels, k);
00831    if(m) free(paths);
00832    return d;
00833    }
00834  */
00835 
00836 data load_data_super(char **paths, int n, int m, int w, int h, int scale)
00837 {
00838     if(m) paths = get_random_paths(paths, n, m);
00839     data d = {0};
00840     d.shallow = 0;
00841 
00842     int i;
00843     d.X.rows = n;
00844     d.X.vals = calloc(n, sizeof(float*));
00845     d.X.cols = w*h*3;
00846 
00847     d.y.rows = n;
00848     d.y.vals = calloc(n, sizeof(float*));
00849     d.y.cols = w*scale * h*scale * 3;
00850 
00851     for(i = 0; i < n; ++i){
00852         image im = load_image_color(paths[i], 0, 0);
00853         image crop = random_crop_image(im, w*scale, h*scale);
00854         int flip = rand()%2;
00855         if (flip) flip_image(crop);
00856         image resize = resize_image(crop, w, h);
00857         d.X.vals[i] = resize.data;
00858         d.y.vals[i] = crop.data;
00859         free_image(im);
00860     }
00861 
00862     if(m) free(paths);
00863     return d;
00864 }
00865 
00866 data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure)
00867 {
00868     if(m) paths = get_random_paths(paths, n, m);
00869     data d = {0};
00870     d.shallow = 0;
00871     d.X = load_image_augment_paths(paths, n, min, max, size, angle, aspect, hue, saturation, exposure);
00872     d.y = load_labels_paths(paths, n, labels, k, hierarchy);
00873     if(m) free(paths);
00874     return d;
00875 }
00876 
00877 data load_data_tag(char **paths, int n, int m, int k, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure)
00878 {
00879     if(m) paths = get_random_paths(paths, n, m);
00880     data d = {0};
00881     d.w = size;
00882     d.h = size;
00883     d.shallow = 0;
00884     d.X = load_image_augment_paths(paths, n, min, max, size, angle, aspect, hue, saturation, exposure);
00885     d.y = load_tags_paths(paths, n, k);
00886     if(m) free(paths);
00887     return d;
00888 }
00889 
00890 matrix concat_matrix(matrix m1, matrix m2)
00891 {
00892     int i, count = 0;
00893     matrix m;
00894     m.cols = m1.cols;
00895     m.rows = m1.rows+m2.rows;
00896     m.vals = calloc(m1.rows + m2.rows, sizeof(float*));
00897     for(i = 0; i < m1.rows; ++i){
00898         m.vals[count++] = m1.vals[i];
00899     }
00900     for(i = 0; i < m2.rows; ++i){
00901         m.vals[count++] = m2.vals[i];
00902     }
00903     return m;
00904 }
00905 
00906 data concat_data(data d1, data d2)
00907 {
00908     data d = {0};
00909     d.shallow = 1;
00910     d.X = concat_matrix(d1.X, d2.X);
00911     d.y = concat_matrix(d1.y, d2.y);
00912     return d;
00913 }
00914 
00915 data concat_datas(data *d, int n)
00916 {
00917     int i;
00918     data out = {0};
00919     for(i = 0; i < n; ++i){
00920         data new = concat_data(d[i], out);
00921         free_data(out);
00922         out = new;
00923     }
00924     return out;
00925 }
00926 
00927 data load_categorical_data_csv(char *filename, int target, int k)
00928 {
00929     data d = {0};
00930     d.shallow = 0;
00931     matrix X = csv_to_matrix(filename);
00932     float *truth_1d = pop_column(&X, target);
00933     float **truth = one_hot_encode(truth_1d, X.rows, k);
00934     matrix y;
00935     y.rows = X.rows;
00936     y.cols = k;
00937     y.vals = truth;
00938     d.X = X;
00939     d.y = y;
00940     free(truth_1d);
00941     return d;
00942 }
00943 
00944 data load_cifar10_data(char *filename)
00945 {
00946     data d = {0};
00947     d.shallow = 0;
00948     long i,j;
00949     matrix X = make_matrix(10000, 3072);
00950     matrix y = make_matrix(10000, 10);
00951     d.X = X;
00952     d.y = y;
00953 
00954     FILE *fp = fopen(filename, "rb");
00955     if(!fp) file_error(filename);
00956     for(i = 0; i < 10000; ++i){
00957         unsigned char bytes[3073];
00958         fread(bytes, 1, 3073, fp);
00959         int class = bytes[0];
00960         y.vals[i][class] = 1;
00961         for(j = 0; j < X.cols; ++j){
00962             X.vals[i][j] = (double)bytes[j+1];
00963         }
00964     }
00965     //translate_data_rows(d, -128);
00966     scale_data_rows(d, 1./255);
00967     //normalize_data_rows(d);
00968     fclose(fp);
00969     return d;
00970 }
00971 
00972 void get_random_batch(data d, int n, float *X, float *y)
00973 {
00974     int j;
00975     for(j = 0; j < n; ++j){
00976         int index = rand()%d.X.rows;
00977         memcpy(X+j*d.X.cols, d.X.vals[index], d.X.cols*sizeof(float));
00978         memcpy(y+j*d.y.cols, d.y.vals[index], d.y.cols*sizeof(float));
00979     }
00980 }
00981 
00982 void get_next_batch(data d, int n, int offset, float *X, float *y)
00983 {
00984     int j;
00985     for(j = 0; j < n; ++j){
00986         int index = offset + j;
00987         memcpy(X+j*d.X.cols, d.X.vals[index], d.X.cols*sizeof(float));
00988         memcpy(y+j*d.y.cols, d.y.vals[index], d.y.cols*sizeof(float));
00989     }
00990 }
00991 
00992 void smooth_data(data d)
00993 {
00994     int i, j;
00995     float scale = 1. / d.y.cols;
00996     float eps = .1;
00997     for(i = 0; i < d.y.rows; ++i){
00998         for(j = 0; j < d.y.cols; ++j){
00999             d.y.vals[i][j] = eps * scale + (1-eps) * d.y.vals[i][j];
01000         }
01001     }
01002 }
01003 
01004 data load_all_cifar10()
01005 {
01006     data d = {0};
01007     d.shallow = 0;
01008     int i,j,b;
01009     matrix X = make_matrix(50000, 3072);
01010     matrix y = make_matrix(50000, 10);
01011     d.X = X;
01012     d.y = y;
01013 
01014 
01015     for(b = 0; b < 5; ++b){
01016         char buff[256];
01017         sprintf(buff, "data/cifar/cifar-10-batches-bin/data_batch_%d.bin", b+1);
01018         FILE *fp = fopen(buff, "rb");
01019         if(!fp) file_error(buff);
01020         for(i = 0; i < 10000; ++i){
01021             unsigned char bytes[3073];
01022             fread(bytes, 1, 3073, fp);
01023             int class = bytes[0];
01024             y.vals[i+b*10000][class] = 1;
01025             for(j = 0; j < X.cols; ++j){
01026                 X.vals[i+b*10000][j] = (double)bytes[j+1];
01027             }
01028         }
01029         fclose(fp);
01030     }
01031     //normalize_data_rows(d);
01032     //translate_data_rows(d, -128);
01033     scale_data_rows(d, 1./255);
01034     smooth_data(d);
01035     return d;
01036 }
01037 
01038 data load_go(char *filename)
01039 {
01040     FILE *fp = fopen(filename, "rb");
01041     matrix X = make_matrix(3363059, 361);
01042     matrix y = make_matrix(3363059, 361);
01043     int row, col;
01044 
01045     if(!fp) file_error(filename);
01046     char *label;
01047     int count = 0;
01048     while((label = fgetl(fp))){
01049         int i;
01050         if(count == X.rows){
01051             X = resize_matrix(X, count*2);
01052             y = resize_matrix(y, count*2);
01053         }
01054         sscanf(label, "%d %d", &row, &col);
01055         char *board = fgetl(fp);
01056 
01057         int index = row*19 + col;
01058         y.vals[count][index] = 1;
01059 
01060         for(i = 0; i < 19*19; ++i){
01061             float val = 0;
01062             if(board[i] == '1') val = 1;
01063             else if(board[i] == '2') val = -1;
01064             X.vals[count][i] = val;
01065         }
01066         ++count;
01067         free(label);
01068         free(board);
01069     }
01070     X = resize_matrix(X, count);
01071     y = resize_matrix(y, count);
01072 
01073     data d = {0};
01074     d.shallow = 0;
01075     d.X = X;
01076     d.y = y;
01077 
01078 
01079     fclose(fp);
01080 
01081     return d;
01082 }
01083 
01084 
01085 void randomize_data(data d)
01086 {
01087     int i;
01088     for(i = d.X.rows-1; i > 0; --i){
01089         int index = rand()%i;
01090         float *swap = d.X.vals[index];
01091         d.X.vals[index] = d.X.vals[i];
01092         d.X.vals[i] = swap;
01093 
01094         swap = d.y.vals[index];
01095         d.y.vals[index] = d.y.vals[i];
01096         d.y.vals[i] = swap;
01097     }
01098 }
01099 
01100 void scale_data_rows(data d, float s)
01101 {
01102     int i;
01103     for(i = 0; i < d.X.rows; ++i){
01104         scale_array(d.X.vals[i], d.X.cols, s);
01105     }
01106 }
01107 
01108 void translate_data_rows(data d, float s)
01109 {
01110     int i;
01111     for(i = 0; i < d.X.rows; ++i){
01112         translate_array(d.X.vals[i], d.X.cols, s);
01113     }
01114 }
01115 
01116 void normalize_data_rows(data d)
01117 {
01118     int i;
01119     for(i = 0; i < d.X.rows; ++i){
01120         normalize_array(d.X.vals[i], d.X.cols);
01121     }
01122 }
01123 
01124 data get_data_part(data d, int part, int total)
01125 {
01126     data p = {0};
01127     p.shallow = 1;
01128     p.X.rows = d.X.rows * (part + 1) / total - d.X.rows * part / total;
01129     p.y.rows = d.y.rows * (part + 1) / total - d.y.rows * part / total;
01130     p.X.cols = d.X.cols;
01131     p.y.cols = d.y.cols;
01132     p.X.vals = d.X.vals + d.X.rows * part / total;
01133     p.y.vals = d.y.vals + d.y.rows * part / total;
01134     return p;
01135 }
01136 
01137 data get_random_data(data d, int num)
01138 {
01139     data r = {0};
01140     r.shallow = 1;
01141 
01142     r.X.rows = num;
01143     r.y.rows = num;
01144 
01145     r.X.cols = d.X.cols;
01146     r.y.cols = d.y.cols;
01147 
01148     r.X.vals = calloc(num, sizeof(float *));
01149     r.y.vals = calloc(num, sizeof(float *));
01150 
01151     int i;
01152     for(i = 0; i < num; ++i){
01153         int index = rand()%d.X.rows;
01154         r.X.vals[i] = d.X.vals[index];
01155         r.y.vals[i] = d.y.vals[index];
01156     }
01157     return r;
01158 }
01159 
01160 data *split_data(data d, int part, int total)
01161 {
01162     data *split = calloc(2, sizeof(data));
01163     int i;
01164     int start = part*d.X.rows/total;
01165     int end = (part+1)*d.X.rows/total;
01166     data train;
01167     data test;
01168     train.shallow = test.shallow = 1;
01169 
01170     test.X.rows = test.y.rows = end-start;
01171     train.X.rows = train.y.rows = d.X.rows - (end-start);
01172     train.X.cols = test.X.cols = d.X.cols;
01173     train.y.cols = test.y.cols = d.y.cols;
01174 
01175     train.X.vals = calloc(train.X.rows, sizeof(float*));
01176     test.X.vals = calloc(test.X.rows, sizeof(float*));
01177     train.y.vals = calloc(train.y.rows, sizeof(float*));
01178     test.y.vals = calloc(test.y.rows, sizeof(float*));
01179 
01180     for(i = 0; i < start; ++i){
01181         train.X.vals[i] = d.X.vals[i];
01182         train.y.vals[i] = d.y.vals[i];
01183     }
01184     for(i = start; i < end; ++i){
01185         test.X.vals[i-start] = d.X.vals[i];
01186         test.y.vals[i-start] = d.y.vals[i];
01187     }
01188     for(i = end; i < d.X.rows; ++i){
01189         train.X.vals[i-(end-start)] = d.X.vals[i];
01190         train.y.vals[i-(end-start)] = d.y.vals[i];
01191     }
01192     split[0] = train;
01193     split[1] = test;
01194     return split;
01195 }
01196 


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:29