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.

41 lines
1.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cpu.h"
  2. #include "../layers/maxpoolLayer.h"
  3. void cpuHOST::forward_layer(maxpoolLayer *layer, cortexSTATE state){
  4. int w_offset = -1 * layer->m_nPad;
  5. int h_offset = -1 * layer->m_nPad;
  6. uint32_t h = layer->m_nOut_Height;
  7. uint32_t w = layer->m_nOut_Width;
  8. uint32_t c = layer->m_nIn_Channels;
  9. for (uint32_t b = 0; b < layer->m_nBatch; ++b){
  10. for (uint32_t k = 0; k < c; ++k){
  11. for (uint32_t i = 0; i < h; ++i){
  12. for (uint32_t j = 0; j < w; ++j){
  13. int out_index = j + w*(i + h*(k + c*b));
  14. float max_flt = -FLT_MAX;
  15. int max_i = -1;
  16. for (uint32_t n = 0; n < layer->m_nSize; ++n){
  17. for (uint32_t m = 0; m < layer->m_nSize; ++m){
  18. uint32_t cur_h = h_offset + i*layer->m_nStride + n;
  19. uint32_t cur_w = w_offset + j*layer->m_nStride + m;
  20. uint32_t index = cur_w + layer->m_nIn_Width*(cur_h + layer->m_nIn_Height*(k + b*layer->m_nIn_Channels));
  21. bool valid = (cur_h >= 0 && cur_h < layer->m_nIn_Height && cur_w >= 0 && cur_w < layer->m_nIn_Width);
  22. float val = (valid) ? state.input[index] : -FLT_MAX;
  23. max_i = (val > max_flt) ? index : max_i;
  24. max_flt = (val > max_flt) ? val : max_flt;
  25. };
  26. };
  27. layer->m_pOutput[out_index] = max_flt;
  28. layer->m_pIndexes[out_index] = max_i;
  29. };
  30. };
  31. };
  32. };
  33. };
  34. void cpuHOST::backward_layer(maxpoolLayer *layer, cortexSTATE state){
  35. for (uint32_t i = 0; i < layer->m_nOutputVol; ++i){
  36. uint32_t index = layer->m_pIndexes[i];
  37. state.delta[index] += layer->m_pDelta[i];
  38. };
  39. };