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.

100 lines
3.3 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
  1. #include "localLayer.h"
  2. /*
  3. #include "utils.h"
  4. #include "im2col.h"
  5. #include "col2im.h"
  6. #include "blas.h"
  7. #include "gemm.h"
  8. #include <stdio.h>
  9. #include <time.h>
  10. */
  11. localLayer::localLayer(bool useGPU) :cortexLayer(LOCAL, useGPU){
  12. m_nColImage = 0;
  13. m_pColImage = NULL;
  14. m_pGpuColImage = NULL;
  15. };
  16. localLayer::~localLayer(){
  17. if (m_pPU != NULL){
  18. if (m_pGpuColImage != NULL)m_pPU->freeMem(m_pGpuColImage);
  19. };
  20. if (m_pColImage != NULL) free(m_pColImage);
  21. };
  22. bool localLayer::create(uint32_t batch, uint32_t w, uint32_t h, uint32_t c, uint32_t n, uint32_t size, uint32_t stride, uint32_t pad, ACTIVATION activation){
  23. m_nIn_Height = h;
  24. m_nIn_Width = w;
  25. m_nIn_Channels = c;
  26. m_nN = n;
  27. m_nBatch = batch;
  28. m_nStride = stride;
  29. m_nSize = size;
  30. m_nPad = pad;
  31. uint32_t out_h = (m_nPad == 0) ? ((m_nIn_Height - m_nSize) / (m_nStride + 1)) : ((m_nIn_Height - 1) / (m_nStride + 1));
  32. uint32_t out_w = (m_nPad == 0) ? ((m_nIn_Width - m_nSize) / (m_nStride + 1)) : ((m_nIn_Width - 1) / (m_nStride + 1));
  33. int locations = out_h*out_w;
  34. m_nOut_Height = out_h;
  35. m_nOut_Width = out_w;
  36. m_nOut_Channels = n;
  37. m_nOutput = m_nOut_Height * m_nOut_Width * m_nOut_Channels;
  38. m_nInput = m_nIn_Width * m_nIn_Height * m_nIn_Channels;
  39. m_nWeight = c*n*size*size*locations;
  40. m_pWeight = (float*)calloc(m_nWeight, sizeof(float));
  41. m_pWeightUpdate = (float*)calloc(m_nWeight, sizeof(float));
  42. m_nBias = m_nOutput;
  43. m_pBias = (float*)calloc(m_nBias, sizeof(float));
  44. m_pBiasUpdate = (float*)calloc(m_nBias, sizeof(float));
  45. // float scale = 1./sqrt(size*size*c);
  46. float scale = (float)sqrt(2.f / (size*size*c));
  47. for ( uint32_t i = 0; i < c*n*size*size; ++i) m_pWeight[i] = scale*rand_uniform(-1, 1);
  48. m_nColImage = out_h*out_w*size*size*c;
  49. m_pColImage = (float*)calloc(m_nColImage, sizeof(float));
  50. m_pOutput = (float*)calloc(m_nOutput, sizeof(float));
  51. m_nDelta = m_nOutput;
  52. m_pDelta = (float*)calloc(m_nDelta, sizeof(float));
  53. #ifdef GPU
  54. if (m_bUseGPU){
  55. m_pGpuWeight = m_pPU->makeFloatArray(m_nWeight);// , m_pWeight);
  56. m_pGpuWeightUpdate = m_pPU->makeFloatArray(m_nWeight);//, m_pWeightUpdate);
  57. m_pGpuBias = m_pPU->makeFloatArray(m_nOutput);//, m_pBias);
  58. m_pGpuBiasUpdate = m_pPU->makeFloatArray(m_nOutput);//, m_pBiasUpdate);
  59. m_pGpuColImage = m_pPU->makeFloatArray(m_nColImage);//, m_pColImage);
  60. m_pGpuDelta = m_pPU->makeFloatArray(m_nDelta);//, m_pDelta);
  61. m_pGpuOutput = m_pPU->makeFloatArray(m_nOutput);//,m_pOutput);
  62. };
  63. #endif
  64. m_eActivation = activation;
  65. fprintf(stderr, "Local Layer: %d x %d x %d image, %d filters -> %d x %d x %d image\n", h, w, c, n, out_h, out_w, n);
  66. return true;
  67. };
  68. void localLayer::forward(cortexSTATE state){
  69. m_pPU->forward_layer(this, state);
  70. };
  71. void localLayer::backward(cortexSTATE state){
  72. m_pPU->backward_layer(this, state);
  73. };
  74. void localLayer::loadWeights(FILE *fp, bool trans){
  75. //int locations = m_nOut_Width*m_nOut_Height;
  76. //int size = m_nSize*m_nSize*m_nIn_Channels*m_nN*locations;
  77. //fread(m_pBias, sizeof(float), m_nOutput, fp);
  78. //fread(m_pWeight, sizeof(float), size, fp);
  79. fread(m_pBias, sizeof(float), m_nBias, fp);
  80. fread(m_pWeight, sizeof(float), m_nWeight, fp);
  81. #ifdef GPU
  82. m_pPU->push_layer(this);
  83. #endif
  84. };
  85. void localLayer::update(uint32_t batch, float learning_rate, float momentum, float decay){
  86. m_pPU->update_layer(this, batch, learning_rate, momentum, decay);
  87. };