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.

156 lines
4.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "normalization_layer.h"
  2. #include "blas.h"
  3. #include <stdio.h>
  4. namespace DN{
  5. layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa)
  6. {
  7. fprintf(stderr, "Local Response Normalization Layer: %d x %d x %d image, %d size\n", w, h, c, size);
  8. layer l;
  9. memset(&l, 0, sizeof(layer));
  10. l.type = NORMALIZATION;
  11. l.batch = batch;
  12. l.h = l.out_h = h;
  13. l.w = l.out_w = w;
  14. l.c = l.out_c = c;
  15. l.kappa = kappa;
  16. l.size = size;
  17. l.alpha = alpha;
  18. l.beta = beta;
  19. l.output = (float*)calloc(h * w * c * batch, sizeof(float));
  20. l.delta = (float*)calloc(h * w * c * batch, sizeof(float));
  21. l.squared = (float*)calloc(h * w * c * batch, sizeof(float));
  22. l.norms = (float*)calloc(h * w * c * batch, sizeof(float));
  23. l.inputs = w*h*c;
  24. l.outputs = l.inputs;
  25. l.forward = forward_normalization_layer;
  26. l.backward = backward_normalization_layer;
  27. #ifdef GPU
  28. l.forward_gpu = forward_normalization_layer_gpu;
  29. l.backward_gpu = backward_normalization_layer_gpu;
  30. l.output_gpu = cuda_make_array(l.output, h * w * c * batch);
  31. l.delta_gpu = cuda_make_array(l.delta, h * w * c * batch);
  32. l.squared_gpu = cuda_make_array(l.squared, h * w * c * batch);
  33. l.norms_gpu = cuda_make_array(l.norms, h * w * c * batch);
  34. #endif
  35. return l;
  36. }
  37. void resize_normalization_layer(layer *l, int w, int h)
  38. {
  39. int c = l->c;
  40. int batch = l->batch;
  41. l->h = h;
  42. l->w = w;
  43. l->out_h = h;
  44. l->out_w = w;
  45. l->inputs = w*h*c;
  46. l->outputs = l->inputs;
  47. l->output = (float*)realloc(l->output, h * w * c * batch * sizeof(float));
  48. l->delta = (float*)realloc(l->delta, h * w * c * batch * sizeof(float));
  49. l->squared = (float*)realloc(l->squared, h * w * c * batch * sizeof(float));
  50. l->norms = (float*)realloc(l->norms, h * w * c * batch * sizeof(float));
  51. #ifdef GPU
  52. cuda_free(l->output_gpu);
  53. cuda_free(l->delta_gpu);
  54. cuda_free(l->squared_gpu);
  55. cuda_free(l->norms_gpu);
  56. l->output_gpu = cuda_make_array(l->output, h * w * c * batch);
  57. l->delta_gpu = cuda_make_array(l->delta, h * w * c * batch);
  58. l->squared_gpu = cuda_make_array(l->squared, h * w * c * batch);
  59. l->norms_gpu = cuda_make_array(l->norms, h * w * c * batch);
  60. #endif
  61. }
  62. void forward_normalization_layer(const layer l, network net)
  63. {
  64. int k, b;
  65. int w = l.w;
  66. int h = l.h;
  67. int c = l.c;
  68. scal_cpu(w*h*c*l.batch, 0, l.squared, 1);
  69. for (b = 0; b < l.batch; ++b){
  70. float *squared = l.squared + w*h*c*b;
  71. float *norms = l.norms + w*h*c*b;
  72. float *input = net.input + w*h*c*b;
  73. pow_cpu(w*h*c, 2, input, 1, squared, 1);
  74. const_cpu(w*h, l.kappa, norms, 1);
  75. for (k = 0; k < l.size / 2; ++k){
  76. axpy_cpu(w*h, l.alpha, squared + w*h*k, 1, norms, 1);
  77. }
  78. for (k = 1; k < l.c; ++k){
  79. copy_cpu(w*h, norms + w*h*(k - 1), 1, norms + w*h*k, 1);
  80. int prev = k - ((l.size - 1) / 2) - 1;
  81. int next = k + (l.size / 2);
  82. if (prev >= 0) axpy_cpu(w*h, -l.alpha, squared + w*h*prev, 1, norms + w*h*k, 1);
  83. if (next < l.c) axpy_cpu(w*h, l.alpha, squared + w*h*next, 1, norms + w*h*k, 1);
  84. }
  85. }
  86. pow_cpu(w*h*c*l.batch, -l.beta, l.norms, 1, l.output, 1);
  87. mul_cpu(w*h*c*l.batch, net.input, 1, l.output, 1);
  88. }
  89. void backward_normalization_layer(const layer l, network net)
  90. {
  91. // TODO This is approximate ;-)
  92. // Also this should add in to delta instead of overwritting.
  93. int w = l.w;
  94. int h = l.h;
  95. int c = l.c;
  96. pow_cpu(w*h*c*l.batch, -l.beta, l.norms, 1, net.delta, 1);
  97. mul_cpu(w*h*c*l.batch, l.delta, 1, net.delta, 1);
  98. }
  99. #ifdef GPU
  100. void forward_normalization_layer_gpu(const layer layer, network net)
  101. {
  102. int k,b;
  103. int w = layer.w;
  104. int h = layer.h;
  105. int c = layer.c;
  106. scal_gpu(w*h*c*layer.batch, 0, layer.squared_gpu, 1);
  107. for(b = 0; b < layer.batch; ++b){
  108. float *squared = layer.squared_gpu + w*h*c*b;
  109. float *norms = layer.norms_gpu + w*h*c*b;
  110. float *input = net.input_gpu + w*h*c*b;
  111. pow_gpu(w*h*c, 2, input, 1, squared, 1);
  112. const_gpu(w*h, layer.kappa, norms, 1);
  113. for(k = 0; k < layer.size/2; ++k){
  114. axpy_gpu(w*h, layer.alpha, squared + w*h*k, 1, norms, 1);
  115. }
  116. for(k = 1; k < layer.c; ++k){
  117. copy_gpu(w*h, norms + w*h*(k-1), 1, norms + w*h*k, 1);
  118. int prev = k - ((layer.size-1)/2) - 1;
  119. int next = k + (layer.size/2);
  120. if(prev >= 0) axpy_gpu(w*h, -layer.alpha, squared + w*h*prev, 1, norms + w*h*k, 1);
  121. if(next < layer.c) axpy_gpu(w*h, layer.alpha, squared + w*h*next, 1, norms + w*h*k, 1);
  122. }
  123. }
  124. pow_gpu(w*h*c*layer.batch, -layer.beta, layer.norms_gpu, 1, layer.output_gpu, 1);
  125. mul_gpu(w*h*c*layer.batch, net.input_gpu, 1, layer.output_gpu, 1);
  126. }
  127. void backward_normalization_layer_gpu(const layer layer, network net)
  128. {
  129. // TODO This is approximate ;-)
  130. int w = layer.w;
  131. int h = layer.h;
  132. int c = layer.c;
  133. pow_gpu(w*h*c*layer.batch, -layer.beta, layer.norms_gpu, 1, net.delta_gpu, 1);
  134. mul_gpu(w*h*c*layer.batch, layer.delta_gpu, 1, net.delta_gpu, 1);
  135. }
  136. #endif
  137. };