You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1624 lines
45 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicData.h"
  2. MUTEX_HANDLE_TYPE MUTEX_INIT(mutex);
  3. COST_TYPE get_cost_type(char *s){
  4. if (strcmp(s, "sse") == 0) return SSE;
  5. if (strcmp(s, "masked") == 0) return MASKED;
  6. if (strcmp(s, "smooth") == 0) return SMOOTH;
  7. fprintf(stderr, "Couldn't find cost type %s, going with SSE\n", s);
  8. return SSE;
  9. }
  10. char *get_cost_string(COST_TYPE a){
  11. switch (a){
  12. case SSE:
  13. return "sse";
  14. case MASKED:
  15. return "masked";
  16. case SMOOTH:
  17. return "smooth";
  18. }
  19. return "sse";
  20. }
  21. cortexList *get_paths(char *filename){
  22. char *path;
  23. FILE *file = fopen(filename, "r");
  24. if(!file) return NULL;
  25. cortexList *lines = new cortexList();
  26. while(( path = fgetl( file ) )){
  27. lines->push_back( path );
  28. };
  29. fclose(file);
  30. return lines;
  31. };
  32. cortexList *get_random_paths_indexes(cortexList *paths, uint32_t m, int *indexes){
  33. cortexList *random_paths;
  34. random_paths = new cortexList();
  35. uint32_t i;
  36. for (i = 0; i < paths->size(); ++i){
  37. int index = rand()%m;
  38. indexes[i] = index;
  39. (*random_paths->at(i)) = (*paths->at(index));
  40. //if(i == 0) printf("%s\n", paths[index]);
  41. };
  42. return random_paths;
  43. };
  44. cortexList *get_random_paths(cortexList *paths, int m){
  45. cortexList *mList;
  46. mList = new cortexList();
  47. if( mList != NULL ){
  48. MUTEX_LOCK(mutex);
  49. mList->assign( paths->size(), NULL );
  50. for( cortexList::iterator index = paths->begin(); index < paths->end(); index++ ){
  51. int rindex = rand()%m;
  52. (*index) = (*paths)[rindex];
  53. };
  54. MUTEX_UNLOCK(&mutex);
  55. };
  56. return mList;
  57. };
  58. cortexList *find_replace_paths(cortexList *paths, char *find, char *replace){
  59. cortexList *mList;
  60. mList = new cortexList();
  61. if( mList != NULL ){
  62. for( cortexList::iterator index = paths->begin(); index < paths->end(); index++ ){
  63. char replaced[4096];
  64. find_replace( (*index), find, replace, replaced);
  65. mList->push_back(copy_string(replaced));
  66. };
  67. };
  68. return mList;
  69. };
  70. cortexMATRIX load_image_paths_gray(cortexList *paths, int w, int h){
  71. cortexMATRIX X;
  72. int i;
  73. X.rows = (uint32_t) paths->size();
  74. X.values = (float**)calloc(X.rows, sizeof(float*));
  75. X.cols = 0;
  76. i = 0;
  77. for (cortexList::iterator index = paths->begin(); index < paths->end(); index++){
  78. cortexImage *im;
  79. im = new cortexImage();
  80. im->fillFromFile( (*index), w, h );
  81. im->grayscale();
  82. X.values[i] = im->data;
  83. X.cols = im->h*im->w*im->c;
  84. i++;
  85. };
  86. return X;
  87. };
  88. cortexMATRIX load_image_paths(cortexList *paths, int w, int h){
  89. cortexMATRIX X;
  90. int i;
  91. X.rows = (uint32_t) paths->size();
  92. X.values = (float**)calloc(X.rows, sizeof(float*));
  93. X.cols = 0;
  94. i = 0;
  95. for (cortexList::iterator index = paths->begin(); index < paths->end(); index++){
  96. cortexImage *im;
  97. im = new cortexImage();
  98. im->fillFromFile((*index), w, h);
  99. X.values[i] = im->data;
  100. X.cols = im->h*im->w*im->c;
  101. i++;
  102. };
  103. return X;
  104. };
  105. cortexMATRIX *load_image_augment_paths(cortexList *paths, uint32_t min, uint32_t max, uint32_t size, float angle, float aspect, float hue, float saturation, float exposure, uint32_t center){
  106. cortexMATRIX *X;
  107. int i;
  108. X = (cortexMATRIX*)calloc(1, sizeof(cortexMATRIX));
  109. X->rows = (uint32_t) paths->size();
  110. X->values = (float**)calloc(X->rows, sizeof(float*));
  111. X->cols = 0;
  112. i = 0;
  113. for (cortexList::iterator index = paths->begin(); index < paths->end(); index++){
  114. cortexImage *im;
  115. im = new cortexImage();
  116. im->fillFromFile((*index), 0, 0);
  117. if(center){
  118. im->centerCrop( size, size);
  119. } else {
  120. im->randomAugment( angle, aspect, min, max, size, size );
  121. }
  122. int flip = rand()%2;
  123. if (flip) im->flip();// flip_image(crop);
  124. im->randomDistort( hue, saturation, exposure );
  125. //show_image(im, "orig");
  126. //show_image(crop, "crop");
  127. //cvWaitKey(0);
  128. X->values[i] = im->data;
  129. X->cols = im->h*im->w*im->c;
  130. i++;
  131. };
  132. return X;
  133. }
  134. cortexBOXLABEL *read_boxes(char *filename, int *n){
  135. cortexBOXLABEL *boxes = (cortexBOXLABEL*)calloc(1, sizeof(cortexBOXLABEL));
  136. FILE *file = fopen(filename, "r");
  137. if(!file) return NULL;
  138. float x, y, h, w;
  139. int id;
  140. int count = 0;
  141. while(fscanf(file, "%d %f %f %f %f", &id, &x, &y, &w, &h) == 5){
  142. boxes = (cortexBOXLABEL*)realloc(boxes, (count + 1)*sizeof(cortexBOXLABEL));
  143. boxes[count].id = id;
  144. boxes[count].x = x;
  145. boxes[count].y = y;
  146. boxes[count].h = h;
  147. boxes[count].w = w;
  148. boxes[count].left = x - w/2;
  149. boxes[count].right = x + w/2;
  150. boxes[count].top = y - h/2;
  151. boxes[count].bottom = y + h/2;
  152. ++count;
  153. }
  154. fclose(file);
  155. *n = count;
  156. return boxes;
  157. }
  158. void randomize_boxes(cortexBOXLABEL *b, int n){
  159. int i;
  160. for(i = 0; i < n; ++i){
  161. cortexBOXLABEL swap = b[i];
  162. int index = rand()%n;
  163. b[i] = b[index];
  164. b[index] = swap;
  165. };
  166. };
  167. void correct_boxes(cortexBOXLABEL *boxes, int n, float dx, float dy, float sx, float sy, int flip){
  168. int i;
  169. for(i = 0; i < n; ++i){
  170. if(boxes[i].x == 0 && boxes[i].y == 0) {
  171. boxes[i].x = 999999;
  172. boxes[i].y = 999999;
  173. boxes[i].w = 999999;
  174. boxes[i].h = 999999;
  175. continue;
  176. }
  177. boxes[i].left = boxes[i].left * sx - dx;
  178. boxes[i].right = boxes[i].right * sx - dx;
  179. boxes[i].top = boxes[i].top * sy - dy;
  180. boxes[i].bottom = boxes[i].bottom* sy - dy;
  181. if(flip){
  182. float swap = boxes[i].left;
  183. boxes[i].left = 1.f - boxes[i].right;
  184. boxes[i].right = 1.f - swap;
  185. }
  186. boxes[i].left = constrain(0, 1, boxes[i].left);
  187. boxes[i].right = constrain(0, 1, boxes[i].right);
  188. boxes[i].top = constrain(0, 1, boxes[i].top);
  189. boxes[i].bottom = constrain(0, 1, boxes[i].bottom);
  190. boxes[i].x = (boxes[i].left+boxes[i].right)/2;
  191. boxes[i].y = (boxes[i].top+boxes[i].bottom)/2;
  192. boxes[i].w = (boxes[i].right - boxes[i].left);
  193. boxes[i].h = (boxes[i].bottom - boxes[i].top);
  194. boxes[i].w = constrain(0, 1, boxes[i].w);
  195. boxes[i].h = constrain(0, 1, boxes[i].h);
  196. }
  197. }
  198. void fill_truth_swag(char *path, float *truth, int classes, int flip, float dx, float dy, float sx, float sy){
  199. char labelpath[4096];
  200. find_replace(path, "images", "labels", labelpath);
  201. find_replace(labelpath, "JPEGImages", "labels", labelpath);
  202. find_replace(labelpath, ".jpg", ".txt", labelpath);
  203. find_replace(labelpath, ".JPG", ".txt", labelpath);
  204. find_replace(labelpath, ".JPEG", ".txt", labelpath);
  205. int count = 0;
  206. cortexBOXLABEL *boxes = read_boxes(labelpath, &count);
  207. randomize_boxes(boxes, count);
  208. correct_boxes(boxes, count, dx, dy, sx, sy, flip);
  209. float x,y,w,h;
  210. int id;
  211. int i;
  212. for (i = 0; i < count && i < 30; ++i) {
  213. x = boxes[i].x;
  214. y = boxes[i].y;
  215. w = boxes[i].w;
  216. h = boxes[i].h;
  217. id = boxes[i].id;
  218. if (w < .0 || h < .0) continue;
  219. int index = (4+classes) * i;
  220. truth[index++] = x;
  221. truth[index++] = y;
  222. truth[index++] = w;
  223. truth[index++] = h;
  224. if (id < classes) truth[index+id] = 1;
  225. }
  226. free(boxes);
  227. }
  228. void fill_truth_region(char *path, float *truth, int classes, int num_boxes, int flip, float dx, float dy, float sx, float sy){
  229. char labelpath[4096];
  230. find_replace(path, "images", "labels", labelpath);
  231. find_replace(labelpath, "JPEGImages", "labels", labelpath);
  232. find_replace(labelpath, ".jpg", ".txt", labelpath);
  233. find_replace(labelpath, ".png", ".txt", labelpath);
  234. find_replace(labelpath, ".JPG", ".txt", labelpath);
  235. find_replace(labelpath, ".JPEG", ".txt", labelpath);
  236. int count = 0;
  237. cortexBOXLABEL *boxes = read_boxes(labelpath, &count);
  238. randomize_boxes(boxes, count);
  239. correct_boxes(boxes, count, dx, dy, sx, sy, flip);
  240. float x,y,w,h;
  241. int id;
  242. int i;
  243. for (i = 0; i < count; ++i) {
  244. x = boxes[i].x;
  245. y = boxes[i].y;
  246. w = boxes[i].w;
  247. h = boxes[i].h;
  248. id = boxes[i].id;
  249. if (w < .005 || h < .005) continue;
  250. int col = (int)(x*num_boxes);
  251. int row = (int)(y*num_boxes);
  252. x = x*num_boxes - col;
  253. y = y*num_boxes - row;
  254. int index = (col+row*num_boxes)*(5+classes);
  255. if (truth[index]) continue;
  256. truth[index++] = 1;
  257. if (id < classes) truth[index+id] = 1;
  258. index += classes;
  259. truth[index++] = x;
  260. truth[index++] = y;
  261. truth[index++] = w;
  262. truth[index++] = h;
  263. }
  264. free(boxes);
  265. }
  266. void load_rle(cortexImage im, int *rle, int n){
  267. int count = 0;
  268. int curr = 0;
  269. int i,j;
  270. for(i = 0; i < n; ++i){
  271. for(j = 0; j < rle[i]; ++j){
  272. im.data[count++] =(float) curr;
  273. }
  274. curr = 1 - curr;
  275. }
  276. for(; count < im.h*im.w*im.c; ++count){
  277. im.data[count] = (float)curr;
  278. }
  279. }
  280. void or_image(cortexImage src, cortexImage dest, int c){
  281. int i;
  282. for(i = 0; i < src.w*src.h; ++i){
  283. if(src.data[i]) dest.data[dest.w*dest.h*c + i] = 1;
  284. }
  285. }
  286. void exclusive_image(cortexImage src){
  287. int k, j, i;
  288. int s = src.w*src.h;
  289. for(k = 0; k < src.c-1; ++k){
  290. for(i = 0; i < s; ++i){
  291. if (src.data[k*s + i]){
  292. for(j = k+1; j < src.c; ++j){
  293. src.data[j*s + i] = 0;
  294. }
  295. }
  296. }
  297. }
  298. }
  299. cortexBOX bound_image(cortexImage im){
  300. int x,y;
  301. int minx = im.w;
  302. int miny = im.h;
  303. int maxx = 0;
  304. int maxy = 0;
  305. for(y = 0; y < im.h; ++y){
  306. for(x = 0; x < im.w; ++x){
  307. if(im.data[y*im.w + x]){
  308. minx = (x < minx) ? x : minx;
  309. miny = (y < miny) ? y : miny;
  310. maxx = (x > maxx) ? x : maxx;
  311. maxy = (y > maxy) ? y : maxy;
  312. }
  313. }
  314. }
  315. cortexBOX b = { (float)minx, (float)miny, (float)(maxx - minx + 1), (float)(maxy - miny + 1) };
  316. //printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);
  317. return b;
  318. }
  319. void fill_truth_iseg(char *path, int num_boxes, float *truth, int classes, int w, int h, cortexARGS aug, int flip, int mw, int mh){
  320. char labelpath[4096];
  321. find_replace(path, "images", "mask", labelpath);
  322. find_replace(labelpath, "JPEGImages", "mask", labelpath);
  323. find_replace(labelpath, ".jpg", ".txt", labelpath);
  324. find_replace(labelpath, ".JPG", ".txt", labelpath);
  325. find_replace(labelpath, ".JPEG", ".txt", labelpath);
  326. FILE *file = fopen(labelpath, "r");
  327. if(!file) return;
  328. char buff[32788];
  329. int id;
  330. int i = 0;
  331. cortexImage part;
  332. part.fillFromVoid(w, h, 1);// = make_image(
  333. while((fscanf(file, "%d %s", &id, buff) == 2) && i < num_boxes){
  334. int n = 0;
  335. int *rle = read_intlist(buff, &n, 0);
  336. load_rle(part, rle, n);
  337. part.rotateCrop(aug.rad, aug.scale, aug.w, aug.h, aug.dx, aug.dy, aug.aspect);
  338. if(flip) part.flip();
  339. cortexBOX b = bound_image(part);
  340. if(b.w > 0){
  341. part.crop((int)b.x, (int)b.y, (int)b.w, (int)b.h);
  342. part.resize(mw, mh);
  343. truth[i*(4 + mw*mh + 1) + 0] = (b.x + b.w/2.f)/part.w;
  344. truth[i*(4 + mw*mh + 1) + 1] = (b.y + b.h/2.f)/part.h;
  345. truth[i*(4 + mw*mh + 1) + 2] = b.w/part.w;
  346. truth[i*(4 + mw*mh + 1) + 3] = b.h/part.h;
  347. int j;
  348. for(j = 0; j < mw*mh; ++j){
  349. truth[i*(4 + mw*mh + 1) + 4 + j] = part.data[j];
  350. };
  351. truth[i*(4 + mw*mh + 1) + 4 + mw*mh] = (float)id;
  352. //free_image(crop);
  353. //free_image(mask);
  354. ++i;
  355. };
  356. //free_image(sized);
  357. //free(rle);
  358. };
  359. //fclose(file);
  360. //free_image(part);
  361. };
  362. void fill_truth_detection(char *path, int num_boxes, float *truth, int classes, int flip, float dx, float dy, float sx, float sy){
  363. char labelpath[4096];
  364. find_replace(path, "images", "labels", labelpath);
  365. find_replace(labelpath, "JPEGImages", "labels", labelpath);
  366. find_replace(labelpath, "raw", "labels", labelpath);
  367. find_replace(labelpath, ".jpg", ".txt", labelpath);
  368. find_replace(labelpath, ".png", ".txt", labelpath);
  369. find_replace(labelpath, ".JPG", ".txt", labelpath);
  370. find_replace(labelpath, ".JPEG", ".txt", labelpath);
  371. int count = 0;
  372. cortexBOXLABEL *boxes = read_boxes(labelpath, &count);
  373. randomize_boxes(boxes, count);
  374. correct_boxes(boxes, count, dx, dy, sx, sy, flip);
  375. if(count > num_boxes) count = num_boxes;
  376. float x,y,w,h;
  377. int id;
  378. int i;
  379. for (i = 0; i < count; ++i) {
  380. x = boxes[i].x;
  381. y = boxes[i].y;
  382. w = boxes[i].w;
  383. h = boxes[i].h;
  384. id = boxes[i].id;
  385. if ((w < .001 || h < .001)) continue;
  386. truth[i*5+0] = x;
  387. truth[i*5+1] = y;
  388. truth[i*5+2] = w;
  389. truth[i*5+3] = h;
  390. truth[i*5+4] = (float)id;
  391. }
  392. free(boxes);
  393. }
  394. #define NUMCHARS 37
  395. /*
  396. void print_letters(float *pred, int n)
  397. {
  398. int i;
  399. for(i = 0; i < n; ++i){
  400. int index = max_index(pred+i*NUMCHARS, NUMCHARS);
  401. printf("%c", int_to_alphanum(index));
  402. }
  403. printf("\n");
  404. }
  405. */
  406. void fill_truth_captcha(char *path, int n, float *truth){
  407. char *begin = strrchr(path, '/');
  408. ++begin;
  409. int i;
  410. for (i = 0; i < (int)strlen(begin) && i < n && begin[i] != '.'; ++i){
  411. int index = alphanum_to_int(begin[i]);
  412. if( index > 35 ) printf("Bad %c\n", begin[i]);
  413. truth[i*NUMCHARS+index] = 1;
  414. }
  415. for(;i < n; ++i){
  416. truth[i*NUMCHARS + NUMCHARS-1] = 1;
  417. }
  418. }
  419. cortexDATA load_data_captcha( cortexList *paths, int m, int k, int w, int h){
  420. if(m) paths = get_random_paths(paths, m);
  421. cortexDATA d = { 0 };
  422. d.bShallow = false;
  423. d.X = load_image_paths(paths, w, h);
  424. d.Y = make_matrix((uint32_t)paths->size(), k*NUMCHARS);
  425. int i;
  426. i = 0;
  427. for (cortexList::iterator index = paths->begin(); index < paths->end(); ++index){
  428. fill_truth_captcha((*index), k, d.Y.values[i] );
  429. i++;
  430. };
  431. if(m) free(paths);
  432. return d;
  433. };
  434. cortexDATA load_data_captcha_encode(cortexList *paths, int m, int w, int h){
  435. if(m) paths = get_random_paths(paths, m);
  436. cortexDATA d = { 0 };
  437. d.bShallow = false;
  438. d.X = load_image_paths(paths, w, h);
  439. d.X.cols = 17100;
  440. d.Y = d.X;
  441. if(m) free(paths);
  442. return d;
  443. }
  444. void fill_truth(char *path, cortexList *labels, float *truth){
  445. int i;
  446. memset(truth, 0, labels->size()*sizeof(float));
  447. int count = 0;
  448. i = 0;
  449. for(cortexList::iterator index = labels->begin(); index < labels->end(); ++index ){
  450. if(strstr(path, (*index))){
  451. truth[i] = 1.0f;
  452. ++count;
  453. };
  454. i++;
  455. };
  456. if(count != 1 && ( labels->size() != 1 || count != 0)) printf("Too many or too few labels: %d, %s\n", count, path);
  457. };
  458. void fill_hierarchy(float *truth, uint32_t k, cortexTREE *hierarchy){
  459. uint32_t j;
  460. for(j = 0; j < k; ++j){
  461. if(truth[j]){
  462. int parent = hierarchy->parent[j];
  463. while(parent >= 0){
  464. truth[parent] = 1.0f;
  465. parent = hierarchy->parent[parent];
  466. };
  467. };
  468. };
  469. uint32_t i;
  470. uint32_t count = 0;
  471. for(j = 0; j < hierarchy->groups; ++j){
  472. //printf("%d\n", count);
  473. int mask = 1;
  474. for(i = 0; i < hierarchy->group_size[j]; ++i){
  475. if(truth[count + i]){
  476. mask = 0;
  477. break;
  478. }
  479. }
  480. if (mask) {
  481. for(i = 0; i < hierarchy->group_size[j]; ++i){
  482. truth[count + i] = SECRET_NUM;
  483. }
  484. }
  485. count += hierarchy->group_size[j];
  486. }
  487. }
  488. cortexMATRIX load_regression_labels_paths(cortexList *paths){
  489. cortexMATRIX y = make_matrix((uint32_t)paths->size(), 1);
  490. uint32_t i;
  491. i = 0;
  492. for(cortexList::iterator index = paths->begin(); index < paths->end(); index++ ){
  493. char labelpath[4096];
  494. find_replace((*index), "images", "targets", labelpath);
  495. find_replace(labelpath, "JPEGImages", "targets", labelpath);
  496. find_replace(labelpath, ".jpg", ".txt", labelpath);
  497. find_replace(labelpath, ".png", ".txt", labelpath);
  498. FILE *file = fopen(labelpath, "r");
  499. fscanf(file, "%f", &(y.values[i][0]));
  500. fclose(file);
  501. i++;
  502. };
  503. return y;
  504. };
  505. cortexMATRIX load_labels_paths(cortexList *paths, cortexList *labels, cortexTREE *hierarchy){
  506. cortexMATRIX y = make_matrix((uint32_t)paths->size(), (uint32_t)labels->size());
  507. int i;
  508. i = 0;
  509. for (cortexList::iterator index = paths->begin(); index < paths->end(); index++){
  510. fill_truth((*index), labels, y.values[i]);
  511. if(hierarchy){
  512. fill_hierarchy(y.values[i], (uint32_t)labels->size(), hierarchy);
  513. }
  514. i++;
  515. };
  516. return y;
  517. };
  518. cortexMATRIX load_tags_paths(cortexList *paths, int k){
  519. cortexMATRIX y = make_matrix((uint32_t)paths->size(), k);
  520. int i;
  521. int count = 0;
  522. i = 0;
  523. for (cortexList::iterator index = paths->begin(); index < paths->end(); index ){
  524. char label[4096];
  525. find_replace((*index), "imgs", "labels", label);
  526. find_replace(label, "_iconl.jpeg", ".txt", label);
  527. FILE *file = fopen(label, "r");
  528. if(!file){
  529. find_replace(label, "labels", "labels2", label);
  530. file = fopen(label, "r");
  531. if(!file) continue;
  532. }
  533. ++count;
  534. int tag;
  535. while(fscanf(file, "%d", &tag) == 1){
  536. if(tag < k){
  537. y.values[i][tag] = 1;
  538. };
  539. };
  540. fclose(file);
  541. };
  542. printf("%d/%d\n", count, paths->size());
  543. return y;
  544. };
  545. char **get_labels(char *filename){
  546. cortexList *plist = get_paths(filename);
  547. char **labels = (char **)calloc(plist->size(), sizeof(char*));// list_to_array(plist);
  548. int i = 0;
  549. for (cortexList::iterator index = plist->begin(); index < plist->end(); index++){
  550. labels[i] = (*index);
  551. i++;
  552. };
  553. plist->clear();
  554. delete(plist);
  555. //free_cortexList( ); Not Used because frees chars
  556. return labels;
  557. };
  558. void free_data(cortexDATA *d){
  559. if(!d->bShallow){
  560. free_matrix(d->X);
  561. free_matrix(d->Y);
  562. }else{
  563. free(d->X.values);
  564. free(d->Y.values);
  565. };
  566. };
  567. void free_data(cortexDATA d){
  568. if (!d.bShallow){
  569. free_matrix(d.X);
  570. free_matrix(d.Y);
  571. }
  572. else{
  573. free(d.X.values);
  574. free(d.Y.values);
  575. };
  576. };
  577. cortexImage get_segmentation_image(char *path, int w, int h, int classes){
  578. char labelpath[4096];
  579. find_replace(path, "images", "mask", labelpath);
  580. find_replace(labelpath, "JPEGImages", "mask", labelpath);
  581. find_replace(labelpath, ".jpg", ".txt", labelpath);
  582. find_replace(labelpath, ".JPG", ".txt", labelpath);
  583. find_replace(labelpath, ".JPEG", ".txt", labelpath);
  584. cortexImage mask;
  585. mask.makeEmpty(w, h, classes);
  586. FILE *file = fopen(labelpath, "r");
  587. if(!file) file_error(labelpath);
  588. char buff[32788];
  589. int id;
  590. cortexImage *tVal;
  591. tVal = new cortexImage();
  592. tVal->makeEmpty( w, h, 1);
  593. while(fscanf(file, "%d %s", &id, buff) == 2){
  594. int n = 0;
  595. int *rle = read_intlist(buff, &n, 0);
  596. load_rle(*tVal, rle, n);
  597. or_image(*tVal, mask, id);
  598. free(rle);
  599. }
  600. //exclusive_image(mask);
  601. fclose(file);
  602. delete(tVal);
  603. return mask;
  604. };
  605. cortexImage get_segmentation_image2(char *path, int w, int h, int classes){
  606. char labelpath[4096];
  607. find_replace(path, "images", "mask", labelpath);
  608. find_replace(labelpath, "JPEGImages", "mask", labelpath);
  609. find_replace(labelpath, ".jpg", ".txt", labelpath);
  610. find_replace(labelpath, ".JPG", ".txt", labelpath);
  611. find_replace(labelpath, ".JPEG", ".txt", labelpath);
  612. cortexImage im;
  613. im.makeEmpty(w, h, classes + 1);
  614. //image mask = make_image(w, h, classes+1);
  615. int i;
  616. for(i = 0; i < w*h; ++i){
  617. im.data[w*h*classes + i] = 1;
  618. };
  619. FILE *file = fopen(labelpath, "r");
  620. if(!file) file_error(labelpath);
  621. char buff[32788];
  622. int id;
  623. cortexImage *tVal;
  624. tVal = new cortexImage();
  625. tVal->makeEmpty(w, h, 1);
  626. while(fscanf(file, "%d %s", &id, buff) == 2){
  627. int n = 0;
  628. int *rle = read_intlist(buff, &n, 0);
  629. load_rle(*tVal, rle, n);
  630. or_image(*tVal, im, id);
  631. for(i = 0; i < w*h; ++i){
  632. if(im.data[i]) im.data[w*h*classes + i] = 0;
  633. }
  634. free(rle);
  635. }
  636. //exclusive_image(mask);
  637. fclose(file);
  638. delete(tVal);
  639. return im;
  640. };
  641. cortexDATA *load_data_seg( cortexList *paths, int m, int w, int h, int classes, int min, int max, float angle, float aspect, float hue, float saturation, float exposure, int div){
  642. cortexList *random_paths = get_random_paths(paths, m);
  643. int i;
  644. cortexDATA *d;
  645. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  646. memset(d, 0, sizeof(cortexDATA));
  647. d->bShallow = false;
  648. d->X.rows = (uint32_t)random_paths->size();
  649. d->X.values = (float**)calloc(d->X.rows, sizeof(float*));
  650. d->X.cols = h*w * 3;
  651. d->Y.rows = (uint32_t)random_paths->size();
  652. d->Y.cols = h*w*classes / div / div;
  653. d->Y.values = (float**)calloc(d->X.rows, sizeof(float*));
  654. i = 0;
  655. for( cortexList::iterator index = random_paths->begin(); index < random_paths->end(); index ){
  656. cortexImage *im;
  657. im = new cortexImage();
  658. im->fillFromFile((*index), 0, 0);
  659. //image orig = load_image_color((*index), 0, 0);
  660. cortexARGS a = im->randomAugmentArgs(angle, aspect, min, max, w, h);
  661. im->rotateCrop(a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect);
  662. //image sized = rotate_crop_image(orig, a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect);
  663. int flip = rand()%2;
  664. if(flip) im->flip();
  665. im->randomDistort(hue, saturation, exposure);
  666. //random_distort_image(sized, hue, saturation, exposure);
  667. d->X.values[i] = im->data;
  668. cortexImage mask = get_segmentation_image((*index), im->w, im->h, classes);
  669. //image mask = make_image(orig.w, orig.h, classes+1);
  670. mask.rotateCrop( a.rad, a.scale/div, a.w/div, a.h/div, a.dx/div, a.dy/div, a.aspect);
  671. if(flip) mask.flip();
  672. d->Y.values[i] = mask.data;
  673. //To Do: Memory Allocation Issues
  674. //free_image(orig);//??
  675. //free_image(mask);//??
  676. /*
  677. image rgb = mask_to_rgb(sized_m, classes);
  678. show_image(rgb, "part");
  679. show_image(sized, "orig");
  680. cvWaitKey(0);
  681. free_image(rgb);
  682. */
  683. i++;
  684. };
  685. free_cortexList(random_paths);
  686. return d;
  687. };
  688. cortexDATA *load_data_iseg( cortexList *paths, int m, int w, int h, int classes, int boxes, int coords, int min, int max, float angle, float aspect, float hue, float saturation, float exposure){
  689. cortexList *random_paths = get_random_paths(paths, m);
  690. int i;
  691. cortexDATA *d;
  692. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  693. memset(d, 0, sizeof(cortexDATA));
  694. d->bShallow = false;
  695. d->X.rows = (uint32_t)random_paths->size();
  696. d->X.values = (float**)calloc(d->X.rows, sizeof(float*));
  697. d->X.cols = h*w * 3;
  698. d->Y = make_matrix((uint32_t)random_paths->size(), (coords + 1)*boxes);
  699. i = 0;
  700. for( cortexList::iterator index = random_paths->begin(); index < random_paths->end(); index++ ){
  701. cortexImage *im;
  702. im = new cortexImage();
  703. im->fillFromFile((*index), 0, 0);
  704. //image orig = load_image_color((*index), 0, 0);
  705. cortexARGS a = im->randomAugmentArgs(angle, aspect, min, max, w, h);
  706. im->rotateCrop(a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect);
  707. //image sized = rotate_crop_image(orig, a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect);
  708. int flip = rand()%2;
  709. if(flip) im->flip();
  710. im->randomDistort( hue, saturation, exposure);
  711. d->X.values[i] = im->data;
  712. //show_image(sized, "image");
  713. fill_truth_iseg((*index), boxes, d->Y.values[i], classes, im->w, im->h, a, flip, 14, 14);
  714. delete(im);
  715. //free_image(orig);
  716. /*
  717. image rgb = mask_to_rgb(sized_m, classes);
  718. show_image(rgb, "part");
  719. show_image(sized, "orig");
  720. cvWaitKey(0);
  721. free_image(rgb);
  722. */
  723. i++;
  724. };
  725. free_cortexList(random_paths);
  726. return d;
  727. }
  728. cortexDATA *load_data_region(cortexList *paths, int m, int w, int h, int size, int classes, float jitter, float hue, float saturation, float exposure){
  729. cortexList *random_paths = get_random_paths(paths, m);
  730. int i;
  731. cortexDATA *d;
  732. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  733. memset(d, 0, sizeof(cortexDATA));
  734. d->bShallow = false;
  735. d->X.rows = (uint32_t)random_paths->size();
  736. d->X.values = (float**)calloc(d->X.rows, sizeof(float*));
  737. d->X.cols = h*w * 3;
  738. int k = size*size*(5+classes);
  739. d->Y = make_matrix((uint32_t)random_paths->size(), k);
  740. i = 0;
  741. for( cortexList::iterator index = random_paths->begin(); index < random_paths->end(); index++ ){
  742. cortexImage *im;
  743. im = new cortexImage();
  744. im->fillFromFile((*index), 0, 0);
  745. //image orig = load_image_color(random_paths[i], 0, 0);
  746. int oh = im->h;
  747. int ow = im->w;
  748. int dw = (int)(ow*jitter);
  749. int dh = (int)(oh*jitter);
  750. int pleft = (int)rand_uniform((float)-dw, (float)dw);
  751. int pright = (int)rand_uniform((float)-dw, (float)dw);
  752. int ptop = (int)rand_uniform((float)-dh, (float)dh);
  753. int pbot = (int)rand_uniform((float)-dh, (float)dh);
  754. int swidth = ow - pleft - pright;
  755. int sheight = oh - ptop - pbot;
  756. float sx = (float)swidth / ow;
  757. float sy = (float)sheight / oh;
  758. int flip = rand()%2;
  759. im->crop(pleft, ptop, swidth, sheight);
  760. //image cropped = crop_image(orig, pleft, ptop, swidth, sheight);
  761. float dx = ((float)pleft/ow)/sx;
  762. float dy = ((float)ptop /oh)/sy;
  763. //image sized = resize_image(cropped, w, h);
  764. im->resize(w, h);
  765. if(flip) im->flip();
  766. im->randomDistort(hue, saturation, exposure);
  767. d->X.values[i] = im->data;
  768. fill_truth_region((*index), d->Y.values[i], classes, size, flip, dx, dy, 1.f / sx, 1.f / sy);
  769. delete(im);
  770. //free_image(orig);
  771. //free_image(cropped);
  772. i++;
  773. }
  774. free(random_paths);
  775. return d;
  776. };
  777. cortexDATA *load_data_compare(cortexList *paths, int m, uint32_t classes, uint32_t w, uint32_t h){
  778. cortexList *random_paths = get_random_paths(paths, false);
  779. uint32_t i, j;
  780. cortexDATA *d;
  781. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  782. memset(d, 0, sizeof(cortexDATA));
  783. d->bShallow = 0;
  784. d->X.rows = (uint32_t)paths->size() / 2;
  785. d->X.values = (float**)calloc(d->X.rows, sizeof(float*));
  786. d->X.cols = h*w * 6;
  787. uint32_t k = 2 * (classes);
  788. d->Y = make_matrix((uint32_t)paths->size() / 2, k);
  789. for( i = 0; i < paths->size() / 2; i++ ){
  790. cortexImage *im1;
  791. im1 = new cortexImage();
  792. im1->fillFromFile(paths->at(i * 2), w, h);
  793. cortexImage *im2;
  794. im2 = new cortexImage();
  795. im2->fillFromFile(paths->at(i * 2 + 1), w, h);
  796. d->X.values[i] = (float*)calloc(d->X.cols, sizeof(float));
  797. memcpy(d->X.values[i], im1->data, h*w * 3 * sizeof(float));
  798. memcpy(d->X.values[i] + h*w * 3, im2->data, h*w * 3 * sizeof(float));
  799. int id;
  800. float iou;
  801. char imlabel1[4096];
  802. char imlabel2[4096];
  803. find_replace(paths->at(i*2), "imgs", "labels", imlabel1);
  804. find_replace(imlabel1, "jpg", "txt", imlabel1);
  805. FILE *fp1 = fopen(imlabel1, "r");
  806. while(fscanf(fp1, "%d %f", &id, &iou) == 2){
  807. if( d->Y.values[i][2*id] < iou) d->Y.values[i][2*id] = iou;
  808. };
  809. find_replace( paths->at(i*2+1), "imgs", "labels", imlabel2 );
  810. find_replace( imlabel2, "jpg", "txt", imlabel2 );
  811. FILE *fp2 = fopen(imlabel2, "r");
  812. while(fscanf(fp2, "%d %f", &id, &iou) == 2){
  813. if (d->Y.values[i][2*id + 1] < iou) d->Y.values[i][2*id + 1] = iou;
  814. };
  815. for (j = 0; j < classes; ++j){
  816. if( d->Y.values[i][2 * j] > .5f && d->Y.values[i][2 * j + 1] < .5f ){
  817. d->Y.values[i][2 * j] = 1.f;
  818. d->Y.values[i][2 * j + 1] = 0.f;
  819. } else if( d->Y.values[i][2 * j] < .5f && d->Y.values[i][2 * j + 1] > .5f ){
  820. d->Y.values[i][2 * j] = 0.f;
  821. d->Y.values[i][2 * j + 1] = 1.f;
  822. } else {
  823. d->Y.values[i][2*j] = SECRET_NUM;
  824. d->Y.values[i][2 * j + 1] = SECRET_NUM;
  825. };
  826. };
  827. fclose(fp1);
  828. fclose(fp2);
  829. delete(im1);
  830. delete(im2);
  831. }
  832. if(m) free(paths);
  833. return d;
  834. };
  835. cortexDATA *load_data_swag(cortexList *paths, int classes, float jitter){
  836. int index = rand() % paths->size();
  837. char *random_path = paths->at(index);
  838. cortexImage *orig;
  839. orig = new cortexImage();
  840. orig->fillFromFile(random_path, 0, 0);
  841. int h = orig->h;
  842. int w = orig->w;
  843. cortexDATA *d;
  844. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  845. memset(d, 0, sizeof(cortexDATA));
  846. d->bShallow = 0;
  847. d->width = w;
  848. d->height = h;
  849. d->X.rows = 1;
  850. d->X.values = (float**)calloc(d->X.rows, sizeof(float*));
  851. d->X.cols = h*w * 3;
  852. int k = (4+classes)*30;
  853. d->Y = make_matrix(1, k);
  854. int dw = (int)(w*jitter);
  855. int dh = (int)(h*jitter);
  856. int pleft = (int)rand_uniform((float)-dw, (float)dw);
  857. int pright = (int)rand_uniform((float)-dw, (float)dw);
  858. int ptop = (int)rand_uniform((float)-dh, (float)dh);
  859. int pbot = (int)rand_uniform((float)-dh, (float)dh);
  860. int swidth = w - pleft - pright;
  861. int sheight = h - ptop - pbot;
  862. float sx = (float)(swidth / w);
  863. float sy = (float)(sheight / h);
  864. int flip = rand()%2;
  865. orig->crop(pleft, ptop, swidth, sheight);
  866. float dx = ((float)pleft/w)/sx;
  867. float dy = ((float)ptop /h)/sy;
  868. orig->resize( w, h);
  869. if(flip) orig->flip();
  870. d->X.values[0] = orig->data;
  871. fill_truth_swag(random_path, d->Y.values[0], classes, flip, (float)dx, (float)dy, 1.f / sx, 1.f / sy);
  872. delete(orig);
  873. return d;
  874. };
  875. cortexDATA *load_data_detection(cortexList *paths, int m, int w, int h, int boxes, int classes, float jitter, float hue, float saturation, float exposure){
  876. cortexList *random_paths = get_random_paths(paths, false);
  877. int i;
  878. cortexDATA *d;
  879. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  880. memset(d, 0, sizeof(cortexDATA));
  881. d->bShallow = false;
  882. d->X.rows = (uint32_t)random_paths->size();
  883. d->X.values = (float**)calloc(d->X.rows, sizeof(float*));
  884. d->X.cols = h*w * 3;
  885. d->Y = make_matrix((uint32_t)random_paths->size(), 5 * boxes);
  886. i = 0;
  887. for( cortexList::iterator index = random_paths->begin(); index < random_paths->end(); index++ ){
  888. cortexImage *orig;
  889. orig = new cortexImage();
  890. orig->fillFromFile((*index), 0, 0);
  891. cortexImage *sized;
  892. sized = new cortexImage();
  893. sized->makeEmpty(w, h, orig->c);
  894. sized->fill(.5f);
  895. float dw = jitter * orig->w;
  896. float dh = jitter * orig->h;
  897. float new_ar = (orig->w + rand_uniform(-dw, dw)) / (orig->h + rand_uniform(-dh, dh));
  898. float scale = rand_uniform(.25, 2);
  899. float nw, nh;
  900. if(new_ar < 1){
  901. nh = scale * h;
  902. nw = nh * new_ar;
  903. } else {
  904. nw = scale * w;
  905. nh = nw / new_ar;
  906. }
  907. float dx = rand_uniform(0.f, (float)(w - nw));
  908. float dy = rand_uniform(0.f, (float)(h - nh));
  909. orig->place((int)nw, (int)nh, (int)dx, (int)dy, sized);
  910. sized->randomDistort( hue, saturation, exposure );
  911. int flip = rand()%2;
  912. if(flip) sized->flip();
  913. d->X.values[i] = sized->data;
  914. fill_truth_detection((*index), boxes, d->Y.values[i], classes, flip, -dx / w, -dy / h, nw / w, nh / h);
  915. delete(orig);
  916. i++;
  917. };
  918. free_cortexList(random_paths);
  919. if (m)free_cortexList(random_paths);
  920. return d;
  921. }
  922. void *load_thread(void *ptr){
  923. //printf("Loading data: %d\n", rand());
  924. cortexTHREAD_ARGS a = *(cortexTHREAD_ARGS*)ptr;
  925. if(a.exposure == 0) a.exposure = 1.f;
  926. if(a.saturation == 0) a.saturation = 1.f;
  927. if(a.aspect == 0) a.aspect = 1.f;
  928. if (a.type == OLD_CLASSIFICATION_DATA){
  929. a.d = load_data_old(a.paths, a.m, a.labels, a.w, a.h);
  930. } else if (a.type == REGRESSION_DATA){
  931. a.d = load_data_regression(a.paths, a.m, a.min, a.max, a.size, a.angle, a.aspect, a.hue, a.saturation, a.exposure);
  932. } else if (a.type == CLASSIFICATION_DATA){
  933. a.d = load_data_augment(a.paths, a.m, a.labels, a.hierarchy, a.min, a.max, a.size, a.angle, a.aspect, a.hue, a.saturation, a.exposure, a.center);
  934. } else if (a.type == SUPER_DATA){
  935. a.d = load_data_super(a.paths, a.m, a.w, a.h, a.scale);
  936. } else if (a.type == WRITING_DATA){
  937. a.d = load_data_writing(a.paths, a.m, a.w, a.h, a.out_w, a.out_h);
  938. } else if (a.type == INSTANCE_DATA){
  939. a.d = load_data_iseg(a.paths, a.m, a.w, a.h, a.classes, a.num_boxes, a.coords, a.min, a.max, a.angle, a.aspect, a.hue, a.saturation, a.exposure);
  940. } else if (a.type == SEGMENTATION_DATA){
  941. a.d = load_data_seg(a.paths, a.m, a.w, a.h, a.classes, a.min, a.max, a.angle, a.aspect, a.hue, a.saturation, a.exposure, a.scale);
  942. } else if (a.type == REGION_DATA){
  943. a.d = load_data_region(a.paths, a.m, a.w, a.h, a.num_boxes, a.classes, a.jitter, a.hue, a.saturation, a.exposure);
  944. } else if (a.type == DETECTION_DATA){
  945. a.d = load_data_detection(a.paths, a.m, a.w, a.h, a.num_boxes, a.classes, a.jitter, a.hue, a.saturation, a.exposure);
  946. } else if (a.type == SWAG_DATA){
  947. a.d = load_data_swag(a.paths, a.classes, a.jitter);
  948. } else if (a.type == COMPARE_DATA){
  949. a.d = load_data_compare(a.paths, a.m, a.classes, a.w, a.h);
  950. } else if (a.type == IMAGE_DATA){
  951. a.im = new cortexImage();
  952. a.im->fillFromFile(a.path, 0, 0);
  953. a.resized = new cortexImage();
  954. a.resized->fillFromFile(a.path, 0, 0);
  955. a.resized->resize(a.w, a.h);
  956. //*(a.resized) = resize_image(*(a.im), a.w, a.h);
  957. } else if (a.type == LETTERBOX_DATA){
  958. a.im->fillFromFile(a.path, 0, 0);
  959. a.resized = new cortexImage();
  960. a.resized->fillFromFile(a.path, 0, 0);
  961. a.resized->resize(a.w, a.h);
  962. //*(a.resized) = letterbox_image(*(a.im), a.w, a.h);
  963. } else if (a.type == TAG_DATA){
  964. a.d = load_data_tag(a.paths, a.m, a.classes, a.min, a.max, a.size, a.angle, a.aspect, a.hue, a.saturation, a.exposure);
  965. }
  966. free(ptr);
  967. return 0;
  968. }
  969. HANDLE load_data_in_thread(cortexTHREAD_ARGS args){
  970. HANDLE thread;
  971. DWORD m_dwThreadID;
  972. cortexTHREAD_ARGS *ptr = (cortexTHREAD_ARGS*)calloc(1, sizeof(cortexTHREAD_ARGS));
  973. *ptr = args;
  974. //if(pthread_create(&thread, 0, load_thread, ptr)) error("Thread creation failed");
  975. thread = CreateThread(
  976. NULL,
  977. 0,
  978. (LPTHREAD_START_ROUTINE)load_thread,
  979. (LPVOID)ptr,
  980. 0,
  981. &m_dwThreadID);
  982. return thread;
  983. }
  984. void *load_threads(void *ptr){
  985. uint32_t i;
  986. cortexTHREAD_ARGS args = *(cortexTHREAD_ARGS *)ptr;
  987. if (args.threads == 0) args.threads = 1;
  988. cortexDATA *out = args.d;
  989. uint32_t total = args.n;
  990. free(ptr);
  991. cortexDATA *buffers = (cortexDATA *)calloc(args.threads, sizeof(cortexDATA));
  992. HANDLE *threads = (HANDLE *)calloc(args.threads, sizeof(HANDLE));
  993. for(i = 0; i < args.threads; ++i){
  994. args.d = buffers + i;
  995. args.n = (i+1) * total/args.threads - i * total/args.threads;
  996. threads[i] = load_data_in_thread(args);
  997. }
  998. for(i = 0; i < args.threads; ++i){
  999. #ifdef __linux__
  1000. pthread_join(threads[i], 0);
  1001. #else
  1002. WaitForSingleObject(threads[i], INFINITE);
  1003. CloseHandle(threads[i]);
  1004. #endif
  1005. }
  1006. out = concat_datas(buffers, args.threads);
  1007. out->bShallow = 0;
  1008. for(i = 0; i < args.threads; ++i){
  1009. buffers[i].bShallow = 1;
  1010. free_data(buffers[i]);
  1011. }
  1012. free(buffers);
  1013. free(threads);
  1014. return 0;
  1015. }
  1016. void load_data_blocking(cortexTHREAD_ARGS args){
  1017. cortexTHREAD_ARGS *ptr = (cortexTHREAD_ARGS *)calloc(1, sizeof(cortexTHREAD_ARGS));
  1018. *ptr = args;
  1019. load_thread(ptr);
  1020. };
  1021. HANDLE load_data(cortexTHREAD_ARGS args){
  1022. HANDLE thread;
  1023. DWORD m_dwThreadID;
  1024. cortexTHREAD_ARGS *ptr = (cortexTHREAD_ARGS*)calloc(1, sizeof(cortexTHREAD_ARGS));
  1025. *ptr = args;
  1026. //if(pthread_create(&thread, 0, load_threads, ptr)) error("Thread creation failed");
  1027. thread = CreateThread(
  1028. NULL,
  1029. 0,
  1030. (LPTHREAD_START_ROUTINE)load_thread,
  1031. (LPVOID)ptr,
  1032. 0,
  1033. &m_dwThreadID);
  1034. return thread;
  1035. }
  1036. cortexDATA *load_data_writing(cortexList *paths, uint32_t m, uint32_t w, uint32_t h, uint32_t out_w, uint32_t out_h){
  1037. cortexList *random_paths = get_random_paths(paths, false);
  1038. cortexList *replace_paths = find_replace_paths(random_paths, ".png", "-label.png");
  1039. cortexDATA *d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1040. memset(d, 0, sizeof(cortexDATA));
  1041. d->bShallow = false;
  1042. d->X = load_image_paths(paths, w, h);
  1043. d->Y = load_image_paths_gray(replace_paths, out_w, out_h);
  1044. free_cortexList(replace_paths);
  1045. free_cortexList(random_paths);
  1046. if(m)free_cortexList(paths);
  1047. return d;
  1048. };
  1049. cortexDATA *load_data_old(cortexList *paths, int m, cortexList *labels, int w, int h){
  1050. cortexList *random_paths = get_random_paths(paths, m);
  1051. cortexDATA *d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1052. memset(d, 0, sizeof(cortexDATA));
  1053. d->bShallow = false;
  1054. d->X = load_image_paths(random_paths, w, h);
  1055. d->Y = load_labels_paths(random_paths, labels, 0);
  1056. free_cortexList(random_paths);
  1057. return d;
  1058. };
  1059. cortexDATA *load_data_study(cortexList *paths, int m, cortexList *labels, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, bool center){
  1060. cortexDATA *d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1061. memset(d, 0, sizeof(cortexDATA));
  1062. d->indexes = (int*)calloc(paths->size(), sizeof(int));
  1063. cortexList *random_paths = get_random_paths_indexes(paths, m, d->indexes);
  1064. d->bShallow = false;
  1065. d->X = *load_image_augment_paths(random_paths, min, max, size, angle, aspect, hue, saturation, exposure,center);
  1066. d->Y = load_labels_paths(random_paths, labels, NULL);
  1067. free_cortexList(random_paths);
  1068. return d;
  1069. };
  1070. cortexDATA *load_data_super(cortexList *paths, uint32_t m, uint32_t w, uint32_t h, uint32_t scale){
  1071. cortexList *random_paths = get_random_paths(paths, false );
  1072. cortexDATA *d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1073. memset(d, 0, sizeof(cortexDATA));
  1074. d->bShallow = false;
  1075. int i;
  1076. d->X.rows = (uint32_t)paths->size();
  1077. d->X.values = (float**)calloc(paths->size(), sizeof(float*));
  1078. d->X.cols = w*h * 3;
  1079. d->Y.rows = (uint32_t)paths->size();
  1080. d->Y.values = (float**)calloc(paths->size(), sizeof(float*));
  1081. d->Y.cols = w*scale * h*scale * 3;
  1082. i = 0;
  1083. for (cortexList::iterator index = paths->begin(); index < paths->end(); ++index){
  1084. cortexImage *im;
  1085. im = new cortexImage();
  1086. im->fillFromFile((*index), 0, 0);
  1087. im->randomCrop(w*scale, h*scale);
  1088. int flip = rand()%2;
  1089. if (flip) im->flip();
  1090. cortexImage *im2;
  1091. im2 = new cortexImage(im);
  1092. im2->resize(w, h);
  1093. d->X.values[i] = im2->data;
  1094. d->Y.values[i] = im->data;
  1095. delete(im2);
  1096. delete(im);
  1097. i++;
  1098. };
  1099. free_cortexList(random_paths);
  1100. if (m) free_cortexList(paths);
  1101. return d;
  1102. };
  1103. cortexDATA *load_data_regression(cortexList *paths, uint32_t m, uint32_t min, uint32_t max, uint32_t size, float angle, float aspect, float hue, float saturation, float exposure){
  1104. cortexList *random_paths = get_random_paths(paths, false);
  1105. cortexDATA *d;
  1106. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1107. memset(d, 0, sizeof(cortexDATA));
  1108. d->bShallow = false;
  1109. d->X = *load_image_augment_paths(random_paths, min, max, size, angle, aspect, hue, saturation, exposure, 0);
  1110. d->Y = load_regression_labels_paths(random_paths);
  1111. free_cortexList(random_paths);
  1112. if (m) free_cortexList(paths);
  1113. return d;
  1114. };
  1115. cortexDATA *load_data_augment(
  1116. cortexList *paths,
  1117. uint32_t m,
  1118. cortexList *labels,
  1119. cortexTREE *hierarchy,
  1120. uint32_t min,
  1121. uint32_t max,
  1122. uint32_t size,
  1123. float angle,
  1124. float aspect,
  1125. float hue,
  1126. float saturation,
  1127. float exposure,
  1128. uint32_t center
  1129. ){
  1130. cortexList *random_paths = get_random_paths(paths, false);
  1131. cortexDATA *d;
  1132. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1133. memset(d, 0, sizeof(cortexDATA));
  1134. d->bShallow = 0;
  1135. d->X = *load_image_augment_paths(paths, min, max, size, angle, aspect, hue, saturation, exposure, center);
  1136. d->Y = load_labels_paths(paths, labels, hierarchy);
  1137. free_cortexList(random_paths);
  1138. if (m) free_cortexList(paths);
  1139. return d;
  1140. };
  1141. cortexDATA *load_data_tag(
  1142. cortexList *paths,
  1143. uint32_t m,
  1144. uint32_t k,
  1145. uint32_t min,
  1146. uint32_t max,
  1147. uint32_t size,
  1148. float angle,
  1149. float aspect,
  1150. float hue,
  1151. float saturation,
  1152. float exposure
  1153. ){
  1154. cortexList *random_paths = get_random_paths(paths, false);
  1155. cortexDATA *d;
  1156. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1157. memset(d, 0, sizeof(cortexDATA));
  1158. d->width = size;
  1159. d->height = size;
  1160. d->bShallow = 0;
  1161. d->X = *load_image_augment_paths(random_paths, min, max, size, angle, aspect, hue, saturation, exposure, 0);
  1162. d->Y = load_tags_paths(random_paths, k);
  1163. free_cortexList(random_paths);
  1164. if (m) free_cortexList(paths);
  1165. return d;
  1166. };
  1167. cortexMATRIX concat_matrix(cortexMATRIX m1, cortexMATRIX m2){
  1168. uint32_t i, count = 0;
  1169. cortexMATRIX m;
  1170. m.cols = m1.cols;
  1171. m.rows = m1.rows+m2.rows;
  1172. m.values = (float**)calloc(m1.rows + m2.rows, sizeof(float*));
  1173. for(i = 0; i < m1.rows; ++i){
  1174. m.values[count++] = m1.values[i];
  1175. };
  1176. for(i = 0; i < m2.rows; ++i){
  1177. m.values[count++] = m2.values[i];
  1178. };
  1179. return m;
  1180. };
  1181. cortexDATA *concat_data(cortexDATA *d1, cortexDATA *d2){
  1182. cortexDATA *d;
  1183. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1184. memset(d, 0, sizeof(cortexDATA));
  1185. d->bShallow = 1;
  1186. d->X = concat_matrix(d1->X, d2->X);
  1187. d->Y = concat_matrix(d1->Y, d2->Y);
  1188. return d;
  1189. };
  1190. cortexDATA *concat_datas(cortexDATA *d, uint32_t n){
  1191. uint32_t i;
  1192. cortexDATA *out;
  1193. out = (cortexDATA *)malloc(sizeof(cortexDATA));
  1194. memset(out, 0, sizeof(cortexDATA));
  1195. for(i = 0; i < n; ++i){
  1196. cortexDATA *nw = concat_data(&d[i], out);
  1197. free_data(out);
  1198. out = nw;
  1199. };
  1200. return out;
  1201. };
  1202. cortexDATA *load_categorical_data_csv(char *filename, int target, int k){
  1203. cortexDATA *d;
  1204. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1205. memset(d, 0, sizeof(cortexDATA));
  1206. d->bShallow = 0;
  1207. cortexMATRIX X = csv_to_matrix(filename);
  1208. float *truth_1d = pop_column(&X, target);
  1209. float **truth = one_hot_encode(truth_1d, X.rows, k);
  1210. cortexMATRIX Y;
  1211. Y.rows = X.rows;
  1212. Y.cols = k;
  1213. Y.values = truth;
  1214. d->X = X;
  1215. d->Y = Y;
  1216. free(truth_1d);
  1217. return d;
  1218. };
  1219. cortexDATA *load_cifar10_data(char *filename){
  1220. cortexDATA *d;
  1221. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1222. memset(d, 0, sizeof(cortexDATA));
  1223. d->bShallow = 0;
  1224. uint32_t i, j;
  1225. cortexMATRIX X = make_matrix(10000, 3072);
  1226. cortexMATRIX Y = make_matrix(10000, 10);
  1227. d->X = X;
  1228. d->Y = Y;
  1229. FILE *fp = fopen(filename, "rb");
  1230. if(!fp) file_error(filename);
  1231. for(i = 0; i < 10000; ++i){
  1232. unsigned char bytes[3073];
  1233. fread(bytes, 1, 3073, fp);
  1234. int cls = bytes[0];
  1235. Y.values[i][cls] = 1;
  1236. for(j = 0; j < X.cols; ++j){
  1237. X.values[i][j] = (float)bytes[j+1];
  1238. }
  1239. }
  1240. scale_data_rows(d, 1.f/255.f);
  1241. //normalize_data_rows(d);
  1242. fclose(fp);
  1243. return d;
  1244. };
  1245. void get_random_batch(cortexDATA *d, uint32_t n, float *X, float *y){
  1246. for (uint32_t j = 0; j < n; ++j){
  1247. int index = rand() % d->X.rows;
  1248. memcpy(X + j*d->X.cols, d->X.values[index], d->X.cols*sizeof(float));
  1249. memcpy(y + j*d->Y.cols, d->Y.values[index], d->Y.cols*sizeof(float));
  1250. };
  1251. };
  1252. void get_next_batch(cortexDATA *d, int n, int offset, float *X, float *Y){
  1253. int j;
  1254. for(j = 0; j < n; ++j){
  1255. int index = offset + j;
  1256. memcpy(X + j*d->X.cols, d->X.values[index], d->X.cols*sizeof(float));
  1257. if (Y) memcpy(Y + j*d->Y.cols, d->Y.values[index], d->Y.cols*sizeof(float));
  1258. };
  1259. };
  1260. void smooth_data(cortexDATA *d){
  1261. uint32_t i, j;
  1262. float scale = 1.f / (float)d->Y.cols;
  1263. float eps = .1f;
  1264. for (i = 0; i < d->Y.rows; ++i){
  1265. for (j = 0; j < d->Y.cols; ++j){
  1266. d->Y.values[i][j] = eps * scale + (1 - eps) * d->Y.values[i][j];
  1267. };
  1268. };
  1269. };
  1270. cortexDATA *load_all_cifar10(){
  1271. cortexDATA *d;
  1272. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1273. memset(d, 0, sizeof(cortexDATA));
  1274. d->bShallow = false;
  1275. uint32_t i, j, b;
  1276. cortexMATRIX X = make_matrix(50000, 3072);
  1277. cortexMATRIX Y = make_matrix(50000, 10);
  1278. d->X = X;
  1279. d->Y = Y;
  1280. for(b = 0; b < 5; ++b){
  1281. char buff[256];
  1282. sprintf(buff, "data/cifar/cifar-10-batches-bin/data_batch_%d.bin", b+1);
  1283. FILE *fp = fopen(buff, "rb");
  1284. if(!fp) file_error(buff);
  1285. for(i = 0; i < 10000; ++i){
  1286. unsigned char bytes[3073];
  1287. fread(bytes, 1, 3073, fp);
  1288. int cls = bytes[0];
  1289. Y.values[i+b*10000][cls] = 1;
  1290. for(j = 0; j < X.cols; ++j){
  1291. X.values[i+b*10000][j] = (float)bytes[j+1];
  1292. };
  1293. };
  1294. fclose(fp);
  1295. };
  1296. //normalize_data_rows(d);
  1297. scale_data_rows(d, 1.f/255.f);
  1298. smooth_data(d);
  1299. return d;
  1300. };
  1301. cortexDATA *load_go(char *filename){
  1302. FILE *fp = fopen(filename, "rb");
  1303. cortexMATRIX X = make_matrix(3363059, 361);
  1304. cortexMATRIX y = make_matrix(3363059, 361);
  1305. int row, col;
  1306. if(!fp) file_error(filename);
  1307. char *label;
  1308. int count = 0;
  1309. while((label = fgetl(fp))){
  1310. int i;
  1311. if(count == X.rows){
  1312. X = resize_matrix(X, count*2);
  1313. y = resize_matrix(y, count*2);
  1314. }
  1315. sscanf(label, "%d %d", &row, &col);
  1316. char *board = fgetl(fp);
  1317. int index = row*19 + col;
  1318. y.values[count][index] = 1;
  1319. for(i = 0; i < 19*19; ++i){
  1320. float val = 0;
  1321. if(board[i] == '1') val = 1;
  1322. else if(board[i] == '2') val = -1;
  1323. X.values[count][i] = val;
  1324. }
  1325. ++count;
  1326. free(label);
  1327. free(board);
  1328. }
  1329. X = resize_matrix(X, count);
  1330. y = resize_matrix(y, count);
  1331. cortexDATA *d;
  1332. d = (cortexDATA *)malloc(sizeof(cortexDATA));
  1333. memset(d, 0, sizeof(cortexDATA));
  1334. d->bShallow = 0;
  1335. d->X = X;
  1336. d->Y = y;
  1337. fclose(fp);
  1338. return d;
  1339. };
  1340. void randomize_data(cortexDATA *d){
  1341. int i;
  1342. for (i = d->X.rows - 1; i > 0; --i){
  1343. int index = rand()%i;
  1344. float *swap = d->X.values[index];
  1345. d->X.values[index] = d->X.values[i];
  1346. d->X.values[i] = swap;
  1347. swap = d->Y.values[index];
  1348. d->Y.values[index] = d->Y.values[i];
  1349. d->Y.values[i] = swap;
  1350. };
  1351. };
  1352. void scale_data_rows(cortexDATA *d, float s){
  1353. uint32_t i;
  1354. for (i = 0; i < d->X.rows; ++i){
  1355. scale_array(d->X.values[i], d->X.cols, s);
  1356. };
  1357. };
  1358. void translate_data_rows(cortexDATA *d, float s){
  1359. uint32_t i;
  1360. for (i = 0; i < d->X.rows; ++i){
  1361. translate_array(d->X.values[i], d->X.cols, s);
  1362. };
  1363. };
  1364. cortexDATA *copy_data(cortexDATA *d){
  1365. cortexDATA *c = (cortexDATA *)malloc(sizeof(cortexDATA));
  1366. memset(c, 0, sizeof(cortexDATA));
  1367. c->width = d->width;
  1368. c->height = d->height;
  1369. c->bShallow = false;
  1370. c->boxes = d->boxes;//To Do: Check if the copy works
  1371. c->X = copy_matrix(d->X);
  1372. c->X = copy_matrix(d->X);
  1373. return c;
  1374. };
  1375. void normalize_data_rows(cortexDATA *d){
  1376. uint32_t i;
  1377. for (i = 0; i < d->X.rows; ++i){
  1378. normalize_array(d->X.values[i], d->X.cols);
  1379. };
  1380. };
  1381. cortexDATA *get_data_part(cortexDATA *d, int part, int total){
  1382. cortexDATA *p = (cortexDATA *)malloc(sizeof(cortexDATA));
  1383. memset(p, 0, sizeof(cortexDATA));
  1384. p->bShallow = true;
  1385. p->X.rows = d->X.rows * (part + 1) / total - d->X.rows * part / total;
  1386. p->Y.rows = d->Y.rows * (part + 1) / total - d->Y.rows * part / total;
  1387. p->X.cols = d->X.cols;
  1388. p->Y.cols = d->Y.cols;
  1389. p->X.values = d->X.values + d->X.rows * part / total;
  1390. p->Y.values = d->Y.values + d->Y.rows * part / total;
  1391. return p;
  1392. };
  1393. cortexDATA *get_random_data(cortexDATA *d, int num){
  1394. cortexDATA *r = (cortexDATA *)malloc(sizeof(cortexDATA));
  1395. memset(r, 0, sizeof(cortexDATA));
  1396. r->bShallow = true;
  1397. r->X.rows = num;
  1398. r->Y.rows = num;
  1399. r->X.cols = r->X.cols;
  1400. r->Y.cols = r->Y.cols;
  1401. r->X.values = (float**)calloc(num, sizeof(float *));
  1402. r->Y.values = (float**)calloc(num, sizeof(float *));
  1403. int i;
  1404. for(i = 0; i < num; ++i){
  1405. int index = rand() % d->X.rows;
  1406. r->X.values[i] = d->X.values[index];
  1407. r->Y.values[i] = d->Y.values[index];
  1408. };
  1409. return r;
  1410. };
  1411. cortexDATA **split_data(cortexDATA *d, int part, int total){
  1412. cortexDATA **split = (cortexDATA **)calloc(2, sizeof(cortexDATA*));
  1413. uint32_t i;
  1414. uint32_t start = part*d->X.rows / total;
  1415. uint32_t end = (part + 1)*d->X.rows / total;
  1416. cortexDATA *train = (cortexDATA *)calloc(1, sizeof(cortexDATA));
  1417. cortexDATA *test = (cortexDATA *)calloc(1, sizeof(cortexDATA));
  1418. memset(train, 0, sizeof(cortexDATA));
  1419. memset(test, 0, sizeof(cortexDATA));
  1420. train->bShallow = test->bShallow = 1;
  1421. test->X.rows = test->Y.rows = end - start;
  1422. train->X.rows = train->Y.rows = d->X.rows - (end - start);
  1423. train->X.cols = test->X.cols = d->X.cols;
  1424. train->Y.cols = test->Y.cols = d->Y.cols;
  1425. train->X.values = (float**)calloc(train->X.rows, sizeof(float*));
  1426. test->X.values = (float**)calloc(test->X.rows, sizeof(float*));
  1427. train->Y.values = (float**)calloc(train->Y.rows, sizeof(float*));
  1428. test->Y.values = (float**)calloc(test->Y.rows, sizeof(float*));
  1429. for(i = 0; i < start; ++i){
  1430. train->X.values[i] = d->X.values[i];
  1431. train->Y.values[i] = d->Y.values[i];
  1432. }
  1433. for(i = start; i < end; ++i){
  1434. test->X.values[i - start] = d->X.values[i];
  1435. test->Y.values[i - start] = d->Y.values[i];
  1436. }
  1437. for (i = end; i < d->X.rows; ++i){
  1438. train->X.values[i - (end - start)] = d->X.values[i];
  1439. train->Y.values[i - (end - start)] = d->Y.values[i];
  1440. }
  1441. split[0] = train;
  1442. split[1] = test;
  1443. return split;
  1444. };