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.

171 lines
4.2 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "avgpoolLayer.h"
  2. #define LOG( X ) m_pLog->Log( "AVGpoolL", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "AVGpoolL", this, _LOG_ERROR, X )
  4. avgpoolLayer::avgpoolLayer(bool useGPU) :cortexLayer(AVGPOOL, useGPU){
  5. };
  6. avgpoolLayer::~avgpoolLayer(){
  7. };
  8. bool avgpoolLayer::create(uint32_t batch, uint32_t w, uint32_t h, uint32_t c){
  9. m_nBatch = batch;
  10. m_nIn_Width = w;
  11. m_nIn_Height = h;
  12. m_nIn_Channels = c;
  13. m_nOut_Width = 1;
  14. m_nOut_Height = 1;
  15. m_nOut_Channels = c;
  16. m_nInput = m_nIn_Height * m_nIn_Width * m_nIn_Channels;
  17. m_nOutput = m_nOut_Channels;
  18. m_nOutputVol = m_nOutput*m_nBatch;
  19. m_pOutput = (float*)calloc(m_nOutputVol, sizeof(float));
  20. if (m_pOutput == NULL) return false;
  21. m_nDelta = m_nOutput;
  22. m_nDeltaVol = m_nDelta*m_nBatch;
  23. m_pDelta = (float*)calloc(m_nDeltaVol, sizeof(float));
  24. if (m_pDelta == NULL){
  25. free(m_pOutput);
  26. m_pOutput = NULL;
  27. return false;
  28. };
  29. #ifdef GPU
  30. if (m_bUseGPU){
  31. m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol);
  32. if (m_pGpuOutput = NULL){
  33. free(m_pOutput); m_pOutput = NULL;
  34. free(m_pDelta); m_pDelta = NULL;
  35. return false;
  36. };
  37. m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol);
  38. if (m_pGpuDelta = NULL){
  39. free(m_pOutput); m_pOutput = NULL;
  40. free(m_pDelta); m_pDelta = NULL;
  41. m_pPU->freeMem(m_pGpuOutput); m_pGpuOutput = NULL;
  42. return false;
  43. };
  44. };
  45. #endif
  46. char tBuff[256];
  47. sprintf( tBuff, "avg %4dx%4dx%4d -> %4d\n", w, h, c, c);
  48. LOG( tBuff );
  49. return true;
  50. };
  51. void avgpoolLayer::forward(cortexSTATE state){
  52. uint32_t Area = m_nIn_Width * m_nIn_Height;
  53. #ifdef GPU
  54. if (m_bUseGPU){
  55. for (uint32_t b = 0; b < m_nBatch; ++b){
  56. for (uint32_t k = 0; k < m_nIn_Channels; ++k){
  57. uint32_t out_index = k + b*m_nIn_Channels;
  58. m_pGpuOutput[out_index] = 0;
  59. for (uint32_t i = 0; i < Area; ++i){
  60. uint32_t in_index = i + Area*out_index;
  61. m_pGpuOutput[out_index] += state.input[in_index];
  62. };
  63. m_pGpuOutput[out_index] /= Area;
  64. };
  65. };
  66. } else {
  67. #endif
  68. for (uint32_t b = 0; b < m_nBatch; ++b){
  69. for (uint32_t k = 0; k < m_nIn_Channels; ++k){
  70. uint32_t out_index = k + b*m_nIn_Channels;
  71. m_pOutput[out_index] = 0;
  72. for (uint32_t i = 0; i < Area; ++i){
  73. uint32_t in_index = i + Area*out_index;
  74. m_pOutput[out_index] += state.input[in_index];
  75. };
  76. m_pOutput[out_index] /= Area;
  77. };
  78. };
  79. #ifdef GPU
  80. };
  81. #endif
  82. };
  83. void avgpoolLayer::backward(cortexSTATE state){
  84. uint32_t Area = m_nIn_Width * m_nIn_Height;
  85. #ifdef GPU
  86. if (m_bUseGPU){
  87. for (uint32_t b = 0; b < m_nBatch; ++b){
  88. for (uint32_t k = 0; k < m_nIn_Channels; ++k){
  89. uint32_t out_index = k + b*m_nIn_Channels;
  90. for (uint32_t i = 0; i < Area; ++i){
  91. uint32_t in_index = i + Area*out_index;
  92. state.delta[in_index] += m_pGpuDelta[out_index] / Area;
  93. };
  94. };
  95. };
  96. } else {
  97. #endif
  98. for (uint32_t b = 0; b < m_nBatch; ++b){
  99. for (uint32_t k = 0; k < m_nIn_Channels; ++k){
  100. uint32_t out_index = k + b*m_nIn_Channels;
  101. for (uint32_t i = 0; i < Area; ++i){
  102. uint32_t in_index = i + Area*out_index;
  103. state.delta[in_index] += m_pDelta[out_index] / Area;
  104. };
  105. };
  106. };
  107. #ifdef GPU
  108. };
  109. #endif
  110. };
  111. // Pushes HOST Memory to GPU memory
  112. void avgpoolLayer::push(void){
  113. #ifdef GPU
  114. if (m_bUseGPU){
  115. if (m_pOutput != NULL && m_pDelta != NULL){
  116. if (m_pGpuOutput != NULL){
  117. m_pPU->pushFloatArray(m_pGpuOutput, m_pOutput, m_nOutputVol);
  118. } else {
  119. m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol, m_pOutput);
  120. };
  121. if (m_pGpuDelta != NULL){
  122. m_pPU->pushFloatArray(m_pGpuDelta, m_pDelta, m_nDeltaVol);
  123. } else{
  124. m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol, m_pDelta);
  125. };
  126. };
  127. };
  128. #endif
  129. };
  130. // Pulls GPU memory to a HOST memory
  131. void avgpoolLayer::pull(void){
  132. #ifdef GPU
  133. if (m_bUseGPU){
  134. if (m_pOutput != NULL && m_pDelta != NULL){
  135. if (m_pGpuOutput != NULL){
  136. m_pPU->pullFloatArray(m_pGpuOutput, m_pOutput, m_nOutputVol);
  137. };
  138. if (m_pGpuDelta != NULL){
  139. m_pPU->pullFloatArray(m_pGpuDelta, m_pDelta, m_nDeltaVol);
  140. };
  141. };
  142. };
  143. #endif
  144. };
  145. bool avgpoolLayer::resize(uint32_t w, uint32_t h){
  146. m_nIn_Width = w;
  147. m_nIn_Height = h;
  148. m_nInput = h*w*m_nIn_Channels;
  149. return true;
  150. };
  151. cortexImage *avgpoolLayer::getImage(void){
  152. return NULL;
  153. };
  154. #undef LOG
  155. #undef LOG_ERROR