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.

1457 lines
43 KiB

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