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.

188 lines
4.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "reorg_layer.h"
  2. #include "dn_cuda.h"
  3. #include "blas.h"
  4. #include <stdio.h>
  5. namespace DN{
  6. layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse, int flatten, int extra)
  7. {
  8. layer l;
  9. memset(&l, 0, sizeof(layer));
  10. l.type = REORG;
  11. l.batch = batch;
  12. l.stride = stride;
  13. l.extra = extra;
  14. l.h = h;
  15. l.w = w;
  16. l.c = c;
  17. l.flatten = flatten;
  18. if (reverse){
  19. l.out_w = w*stride;
  20. l.out_h = h*stride;
  21. l.out_c = c / (stride*stride);
  22. }
  23. else{
  24. l.out_w = w / stride;
  25. l.out_h = h / stride;
  26. l.out_c = c*(stride*stride);
  27. }
  28. l.reverse = reverse;
  29. l.outputs = l.out_h * l.out_w * l.out_c;
  30. l.inputs = h*w*c;
  31. if (l.extra){
  32. l.out_w = l.out_h = l.out_c = 0;
  33. l.outputs = l.inputs + l.extra;
  34. }
  35. if (extra){
  36. fprintf(stderr, "reorg %4d -> %4d\n", l.inputs, l.outputs);
  37. }
  38. else {
  39. fprintf(stderr, "reorg /%2d %4d x%4d x%4d -> %4d x%4d x%4d\n", stride, w, h, c, l.out_w, l.out_h, l.out_c);
  40. }
  41. int output_size = l.outputs * batch;
  42. l.output = (float*)calloc(output_size, sizeof(float));
  43. l.delta = (float*)calloc(output_size, sizeof(float));
  44. l.forward = forward_reorg_layer;
  45. l.backward = backward_reorg_layer;
  46. #ifdef GPU
  47. l.forward_gpu = forward_reorg_layer_gpu;
  48. l.backward_gpu = backward_reorg_layer_gpu;
  49. l.output_gpu = cuda_make_array(l.output, output_size);
  50. l.delta_gpu = cuda_make_array(l.delta, output_size);
  51. #endif
  52. return l;
  53. }
  54. void resize_reorg_layer(layer *l, int w, int h)
  55. {
  56. int stride = l->stride;
  57. int c = l->c;
  58. l->h = h;
  59. l->w = w;
  60. if (l->reverse){
  61. l->out_w = w*stride;
  62. l->out_h = h*stride;
  63. l->out_c = c / (stride*stride);
  64. }
  65. else{
  66. l->out_w = w / stride;
  67. l->out_h = h / stride;
  68. l->out_c = c*(stride*stride);
  69. }
  70. l->outputs = l->out_h * l->out_w * l->out_c;
  71. l->inputs = l->outputs;
  72. int output_size = l->outputs * l->batch;
  73. l->output = (float*)realloc(l->output, output_size * sizeof(float));
  74. l->delta = (float*)realloc(l->delta, output_size * sizeof(float));
  75. #ifdef GPU
  76. cuda_free(l->output_gpu);
  77. cuda_free(l->delta_gpu);
  78. l->output_gpu = cuda_make_array(l->output, output_size);
  79. l->delta_gpu = cuda_make_array(l->delta, output_size);
  80. #endif
  81. }
  82. void forward_reorg_layer(const layer l, network net)
  83. {
  84. int i;
  85. if (l.flatten){
  86. memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float));
  87. if (l.reverse){
  88. flatten(l.output, l.w*l.h, l.c, l.batch, 0);
  89. }
  90. else{
  91. flatten(l.output, l.w*l.h, l.c, l.batch, 1);
  92. }
  93. }
  94. else if (l.extra) {
  95. for (i = 0; i < l.batch; ++i){
  96. copy_cpu(l.inputs, net.input + i*l.inputs, 1, l.output + i*l.outputs, 1);
  97. }
  98. }
  99. else if (l.reverse){
  100. reorg_cpu(net.input, l.w, l.h, l.c, l.batch, l.stride, 1, l.output);
  101. }
  102. else {
  103. reorg_cpu(net.input, l.w, l.h, l.c, l.batch, l.stride, 0, l.output);
  104. }
  105. }
  106. void backward_reorg_layer(const layer l, network net)
  107. {
  108. int i;
  109. if (l.flatten){
  110. memcpy(net.delta, l.delta, l.outputs*l.batch*sizeof(float));
  111. if (l.reverse){
  112. flatten(net.delta, l.w*l.h, l.c, l.batch, 1);
  113. }
  114. else{
  115. flatten(net.delta, l.w*l.h, l.c, l.batch, 0);
  116. }
  117. }
  118. else if (l.reverse){
  119. reorg_cpu(l.delta, l.w, l.h, l.c, l.batch, l.stride, 0, net.delta);
  120. }
  121. else if (l.extra) {
  122. for (i = 0; i < l.batch; ++i){
  123. copy_cpu(l.inputs, l.delta + i*l.outputs, 1, net.delta + i*l.inputs, 1);
  124. }
  125. }
  126. else{
  127. reorg_cpu(l.delta, l.w, l.h, l.c, l.batch, l.stride, 1, net.delta);
  128. }
  129. }
  130. #ifdef GPU
  131. void forward_reorg_layer_gpu(layer l, network net)
  132. {
  133. int i;
  134. if(l.flatten){
  135. if(l.reverse){
  136. flatten_gpu(net.input_gpu, l.w*l.h, l.c, l.batch, 0, l.output_gpu);
  137. }else{
  138. flatten_gpu(net.input_gpu, l.w*l.h, l.c, l.batch, 1, l.output_gpu);
  139. }
  140. } else if (l.extra) {
  141. for(i = 0; i < l.batch; ++i){
  142. copy_gpu(l.inputs, net.input_gpu + i*l.inputs, 1, l.output_gpu + i*l.outputs, 1);
  143. }
  144. } else if (l.reverse) {
  145. reorg_gpu(net.input_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, l.output_gpu);
  146. }else {
  147. reorg_gpu(net.input_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, l.output_gpu);
  148. }
  149. }
  150. void backward_reorg_layer_gpu(layer l, network net)
  151. {
  152. if(l.flatten){
  153. if(l.reverse){
  154. flatten_gpu(l.delta_gpu, l.w*l.h, l.c, l.batch, 1, net.delta_gpu);
  155. }else{
  156. flatten_gpu(l.delta_gpu, l.w*l.h, l.c, l.batch, 0, net.delta_gpu);
  157. }
  158. } else if (l.extra) {
  159. int i;
  160. for(i = 0; i < l.batch; ++i){
  161. copy_gpu(l.inputs, l.delta_gpu + i*l.outputs, 1, net.delta_gpu + i*l.inputs, 1);
  162. }
  163. } else if(l.reverse){
  164. reorg_gpu(l.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, net.delta_gpu);
  165. } else {
  166. reorg_gpu(l.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, net.delta_gpu);
  167. }
  168. }
  169. #endif
  170. };