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.

280 lines
8.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
  1. #include "detectionLayer.h"
  2. #include "../base/basicCortex.h"
  3. #define LOG( X ) m_pLog->Log( "DETECTL", this, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "DETECTL", this, _LOG_ERROR, X )
  5. detectionLayer::detectionLayer(bool useGPU) : cortexLayer(DETECTION, useGPU){
  6. };
  7. detectionLayer::~detectionLayer(){
  8. };
  9. bool detectionLayer::create(uint32_t batch, uint32_t inputs, uint32_t n, uint32_t side, uint32_t classes, uint32_t coords, uint32_t rescore){
  10. //l.n = n;
  11. m_nIn_Channels = n;
  12. m_nBatch = batch;
  13. m_nInput = inputs;
  14. m_nClasses = classes;
  15. m_nCoords = coords;
  16. m_nRescore = rescore;
  17. m_nSide = side;
  18. m_nIn_Width = side;
  19. m_nIn_Height = side;
  20. assert(m_nSide*m_nSide*((1 + m_nCoords)*m_nIn_Channels + m_nClasses) == inputs);
  21. m_nOutput = m_nInput;
  22. m_nTruth = m_nSide*m_nSide*(1 + m_nCoords + m_nClasses);
  23. m_pOutput = (float*)calloc(batch*m_nOutput, sizeof(float));
  24. m_pDelta = (float*)calloc(batch*m_nOutput, sizeof(float));
  25. m_pGpuOutput = NULL;// cuda_make_array(l.output, batch*l.outputs);
  26. m_pGpuDelta = NULL;// cuda_make_array(l.delta, batch*l.outputs);
  27. char tBuff[256];
  28. sprintf(tBuff, "Detection Layer");
  29. LOG(tBuff);
  30. srand(0);
  31. return true;
  32. };
  33. /*
  34. void activationLayer::forward(cortexSTATE state){
  35. #ifdef GPU
  36. if (m_bUseGPU){
  37. }
  38. else {
  39. #endif
  40. #ifdef GPU
  41. };
  42. #endif
  43. };
  44. void activationLayer::backward(cortexSTATE state){
  45. #ifdef GPU
  46. if (m_bUseGPU){
  47. }
  48. else {
  49. #endif
  50. #ifdef GPU
  51. };
  52. #endif
  53. };
  54. */
  55. void detectionLayer::forward(cortexSTATE state){
  56. uint32_t locations = m_nSide*m_nSide;
  57. uint32_t i, j, b;
  58. memcpy(m_pOutput, state.input, m_nOutput*m_nBatch*sizeof(float));
  59. //if(l.reorg) reorg(l.output, l.w*l.h, size*l.n, l.batch, 1);
  60. if (m_bSoftMax){
  61. for (b = 0; b < m_nBatch; ++b){
  62. int index = b*m_nInput;
  63. for (i = 0; i < locations; ++i) {
  64. int offset = i*m_nClasses;
  65. m_pPU->makeSoftMax(m_pOutput + index + offset, m_nClasses, 1, m_pOutput + index + offset);
  66. };
  67. };
  68. };
  69. if (state.train){
  70. float avg_iou = 0;
  71. float avg_cat = 0;
  72. float avg_allcat = 0;
  73. float avg_obj = 0;
  74. float avg_anyobj = 0;
  75. uint32_t count = 0;
  76. m_fCost = 0.f;
  77. uint32_t size = m_nInput * m_nBatch;
  78. memset(m_pDelta, 0, size * sizeof(float));
  79. for (b = 0; b < m_nBatch; ++b){
  80. uint32_t index = b*m_nInput;
  81. for (i = 0; i < locations; ++i) {
  82. uint32_t truth_index = (b*locations + i)*(1 + m_nCoords + m_nClasses);
  83. uint32_t is_obj = (int)state.truth[truth_index];
  84. for (j = 0; j < m_nIn_Channels; ++j) {
  85. uint32_t p_index = index + locations*m_nClasses + i*m_nIn_Channels + j;
  86. m_pDelta[p_index] = m_fNoObjectScale*(0 - m_pOutput[p_index]);
  87. m_fCost += (float)(m_fNoObjectScale*pow(m_pOutput[p_index], 2));
  88. avg_anyobj += m_pOutput[p_index];
  89. };
  90. int best_index = -1;
  91. float best_iou = 0.f;
  92. float best_rmse = 20.f;
  93. if (!is_obj){
  94. continue;
  95. };
  96. uint32_t class_index = index + i* m_nClasses;
  97. for (j = 0; j < m_nClasses; ++j) {
  98. m_pDelta[class_index + j] = m_fClassScale * (state.truth[truth_index + 1 + j] - m_pOutput[class_index + j]);
  99. m_fCost += (float)(m_fClassScale * pow(state.truth[truth_index + 1 + j] - m_pOutput[class_index + j], 2));
  100. if (state.truth[truth_index + 1 + j]) avg_cat += m_pOutput[class_index + j];
  101. avg_allcat += m_pOutput[class_index + j];
  102. }
  103. cortexBOXLABEL truth;
  104. truth._float(state.truth + truth_index + 1 + m_nClasses);
  105. truth.x /= m_nSide;
  106. truth.y /= m_nSide;
  107. for (j = 0; j < m_nIn_Channels; ++j){
  108. int box_index = index + locations*(m_nClasses + m_nIn_Channels) + (i*m_nIn_Channels + j) * m_nCoords;
  109. cortexBOXLABEL out;
  110. out._float(m_pOutput + box_index);
  111. out.x /= m_nSide;
  112. out.y /= m_nSide;
  113. if ( m_bSQRT ){
  114. out.w = out.w*out.w;
  115. out.h = out.h*out.h;
  116. };
  117. float iou = out._iou(truth);
  118. //iou = 0;
  119. float rmse = out._rmse(truth);
  120. if (best_iou > 0 || iou > 0){
  121. if (iou > best_iou){
  122. best_iou = iou;
  123. best_index = j;
  124. };
  125. } else{
  126. if (rmse < best_rmse){
  127. best_rmse = rmse;
  128. best_index = j;
  129. };
  130. };
  131. };
  132. if(m_bForced){
  133. if (truth.w*truth.h < .1){
  134. best_index = 1;
  135. } else{
  136. best_index = 0;
  137. };
  138. };
  139. if ( m_bRandom && ( state.net->getSeen() < 64000 ) ){
  140. best_index = rand() % m_nIn_Channels;
  141. };
  142. int box_index = index + locations*(m_nClasses + m_nIn_Channels) + (i*m_nIn_Channels + best_index) * m_nCoords;
  143. int tbox_index = truth_index + 1 + m_nClasses;
  144. cortexBOXLABEL out;
  145. out._float( m_pOutput + box_index );
  146. out.x /= m_nSide;
  147. out.y /= m_nSide;
  148. if ( m_bSQRT ) {
  149. out.w = out.w*out.w;
  150. out.h = out.h*out.h;
  151. };
  152. float iou = out._iou(truth);
  153. //printf("%d,", best_index);
  154. int p_index = index + locations*m_nClasses + i*m_nIn_Channels + best_index;
  155. m_fCost -= (float)(m_fNoObjectScale * pow(m_pOutput[p_index], 2));
  156. m_fCost += (float)(m_fObjectScale * pow(1 - m_pOutput[p_index], 2));
  157. avg_obj += m_pOutput[p_index];
  158. m_pDelta[p_index] = m_fObjectScale * (1.f - m_pOutput[p_index]);
  159. if (m_bRescore){
  160. m_pDelta[p_index] = m_fObjectScale * (iou - m_pOutput[p_index]);
  161. }
  162. m_pDelta[box_index + 0] = m_fCoordScale*(state.truth[tbox_index + 0] - m_pOutput[box_index + 0]);
  163. m_pDelta[box_index + 1] = m_fCoordScale*(state.truth[tbox_index + 1] - m_pOutput[box_index + 1]);
  164. m_pDelta[box_index + 2] = m_fCoordScale*(state.truth[tbox_index + 2] - m_pOutput[box_index + 2]);
  165. m_pDelta[box_index + 3] = m_fCoordScale*(state.truth[tbox_index + 3] - m_pOutput[box_index + 3]);
  166. if ( m_bSQRT ){
  167. m_pDelta[box_index + 2] = (float)(m_fCoordScale*(sqrt(state.truth[tbox_index + 2]) - m_pOutput[box_index + 2]));
  168. m_pDelta[box_index + 3] = (float)(m_fCoordScale*(sqrt(state.truth[tbox_index + 3]) - m_pOutput[box_index + 3]));
  169. };
  170. m_fCost += (float)pow(1 - iou, 2);
  171. avg_iou += iou;
  172. ++count;
  173. };
  174. };
  175. if (0){
  176. float *costs = (float*)calloc(m_nBatch*locations*m_nIn_Channels, sizeof(float));
  177. for (b = 0; b < m_nBatch; ++b) {
  178. int index = b*m_nInput;
  179. for (i = 0; i < locations; ++i) {
  180. for (j = 0; j < m_nIn_Channels; ++j) {
  181. int p_index = index + locations*m_nClasses + i*m_nIn_Channels + j;
  182. costs[b*locations*m_nIn_Channels + i*m_nIn_Channels + j] = m_pDelta[p_index] * m_pDelta[p_index];
  183. };
  184. };
  185. };
  186. int indexes[100];
  187. top_k(costs, m_nBatch*locations*m_nIn_Channels, 100, indexes);
  188. float cutoff = costs[indexes[99]];
  189. for (b = 0; b < m_nBatch; ++b) {
  190. int index = b*m_nInput;
  191. for (i = 0; i < locations; ++i) {
  192. for (j = 0; j < m_nIn_Channels; ++j) {
  193. int p_index = index + locations*m_nClasses + i*m_nIn_Channels + j;
  194. if (m_pDelta[p_index] * m_pDelta[p_index] < cutoff) m_pDelta[p_index] = 0;
  195. };
  196. };
  197. };
  198. free(costs);
  199. };
  200. //m_fCost = (float)pow( m_pPU->mag_array(m_pDelta, m_nOutput * m_nBatch), 2);
  201. m_fCost = m_pPU->mag2_array(m_pDelta, m_nOutput * m_nBatch);
  202. char tBuff[256];
  203. sprintf(tBuff, "Detection Avg IOU: %f, Pos Cat: %f, All Cat: %f, Pos Obj: %f, Any Obj: %f, count: %d", avg_iou / count, avg_cat / count, avg_allcat / (count*m_nClasses), avg_obj / count, avg_anyobj / (m_nBatch*locations*m_nIn_Channels), count);
  204. LOG(tBuff);
  205. //if(l.reorg) reorg(l.delta, l.w*l.h, size*l.n, l.batch, 0);
  206. };
  207. };
  208. void detectionLayer::backward(cortexSTATE state){
  209. m_pPU->calcAxpy(m_nBatch*m_nInput, 1, m_pDelta, 1, state.delta, 1);
  210. };
  211. void detectionLayer::getDetectionBoxes(uint32_t w, uint32_t h, float thresh, float **probs, cortexBOXLABEL *boxes, bool only_objectness){
  212. uint32_t i, j, n;
  213. float *predictions = m_pOutput;
  214. //int per_cell = 5*num+classes;
  215. for (i = 0; i < m_nSide*m_nSide; ++i){
  216. int row = i / m_nSide;
  217. int col = i % m_nSide;
  218. for (n = 0; n < m_nIn_Channels; ++n){
  219. int index = i*m_nIn_Channels + n;
  220. int p_index = m_nSide*m_nSide*m_nClasses + i*m_nIn_Channels + n;
  221. float scale = predictions[p_index];
  222. int box_index = m_nSide*m_nSide*(m_nClasses + m_nIn_Channels) + (i*m_nIn_Channels + n) * 4;
  223. boxes[index].x = (predictions[box_index + 0] + col) / m_nSide * w;
  224. boxes[index].y = (predictions[box_index + 1] + row) / m_nSide * h;
  225. boxes[index].w = (float)pow(predictions[box_index + 2], (m_bSQRT? 2 : 1)) * w;
  226. boxes[index].h = (float)pow(predictions[box_index + 3], (m_bSQRT? 2 : 1)) * h;
  227. for (j = 0; j < m_nClasses; ++j){
  228. int class_index = i*m_nClasses;
  229. float prob = scale*predictions[class_index + j];
  230. probs[index][j] = (prob > thresh) ? prob : 0;
  231. };
  232. if (only_objectness){
  233. probs[index][0] = scale;
  234. };
  235. };
  236. };
  237. };
  238. uint32_t detectionLayer::getBoxesCount(void){
  239. if (m_nIn_Height == m_nIn_Width) m_nSide = m_nIn_Height;
  240. return (m_nIn_Width*m_nIn_Height*m_nN);
  241. };
  242. uint32_t detectionLayer::getClassesCount(void){
  243. return m_nClasses;
  244. };
  245. #undef LOG
  246. #undef LOG_ERROR