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.

389 lines
13 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
  1. #include "regionLayer.h"
  2. #include "../base/basicTree.h"
  3. #include "../base/basicBox.h"
  4. #include "../base/basicActivations.h"
  5. #include "../base/basicData.h"
  6. #include "../base/basicCortex.h"
  7. #define LOG( X ) m_pLog->Log( "REGION", this, X )
  8. #define LOG_ERROR( X ) m_pLog->Log( "REGION", this, _LOG_ERROR, X )
  9. #define DOABS 1
  10. regionLayer::regionLayer(bool useGPU) : cortexLayer(REGION,useGPU){
  11. m_pSoftmaxTree = NULL;
  12. };
  13. regionLayer::~regionLayer(){
  14. if (m_pSoftmaxTree != NULL){
  15. //ToDo: Cleanup Tree
  16. };
  17. };
  18. bool regionLayer::create(uint32_t batch, uint32_t w, uint32_t h, uint32_t n, uint32_t classes, uint32_t coords){
  19. m_nN = n;
  20. m_nBatch = batch;
  21. m_nIn_Height = h;
  22. m_nIn_Width = w;
  23. m_nClasses = classes;
  24. m_nCoords = coords;
  25. m_nBias = m_nN * m_nN;
  26. m_pBias = (float *)calloc(m_nBias, sizeof(float));
  27. if (m_pBias == NULL) return false;
  28. m_pBiasUpdate = (float *)calloc(m_nBias, sizeof(float));
  29. if (m_pBiasUpdate == NULL) return false;
  30. m_nInput = h*w*n*(classes + coords + 1);
  31. m_nDelta = m_nOutput = m_nInput;
  32. m_nDeltaVol = m_nOutputVol = m_nOutput * m_nBatch;
  33. m_pOutput = (float *)calloc(m_nOutputVol, sizeof(float));
  34. if (m_pOutput == NULL)return false;
  35. m_nTruth = 30 * (5);
  36. m_pDelta = (float *)calloc(m_nDeltaVol, sizeof(float));
  37. if (m_pDelta == NULL)return false;
  38. for (uint32_t i = 0; i < m_nBias; ++i){
  39. m_pBias[i] = .5;
  40. };
  41. #ifdef GPU
  42. if (m_bUseGPU){
  43. m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol);
  44. m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol);
  45. };
  46. #endif
  47. char tBuff[256];
  48. sprintf(tBuff, "\t%5d\t%2dx%2d\t%2d\t%4dx%4dx%4d -> %4dx%4dx%4d", n, m_nSize, m_nSize, m_nStride, w, h, m_nIn_Channels, m_nOut_Width, m_nOut_Height, m_nOut_Channels);
  49. LOG(tBuff);
  50. srand(0);
  51. return true;
  52. };
  53. bool regionLayer::resize(uint32_t w, uint32_t h){
  54. m_nIn_Width = w;
  55. m_nIn_Height = h;
  56. m_nDelta = m_nOutput = m_nInput = h*w*m_nN*(m_nClasses + m_nCoords + 1);
  57. m_nDeltaVol = m_nOutputVol = m_nOutput *m_nBatch;
  58. m_pOutput = (float *)realloc(m_pOutput, m_nOutputVol*sizeof(float));
  59. if (m_pOutput == NULL) return false;
  60. m_nDelta = m_nOutput;
  61. m_pDelta = (float *)realloc(m_pDelta, m_nDeltaVol*sizeof(float));
  62. if (m_pDelta == NULL) return false;
  63. #ifdef GPU
  64. if (m_bUseGPU){
  65. m_pPU->freeMem(m_pGpuDelta);
  66. m_pPU->freeMem(m_pGpuOutput);
  67. m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol, m_pDelta);// cuda_make_array(l->delta, l->batch*l->outputs);
  68. m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol, m_pOutput);
  69. };
  70. #endif
  71. return true;
  72. };
  73. cortexBOXLABEL get_region_box(float *x, float *biases, uint32_t n, uint32_t index, uint32_t i, uint32_t j, uint32_t w, uint32_t h){
  74. cortexBOXLABEL b;
  75. b.x = (i + logistic_activate(x[index + 0])) / w;
  76. b.y = (j + logistic_activate(x[index + 1])) / h;
  77. b.w = (float)exp(x[index + 2]) * biases[2 * n];
  78. b.h = (float)exp(x[index + 3]) * biases[2 * n + 1];
  79. if (DOABS){
  80. b.w = (float)exp(x[index + 2]) * biases[2 * n] / w;
  81. b.h = (float)exp(x[index + 3]) * biases[2 * n + 1] / h;
  82. };
  83. return b;
  84. }
  85. float delta_region_box(cortexBOXLABEL truth, float *x, float *biases, uint32_t n, uint32_t index, uint32_t i, uint32_t j, uint32_t w, uint32_t h, float *delta, float scale){
  86. cortexBOXLABEL pred = get_region_box(x, biases, n, index, i, j, w, h);
  87. float iou = pred._iou(truth);
  88. float tx = (truth.x*w - i);
  89. float ty = (truth.y*h - j);
  90. float tw = (float)log(truth.w / biases[2 * n]);
  91. float th = (float)log(truth.h / biases[2 * n + 1]);
  92. if (DOABS){
  93. tw = (float)log(truth.w*w / biases[2 * n]);
  94. th = (float)log(truth.h*h / biases[2 * n + 1]);
  95. };
  96. delta[index + 0] = scale * (tx - logistic_activate(x[index + 0])) * logistic_gradient(logistic_activate(x[index + 0]));
  97. delta[index + 1] = scale * (ty - logistic_activate(x[index + 1])) * logistic_gradient(logistic_activate(x[index + 1]));
  98. delta[index + 2] = scale * (tw - x[index + 2]);
  99. delta[index + 3] = scale * (th - x[index + 3]);
  100. return iou;
  101. };
  102. void delta_region_class(float *output, float *delta, uint32_t index, uint32_t cls, uint32_t classes, cortexTREE *hier, float scale, float *avg_cat){
  103. uint32_t i, n;
  104. if (hier){
  105. float pred = 1;
  106. while (cls >= 0){
  107. pred *= output[index + cls];
  108. int g = hier->group[cls];
  109. int offset = hier->group_offset[g];
  110. for (i = 0; i < hier->group_size[g]; ++i){
  111. delta[index + offset + i] = scale * (0 - output[index + offset + i]);
  112. };
  113. delta[index + cls] = scale * (1 - output[index + cls]);
  114. cls = hier->parent[cls];
  115. };
  116. *avg_cat += pred;
  117. } else {
  118. for (n = 0; n < classes; ++n){
  119. delta[index + n] = scale * (((n == cls) ? 1 : 0) - output[index + n]);
  120. if (n == cls) *avg_cat += output[index + n];
  121. };
  122. };
  123. };
  124. float logit(float x){
  125. return (float)log(x / (1. - x));
  126. };
  127. float tisnan(float x){
  128. return (float)(x != x);
  129. };
  130. void softmax_tree(float *input, int batch, int inputs, float temp, cortexTREE *hierarchy, float *output);
  131. void regionLayer::forward(cortexSTATE state){
  132. uint32_t i, j, b, t, n;
  133. uint32_t size = m_nCoords + m_nClasses + 1;
  134. memcpy( m_pOutput, state.input, m_nOutput*sizeof(float) );
  135. #ifndef GPU
  136. flatten(l.output, l.w*l.h, size*l.n, l.batch, 1);
  137. #endif
  138. for (b = 0; b < m_nBatch; ++b){
  139. for (i = 0; i < m_nIn_Height*m_nIn_Width*m_nN; ++i){
  140. int index = size * i + b * m_nInput;
  141. m_pOutput[index + 4] = logistic_activate(m_pOutput[index + 4]);
  142. };
  143. };
  144. #ifndef GPU
  145. if (l.softmax_tree){
  146. for (b = 0; b < l.batch; ++b){
  147. for (i = 0; i < l.h*l.w*l.n; ++i){
  148. int index = size*i + b*l.outputs;
  149. softmax_tree(l.output + index + 5, 1, 0, 1, l.softmax_tree, l.output + index + 5);
  150. }
  151. }
  152. }
  153. else if (l.softmax){
  154. for (b = 0; b < l.batch; ++b){
  155. for (i = 0; i < l.h*l.w*l.n; ++i){
  156. int index = size*i + b*l.outputs;
  157. softmax(l.output + index + 5, l.classes, 1, l.output + index + 5);
  158. }
  159. }
  160. }
  161. #endif
  162. if (!state.train) return;
  163. memset( m_pDelta, 0, m_nDelta * sizeof(float));
  164. float avg_iou = 0;
  165. float recall = 0;
  166. float avg_cat = 0;
  167. float avg_obj = 0;
  168. float avg_anyobj = 0;
  169. uint32_t count = 0;
  170. uint32_t class_count = 0;
  171. m_fCost = 0.f;
  172. for (b = 0; b < m_nBatch; ++b) {
  173. if ( m_pSoftmaxTree != NULL ){
  174. bool onlyclass = false;
  175. for (t = 0; t < 30; ++t){
  176. cortexBOXLABEL truth;// = float_to_box(state.truth + t * 5 + b*l.truths);
  177. truth._float( state.truth + t * 5 + b * m_nTruth );
  178. if (truth.x == 0) break;
  179. int cls = (int)state.truth[ t * 5 + b * m_nTruth + 4];
  180. float maxp = 0;
  181. int maxi = 0;
  182. if (truth.x > 100000 && truth.y > 100000){
  183. for (n = 0; n < m_nN*m_nIn_Width*m_nIn_Height; ++n){
  184. int index = size*n + b*m_nInput + 5;
  185. float scale = m_pOutput[index - 1];
  186. float p = scale*get_hierarchy_probability(m_pOutput + index, m_pSoftmaxTree, cls);
  187. if (p > maxp){
  188. maxp = p;
  189. maxi = n;
  190. };
  191. };
  192. int index = size*maxi + b*m_nInput + 5;
  193. delta_region_class(m_pOutput, m_pDelta, index, cls, m_nClasses, m_pSoftmaxTree, m_fClassScale, &avg_cat);
  194. ++class_count;
  195. onlyclass = true;
  196. break;
  197. };
  198. };
  199. if (onlyclass) continue;
  200. };
  201. for (j = 0; j < m_nIn_Height; ++j) {
  202. for (i = 0; i < m_nIn_Width; ++i) {
  203. for (n = 0; n < m_nN; ++n) {
  204. int index = size*(j*m_nIn_Width*m_nN + i*m_nN + n) + b*m_nOutput;
  205. cortexBOXLABEL pred = get_region_box(m_pOutput, m_pBias, n, index, i, j, m_nIn_Width, m_nIn_Height);
  206. float best_iou = 0;
  207. int best_class = -1;
  208. for (t = 0; t < 30; ++t){
  209. cortexBOXLABEL truth;// = float_to_box(state.truth + t * 5 + b*l.truths);
  210. truth._float(state.truth + t * 5 + b * m_nTruth);
  211. if(truth.x == 0) break;
  212. float iou = pred._iou(truth);
  213. if (iou > best_iou) {
  214. best_class = (int)state.truth[t * 5 + b*m_nTruth + 4];
  215. best_iou = iou;
  216. };
  217. };
  218. avg_anyobj += m_pOutput[index + 4];
  219. m_pDelta[index + 4] = m_fNoObjectScale * ((0 - m_pOutput[index + 4]) * logistic_gradient(m_pOutput[index + 4]));
  220. if( m_sClassFix == -1 ){
  221. m_pDelta[index + 4] = m_fNoObjectScale * ((best_iou - m_pOutput[index + 4]) * logistic_gradient(m_pOutput[index + 4]));
  222. } else {
  223. if (best_iou > m_fThreshold) {
  224. m_pDelta[index + 4] = 0;
  225. if( m_sClassFix > 0 ){
  226. delta_region_class( m_pOutput, m_pDelta, index + 5, best_class, m_nClasses, m_pSoftmaxTree, m_fClassScale*(m_sClassFix == 2 ? m_pOutput[index + 4] : 1), &avg_cat);
  227. ++class_count;
  228. };
  229. };
  230. };
  231. if( state.net->getSeen() < 12800 ){
  232. cortexBOXLABEL truth = { 0 };
  233. truth.x = (i + .5f) / m_nIn_Width;
  234. truth.y = (j + .5f) / m_nIn_Height;
  235. truth.w = m_pBias[2 * n];
  236. truth.h = m_pBias[2 * n + 1];
  237. if (DOABS){
  238. truth.w = m_pBias[2 * n] / m_nIn_Width;
  239. truth.h = m_pBias[2 * n + 1] / m_nIn_Height;
  240. };
  241. delta_region_box(truth, m_pOutput, m_pBias, n, index, i, j, m_nIn_Width, m_nIn_Height, m_pDelta, .01f);
  242. };
  243. };
  244. };
  245. };
  246. for (t = 0; t < 30; ++t){
  247. cortexBOXLABEL truth;// = float_to_box(state.truth + t * 5 + b*l.truths);
  248. truth._float( state.truth + t * 5 + b * m_nTruth );
  249. if (truth.x == 0) break;
  250. float best_iou = 0;
  251. uint32_t best_index = 0;
  252. uint32_t best_n = 0;
  253. i = (int)(truth.x * m_nIn_Width);
  254. j = (int)(truth.y * m_nIn_Height);
  255. //printf("%d %f %d %f\n", i, truth.x*l.w, j, truth.y*l.h);
  256. cortexBOXLABEL truth_shift = truth;
  257. truth_shift.x = 0;
  258. truth_shift.y = 0;
  259. //printf("index %d %d\n",i, j);
  260. for (n = 0; n < m_nN; ++n){
  261. int index = size*(j*m_nIn_Width*m_nN + i*m_nN + n) + b * m_nInput;
  262. cortexBOXLABEL pred = get_region_box(m_pOutput, m_pBias, n, index, i, j, m_nIn_Width, m_nIn_Height);
  263. if (m_bBiasMatch){
  264. pred.w = m_pBias[2 * n];
  265. pred.h = m_pBias[2 * n + 1];
  266. if (DOABS){
  267. pred.w = m_pBias[2 * n] / m_nIn_Width;
  268. pred.h = m_pBias[2 * n + 1] / m_nIn_Height;
  269. };
  270. };
  271. //printf("pred: (%f, %f) %f x %f\n", pred.x, pred.y, pred.w, pred.h);
  272. pred.x = 0;
  273. pred.y = 0;
  274. float iou = pred._iou(truth_shift);
  275. if (iou > best_iou){
  276. best_index = index;
  277. best_iou = iou;
  278. best_n = n;
  279. };
  280. };
  281. //printf("%d %f (%f, %f) %f x %f\n", best_n, best_iou, truth.x, truth.y, truth.w, truth.h);
  282. float iou = delta_region_box(truth, m_pOutput, m_pBias, best_n, best_index, i, j, m_nIn_Width, m_nIn_Height, m_pDelta, m_fCoordScale);
  283. if (iou > .5) recall += 1;
  284. avg_iou += iou;
  285. //l.delta[best_index + 4] = iou - l.output[best_index + 4];
  286. avg_obj += m_pOutput[best_index + 4];
  287. m_pDelta[best_index + 4] = m_fObjectScale * (1 - m_pOutput[best_index + 4]) * logistic_gradient(m_pOutput[best_index + 4]);
  288. if (m_bRescore) {
  289. m_pDelta[best_index + 4] = m_fObjectScale * (iou - m_pOutput[best_index + 4]) * logistic_gradient(m_pOutput[best_index + 4]);
  290. };
  291. int cls = (int)state.truth[t * 5 + b * m_nTruth + 4];
  292. if( m_pMap != NULL ) cls = m_pMap[cls];
  293. delta_region_class(m_pOutput, m_pDelta, best_index + 5, cls, m_nClasses, m_pSoftmaxTree, m_fClassScale, &avg_cat);
  294. ++count;
  295. ++class_count;
  296. };
  297. };
  298. //printf("\n");
  299. #ifndef GPU
  300. flatten(m_pDelta, m_nIn_Width*m_nIn_Height, size*m_nN, m_nBatch, 0);
  301. #endif
  302. m_fCost = (float)pow( mag_array(m_pDelta, m_nDelta), 2 );
  303. printf("Region Avg IOU: %f, Class: %f, Obj: %f, No Obj: %f, Avg Recall: %f, count: %d\n", avg_iou / count, avg_cat / class_count, avg_obj / count, avg_anyobj / (m_nIn_Width*m_nIn_Height*m_nN*m_nBatch), recall / count, count);
  304. };
  305. void regionLayer::backward(cortexSTATE state){
  306. m_pPU->calcAxpy( m_nDelta, 1, m_pDelta, 1, state.delta, 1 );
  307. };
  308. void regionLayer::get_region_boxes(uint32_t w, uint32_t h, float thresh, float **probs, cortexBOXLABEL *boxes, bool only_objectness, uint32_t *map){
  309. uint32_t i, j, n;
  310. float *predictions = m_pOutput;
  311. for (i = 0; i < m_nIn_Width*m_nIn_Height; ++i){
  312. uint32_t row = i / m_nIn_Width;
  313. uint32_t col = i % m_nIn_Width;
  314. for (n = 0; n < m_nN; ++n){
  315. int index = i*m_nN + n;
  316. int p_index = index * (m_nClasses + 5) + 4;
  317. float scale = predictions[p_index];
  318. if (m_sClassFix == -1 && scale < .5) scale = 0;
  319. int box_index = index * (m_nClasses + 5);
  320. boxes[index] = get_region_box(predictions, m_pBias, n, box_index, col, row, m_nIn_Width, m_nIn_Height);
  321. boxes[index].x *= w;
  322. boxes[index].y *= h;
  323. boxes[index].w *= w;
  324. boxes[index].h *= h;
  325. int class_index = index * (m_nClasses + 5) + 5;
  326. if( m_pSoftmaxTree != NULL ){
  327. hierarchy_predictions(predictions + class_index, m_nClasses, m_pSoftmaxTree, 0);
  328. int found = 0;
  329. if (map){
  330. for (j = 0; j < 200; ++j){
  331. float prob = scale*predictions[class_index + map[j]];
  332. probs[index][j] = (prob > thresh) ? prob : 0;
  333. };
  334. } else {
  335. for (j = m_nClasses - 1; j >= 0; --j){
  336. if (!found && predictions[class_index + j] > .5){
  337. found = 1;
  338. } else {
  339. predictions[class_index + j] = 0;
  340. };
  341. float prob = predictions[class_index + j];
  342. probs[index][j] = (scale > thresh) ? prob : 0;
  343. };
  344. };
  345. } else {
  346. for (j = 0; j < m_nClasses; ++j){
  347. float prob = scale*predictions[class_index + j];
  348. probs[index][j] = (prob > thresh) ? prob : 0;
  349. };
  350. };
  351. if (only_objectness){
  352. probs[index][0] = scale;
  353. };
  354. };
  355. };
  356. };
  357. uint32_t regionLayer::getBoxesCount(void){
  358. if (m_nIn_Height == m_nIn_Width) m_nSide = m_nIn_Height;
  359. return (m_nIn_Width*m_nIn_Height*m_nN);
  360. };
  361. uint32_t regionLayer::getClassesCount(void){
  362. return m_nClasses;
  363. };
  364. #undef LOG
  365. #undef LOG_ERROR