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.

148 lines
4.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <cuda_runtime.h>
  2. #include <curand.h>
  3. #include <cublas_v2.h>
  4. extern "C" {
  5. #include "convolutional_layer.h"
  6. #include "deconvolutional_layer.h"
  7. #include "batchnorm_layer.h"
  8. #include "gemm.h"
  9. #include "blas.h"
  10. #include "im2col.h"
  11. #include "col2im.h"
  12. #include "utils.h"
  13. #include "dn_cuda.h"
  14. }
  15. namespace DN{
  16. extern "C" void forward_deconvolutional_layer_gpu(layer l, network net)
  17. {
  18. int i;
  19. int m = l.size*l.size*l.n;
  20. int n = l.h*l.w;
  21. int k = l.c;
  22. fill_gpu(l.outputs*l.batch, 0, l.output_gpu, 1);
  23. for (i = 0; i < l.batch; ++i){
  24. float *a = l.weights_gpu;
  25. float *b = net.input_gpu + i*l.c*l.h*l.w;
  26. float *c = net.workspace;
  27. gemm_gpu(1, 0, m, n, k, 1, a, m, b, n, 0, c, n);
  28. col2im_gpu(net.workspace, l.out_c, l.out_h, l.out_w, l.size, l.stride, l.pad, l.output_gpu + i*l.outputs);
  29. }
  30. if (l.batch_normalize) {
  31. forward_batchnorm_layer_gpu(l, net);
  32. }
  33. else {
  34. add_bias_gpu(l.output_gpu, l.biases_gpu, l.batch, l.n, l.out_w*l.out_h);
  35. }
  36. activate_array_gpu(l.output_gpu, l.batch*l.n*l.out_w*l.out_h, l.activation);
  37. }
  38. extern "C" void backward_deconvolutional_layer_gpu(layer l, network net)
  39. {
  40. int i;
  41. constrain_gpu(l.outputs*l.batch, 1, l.delta_gpu, 1);
  42. gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu);
  43. if (l.batch_normalize){
  44. backward_batchnorm_layer_gpu(l, net);
  45. }
  46. else {
  47. backward_bias_gpu(l.bias_updates_gpu, l.delta_gpu, l.batch, l.n, l.out_w*l.out_h);
  48. }
  49. //if(net.delta_gpu) memset(net.delta_gpu, 0, l.batch*l.h*l.w*l.c*sizeof(float));
  50. for (i = 0; i < l.batch; ++i){
  51. int m = l.c;
  52. int n = l.size*l.size*l.n;
  53. int k = l.h*l.w;
  54. float *a = net.input_gpu + i*m*k;
  55. float *b = net.workspace;
  56. float *c = l.weight_updates_gpu;
  57. im2col_gpu(l.delta_gpu + i*l.outputs, l.out_c, l.out_h, l.out_w,
  58. l.size, l.stride, l.pad, b);
  59. gemm_gpu(0, 1, m, n, k, 1, a, k, b, k, 1, c, n);
  60. if (net.delta_gpu){
  61. int m = l.c;
  62. int n = l.h*l.w;
  63. int k = l.size*l.size*l.n;
  64. float *a = l.weights_gpu;
  65. float *b = net.workspace;
  66. float *c = net.delta_gpu + i*n*m;
  67. gemm_gpu(0, 0, m, n, k, 1, a, k, b, n, 1, c, n);
  68. }
  69. }
  70. }
  71. extern "C" void pull_deconvolutional_layer(layer l)
  72. {
  73. cuda_pull_array(l.weights_gpu, l.weights, l.c*l.n*l.size*l.size);
  74. cuda_pull_array(l.biases_gpu, l.biases, l.n);
  75. cuda_pull_array(l.weight_updates_gpu, l.weight_updates, l.c*l.n*l.size*l.size);
  76. cuda_pull_array(l.bias_updates_gpu, l.bias_updates, l.n);
  77. if (l.batch_normalize){
  78. cuda_pull_array(l.scales_gpu, l.scales, l.n);
  79. cuda_pull_array(l.rolling_mean_gpu, l.rolling_mean, l.n);
  80. cuda_pull_array(l.rolling_variance_gpu, l.rolling_variance, l.n);
  81. }
  82. }
  83. extern "C" void push_deconvolutional_layer(layer l)
  84. {
  85. cuda_push_array(l.weights_gpu, l.weights, l.c*l.n*l.size*l.size);
  86. cuda_push_array(l.biases_gpu, l.biases, l.n);
  87. cuda_push_array(l.weight_updates_gpu, l.weight_updates, l.c*l.n*l.size*l.size);
  88. cuda_push_array(l.bias_updates_gpu, l.bias_updates, l.n);
  89. if (l.batch_normalize){
  90. cuda_push_array(l.scales_gpu, l.scales, l.n);
  91. cuda_push_array(l.rolling_mean_gpu, l.rolling_mean, l.n);
  92. cuda_push_array(l.rolling_variance_gpu, l.rolling_variance, l.n);
  93. }
  94. }
  95. void update_deconvolutional_layer_gpu(layer l, update_args a)
  96. {
  97. float learning_rate = a.learning_rate*l.learning_rate_scale;
  98. float momentum = a.momentum;
  99. float decay = a.decay;
  100. int batch = a.batch;
  101. int size = l.size*l.size*l.c*l.n;
  102. if (a.adam){
  103. adam_update_gpu(l.weights_gpu, l.weight_updates_gpu, l.m_gpu, l.v_gpu, a.B1, a.B2, a.eps, decay, learning_rate, size, batch, a.t);
  104. adam_update_gpu(l.biases_gpu, l.bias_updates_gpu, l.bias_m_gpu, l.bias_v_gpu, a.B1, a.B2, a.eps, decay, learning_rate, l.n, batch, a.t);
  105. if (l.scales_gpu){
  106. adam_update_gpu(l.scales_gpu, l.scale_updates_gpu, l.scale_m_gpu, l.scale_v_gpu, a.B1, a.B2, a.eps, decay, learning_rate, l.n, batch, a.t);
  107. }
  108. }
  109. else{
  110. axpy_gpu(size, -decay*batch, l.weights_gpu, 1, l.weight_updates_gpu, 1);
  111. axpy_gpu(size, learning_rate / batch, l.weight_updates_gpu, 1, l.weights_gpu, 1);
  112. scal_gpu(size, momentum, l.weight_updates_gpu, 1);
  113. axpy_gpu(l.n, learning_rate / batch, l.bias_updates_gpu, 1, l.biases_gpu, 1);
  114. scal_gpu(l.n, momentum, l.bias_updates_gpu, 1);
  115. if (l.scales_gpu){
  116. axpy_gpu(l.n, learning_rate / batch, l.scale_updates_gpu, 1, l.scales_gpu, 1);
  117. scal_gpu(l.n, momentum, l.scale_updates_gpu, 1);
  118. }
  119. }
  120. }
  121. };