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.

235 lines
7.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "deconvolutionalLayer.h"
  2. #include "convolutionalLayer.h"
  3. //#include "utils.h"
  4. //#include "im2col.h"
  5. //#include "col2im.h"
  6. //#include "blas.h"
  7. //#include "gemm.h"
  8. /*
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include "../cuda/cuda.h"
  12. #include "../cpu/activations.h"
  13. #include "../cpu/im2col.h"
  14. #include "../cpu/col2im.h"
  15. #include "../cpu/blas.h"
  16. #include "../cpu/gemm.h"
  17. */
  18. #define LOG( X ) m_pLog->Log( "DECONVL", this, X )
  19. #define LOG_ERROR( X ) m_pLog->Log( "DECONVL", this, _LOG_ERROR, X )
  20. deconvolutionalLayer::deconvolutionalLayer(bool useGPU) :cortexLayer(DECONVOLUTIONAL, useGPU){
  21. m_nColImage = 0;
  22. m_pColImage = NULL;
  23. m_pGpuColImage = NULL;
  24. };
  25. deconvolutionalLayer::~deconvolutionalLayer(){
  26. if (m_pColImage)free(m_pColImage);
  27. if (m_pPU != NULL){
  28. if (m_pGpuColImage != NULL)m_pPU->freeMem(m_pGpuColImage);
  29. };
  30. };
  31. /*
  32. int deconvolutional_out_height(deconvolutional_layer l)
  33. {
  34. int h = l.stride*(l.h - 1) + l.size;
  35. return h;
  36. }
  37. int deconvolutional_out_width(deconvolutional_layer l)
  38. {
  39. int w = l.stride*(l.w - 1) + l.size;
  40. return w;
  41. }
  42. int deconvolutional_out_size(deconvolutional_layer l)
  43. {
  44. return deconvolutional_out_height(l) * deconvolutional_out_width(l);
  45. }
  46. image get_deconvolutional_image(deconvolutional_layer l)
  47. {
  48. int h,w,c;
  49. h = deconvolutional_out_height(l);
  50. w = deconvolutional_out_width(l);
  51. c = l.n;
  52. return float_to_image(w,h,c,l.output);
  53. }
  54. image get_deconvolutional_delta(deconvolutional_layer l)
  55. {
  56. int h,w,c;
  57. h = deconvolutional_out_height(l);
  58. w = deconvolutional_out_width(l);
  59. c = l.n;
  60. return float_to_image(w,h,c,l.delta);
  61. }
  62. */
  63. bool deconvolutionalLayer::create(uint32_t batch, uint32_t h, uint32_t w, uint32_t c, uint32_t n, uint32_t size, uint32_t stride, ACTIVATION activation){
  64. uint32_t i;
  65. m_nIn_Height = h;
  66. m_nIn_Width = w;
  67. m_nIn_Channels = c;
  68. m_nOut_Channels = n;
  69. m_nBatch = batch;
  70. m_nStride = stride;
  71. m_nSize = size;
  72. m_nWeight = c*n*size*size;
  73. m_pWeight = (float*)calloc(m_nWeight, sizeof(float));
  74. m_pWeightUpdate = (float*)calloc(m_nWeight, sizeof(float));
  75. m_nBias = n;
  76. m_pBias = (float*)calloc(m_nBias, sizeof(float));
  77. m_pBiasUpdate = (float*)calloc(m_nBias, sizeof(float));
  78. float scale = (float)(1.f/sqrt((float)(size*size*c)));
  79. for (i = 0; i < m_nWeight; ++i) m_pWeight[i] = scale*rand_normal();
  80. for (i = 0; i < m_nBias; ++i) m_pBias[i] = scale;
  81. //int out_h = deconvolutional_out_height(l);
  82. //int out_w = deconvolutional_out_width(l);
  83. m_nOut_Height = m_nStride * (m_nIn_Height - 1) + m_nSize;
  84. m_nOut_Width = m_nStride * (m_nIn_Width - 1) + m_nSize;
  85. char tBuff[256];
  86. sprintf(tBuff, "Deconvolutional Layer: %d x %d x %d image, %d filters -> %d x %d x %d image", h, w, c, n, m_nOut_Height, m_nOut_Width, n);
  87. LOG(tBuff);
  88. //l.out_h = out_h;
  89. //l.out_w = out_w;
  90. //l.out_c = n;
  91. //l.outputs = l.out_w * l.out_h * l.out_c;
  92. //l.inputs = l.w * l.h * l.c;
  93. m_nDelta = m_nOutput = m_nOut_Height * m_nOut_Width * m_nOut_Channels;
  94. m_nDeltaVol = m_nOutputVol = m_nOutput*m_nBatch;
  95. m_nInput = m_nIn_Height * m_nIn_Width * m_nIn_Channels;
  96. m_nColImage = m_nOut_Height*m_nOut_Width*m_nSize*m_nSize*m_nOut_Channels;
  97. m_pOutput = (float*)calloc(m_nOutputVol, sizeof(float));
  98. m_pDelta = (float*)calloc(m_nDeltaVol, sizeof(float));
  99. m_pColImage = (float*)calloc(m_nColImage, sizeof(float));
  100. #ifdef GPU
  101. m_pGpuWeight = m_pPU->makeFloatArray(m_nWeight);
  102. m_pGpuWeightUpdate = m_pPU->makeFloatArray(m_nWeight);
  103. m_pGpuBias = m_pPU->makeFloatArray(m_nBias);
  104. m_pGpuBiasUpdate = m_pPU->makeFloatArray(m_nBias);
  105. m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol);
  106. m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol);
  107. /* Layer Specific */
  108. m_pGpuColImage = m_pPU->makeFloatArray(m_nColImage, m_pColImage);
  109. #endif
  110. m_eActivation = activation;
  111. return true;
  112. };
  113. bool deconvolutionalLayer::resize(uint32_t w, uint32_t h){
  114. m_nIn_Height = h;
  115. m_nIn_Width = w;
  116. m_nOut_Height = m_nStride * (m_nIn_Height - 1) + m_nSize;
  117. m_nOut_Width = m_nStride * (m_nIn_Width - 1) + m_nSize;
  118. m_nInput = m_nIn_Height * m_nIn_Width * m_nIn_Channels;
  119. m_nOutput = m_nOut_Height * m_nOut_Width * m_nOut_Channels;
  120. m_nColImage = m_nOut_Height * m_nOut_Width * m_nSize * m_nSize * m_nOut_Channels;
  121. m_nDelta = m_nOut_Height * m_nOut_Width * m_nOut_Channels;
  122. m_pColImage = (float*)realloc(m_pColImage, m_nColImage * sizeof(float));
  123. m_pOutput = (float*)realloc(m_pOutput, m_nBatch * m_nOutput * sizeof(float));
  124. m_pDelta = (float*)realloc(m_pDelta, m_nBatch * m_nDelta * sizeof(float));
  125. #ifdef GPU
  126. m_pPU->freeMem(m_pGpuColImage);
  127. m_pPU->freeMem(m_pGpuOutput);
  128. m_pPU->freeMem(m_pGpuDelta);
  129. m_pGpuColImage = m_pPU->makeFloatArray(m_nColImage, m_pColImage);
  130. m_pGpuOutput = m_pPU->makeFloatArray(m_nOutput,m_pOutput);
  131. m_pGpuDelta = m_pPU->makeFloatArray(m_nDelta, m_pDelta);
  132. #endif
  133. return true;
  134. };
  135. void deconvolutionalLayer::forward(cortexSTATE state){
  136. uint32_t i;
  137. //int out_h = deconvolutional_out_height(l);
  138. //int out_w = deconvolutional_out_width(l);
  139. uint32_t size = m_nOut_Height * m_nOut_Width;
  140. uint32_t m = m_nSize*m_nSize*m_nOut_Channels; //this->n;
  141. uint32_t n = m_nIn_Height * m_nIn_Width;
  142. uint32_t k = m_nIn_Channels;
  143. m_pPU->makeFill(m_nOutput*m_nBatch, 0, m_pOutput, 1);
  144. for(i = 0; i < m_nBatch; ++i){
  145. float *a = m_pWeight;
  146. float *b = &state.input[ i * m_nIn_Channels * m_nIn_Height * m_nIn_Width ];
  147. float *c = m_pColImage;
  148. m_pPU->gemm( 1, 0, m, n, k, 1, a, m, b, n, 0, c, n);
  149. m_pPU->col2im(c, m_nOut_Channels, m_nOut_Height, m_nOut_Width, m_nSize, m_nStride, 0, &m_pOutput[ i * m_nOut_Channels * m_nSize]);
  150. };
  151. m_pPU->addBias(m_pOutput, m_pBias, m_nBatch, m_nOut_Channels, m_nSize);
  152. m_pPU->makeActivatedArray( m_pOutput, m_nBatch*m_nOut_Channels*m_nSize, m_eActivation);
  153. };
  154. void deconvolutionalLayer::backward( cortexSTATE state){
  155. float alpha = 1.f/m_nBatch;
  156. //int out_h = deconvolutional_out_height(l);
  157. //int out_w = deconvolutional_out_width(l);
  158. uint32_t size = m_nOut_Height * m_nOut_Width;
  159. uint32_t i;
  160. m_pPU->makeGradientArray(m_pOutput, size*m_nOut_Channels*m_nBatch, m_eActivation, m_pDelta);
  161. m_pPU->makeBackwardBias(m_pBiasUpdate, m_pDelta, m_nBatch, m_nOut_Channels, size);
  162. for(i = 0; i < m_nBatch; ++i){
  163. uint32_t m = m_nIn_Channels;
  164. uint32_t n = m_nSize*m_nSize*m_nOut_Channels;
  165. uint32_t k = m_nIn_Height*m_nIn_Width;
  166. float *a = &state.input[i*m*n];
  167. float *b = m_pColImage;
  168. float *c = m_pWeightUpdate;
  169. m_pPU->im2col(&m_pDelta[i*m_nOut_Channels*size], m_nOut_Channels, m_nOut_Height, m_nOut_Width, m_nSize, m_nStride, 0, b);
  170. m_pPU->gemm( 0, 1, m, n, k, alpha, a, k, b, k, 1, c, n );
  171. if(state.delta){
  172. uint32_t m = m_nIn_Channels;
  173. uint32_t n = m_nIn_Height*m_nIn_Width;
  174. uint32_t k = m_nSize*m_nSize*m_nOut_Channels;
  175. float *a = m_pWeight;
  176. float *b = m_pColImage;
  177. float *c = &state.delta[i*n*m];
  178. m_pPU->gemm(0, 0, m, n, k, 1, a, k, b, n, 1, c, n);
  179. };
  180. };
  181. };
  182. void deconvolutionalLayer::update(int batch, float learning_rate, float momentum, float decay){
  183. // int size = m_nSize*m_nSize*m_nIn_Channels*m_nOut_Channels;
  184. m_pPU->calcAxpy( m_nBias, learning_rate, m_pBiasUpdate, 1, m_pBias, 1 );
  185. m_pPU->calcScal( m_nBias, momentum, m_pBiasUpdate, 1 );
  186. m_pPU->calcAxpy(m_nWeight, -decay, m_pWeight, 1, m_pWeightUpdate, 1);
  187. m_pPU->calcAxpy(m_nWeight, learning_rate, m_pWeightUpdate, 1, m_pWeight, 1);
  188. m_pPU->calcScal(m_nWeight, momentum, m_pWeightUpdate, 1);
  189. };
  190. #undef LOG
  191. #undef LOG_ERROR