1
0

maxpool_layer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "maxpool_layer.h"
  2. #include "cuda.h"
  3. #include <stdio.h>
  4. image get_maxpool_image(maxpool_layer l)
  5. {
  6. int h = l.out_h;
  7. int w = l.out_w;
  8. int c = l.c;
  9. return float_to_image(w,h,c,l.output);
  10. }
  11. image get_maxpool_delta(maxpool_layer l)
  12. {
  13. int h = l.out_h;
  14. int w = l.out_w;
  15. int c = l.c;
  16. return float_to_image(w,h,c,l.delta);
  17. }
  18. maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding)
  19. {
  20. maxpool_layer l = {0};
  21. l.type = MAXPOOL;
  22. l.batch = batch;
  23. l.h = h;
  24. l.w = w;
  25. l.c = c;
  26. l.pad = padding;
  27. l.out_w = (w + padding - size)/stride + 1;
  28. l.out_h = (h + padding - size)/stride + 1;
  29. l.out_c = c;
  30. l.outputs = l.out_h * l.out_w * l.out_c;
  31. l.inputs = h*w*c;
  32. l.size = size;
  33. l.stride = stride;
  34. int output_size = l.out_h * l.out_w * l.out_c * batch;
  35. l.indexes = calloc(output_size, sizeof(int));
  36. l.output = calloc(output_size, sizeof(float));
  37. l.delta = calloc(output_size, sizeof(float));
  38. l.forward = forward_maxpool_layer;
  39. l.backward = backward_maxpool_layer;
  40. #ifdef GPU
  41. l.forward_gpu = forward_maxpool_layer_gpu;
  42. l.backward_gpu = backward_maxpool_layer_gpu;
  43. l.indexes_gpu = cuda_make_int_array(0, output_size);
  44. l.output_gpu = cuda_make_array(l.output, output_size);
  45. l.delta_gpu = cuda_make_array(l.delta, output_size);
  46. #endif
  47. fprintf(stderr, "max %d x %d / %d %4d x%4d x%4d -> %4d x%4d x%4d\n", size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c);
  48. return l;
  49. }
  50. void resize_maxpool_layer(maxpool_layer *l, int w, int h)
  51. {
  52. l->h = h;
  53. l->w = w;
  54. l->inputs = h*w*l->c;
  55. l->out_w = (w + l->pad - l->size)/l->stride + 1;
  56. l->out_h = (h + l->pad - l->size)/l->stride + 1;
  57. l->outputs = l->out_w * l->out_h * l->c;
  58. int output_size = l->outputs * l->batch;
  59. l->indexes = realloc(l->indexes, output_size * sizeof(int));
  60. l->output = realloc(l->output, output_size * sizeof(float));
  61. l->delta = realloc(l->delta, output_size * sizeof(float));
  62. #ifdef GPU
  63. cuda_free((float *)l->indexes_gpu);
  64. cuda_free(l->output_gpu);
  65. cuda_free(l->delta_gpu);
  66. l->indexes_gpu = cuda_make_int_array(0, output_size);
  67. l->output_gpu = cuda_make_array(l->output, output_size);
  68. l->delta_gpu = cuda_make_array(l->delta, output_size);
  69. #endif
  70. }
  71. void forward_maxpool_layer(const maxpool_layer l, network net)
  72. {
  73. int b,i,j,k,m,n;
  74. int w_offset = -l.pad/2;
  75. int h_offset = -l.pad/2;
  76. int h = l.out_h;
  77. int w = l.out_w;
  78. int c = l.c;
  79. for(b = 0; b < l.batch; ++b){
  80. for(k = 0; k < c; ++k){
  81. for(i = 0; i < h; ++i){
  82. for(j = 0; j < w; ++j){
  83. int out_index = j + w*(i + h*(k + c*b));
  84. float max = -FLT_MAX;
  85. int max_i = -1;
  86. for(n = 0; n < l.size; ++n){
  87. for(m = 0; m < l.size; ++m){
  88. int cur_h = h_offset + i*l.stride + n;
  89. int cur_w = w_offset + j*l.stride + m;
  90. int index = cur_w + l.w*(cur_h + l.h*(k + b*l.c));
  91. int valid = (cur_h >= 0 && cur_h < l.h &&
  92. cur_w >= 0 && cur_w < l.w);
  93. float val = (valid != 0) ? net.input[index] : -FLT_MAX;
  94. max_i = (val > max) ? index : max_i;
  95. max = (val > max) ? val : max;
  96. }
  97. }
  98. l.output[out_index] = max;
  99. l.indexes[out_index] = max_i;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. void backward_maxpool_layer(const maxpool_layer l, network net)
  106. {
  107. int i;
  108. int h = l.out_h;
  109. int w = l.out_w;
  110. int c = l.c;
  111. for(i = 0; i < h*w*c*l.batch; ++i){
  112. int index = l.indexes[i];
  113. net.delta[index] += l.delta[i];
  114. }
  115. }