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.

289 lines
8.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "crnn_layer.h"
  2. #include "convolutional_layer.h"
  3. #include "utils.h"
  4. #include "dn_cuda.h"
  5. #include "blas.h"
  6. #include "gemm.h"
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. namespace DN{
  12. static void increment_layer(layer *l, int steps)
  13. {
  14. int num = l->outputs*l->batch*steps;
  15. l->output += num;
  16. l->delta += num;
  17. l->x += num;
  18. l->x_norm += num;
  19. #ifdef GPU
  20. l->output_gpu += num;
  21. l->delta_gpu += num;
  22. l->x_gpu += num;
  23. l->x_norm_gpu += num;
  24. #endif
  25. }
  26. layer make_crnn_layer(int batch, int h, int w, int c, int hidden_filters, int output_filters, int steps, ACTIVATION activation, int batch_normalize)
  27. {
  28. fprintf(stderr, "CRNN Layer: %d x %d x %d image, %d filters\n", h, w, c, output_filters);
  29. batch = batch / steps;
  30. layer l;
  31. memset(&l, 0, sizeof(layer));
  32. l.batch = batch;
  33. l.type = CRNN;
  34. l.steps = steps;
  35. l.h = h;
  36. l.w = w;
  37. l.c = c;
  38. l.out_h = h;
  39. l.out_w = w;
  40. l.out_c = output_filters;
  41. l.inputs = h*w*c;
  42. l.hidden = h * w * hidden_filters;
  43. l.outputs = l.out_h * l.out_w * l.out_c;
  44. l.state = (float*)calloc(l.hidden*batch*(steps + 1), sizeof(float));
  45. l.input_layer = (layer*)malloc(sizeof(layer));
  46. fprintf(stderr, "\t\t");
  47. *(l.input_layer) = make_convolutional_layer(batch*steps, h, w, c, hidden_filters, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
  48. l.input_layer->batch = batch;
  49. l.self_layer = (layer*)malloc(sizeof(layer));
  50. fprintf(stderr, "\t\t");
  51. *(l.self_layer) = make_convolutional_layer(batch*steps, h, w, hidden_filters, hidden_filters, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
  52. l.self_layer->batch = batch;
  53. l.output_layer = (layer*)malloc(sizeof(layer));
  54. fprintf(stderr, "\t\t");
  55. *(l.output_layer) = make_convolutional_layer(batch*steps, h, w, hidden_filters, output_filters, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
  56. l.output_layer->batch = batch;
  57. l.output = l.output_layer->output;
  58. l.delta = l.output_layer->delta;
  59. l.forward = forward_crnn_layer;
  60. l.backward = backward_crnn_layer;
  61. l.update = update_crnn_layer;
  62. #ifdef GPU
  63. l.forward_gpu = forward_crnn_layer_gpu;
  64. l.backward_gpu = backward_crnn_layer_gpu;
  65. l.update_gpu = update_crnn_layer_gpu;
  66. l.state_gpu = cuda_make_array(l.state, l.hidden*batch*(steps+1));
  67. l.output_gpu = l.output_layer->output_gpu;
  68. l.delta_gpu = l.output_layer->delta_gpu;
  69. #endif
  70. return l;
  71. }
  72. void update_crnn_layer(layer l, update_args a)
  73. {
  74. update_convolutional_layer(*(l.input_layer), a);
  75. update_convolutional_layer(*(l.self_layer), a);
  76. update_convolutional_layer(*(l.output_layer), a);
  77. }
  78. void forward_crnn_layer(layer l, network net)
  79. {
  80. network s = net;
  81. s.train = net.train;
  82. int i;
  83. layer input_layer = *(l.input_layer);
  84. layer self_layer = *(l.self_layer);
  85. layer output_layer = *(l.output_layer);
  86. fill_cpu(l.outputs * l.batch * l.steps, 0, output_layer.delta, 1);
  87. fill_cpu(l.hidden * l.batch * l.steps, 0, self_layer.delta, 1);
  88. fill_cpu(l.hidden * l.batch * l.steps, 0, input_layer.delta, 1);
  89. if (net.train) fill_cpu(l.hidden * l.batch, 0, l.state, 1);
  90. for (i = 0; i < l.steps; ++i) {
  91. s.input = net.input;
  92. forward_convolutional_layer(input_layer, s);
  93. s.input = l.state;
  94. forward_convolutional_layer(self_layer, s);
  95. float *old_state = l.state;
  96. if (net.train) l.state += l.hidden*l.batch;
  97. if (l.shortcut){
  98. copy_cpu(l.hidden * l.batch, old_state, 1, l.state, 1);
  99. }
  100. else{
  101. fill_cpu(l.hidden * l.batch, 0, l.state, 1);
  102. }
  103. axpy_cpu(l.hidden * l.batch, 1, input_layer.output, 1, l.state, 1);
  104. axpy_cpu(l.hidden * l.batch, 1, self_layer.output, 1, l.state, 1);
  105. s.input = l.state;
  106. forward_convolutional_layer(output_layer, s);
  107. net.input += l.inputs*l.batch;
  108. increment_layer(&input_layer, 1);
  109. increment_layer(&self_layer, 1);
  110. increment_layer(&output_layer, 1);
  111. }
  112. }
  113. void backward_crnn_layer(layer l, network net)
  114. {
  115. network s = net;
  116. int i;
  117. layer input_layer = *(l.input_layer);
  118. layer self_layer = *(l.self_layer);
  119. layer output_layer = *(l.output_layer);
  120. increment_layer(&input_layer, l.steps - 1);
  121. increment_layer(&self_layer, l.steps - 1);
  122. increment_layer(&output_layer, l.steps - 1);
  123. l.state += l.hidden*l.batch*l.steps;
  124. for (i = l.steps - 1; i >= 0; --i) {
  125. copy_cpu(l.hidden * l.batch, input_layer.output, 1, l.state, 1);
  126. axpy_cpu(l.hidden * l.batch, 1, self_layer.output, 1, l.state, 1);
  127. s.input = l.state;
  128. s.delta = self_layer.delta;
  129. backward_convolutional_layer(output_layer, s);
  130. l.state -= l.hidden*l.batch;
  131. /*
  132. if(i > 0){
  133. copy_cpu(l.hidden * l.batch, input_layer.output - l.hidden*l.batch, 1, l.state, 1);
  134. axpy_cpu(l.hidden * l.batch, 1, self_layer.output - l.hidden*l.batch, 1, l.state, 1);
  135. }else{
  136. fill_cpu(l.hidden * l.batch, 0, l.state, 1);
  137. }
  138. */
  139. s.input = l.state;
  140. s.delta = self_layer.delta - l.hidden*l.batch;
  141. if (i == 0) s.delta = 0;
  142. backward_convolutional_layer(self_layer, s);
  143. copy_cpu(l.hidden*l.batch, self_layer.delta, 1, input_layer.delta, 1);
  144. if (i > 0 && l.shortcut) axpy_cpu(l.hidden*l.batch, 1, self_layer.delta, 1, self_layer.delta - l.hidden*l.batch, 1);
  145. s.input = net.input + i*l.inputs*l.batch;
  146. if (net.delta) s.delta = net.delta + i*l.inputs*l.batch;
  147. else s.delta = 0;
  148. backward_convolutional_layer(input_layer, s);
  149. increment_layer(&input_layer, -1);
  150. increment_layer(&self_layer, -1);
  151. increment_layer(&output_layer, -1);
  152. }
  153. }
  154. #ifdef GPU
  155. void pull_crnn_layer(layer l)
  156. {
  157. pull_convolutional_layer(*(l.input_layer));
  158. pull_convolutional_layer(*(l.self_layer));
  159. pull_convolutional_layer(*(l.output_layer));
  160. }
  161. void push_crnn_layer(layer l)
  162. {
  163. push_convolutional_layer(*(l.input_layer));
  164. push_convolutional_layer(*(l.self_layer));
  165. push_convolutional_layer(*(l.output_layer));
  166. }
  167. void update_crnn_layer_gpu(layer l, update_args a)
  168. {
  169. update_convolutional_layer_gpu(*(l.input_layer), a);
  170. update_convolutional_layer_gpu(*(l.self_layer), a);
  171. update_convolutional_layer_gpu(*(l.output_layer), a);
  172. }
  173. void forward_crnn_layer_gpu(layer l, network net)
  174. {
  175. network s = net;
  176. int i;
  177. layer input_layer = *(l.input_layer);
  178. layer self_layer = *(l.self_layer);
  179. layer output_layer = *(l.output_layer);
  180. fill_gpu(l.outputs * l.batch * l.steps, 0, output_layer.delta_gpu, 1);
  181. fill_gpu(l.hidden * l.batch * l.steps, 0, self_layer.delta_gpu, 1);
  182. fill_gpu(l.hidden * l.batch * l.steps, 0, input_layer.delta_gpu, 1);
  183. if(net.train) fill_gpu(l.hidden * l.batch, 0, l.state_gpu, 1);
  184. for (i = 0; i < l.steps; ++i) {
  185. s.input_gpu = net.input_gpu;
  186. forward_convolutional_layer_gpu(input_layer, s);
  187. s.input_gpu = l.state_gpu;
  188. forward_convolutional_layer_gpu(self_layer, s);
  189. float *old_state = l.state_gpu;
  190. if(net.train) l.state_gpu += l.hidden*l.batch;
  191. if(l.shortcut){
  192. copy_gpu(l.hidden * l.batch, old_state, 1, l.state_gpu, 1);
  193. }else{
  194. fill_gpu(l.hidden * l.batch, 0, l.state_gpu, 1);
  195. }
  196. axpy_gpu(l.hidden * l.batch, 1, input_layer.output_gpu, 1, l.state_gpu, 1);
  197. axpy_gpu(l.hidden * l.batch, 1, self_layer.output_gpu, 1, l.state_gpu, 1);
  198. s.input_gpu = l.state_gpu;
  199. forward_convolutional_layer_gpu(output_layer, s);
  200. net.input_gpu += l.inputs*l.batch;
  201. increment_layer(&input_layer, 1);
  202. increment_layer(&self_layer, 1);
  203. increment_layer(&output_layer, 1);
  204. }
  205. }
  206. void backward_crnn_layer_gpu(layer l, network net)
  207. {
  208. network s = net;
  209. s.train = net.train;
  210. int i;
  211. layer input_layer = *(l.input_layer);
  212. layer self_layer = *(l.self_layer);
  213. layer output_layer = *(l.output_layer);
  214. increment_layer(&input_layer, l.steps - 1);
  215. increment_layer(&self_layer, l.steps - 1);
  216. increment_layer(&output_layer, l.steps - 1);
  217. l.state_gpu += l.hidden*l.batch*l.steps;
  218. for (i = l.steps-1; i >= 0; --i) {
  219. copy_gpu(l.hidden * l.batch, input_layer.output_gpu, 1, l.state_gpu, 1);
  220. axpy_gpu(l.hidden * l.batch, 1, self_layer.output_gpu, 1, l.state_gpu, 1);
  221. s.input_gpu = l.state_gpu;
  222. s.delta_gpu = self_layer.delta_gpu;
  223. backward_convolutional_layer_gpu(output_layer, s);
  224. l.state_gpu -= l.hidden*l.batch;
  225. s.input_gpu = l.state_gpu;
  226. s.delta_gpu = self_layer.delta_gpu - l.hidden*l.batch;
  227. if (i == 0) s.delta_gpu = 0;
  228. backward_convolutional_layer_gpu(self_layer, s);
  229. copy_gpu(l.hidden*l.batch, self_layer.delta_gpu, 1, input_layer.delta_gpu, 1);
  230. if (i > 0 && l.shortcut) axpy_gpu(l.hidden*l.batch, 1, self_layer.delta_gpu, 1, self_layer.delta_gpu - l.hidden*l.batch, 1);
  231. s.input_gpu = net.input_gpu + i*l.inputs*l.batch;
  232. if(net.delta_gpu) s.delta_gpu = net.delta_gpu + i*l.inputs*l.batch;
  233. else s.delta_gpu = 0;
  234. backward_convolutional_layer_gpu(input_layer, s);
  235. increment_layer(&input_layer, -1);
  236. increment_layer(&self_layer, -1);
  237. increment_layer(&output_layer, -1);
  238. }
  239. }
  240. #endif
  241. };