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.

298 lines
8.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "local_layer.h"
  2. #include "utils.h"
  3. #include "im2col.h"
  4. #include "col2im.h"
  5. #include "blas.h"
  6. #include "gemm.h"
  7. #include <stdio.h>
  8. #include <time.h>
  9. namespace DN{
  10. int local_out_height(local_layer l)
  11. {
  12. int h = l.h;
  13. if (!l.pad) h -= l.size;
  14. else h -= 1;
  15. return h / l.stride + 1;
  16. }
  17. int local_out_width(local_layer l)
  18. {
  19. int w = l.w;
  20. if (!l.pad) w -= l.size;
  21. else w -= 1;
  22. return w / l.stride + 1;
  23. }
  24. local_layer make_local_layer(int batch, int h, int w, int c, int n, int size, int stride, int pad, ACTIVATION activation)
  25. {
  26. int i;
  27. local_layer l;
  28. memset(&l, 0, sizeof(local_layer));
  29. l.type = LOCAL;
  30. l.h = h;
  31. l.w = w;
  32. l.c = c;
  33. l.n = n;
  34. l.batch = batch;
  35. l.stride = stride;
  36. l.size = size;
  37. l.pad = pad;
  38. int out_h = local_out_height(l);
  39. int out_w = local_out_width(l);
  40. int locations = out_h*out_w;
  41. l.out_h = out_h;
  42. l.out_w = out_w;
  43. l.out_c = n;
  44. l.outputs = l.out_h * l.out_w * l.out_c;
  45. l.inputs = l.w * l.h * l.c;
  46. l.weights = (float*)calloc(c*n*size*size*locations, sizeof(float));
  47. l.weight_updates = (float*)calloc(c*n*size*size*locations, sizeof(float));
  48. l.biases = (float*)calloc(l.outputs, sizeof(float));
  49. l.bias_updates = (float*)calloc(l.outputs, sizeof(float));
  50. // float scale = 1./sqrt(size*size*c);
  51. float scale = (float)sqrt(2. / (size*size*c));
  52. for (i = 0; i < c*n*size*size; ++i) l.weights[i] = scale*rand_uniform(-1, 1);
  53. l.output = (float*)calloc(l.batch*out_h * out_w * n, sizeof(float));
  54. l.delta = (float*)calloc(l.batch*out_h * out_w * n, sizeof(float));
  55. l.workspace_size = out_h*out_w*size*size*c;
  56. l.forward = forward_local_layer;
  57. l.backward = backward_local_layer;
  58. l.update = update_local_layer;
  59. #ifdef GPU
  60. l.forward_gpu = forward_local_layer_gpu;
  61. l.backward_gpu = backward_local_layer_gpu;
  62. l.update_gpu = update_local_layer_gpu;
  63. l.weights_gpu = cuda_make_array(l.weights, c*n*size*size*locations);
  64. l.weight_updates_gpu = cuda_make_array(l.weight_updates, c*n*size*size*locations);
  65. l.biases_gpu = cuda_make_array(l.biases, l.outputs);
  66. l.bias_updates_gpu = cuda_make_array(l.bias_updates, l.outputs);
  67. l.delta_gpu = cuda_make_array(l.delta, l.batch*out_h*out_w*n);
  68. l.output_gpu = cuda_make_array(l.output, l.batch*out_h*out_w*n);
  69. #endif
  70. l.activation = activation;
  71. fprintf(stderr, "Local Layer: %d x %d x %d image, %d filters -> %d x %d x %d image\n", h, w, c, n, out_h, out_w, n);
  72. return l;
  73. }
  74. void forward_local_layer(const local_layer l, network net)
  75. {
  76. int out_h = local_out_height(l);
  77. int out_w = local_out_width(l);
  78. int i, j;
  79. int locations = out_h * out_w;
  80. for (i = 0; i < l.batch; ++i){
  81. copy_cpu(l.outputs, l.biases, 1, l.output + i*l.outputs, 1);
  82. }
  83. for (i = 0; i < l.batch; ++i){
  84. float *input = net.input + i*l.w*l.h*l.c;
  85. im2col_cpu(input, l.c, l.h, l.w,
  86. l.size, l.stride, l.pad, net.workspace);
  87. float *output = l.output + i*l.outputs;
  88. for (j = 0; j < locations; ++j){
  89. float *a = l.weights + j*l.size*l.size*l.c*l.n;
  90. float *b = net.workspace + j;
  91. float *c = output + j;
  92. int m = l.n;
  93. int n = 1;
  94. int k = l.size*l.size*l.c;
  95. gemm(0, 0, m, n, k, 1, a, k, b, locations, 1, c, locations);
  96. }
  97. }
  98. activate_array(l.output, l.outputs*l.batch, l.activation);
  99. }
  100. void backward_local_layer(local_layer l, network net)
  101. {
  102. int i, j;
  103. int locations = l.out_w*l.out_h;
  104. gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta);
  105. for (i = 0; i < l.batch; ++i){
  106. axpy_cpu(l.outputs, 1, l.delta + i*l.outputs, 1, l.bias_updates, 1);
  107. }
  108. for (i = 0; i < l.batch; ++i){
  109. float *input = net.input + i*l.w*l.h*l.c;
  110. im2col_cpu(input, l.c, l.h, l.w,
  111. l.size, l.stride, l.pad, net.workspace);
  112. for (j = 0; j < locations; ++j){
  113. float *a = l.delta + i*l.outputs + j;
  114. float *b = net.workspace + j;
  115. float *c = l.weight_updates + j*l.size*l.size*l.c*l.n;
  116. int m = l.n;
  117. int n = l.size*l.size*l.c;
  118. int k = 1;
  119. gemm(0, 1, m, n, k, 1, a, locations, b, locations, 1, c, n);
  120. }
  121. if (net.delta){
  122. for (j = 0; j < locations; ++j){
  123. float *a = l.weights + j*l.size*l.size*l.c*l.n;
  124. float *b = l.delta + i*l.outputs + j;
  125. float *c = net.workspace + j;
  126. int m = l.size*l.size*l.c;
  127. int n = 1;
  128. int k = l.n;
  129. gemm(1, 0, m, n, k, 1, a, m, b, locations, 0, c, locations);
  130. }
  131. col2im_cpu(net.workspace, l.c, l.h, l.w, l.size, l.stride, l.pad, net.delta + i*l.c*l.h*l.w);
  132. }
  133. }
  134. }
  135. void update_local_layer(local_layer l, update_args a)
  136. {
  137. float learning_rate = a.learning_rate*l.learning_rate_scale;
  138. float momentum = a.momentum;
  139. float decay = a.decay;
  140. int batch = a.batch;
  141. int locations = l.out_w*l.out_h;
  142. int size = l.size*l.size*l.c*l.n*locations;
  143. axpy_cpu(l.outputs, learning_rate / batch, l.bias_updates, 1, l.biases, 1);
  144. scal_cpu(l.outputs, momentum, l.bias_updates, 1);
  145. axpy_cpu(size, -decay*batch, l.weights, 1, l.weight_updates, 1);
  146. axpy_cpu(size, learning_rate / batch, l.weight_updates, 1, l.weights, 1);
  147. scal_cpu(size, momentum, l.weight_updates, 1);
  148. }
  149. #ifdef GPU
  150. void forward_local_layer_gpu(const local_layer l, network net)
  151. {
  152. int out_h = local_out_height(l);
  153. int out_w = local_out_width(l);
  154. int i, j;
  155. int locations = out_h * out_w;
  156. for(i = 0; i < l.batch; ++i){
  157. copy_gpu(l.outputs, l.biases_gpu, 1, l.output_gpu + i*l.outputs, 1);
  158. }
  159. for(i = 0; i < l.batch; ++i){
  160. float *input = net.input_gpu + i*l.w*l.h*l.c;
  161. im2col_gpu(input, l.c, l.h, l.w,
  162. l.size, l.stride, l.pad, net.workspace);
  163. float *output = l.output_gpu + i*l.outputs;
  164. for(j = 0; j < locations; ++j){
  165. float *a = l.weights_gpu + j*l.size*l.size*l.c*l.n;
  166. float *b = net.workspace + j;
  167. float *c = output + j;
  168. int m = l.n;
  169. int n = 1;
  170. int k = l.size*l.size*l.c;
  171. gemm_gpu(0,0,m,n,k,1,a,k,b,locations,1,c,locations);
  172. }
  173. }
  174. activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation);
  175. }
  176. void backward_local_layer_gpu(local_layer l, network net)
  177. {
  178. int i, j;
  179. int locations = l.out_w*l.out_h;
  180. gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu);
  181. for(i = 0; i < l.batch; ++i){
  182. axpy_gpu(l.outputs, 1, l.delta_gpu + i*l.outputs, 1, l.bias_updates_gpu, 1);
  183. }
  184. for(i = 0; i < l.batch; ++i){
  185. float *input = net.input_gpu + i*l.w*l.h*l.c;
  186. im2col_gpu(input, l.c, l.h, l.w,
  187. l.size, l.stride, l.pad, net.workspace);
  188. for(j = 0; j < locations; ++j){
  189. float *a = l.delta_gpu + i*l.outputs + j;
  190. float *b = net.workspace + j;
  191. float *c = l.weight_updates_gpu + j*l.size*l.size*l.c*l.n;
  192. int m = l.n;
  193. int n = l.size*l.size*l.c;
  194. int k = 1;
  195. gemm_gpu(0,1,m,n,k,1,a,locations,b,locations,1,c,n);
  196. }
  197. if(net.delta_gpu){
  198. for(j = 0; j < locations; ++j){
  199. float *a = l.weights_gpu + j*l.size*l.size*l.c*l.n;
  200. float *b = l.delta_gpu + i*l.outputs + j;
  201. float *c = net.workspace + j;
  202. int m = l.size*l.size*l.c;
  203. int n = 1;
  204. int k = l.n;
  205. gemm_gpu(1,0,m,n,k,1,a,m,b,locations,0,c,locations);
  206. }
  207. col2im_gpu(net.workspace, l.c, l.h, l.w, l.size, l.stride, l.pad, net.delta_gpu+i*l.c*l.h*l.w);
  208. }
  209. }
  210. }
  211. void update_local_layer_gpu(local_layer l, update_args a)
  212. {
  213. float learning_rate = a.learning_rate*l.learning_rate_scale;
  214. float momentum = a.momentum;
  215. float decay = a.decay;
  216. int batch = a.batch;
  217. int locations = l.out_w*l.out_h;
  218. int size = l.size*l.size*l.c*l.n*locations;
  219. axpy_gpu(l.outputs, learning_rate/batch, l.bias_updates_gpu, 1, l.biases_gpu, 1);
  220. scal_gpu(l.outputs, momentum, l.bias_updates_gpu, 1);
  221. axpy_gpu(size, -decay*batch, l.weights_gpu, 1, l.weight_updates_gpu, 1);
  222. axpy_gpu(size, learning_rate/batch, l.weight_updates_gpu, 1, l.weights_gpu, 1);
  223. scal_gpu(size, momentum, l.weight_updates_gpu, 1);
  224. }
  225. void pull_local_layer(local_layer l)
  226. {
  227. int locations = l.out_w*l.out_h;
  228. int size = l.size*l.size*l.c*l.n*locations;
  229. cuda_pull_array(l.weights_gpu, l.weights, size);
  230. cuda_pull_array(l.biases_gpu, l.biases, l.outputs);
  231. }
  232. void push_local_layer(local_layer l)
  233. {
  234. int locations = l.out_w*l.out_h;
  235. int size = l.size*l.size*l.c*l.n*locations;
  236. cuda_push_array(l.weights_gpu, l.weights, size);
  237. cuda_push_array(l.biases_gpu, l.biases, l.outputs);
  238. }
  239. #endif
  240. };