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.

91 lines
2.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
  1. #include "crnnLayer.h"
  2. #include "convolutionalLayer.h"
  3. #include "../cuda/cuda.h"
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. crnnLayer::crnnLayer(bool useGPU) : cortexLayer(CRNN, useGPU){
  9. m_pState = NULL;
  10. m_nState = 0;
  11. m_pInputLayer = NULL;
  12. m_pSelfLayer = NULL;
  13. m_pOutputLayer = NULL;
  14. };
  15. crnnLayer::~crnnLayer(){
  16. if (m_pInputLayer != NULL)delete(m_pInputLayer);
  17. if (m_pSelfLayer != NULL)delete(m_pSelfLayer);
  18. if (m_pOutputLayer != NULL)delete(m_pOutputLayer);
  19. if (m_pState != NULL) free(m_pState);
  20. m_nState = 0;
  21. };
  22. bool crnnLayer::create(uint32_t batch, uint32_t h, uint32_t w, uint32_t c, uint32_t hidden_filters, uint32_t output_filters, uint32_t steps, ACTIVATION activation, bool batch_normalize){
  23. fprintf(stderr, "CRNN Layer: %d x %d x %d image, %d filters\n", h,w,c,output_filters);
  24. batch = batch / steps;
  25. m_nBatch = batch;
  26. m_nSteps = steps;
  27. m_nOut_Height = m_nIn_Height = h;
  28. m_nOut_Width = m_nIn_Width = w;
  29. m_nIn_Channels = c;
  30. m_nOut_Channels = output_filters;
  31. m_nInput = h * w * c;
  32. m_nHidden = h * w * hidden_filters;
  33. m_nOutput = m_nOut_Height * m_nOut_Height * m_nOut_Channels;
  34. m_nOutputVol = m_nBatch * m_nOutput;
  35. m_nState = m_nHidden*m_nBatch*(m_nSteps + 1);
  36. m_pState = (float*)calloc(m_nState, sizeof(float));
  37. m_pInputLayer = new convolutionalLayer(m_bUseGPU);
  38. if (m_pInputLayer == NULL) return false;
  39. m_pInputLayer->create(batch*steps, h, w, c, hidden_filters, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
  40. m_pInputLayer->m_nBatch = batch;
  41. m_pSelfLayer = new convolutionalLayer(m_bUseGPU);
  42. if (m_pSelfLayer == NULL){
  43. delete(m_pInputLayer);
  44. return false;
  45. };
  46. m_pSelfLayer->create(batch*steps, h, w, hidden_filters, hidden_filters, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
  47. m_pSelfLayer->m_nBatch = batch;
  48. m_pOutputLayer = new convolutionalLayer(m_bUseGPU);
  49. if (m_pOutputLayer == NULL){
  50. delete(m_pInputLayer);
  51. delete(m_pSelfLayer);
  52. return false;
  53. };
  54. m_pOutputLayer->create(batch*steps, h, w, hidden_filters, output_filters, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
  55. m_pOutputLayer->m_nBatch = batch;
  56. m_pOutput = m_pOutputLayer->m_pOutput;
  57. m_pDelta = m_pOutputLayer->m_pDelta;
  58. #ifdef GPU
  59. m_pGpuState = m_pPU->makeFloatArray(m_nState);
  60. m_pGpuOutput = m_pOutputLayer->m_pGpuOutput;
  61. m_pGpuDelta = m_pOutputLayer->m_pDelta;
  62. #endif
  63. return true;
  64. };
  65. void crnnLayer::forward(cortexSTATE state){
  66. m_pPU->forward_layer(this, state);
  67. };
  68. void crnnLayer::backward(cortexSTATE state){
  69. m_pPU->backward_layer(this, state);
  70. };
  71. void crnnLayer::loadWeights(FILE *fp, bool trans){
  72. m_pInputLayer->loadWeights(fp, trans);
  73. m_pSelfLayer->loadWeights(fp, trans);
  74. m_pOutputLayer->loadWeights(fp, trans);
  75. };
  76. void crnnLayer::update(uint32_t batch, float learning_rate, float momentum, float decay){
  77. m_pPU->update_layer(this, batch, learning_rate, momentum, decay);
  78. };