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.

179 lines
5.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __CORTEX_DATA_HEADER__
  2. #define __CORTEX_DATA_HEADER__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "basicUtils.h"
  7. #include "basicImage.h"
  8. #include "basicBox.h"
  9. #define SECRET_NUM -1234
  10. //#include "dn_cuda.h"
  11. #ifndef MUTEX_HANDLE_TYPE
  12. #ifdef __linux__
  13. // Linux impl.
  14. #include <pthread.h>
  15. #define MUTEX_HANDLE_TYPE pthread_mutex_t
  16. #define MUTEX_INIT( X ) pthread_mutex_init( &X, NULL )
  17. #define MUTEX_LOCK( X ) pthread_mutex_lock( &X )
  18. #define MUTEX_TRY_LOCK( X ) pthread_mutex_trylock( &X )
  19. #define MUTEX_UNLOCK( X ) pthread_mutex_unlock( &X )
  20. #define MUTEX_DESTROY( X ) pthread_mutex_destroy( &X )
  21. #else
  22. // Windows impl.
  23. #include "Windows.h"
  24. #define MUTEX_HANDLE_TYPE HANDLE
  25. #define MUTEX_INIT( X ) X = CreateMutex( NULL, FALSE, NULL )
  26. #define MUTEX_LOCK( X ) WaitForSingleObject( X, INFINITE )
  27. #define MUTEX_TRY_LOCK( X ) (WaitForSingleObject( X, 0 )==WAIT_OBJECT_0)
  28. #define MUTEX_UNLOCK( X ) ReleaseMutex( X )
  29. #define MUTEX_DESTROY( X ) CloseHandle( X )
  30. #endif
  31. #endif
  32. #include "basicMatrix.h"
  33. #include "basicTree.h"
  34. #include "basicImage.h"
  35. class basicCortex;
  36. typedef enum E_DATATYPE {
  37. CLASSIFICATION_DATA, DETECTION_DATA, CAPTCHA_DATA, REGION_DATA, IMAGE_DATA, COMPARE_DATA, WRITING_DATA, SWAG_DATA, TAG_DATA, OLD_CLASSIFICATION_DATA, STUDY_DATA, DET_DATA, SUPER_DATA, LETTERBOX_DATA, REGRESSION_DATA, SEGMENTATION_DATA, INSTANCE_DATA
  38. } cortexDATATYPE;
  39. typedef struct S_network_state {
  40. float *truth;
  41. float *input;
  42. float *delta;
  43. float *workspace;
  44. bool train;
  45. int32_t index;
  46. //struct S_network net;
  47. basicCortex *net;
  48. } cortexSTATE;
  49. typedef struct S_DATA{
  50. uint32_t width;
  51. uint32_t height;
  52. cortexMATRIX X;
  53. cortexMATRIX Y;
  54. bool bShallow;
  55. int *indexes;
  56. arrayBOX boxes;
  57. } cortexDATA;
  58. typedef struct S_THREAD_ARGS{
  59. uint32_t threads;
  60. cortexList *paths;
  61. void *me;
  62. char *path;
  63. uint32_t n;
  64. uint32_t m;
  65. cortexList *labels;
  66. uint32_t h;
  67. uint32_t w;
  68. uint32_t out_w;
  69. uint32_t out_h;
  70. uint32_t nh;
  71. uint32_t nw;
  72. uint32_t num_boxes;
  73. uint32_t min, max, size;
  74. uint32_t classes;
  75. uint32_t background;
  76. bool scale;
  77. uint32_t center;
  78. uint32_t coords;
  79. float jitter;
  80. float angle;
  81. float aspect;
  82. float saturation;
  83. float exposure;
  84. float hue;
  85. cortexDATA *d;
  86. cortexImage *im;
  87. cortexImage *resized;
  88. cortexDATATYPE type;
  89. cortexTREE *hierarchy;
  90. } cortexTHREAD_ARGS;
  91. typedef enum E_ACTIVATION{
  92. LOGISTIC, RELU, RELIE, LINEAR, RAMP, TANH, PLSE, LEAKY, ELU, LOGGY, STAIR, HARDTAN, LHTAN
  93. } ACTIVATION;
  94. typedef enum E_LAYER_TYPE{
  95. CONVOLUTIONAL,
  96. DECONVOLUTIONAL,
  97. CONNECTED,
  98. MAXPOOL,
  99. SOFTMAX,
  100. DETECTION,
  101. DROPOUT,
  102. CROP,
  103. ROUTE,
  104. COST,
  105. NORMALIZATION,
  106. AVGPOOL,
  107. LOCAL,
  108. SHORTCUT,
  109. ACTIVE,
  110. RNN,
  111. GRU,
  112. LSTM,
  113. CRNN,
  114. BATCHNORM,
  115. NETWORK,
  116. XNOR,
  117. REGION,
  118. REORG,
  119. BLANK
  120. } LAYER_TYPE;
  121. typedef enum E_COST_TYPE{
  122. SSE, MASKED, L1, SEG, SMOOTH
  123. } COST_TYPE;
  124. static __inline float distance_from_edge(int x, int max){
  125. int dx = (max / 2) - x;
  126. if (dx < 0) dx = -dx;
  127. dx = (max / 2) + 1 - dx;
  128. dx *= 2;
  129. float dist = (float)dx / max;
  130. if (dist > 1) dist = 1;
  131. return dist;
  132. };
  133. void loadBlocking(cortexTHREAD_ARGS *args);
  134. cortexList * get_paths( char *filename );
  135. cortexDATA * load_data_captcha( cortexList *paths, uint32_t n, uint32_t m, uint32_t k, uint32_t w, uint32_t h );
  136. cortexDATA * load_data_captcha_encode( cortexList *paths, uint32_t n, uint32_t m, uint32_t w, uint32_t h );
  137. cortexDATA * load_data_detection( uint32_t n, cortexList *paths, uint32_t m, uint32_t w, uint32_t h, uint32_t boxes, uint32_t classes, float jitter, float hue, float saturation, float exposure );
  138. cortexDATA * load_data_old( cortexList *paths, int m, cortexList *labels, int w, int h );
  139. cortexDATA * load_data_tag( cortexList *paths, uint32_t m, uint32_t k, uint32_t min, uint32_t max, uint32_t size, float angle, float aspect, float hue, float saturation, float exposure );
  140. cortexDATA * load_data_super( cortexList *paths, uint32_t m, uint32_t w, uint32_t h, uint32_t scale );
  141. cortexDATA * load_data_augment( cortexList *paths, uint32_t m, 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 );
  142. cortexDATA * load_data_regression( cortexList *paths, uint32_t m, uint32_t min, uint32_t max, uint32_t size, float angle, float aspect, float hue, float saturation, float exposure );
  143. cortexDATA * load_go( char *filename );
  144. cortexDATA * load_data_writing( cortexList *paths, uint32_t m, uint32_t w, uint32_t h, uint32_t out_w, uint32_t out_h );
  145. cortexDATA * get_data_part( cortexDATA *d, uint32_t part, uint32_t total );
  146. cortexDATA * get_random_data( cortexDATA *d, uint32_t num );
  147. cortexDATA * load_categorical_data_csv( char *filename, uint32_t target, uint32_t k );
  148. cortexDATA * split_data( cortexDATA *d, uint32_t part, uint32_t total );
  149. cortexDATA * concat_datas( cortexDATA *d, uint32_t n );
  150. cortexMATRIX * load_image_augment_paths( cortexList *paths, uint32_t min, uint32_t max, uint32_t size, float angle, float aspect, float hue, float saturation, float exposure, uint32_t center);
  151. void get_random_batch( cortexDATA *d, uint32_t n, float *X, float *Y);
  152. void normalize_data_rows( cortexDATA *d);
  153. void scale_data_rows( cortexDATA *d, float s);
  154. void translate_data_rows( cortexDATA *d, float s);
  155. void randomize_data( cortexDATA *d);
  156. void fill_truth( char *path, cortexList *labels, uint32_t k, float *truth );
  157. void print_letters( float *pred, uint32_t n);
  158. COST_TYPE get_cost_type(char *s);
  159. char * get_cost_string(COST_TYPE a);
  160. #endif