darknet  v3
matrix.c
Go to the documentation of this file.
1 #include "matrix.h"
2 #include "utils.h"
3 #include "blas.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <assert.h>
8 #include <math.h>
9 
11 {
12  int i;
13  for(i = 0; i < m.rows; ++i) free(m.vals[i]);
14  free(m.vals);
15 }
16 
17 float matrix_topk_accuracy(matrix truth, matrix guess, int k)
18 {
19  int *indexes = calloc(k, sizeof(int));
20  int n = truth.cols;
21  int i,j;
22  int correct = 0;
23  for(i = 0; i < truth.rows; ++i){
24  top_k(guess.vals[i], n, k, indexes);
25  for(j = 0; j < k; ++j){
26  int class = indexes[j];
27  if(truth.vals[i][class]){
28  ++correct;
29  break;
30  }
31  }
32  }
33  free(indexes);
34  return (float)correct/truth.rows;
35 }
36 
37 void scale_matrix(matrix m, float scale)
38 {
39  int i,j;
40  for(i = 0; i < m.rows; ++i){
41  for(j = 0; j < m.cols; ++j){
42  m.vals[i][j] *= scale;
43  }
44  }
45 }
46 
48 {
49  int i;
50  if (m.rows == size) return m;
51  if (m.rows < size) {
52  m.vals = realloc(m.vals, size*sizeof(float*));
53  for (i = m.rows; i < size; ++i) {
54  m.vals[i] = calloc(m.cols, sizeof(float));
55  }
56  } else if (m.rows > size) {
57  for (i = size; i < m.rows; ++i) {
58  free(m.vals[i]);
59  }
60  m.vals = realloc(m.vals, size*sizeof(float*));
61  }
62  m.rows = size;
63  return m;
64 }
65 
67 {
68  assert(from.rows == to.rows && from.cols == to.cols);
69  int i,j;
70  for(i = 0; i < from.rows; ++i){
71  for(j = 0; j < from.cols; ++j){
72  to.vals[i][j] += from.vals[i][j];
73  }
74  }
75 }
76 
78 {
79  matrix c = {0};
80  c.rows = m.rows;
81  c.cols = m.cols;
82  c.vals = calloc(c.rows, sizeof(float *));
83  int i;
84  for(i = 0; i < c.rows; ++i){
85  c.vals[i] = calloc(c.cols, sizeof(float));
86  copy_cpu(c.cols, m.vals[i], 1, c.vals[i], 1);
87  }
88  return c;
89 }
90 
91 matrix make_matrix(int rows, int cols)
92 {
93  int i;
94  matrix m;
95  m.rows = rows;
96  m.cols = cols;
97  m.vals = calloc(m.rows, sizeof(float *));
98  for(i = 0; i < m.rows; ++i){
99  m.vals[i] = calloc(m.cols, sizeof(float));
100  }
101  return m;
102 }
103 
105 {
106  int i;
107  matrix h;
108  h.rows = n;
109  h.cols = m->cols;
110  h.vals = calloc(h.rows, sizeof(float *));
111  for(i = 0; i < n; ++i){
112  int index = rand()%m->rows;
113  h.vals[i] = m->vals[index];
114  m->vals[index] = m->vals[--(m->rows)];
115  }
116  return h;
117 }
118 
119 float *pop_column(matrix *m, int c)
120 {
121  float *col = calloc(m->rows, sizeof(float));
122  int i, j;
123  for(i = 0; i < m->rows; ++i){
124  col[i] = m->vals[i][c];
125  for(j = c; j < m->cols-1; ++j){
126  m->vals[i][j] = m->vals[i][j+1];
127  }
128  }
129  --m->cols;
130  return col;
131 }
132 
133 matrix csv_to_matrix(char *filename)
134 {
135  FILE *fp = fopen(filename, "r");
136  if(!fp) file_error(filename);
137 
138  matrix m;
139  m.cols = -1;
140 
141  char *line;
142 
143  int n = 0;
144  int size = 1024;
145  m.vals = calloc(size, sizeof(float*));
146  while((line = fgetl(fp))){
147  if(m.cols == -1) m.cols = count_fields(line);
148  if(n == size){
149  size *= 2;
150  m.vals = realloc(m.vals, size*sizeof(float*));
151  }
152  m.vals[n] = parse_fields(line, m.cols);
153  free(line);
154  ++n;
155  }
156  m.vals = realloc(m.vals, n*sizeof(float*));
157  m.rows = n;
158  return m;
159 }
160 
162 {
163  int i, j;
164 
165  for(i = 0; i < m.rows; ++i){
166  for(j = 0; j < m.cols; ++j){
167  if(j > 0) printf(",");
168  printf("%.17g", m.vals[i][j]);
169  }
170  printf("\n");
171  }
172 }
173 
175 {
176  int i, j;
177  printf("%d X %d Matrix:\n",m.rows, m.cols);
178  printf(" __");
179  for(j = 0; j < 16*m.cols-1; ++j) printf(" ");
180  printf("__ \n");
181 
182  printf("| ");
183  for(j = 0; j < 16*m.cols-1; ++j) printf(" ");
184  printf(" |\n");
185 
186  for(i = 0; i < m.rows; ++i){
187  printf("| ");
188  for(j = 0; j < m.cols; ++j){
189  printf("%15.7f ", m.vals[i][j]);
190  }
191  printf(" |\n");
192  }
193  printf("|__");
194  for(j = 0; j < 16*m.cols-1; ++j) printf(" ");
195  printf("__|\n");
196 }
float * parse_fields(char *line, int n)
Definition: utils.c:459
matrix csv_to_matrix(char *filename)
Definition: matrix.c:133
int rows
Definition: darknet.h:533
matrix make_matrix(int rows, int cols)
Definition: matrix.c:91
matrix hold_out_matrix(matrix *m, int n)
Definition: matrix.c:104
int cols
Definition: darknet.h:533
matrix resize_matrix(matrix m, int size)
Definition: matrix.c:47
void top_k(float *a, int n, int k, int *index)
Definition: utils.c:237
matrix copy_matrix(matrix m)
Definition: matrix.c:77
void file_error(char *s)
Definition: utils.c:281
void print_matrix(matrix m)
Definition: matrix.c:174
float matrix_topk_accuracy(matrix truth, matrix guess, int k)
Definition: matrix.c:17
char * fgetl(FILE *fp)
Definition: utils.c:335
void scale_matrix(matrix m, float scale)
Definition: matrix.c:37
void copy_cpu(int N, float *X, int INCX, float *Y, int INCY)
Definition: blas.c:226
void free_matrix(matrix m)
Definition: matrix.c:10
float ** vals
Definition: darknet.h:534
int count_fields(char *line)
Definition: utils.c:447
void matrix_add_matrix(matrix from, matrix to)
Definition: matrix.c:66
void matrix_to_csv(matrix m)
Definition: matrix.c:161
float * pop_column(matrix *m, int c)
Definition: matrix.c:119