2013-11-13 22:50:38 +04:00
|
|
|
#include "data.h"
|
|
|
|
#include "utils.h"
|
2013-12-07 01:26:09 +04:00
|
|
|
#include "image.h"
|
2013-11-13 22:50:38 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
list *get_paths(char *filename)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
FILE *file = fopen(filename, "r");
|
2014-02-25 00:21:31 +04:00
|
|
|
if(!file) file_error(filename);
|
2013-11-13 22:50:38 +04:00
|
|
|
list *lines = make_list();
|
|
|
|
while((path=fgetl(file))){
|
|
|
|
list_insert(lines, path);
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2014-12-12 00:15:26 +03:00
|
|
|
void fill_truth_detection(char *path, float *truth, int height, int width, int num_height, int num_width, float scale, int dx, int dy)
|
2014-11-19 00:51:04 +03:00
|
|
|
{
|
2014-11-22 02:35:19 +03:00
|
|
|
int box_height = height/num_height;
|
|
|
|
int box_width = width/num_width;
|
|
|
|
char *labelpath = find_replace(path, "imgs", "det");
|
|
|
|
labelpath = find_replace(labelpath, ".JPEG", ".txt");
|
|
|
|
FILE *file = fopen(labelpath, "r");
|
2014-11-28 21:38:26 +03:00
|
|
|
if(!file) file_error(labelpath);
|
2014-11-22 02:35:19 +03:00
|
|
|
int x, y, h, w;
|
|
|
|
while(fscanf(file, "%d %d %d %d", &x, &y, &w, &h) == 4){
|
2014-12-12 00:15:26 +03:00
|
|
|
x -= dx;
|
|
|
|
y -= dy;
|
2014-11-22 02:35:19 +03:00
|
|
|
int i = x/box_width;
|
|
|
|
int j = y/box_height;
|
2014-12-12 00:15:26 +03:00
|
|
|
|
|
|
|
if(i < 0) i = 0;
|
|
|
|
if(i >= num_width) i = num_width-1;
|
|
|
|
if(j < 0) j = 0;
|
|
|
|
if(j >= num_height) j = num_height-1;
|
|
|
|
|
2014-12-03 19:48:07 +03:00
|
|
|
float dw = (float)(x%box_width)/box_height;
|
|
|
|
float dh = (float)(y%box_width)/box_width;
|
2014-11-22 02:35:19 +03:00
|
|
|
float sh = h/scale;
|
|
|
|
float sw = w/scale;
|
2014-11-28 21:38:26 +03:00
|
|
|
//printf("%d %d %f %f\n", i, j, dh, dw);
|
2014-11-22 02:35:19 +03:00
|
|
|
int index = (i+j*num_width)*5;
|
|
|
|
truth[index++] = 1;
|
|
|
|
truth[index++] = dh;
|
|
|
|
truth[index++] = dw;
|
|
|
|
truth[index++] = sh;
|
|
|
|
truth[index++] = sw;
|
|
|
|
}
|
2014-11-28 21:38:26 +03:00
|
|
|
fclose(file);
|
2014-11-19 00:51:04 +03:00
|
|
|
}
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
void fill_truth(char *path, char **labels, int k, float *truth)
|
2013-11-13 22:50:38 +04:00
|
|
|
{
|
2013-12-03 04:41:40 +04:00
|
|
|
int i;
|
2014-01-29 04:28:42 +04:00
|
|
|
memset(truth, 0, k*sizeof(float));
|
2013-12-03 04:41:40 +04:00
|
|
|
for(i = 0; i < k; ++i){
|
|
|
|
if(strstr(path, labels[i])){
|
|
|
|
truth[i] = 1;
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
|
|
|
|
2014-11-22 02:35:19 +03:00
|
|
|
matrix load_image_paths(char **paths, int n, int h, int w)
|
2013-11-13 22:50:38 +04:00
|
|
|
{
|
|
|
|
int i;
|
2014-11-22 02:35:19 +03:00
|
|
|
matrix X;
|
|
|
|
X.rows = n;
|
|
|
|
X.vals = calloc(X.rows, sizeof(float*));
|
|
|
|
X.cols = 0;
|
2013-12-07 01:26:09 +04:00
|
|
|
|
|
|
|
for(i = 0; i < n; ++i){
|
2014-10-25 22:57:26 +04:00
|
|
|
image im = load_image_color(paths[i], h, w);
|
2014-11-22 02:35:19 +03:00
|
|
|
X.vals[i] = im.data;
|
|
|
|
X.cols = im.h*im.w*im.c;
|
2014-10-25 22:57:26 +04:00
|
|
|
}
|
2014-11-22 02:35:19 +03:00
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix load_labels_paths(char **paths, int n, char **labels, int k)
|
|
|
|
{
|
|
|
|
matrix y = make_matrix(n, k);
|
|
|
|
int i;
|
2014-10-25 22:57:26 +04:00
|
|
|
for(i = 0; i < n; ++i){
|
2014-11-22 02:35:19 +03:00
|
|
|
fill_truth(paths[i], labels, k, y.vals[i]);
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
2014-11-22 02:35:19 +03:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix load_labels_detection(char **paths, int n, int height, int width, int num_height, int num_width, float scale)
|
|
|
|
{
|
|
|
|
int k = num_height*num_width*5;
|
|
|
|
matrix y = make_matrix(n, k);
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; ++i){
|
2014-12-12 00:15:26 +03:00
|
|
|
fill_truth_detection(paths[i], y.vals[i], height, width, num_height, num_width, scale,0,0);
|
2014-11-22 02:35:19 +03:00
|
|
|
}
|
|
|
|
return y;
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
|
|
|
|
2014-02-14 22:26:31 +04:00
|
|
|
data load_data_image_pathfile(char *filename, char **labels, int k, int h, int w)
|
2013-11-13 22:50:38 +04:00
|
|
|
{
|
2013-12-07 01:26:09 +04:00
|
|
|
list *plist = get_paths(filename);
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
2014-11-22 02:35:19 +03:00
|
|
|
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);
|
2013-12-07 01:26:09 +04:00
|
|
|
free_list_contents(plist);
|
|
|
|
free_list(plist);
|
|
|
|
free(paths);
|
|
|
|
return d;
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
|
|
|
|
2014-10-25 22:57:26 +04:00
|
|
|
char **get_labels(char *filename)
|
|
|
|
{
|
|
|
|
list *plist = get_paths(filename);
|
|
|
|
char **labels = (char **)list_to_array(plist);
|
|
|
|
free_list(plist);
|
|
|
|
return labels;
|
|
|
|
}
|
|
|
|
|
2013-12-07 01:26:09 +04:00
|
|
|
void free_data(data d)
|
2013-11-13 22:50:38 +04:00
|
|
|
{
|
2013-12-07 01:26:09 +04:00
|
|
|
if(!d.shallow){
|
|
|
|
free_matrix(d.X);
|
|
|
|
free_matrix(d.y);
|
|
|
|
}else{
|
|
|
|
free(d.X.vals);
|
|
|
|
free(d.y.vals);
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 00:15:26 +03:00
|
|
|
data load_data_detection_jitter_random(int n, char **paths, int m, int h, int w, int nh, int nw, float scale)
|
|
|
|
{
|
|
|
|
char **random_paths = calloc(n, sizeof(char*));
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
int index = rand()%m;
|
|
|
|
random_paths[i] = paths[index];
|
|
|
|
if(i == 0) printf("%s\n", paths[index]);
|
|
|
|
}
|
|
|
|
data d;
|
|
|
|
d.shallow = 0;
|
|
|
|
d.X = load_image_paths(random_paths, n, h, w);
|
|
|
|
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);
|
|
|
|
|
|
|
|
image a = float_to_image(h, w, 3, d.X.vals[i]);
|
|
|
|
jitter_image(a,224,224,dy,dx);
|
|
|
|
}
|
|
|
|
free(random_paths);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-28 21:38:26 +03:00
|
|
|
data load_data_detection_random(int n, char **paths, int m, int h, int w, int nh, int nw, float scale)
|
2014-10-25 22:57:26 +04:00
|
|
|
{
|
|
|
|
char **random_paths = calloc(n, sizeof(char*));
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
int index = rand()%m;
|
|
|
|
random_paths[i] = paths[index];
|
|
|
|
if(i == 0) printf("%s\n", paths[index]);
|
|
|
|
}
|
2014-11-22 02:35:19 +03:00
|
|
|
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);
|
2014-10-25 22:57:26 +04:00
|
|
|
free(random_paths);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2014-12-13 23:01:21 +03:00
|
|
|
char **get_random_paths(char **paths, int n, int m)
|
2013-11-13 22:50:38 +04:00
|
|
|
{
|
2013-12-07 01:26:09 +04:00
|
|
|
char **random_paths = calloc(n, sizeof(char*));
|
2014-11-22 02:35:19 +03:00
|
|
|
int i;
|
2013-11-13 22:50:38 +04:00
|
|
|
for(i = 0; i < n; ++i){
|
2014-11-22 02:35:19 +03:00
|
|
|
int index = rand()%m;
|
2013-12-07 01:26:09 +04:00
|
|
|
random_paths[i] = paths[index];
|
2014-02-14 22:26:31 +04:00
|
|
|
if(i == 0) printf("%s\n", paths[index]);
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
2014-12-13 23:01:21 +03:00
|
|
|
return random_paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
data load_data(char **paths, int n, int m, char **labels, int k, int h, int w)
|
|
|
|
{
|
|
|
|
if(m) paths = get_random_paths(paths, n, m);
|
|
|
|
data d;
|
|
|
|
d.shallow = 0;
|
|
|
|
d.X = load_image_paths(paths, n, h, w);
|
|
|
|
d.y = load_labels_paths(paths, n, labels, k);
|
|
|
|
if(m) free(paths);
|
2013-12-07 01:26:09 +04:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2014-12-12 00:15:26 +03:00
|
|
|
struct load_args{
|
|
|
|
char **paths;
|
2014-12-13 23:01:21 +03:00
|
|
|
int n;
|
2014-12-12 00:15:26 +03:00
|
|
|
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;
|
2014-12-13 23:01:21 +03:00
|
|
|
*a.d = load_data(a.paths, a.n, a.m, a.labels, a.k, a.h, a.w);
|
2014-12-12 00:15:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-13 23:01:21 +03:00
|
|
|
pthread_t load_data_thread(char **paths, int n, int m, char **labels, int k, int h, int w, data *d)
|
2014-12-12 00:15:26 +03:00
|
|
|
{
|
|
|
|
pthread_t thread;
|
|
|
|
struct load_args *args = calloc(1, sizeof(struct load_args));
|
|
|
|
args->n = n;
|
|
|
|
args->paths = paths;
|
|
|
|
args->m = m;
|
|
|
|
args->labels = labels;
|
|
|
|
args->k = k;
|
|
|
|
args->h = h;
|
|
|
|
args->w = w;
|
|
|
|
args->d = d;
|
|
|
|
if(pthread_create(&thread, 0, load_in_thread, args)) {
|
|
|
|
error("Thread creation failed");
|
|
|
|
}
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
2013-12-07 01:26:09 +04:00
|
|
|
data load_categorical_data_csv(char *filename, int target, int k)
|
|
|
|
{
|
|
|
|
data d;
|
|
|
|
d.shallow = 0;
|
|
|
|
matrix X = csv_to_matrix(filename);
|
2014-01-29 04:28:42 +04:00
|
|
|
float *truth_1d = pop_column(&X, target);
|
|
|
|
float **truth = one_hot_encode(truth_1d, X.rows, k);
|
2013-12-07 01:26:09 +04:00
|
|
|
matrix y;
|
|
|
|
y.rows = X.rows;
|
|
|
|
y.cols = k;
|
|
|
|
y.vals = truth;
|
|
|
|
d.X = X;
|
|
|
|
d.y = y;
|
|
|
|
free(truth_1d);
|
|
|
|
return d;
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
2013-12-07 01:26:09 +04:00
|
|
|
|
2014-03-13 08:57:34 +04:00
|
|
|
data load_cifar10_data(char *filename)
|
|
|
|
{
|
|
|
|
data d;
|
|
|
|
d.shallow = 0;
|
2014-05-03 02:20:34 +04:00
|
|
|
long i,j;
|
2014-03-13 08:57:34 +04:00
|
|
|
matrix X = make_matrix(10000, 3072);
|
|
|
|
matrix y = make_matrix(10000, 10);
|
|
|
|
d.X = X;
|
|
|
|
d.y = y;
|
|
|
|
|
|
|
|
FILE *fp = fopen(filename, "rb");
|
2014-08-08 23:04:15 +04:00
|
|
|
if(!fp) file_error(filename);
|
2014-03-13 08:57:34 +04:00
|
|
|
for(i = 0; i < 10000; ++i){
|
|
|
|
unsigned char bytes[3073];
|
|
|
|
fread(bytes, 1, 3073, fp);
|
|
|
|
int class = bytes[0];
|
|
|
|
y.vals[i][class] = 1;
|
|
|
|
for(j = 0; j < X.cols; ++j){
|
|
|
|
X.vals[i][j] = (double)bytes[j+1];
|
|
|
|
}
|
|
|
|
}
|
2014-08-08 23:04:15 +04:00
|
|
|
translate_data_rows(d, -144);
|
|
|
|
scale_data_rows(d, 1./128);
|
|
|
|
//normalize_data_rows(d);
|
2014-03-13 08:57:34 +04:00
|
|
|
fclose(fp);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2014-10-28 05:45:06 +03:00
|
|
|
void get_random_batch(data d, int n, float *X, float *y)
|
2014-08-28 06:11:46 +04:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
for(j = 0; j < n; ++j){
|
|
|
|
int index = rand()%d.X.rows;
|
|
|
|
memcpy(X+j*d.X.cols, d.X.vals[index], d.X.cols*sizeof(float));
|
|
|
|
memcpy(y+j*d.y.cols, d.y.vals[index], d.y.cols*sizeof(float));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 05:45:06 +03:00
|
|
|
void get_next_batch(data d, int n, int offset, float *X, float *y)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
for(j = 0; j < n; ++j){
|
|
|
|
int index = offset + j;
|
|
|
|
memcpy(X+j*d.X.cols, d.X.vals[index], d.X.cols*sizeof(float));
|
|
|
|
memcpy(y+j*d.y.cols, d.y.vals[index], d.y.cols*sizeof(float));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-08 23:04:15 +04:00
|
|
|
data load_all_cifar10()
|
|
|
|
{
|
|
|
|
data d;
|
|
|
|
d.shallow = 0;
|
|
|
|
int i,j,b;
|
|
|
|
matrix X = make_matrix(50000, 3072);
|
|
|
|
matrix y = make_matrix(50000, 10);
|
|
|
|
d.X = X;
|
|
|
|
d.y = y;
|
|
|
|
|
2014-08-28 06:11:46 +04:00
|
|
|
|
2014-08-08 23:04:15 +04:00
|
|
|
for(b = 0; b < 5; ++b){
|
|
|
|
char buff[256];
|
|
|
|
sprintf(buff, "data/cifar10/data_batch_%d.bin", b+1);
|
|
|
|
FILE *fp = fopen(buff, "rb");
|
|
|
|
if(!fp) file_error(buff);
|
|
|
|
for(i = 0; i < 10000; ++i){
|
|
|
|
unsigned char bytes[3073];
|
|
|
|
fread(bytes, 1, 3073, fp);
|
|
|
|
int class = bytes[0];
|
|
|
|
y.vals[i+b*10000][class] = 1;
|
|
|
|
for(j = 0; j < X.cols; ++j){
|
|
|
|
X.vals[i+b*10000][j] = (double)bytes[j+1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
//normalize_data_rows(d);
|
2014-08-28 06:11:46 +04:00
|
|
|
translate_data_rows(d, -144);
|
|
|
|
scale_data_rows(d, 1./128);
|
2014-08-08 23:04:15 +04:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2013-12-07 01:26:09 +04:00
|
|
|
void randomize_data(data d)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = d.X.rows-1; i > 0; --i){
|
|
|
|
int index = rand()%i;
|
2014-01-29 04:28:42 +04:00
|
|
|
float *swap = d.X.vals[index];
|
2013-12-07 01:26:09 +04:00
|
|
|
d.X.vals[index] = d.X.vals[i];
|
|
|
|
d.X.vals[i] = swap;
|
|
|
|
|
|
|
|
swap = d.y.vals[index];
|
|
|
|
d.y.vals[index] = d.y.vals[i];
|
|
|
|
d.y.vals[i] = swap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 22:26:31 +04:00
|
|
|
void scale_data_rows(data d, float s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < d.X.rows; ++i){
|
|
|
|
scale_array(d.X.vals[i], d.X.cols, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 09:07:51 +04:00
|
|
|
void translate_data_rows(data d, float s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < d.X.rows; ++i){
|
|
|
|
translate_array(d.X.vals[i], d.X.cols, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 01:26:09 +04:00
|
|
|
void normalize_data_rows(data d)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < d.X.rows; ++i){
|
|
|
|
normalize_array(d.X.vals[i], d.X.cols);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 21:38:50 +04:00
|
|
|
data *split_data(data d, int part, int total)
|
2013-12-07 01:26:09 +04:00
|
|
|
{
|
|
|
|
data *split = calloc(2, sizeof(data));
|
|
|
|
int i;
|
|
|
|
int start = part*d.X.rows/total;
|
|
|
|
int end = (part+1)*d.X.rows/total;
|
|
|
|
data train;
|
|
|
|
data test;
|
|
|
|
train.shallow = test.shallow = 1;
|
|
|
|
|
|
|
|
test.X.rows = test.y.rows = end-start;
|
|
|
|
train.X.rows = train.y.rows = d.X.rows - (end-start);
|
|
|
|
train.X.cols = test.X.cols = d.X.cols;
|
|
|
|
train.y.cols = test.y.cols = d.y.cols;
|
2013-12-07 21:38:50 +04:00
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
train.X.vals = calloc(train.X.rows, sizeof(float*));
|
|
|
|
test.X.vals = calloc(test.X.rows, sizeof(float*));
|
|
|
|
train.y.vals = calloc(train.y.rows, sizeof(float*));
|
|
|
|
test.y.vals = calloc(test.y.rows, sizeof(float*));
|
2013-12-07 21:38:50 +04:00
|
|
|
|
2013-12-07 01:26:09 +04:00
|
|
|
for(i = 0; i < start; ++i){
|
|
|
|
train.X.vals[i] = d.X.vals[i];
|
|
|
|
train.y.vals[i] = d.y.vals[i];
|
|
|
|
}
|
|
|
|
for(i = start; i < end; ++i){
|
|
|
|
test.X.vals[i-start] = d.X.vals[i];
|
|
|
|
test.y.vals[i-start] = d.y.vals[i];
|
|
|
|
}
|
|
|
|
for(i = end; i < d.X.rows; ++i){
|
2013-12-07 21:38:50 +04:00
|
|
|
train.X.vals[i-(end-start)] = d.X.vals[i];
|
|
|
|
train.y.vals[i-(end-start)] = d.y.vals[i];
|
2013-12-07 01:26:09 +04:00
|
|
|
}
|
|
|
|
split[0] = train;
|
|
|
|
split[1] = test;
|
|
|
|
return split;
|
|
|
|
}
|
|
|
|
|