| | |
| | | #include <string.h> |
| | | |
| | | unsigned int data_seed; |
| | | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| | | |
| | | list *get_paths(char *filename) |
| | | { |
| | |
| | | { |
| | | char **random_paths = calloc(n, sizeof(char*)); |
| | | int i; |
| | | pthread_mutex_lock(&mutex); |
| | | for(i = 0; i < n; ++i){ |
| | | int index = rand_r(&data_seed)%m; |
| | | indexes[i] = index; |
| | | random_paths[i] = paths[index]; |
| | | if(i == 0) printf("%s\n", paths[index]); |
| | | } |
| | | pthread_mutex_unlock(&mutex); |
| | | return random_paths; |
| | | } |
| | | |
| | |
| | | { |
| | | char **random_paths = calloc(n, sizeof(char*)); |
| | | int i; |
| | | pthread_mutex_lock(&mutex); |
| | | for(i = 0; i < n; ++i){ |
| | | int index = rand_r(&data_seed)%m; |
| | | random_paths[i] = paths[index]; |
| | | if(i == 0) printf("%s\n", paths[index]); |
| | | } |
| | | pthread_mutex_unlock(&mutex); |
| | | return random_paths; |
| | | } |
| | | |
| | |
| | | return X; |
| | | } |
| | | |
| | | matrix load_image_cropped_paths(char **paths, int n, int min, int max, int size) |
| | | 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) |
| | | { |
| | | int i; |
| | | matrix X; |
| | |
| | | |
| | | for(i = 0; i < n; ++i){ |
| | | image im = load_image_color(paths[i], 0, 0); |
| | | image crop = random_crop_image(im, min, max, size); |
| | | image crop = random_augment_image(im, angle, aspect, min, max, size); |
| | | int flip = rand_r(&data_seed)%2; |
| | | if (flip) flip_image(crop); |
| | | random_distort_image(crop, hue, saturation, exposure); |
| | | |
| | | /* |
| | | show_image(im, "orig"); |
| | | show_image(crop, "crop"); |
| | |
| | | labelpath = find_replace(labelpath, "JPEGImages", "labels"); |
| | | |
| | | labelpath = find_replace(labelpath, ".jpg", ".txt"); |
| | | labelpath = find_replace(labelpath, ".png", ".txt"); |
| | | labelpath = find_replace(labelpath, ".JPG", ".txt"); |
| | | labelpath = find_replace(labelpath, ".JPEG", ".txt"); |
| | | int count = 0; |
| | |
| | | labelpath = find_replace(labelpath, "JPEGImages", "labels"); |
| | | |
| | | labelpath = find_replace(labelpath, ".jpg", ".txt"); |
| | | labelpath = find_replace(labelpath, ".png", ".txt"); |
| | | labelpath = find_replace(labelpath, ".JPG", ".txt"); |
| | | labelpath = find_replace(labelpath, ".JPEG", ".txt"); |
| | | int count = 0; |
| | |
| | | |
| | | if (w < .01 || h < .01) continue; |
| | | |
| | | truth[i*5+0] = id; |
| | | truth[i*5+1] = x; |
| | | truth[i*5+2] = y; |
| | | truth[i*5+3] = w; |
| | | truth[i*5+4] = h; |
| | | truth[i*5+0] = x; |
| | | truth[i*5+1] = y; |
| | | truth[i*5+2] = w; |
| | | truth[i*5+3] = h; |
| | | truth[i*5+4] = id; |
| | | } |
| | | free(boxes); |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | data load_data_region(int n, char **paths, int m, int w, int h, int size, int classes, float jitter) |
| | | 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) |
| | | { |
| | | char **random_paths = get_random_paths(paths, n, m); |
| | | int i; |
| | |
| | | |
| | | image sized = resize_image(cropped, w, h); |
| | | if(flip) flip_image(sized); |
| | | random_distort_image(sized, hue, saturation, exposure); |
| | | d.X.vals[i] = sized.data; |
| | | |
| | | fill_truth_region(random_paths[i], d.y.vals[i], classes, size, flip, dx, dy, 1./sx, 1./sy); |
| | |
| | | return d; |
| | | } |
| | | |
| | | data load_data_detection(int n, char **paths, int m, int w, int h, int boxes, int classes, float jitter) |
| | | 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) |
| | | { |
| | | char **random_paths = get_random_paths(paths, n, m); |
| | | int i; |
| | |
| | | |
| | | image sized = resize_image(cropped, w, h); |
| | | if(flip) flip_image(sized); |
| | | random_distort_image(sized, hue, saturation, exposure); |
| | | d.X.vals[i] = sized.data; |
| | | |
| | | fill_truth_detection(random_paths[i], boxes, d.y.vals[i], classes, flip, dx, dy, 1./sx, 1./sy); |
| | |
| | | |
| | | //printf("Loading data: %d\n", rand_r(&data_seed)); |
| | | load_args a = *(struct load_args*)ptr; |
| | | if(a.exposure == 0) a.exposure = 1; |
| | | if(a.saturation == 0) a.saturation = 1; |
| | | if(a.aspect == 0) a.aspect = 1; |
| | | |
| | | if (a.type == OLD_CLASSIFICATION_DATA){ |
| | | *a.d = load_data(a.paths, a.n, a.m, a.labels, a.classes, a.w, a.h); |
| | | } else if (a.type == CLASSIFICATION_DATA){ |
| | | *a.d = load_data_augment(a.paths, a.n, a.m, a.labels, a.classes, a.min, a.max, a.size); |
| | | *a.d = load_data_augment(a.paths, a.n, a.m, a.labels, a.classes, a.min, a.max, a.size, a.angle, a.aspect, a.hue, a.saturation, a.exposure); |
| | | } else if (a.type == SUPER_DATA){ |
| | | *a.d = load_data_super(a.paths, a.n, a.m, a.w, a.h, a.scale); |
| | | } else if (a.type == STUDY_DATA){ |
| | | *a.d = load_data_study(a.paths, a.n, a.m, a.labels, a.classes, a.min, a.max, a.size); |
| | | *a.d = load_data_study(a.paths, a.n, a.m, a.labels, a.classes, a.min, a.max, a.size, a.angle, a.aspect, a.hue, a.saturation, a.exposure); |
| | | } else if (a.type == WRITING_DATA){ |
| | | *a.d = load_data_writing(a.paths, a.n, a.m, a.w, a.h, a.out_w, a.out_h); |
| | | } else if (a.type == REGION_DATA){ |
| | | *a.d = load_data_region(a.n, a.paths, a.m, a.w, a.h, a.num_boxes, a.classes, a.jitter); |
| | | *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); |
| | | } else if (a.type == DETECTION_DATA){ |
| | | *a.d = load_data_detection(a.n, a.paths, a.m, a.w, a.h, a.num_boxes, a.classes, a.jitter); |
| | | *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); |
| | | } else if (a.type == SWAG_DATA){ |
| | | *a.d = load_data_swag(a.paths, a.n, a.classes, a.jitter); |
| | | } else if (a.type == COMPARE_DATA){ |
| | |
| | | *(a.im) = load_image_color(a.path, 0, 0); |
| | | *(a.resized) = resize_image(*(a.im), a.w, a.h); |
| | | } else if (a.type == TAG_DATA){ |
| | | *a.d = load_data_tag(a.paths, a.n, a.m, a.classes, a.min, a.max, a.size); |
| | | *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); |
| | | //*a.d = load_data(a.paths, a.n, a.m, a.labels, a.classes, a.w, a.h); |
| | | } |
| | | free(ptr); |
| | |
| | | return d; |
| | | } |
| | | |
| | | data load_data_study(char **paths, int n, int m, char **labels, int k, int min, int max, int size) |
| | | 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) |
| | | { |
| | | data d = {0}; |
| | | d.indexes = calloc(n, sizeof(int)); |
| | | if(m) paths = get_random_paths_indexes(paths, n, m, d.indexes); |
| | | d.shallow = 0; |
| | | d.X = load_image_cropped_paths(paths, n, min, max, size); |
| | | d.X = load_image_augment_paths(paths, n, min, max, size, angle, aspect, hue, saturation, exposure); |
| | | d.y = load_labels_paths(paths, n, labels, k); |
| | | if(m) free(paths); |
| | | return d; |
| | | } |
| | | |
| | | data load_data_augment(char **paths, int n, int m, char **labels, int k, int min, int max, int size) |
| | | data load_data_super(char **paths, int n, int m, int w, int h, int scale) |
| | | { |
| | | if(m) paths = get_random_paths(paths, n, m); |
| | | data d = {0}; |
| | | d.shallow = 0; |
| | | d.X = load_image_cropped_paths(paths, n, min, max, size); |
| | | |
| | | int i; |
| | | d.X.rows = n; |
| | | d.X.vals = calloc(n, sizeof(float*)); |
| | | d.X.cols = w*h*3; |
| | | |
| | | d.y.rows = n; |
| | | d.y.vals = calloc(n, sizeof(float*)); |
| | | d.y.cols = w*scale * h*scale * 3; |
| | | |
| | | for(i = 0; i < n; ++i){ |
| | | image im = load_image_color(paths[i], 0, 0); |
| | | image crop = random_crop_image(im, w*scale, h*scale); |
| | | int flip = rand_r(&data_seed)%2; |
| | | if (flip) flip_image(crop); |
| | | image resize = resize_image(crop, w, h); |
| | | d.X.vals[i] = resize.data; |
| | | d.y.vals[i] = crop.data; |
| | | free_image(im); |
| | | } |
| | | |
| | | if(m) free(paths); |
| | | return d; |
| | | } |
| | | |
| | | data load_data_augment(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) |
| | | { |
| | | if(m) paths = get_random_paths(paths, n, m); |
| | | data d = {0}; |
| | | d.shallow = 0; |
| | | d.X = load_image_augment_paths(paths, n, min, max, size, angle, aspect, hue, saturation, exposure); |
| | | d.y = load_labels_paths(paths, n, labels, k); |
| | | if(m) free(paths); |
| | | return d; |
| | | } |
| | | |
| | | data load_data_tag(char **paths, int n, int m, int k, int min, int max, int size) |
| | | 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) |
| | | { |
| | | if(m) paths = get_random_paths(paths, n, m); |
| | | data d = {0}; |
| | | d.w = size; |
| | | d.h = size; |
| | | d.shallow = 0; |
| | | d.X = load_image_cropped_paths(paths, n, min, max, size); |
| | | d.X = load_image_augment_paths(paths, n, min, max, size, angle, aspect, hue, saturation, exposure); |
| | | d.y = load_tags_paths(paths, n, k); |
| | | if(m) free(paths); |
| | | return d; |
| | |
| | | return d; |
| | | } |
| | | |
| | | data concat_datas(data *d, int n) |
| | | { |
| | | int i; |
| | | data out = {0}; |
| | | out.shallow = 1; |
| | | for(i = 0; i < n; ++i){ |
| | | data new = concat_data(d[i], out); |
| | | free_data(out); |
| | | out = new; |
| | | } |
| | | return out; |
| | | } |
| | | |
| | | data load_categorical_data_csv(char *filename, int target, int k) |
| | | { |
| | | data d = {0}; |