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.

1604 lines
46 KiB

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