darknet/src/data.c

175 lines
4.1 KiB
C
Raw Normal View History

2013-11-13 22:50:38 +04:00
#include "data.h"
#include "list.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");
list *lines = make_list();
while((path=fgetl(file))){
list_insert(lines, path);
}
fclose(file);
return lines;
}
2013-12-03 04:41:40 +04:00
void fill_truth(char *path, char **labels, int k, double *truth)
2013-11-13 22:50:38 +04:00
{
2013-12-03 04:41:40 +04:00
int i;
memset(truth, 0, k*sizeof(double));
for(i = 0; i < k; ++i){
if(strstr(path, labels[i])){
truth[i] = 1;
}
}
2013-11-13 22:50:38 +04:00
}
2013-12-07 01:26:09 +04:00
data load_data_image_paths(char **paths, int n, char **labels, int k)
2013-11-13 22:50:38 +04:00
{
int i;
2013-12-07 01:26:09 +04:00
data d;
d.shallow = 0;
d.X.rows = n;
d.X.vals = calloc(d.X.rows, sizeof(double*));
d.y = make_matrix(n, k);
for(i = 0; i < n; ++i){
image im = load_image(paths[i]);
d.X.vals[i] = im.data;
d.X.cols = im.h*im.w*im.c;
fill_truth(paths[i], labels, k, d.y.vals[i]);
2013-11-13 22:50:38 +04:00
}
2013-12-07 01:26:09 +04:00
return d;
2013-11-13 22:50:38 +04:00
}
2013-12-07 01:26:09 +04:00
data load_data_image_pathfile(char *filename, char **labels, int k)
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);
data d = load_data_image_paths(paths, plist->size, labels, k);
free_list_contents(plist);
free_list(plist);
free(paths);
return d;
2013-11-13 22:50:38 +04:00
}
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
}
}
2013-12-07 01:26:09 +04:00
data load_data_image_pathfile_part(char *filename, int part, int total, char **labels, int k)
2013-11-13 22:50:38 +04:00
{
list *plist = get_paths(filename);
char **paths = (char **)list_to_array(plist);
2013-12-07 01:26:09 +04:00
int start = part*plist->size/total;
int end = (part+1)*plist->size/total;
data d = load_data_image_paths(paths+start, end-start, labels, k);
2013-11-13 22:50:38 +04:00
free_list_contents(plist);
free_list(plist);
free(paths);
2013-12-07 01:26:09 +04:00
return d;
2013-11-13 22:50:38 +04:00
}
2013-12-07 01:26:09 +04:00
data load_data_image_pathfile_random(char *filename, int n, char **labels, int k)
2013-11-13 22:50:38 +04:00
{
2013-12-07 01:26:09 +04:00
int i;
2013-11-13 22:50:38 +04:00
list *plist = get_paths(filename);
char **paths = (char **)list_to_array(plist);
2013-12-07 01:26:09 +04:00
char **random_paths = calloc(n, sizeof(char*));
2013-11-13 22:50:38 +04:00
for(i = 0; i < n; ++i){
int index = rand()%plist->size;
2013-12-07 01:26:09 +04:00
random_paths[i] = paths[index];
2013-11-13 22:50:38 +04:00
}
2013-12-07 01:26:09 +04:00
data d = load_data_image_paths(random_paths, n, labels, k);
2013-11-13 22:50:38 +04:00
free_list_contents(plist);
free_list(plist);
free(paths);
2013-12-07 01:26:09 +04:00
free(random_paths);
return d;
}
data load_categorical_data_csv(char *filename, int target, int k)
{
data d;
d.shallow = 0;
matrix X = csv_to_matrix(filename);
double *truth_1d = pop_column(&X, target);
double **truth = one_hot_encode(truth_1d, X.rows, k);
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
void randomize_data(data d)
{
int i;
for(i = d.X.rows-1; i > 0; --i){
int index = rand()%i;
double *swap = d.X.vals[index];
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;
}
}
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);
}
}
data *cv_split_data(data d, int part, int total)
{
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;
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){
train.X.vals[i-(start-end)] = d.X.vals[i];
train.y.vals[i-(start-end)] = d.y.vals[i];
}
split[0] = train;
split[1] = test;
return split;
}