darknet  v3
crnn_layer.c
Go to the documentation of this file.
1 #include "crnn_layer.h"
2 #include "convolutional_layer.h"
3 #include "utils.h"
4 #include "cuda.h"
5 #include "blas.h"
6 #include "gemm.h"
7 
8 #include <math.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 static void increment_layer(layer *l, int steps)
14 {
15  int num = l->outputs*l->batch*steps;
16  l->output += num;
17  l->delta += num;
18  l->x += num;
19  l->x_norm += num;
20 
21 #ifdef GPU
22  l->output_gpu += num;
23  l->delta_gpu += num;
24  l->x_gpu += num;
25  l->x_norm_gpu += num;
26 #endif
27 }
28 
29 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)
30 {
31  fprintf(stderr, "CRNN Layer: %d x %d x %d image, %d filters\n", h,w,c,output_filters);
32  batch = batch / steps;
33  layer l = {0};
34  l.batch = batch;
35  l.type = CRNN;
36  l.steps = steps;
37  l.h = h;
38  l.w = w;
39  l.c = c;
40  l.out_h = h;
41  l.out_w = w;
42  l.out_c = output_filters;
43  l.inputs = h*w*c;
44  l.hidden = h * w * hidden_filters;
45  l.outputs = l.out_h * l.out_w * l.out_c;
46 
47  l.state = calloc(l.hidden*batch*(steps+1), sizeof(float));
48 
49  l.input_layer = malloc(sizeof(layer));
50  fprintf(stderr, "\t\t");
51  *(l.input_layer) = make_convolutional_layer(batch*steps, h, w, c, hidden_filters, 1, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
52  l.input_layer->batch = batch;
53 
54  l.self_layer = malloc(sizeof(layer));
55  fprintf(stderr, "\t\t");
56  *(l.self_layer) = make_convolutional_layer(batch*steps, h, w, hidden_filters, hidden_filters, 1, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
57  l.self_layer->batch = batch;
58 
59  l.output_layer = malloc(sizeof(layer));
60  fprintf(stderr, "\t\t");
61  *(l.output_layer) = make_convolutional_layer(batch*steps, h, w, hidden_filters, output_filters, 1, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
62  l.output_layer->batch = batch;
63 
64  l.output = l.output_layer->output;
65  l.delta = l.output_layer->delta;
66 
70 
71 #ifdef GPU
72  l.forward_gpu = forward_crnn_layer_gpu;
73  l.backward_gpu = backward_crnn_layer_gpu;
74  l.update_gpu = update_crnn_layer_gpu;
75 
76  l.state_gpu = cuda_make_array(l.state, l.hidden*batch*(steps+1));
77  l.output_gpu = l.output_layer->output_gpu;
78  l.delta_gpu = l.output_layer->delta_gpu;
79 #endif
80 
81  return l;
82 }
83 
85 {
89 }
90 
92 {
93  network s = net;
94  s.train = net.train;
95  int i;
96  layer input_layer = *(l.input_layer);
97  layer self_layer = *(l.self_layer);
98  layer output_layer = *(l.output_layer);
99 
100  fill_cpu(l.outputs * l.batch * l.steps, 0, output_layer.delta, 1);
101  fill_cpu(l.hidden * l.batch * l.steps, 0, self_layer.delta, 1);
102  fill_cpu(l.hidden * l.batch * l.steps, 0, input_layer.delta, 1);
103  if(net.train) fill_cpu(l.hidden * l.batch, 0, l.state, 1);
104 
105  for (i = 0; i < l.steps; ++i) {
106  s.input = net.input;
107  forward_convolutional_layer(input_layer, s);
108 
109  s.input = l.state;
110  forward_convolutional_layer(self_layer, s);
111 
112  float *old_state = l.state;
113  if(net.train) l.state += l.hidden*l.batch;
114  if(l.shortcut){
115  copy_cpu(l.hidden * l.batch, old_state, 1, l.state, 1);
116  }else{
117  fill_cpu(l.hidden * l.batch, 0, l.state, 1);
118  }
119  axpy_cpu(l.hidden * l.batch, 1, input_layer.output, 1, l.state, 1);
120  axpy_cpu(l.hidden * l.batch, 1, self_layer.output, 1, l.state, 1);
121 
122  s.input = l.state;
123  forward_convolutional_layer(output_layer, s);
124 
125  net.input += l.inputs*l.batch;
126  increment_layer(&input_layer, 1);
127  increment_layer(&self_layer, 1);
128  increment_layer(&output_layer, 1);
129  }
130 }
131 
133 {
134  network s = net;
135  int i;
136  layer input_layer = *(l.input_layer);
137  layer self_layer = *(l.self_layer);
138  layer output_layer = *(l.output_layer);
139 
140  increment_layer(&input_layer, l.steps-1);
141  increment_layer(&self_layer, l.steps-1);
142  increment_layer(&output_layer, l.steps-1);
143 
144  l.state += l.hidden*l.batch*l.steps;
145  for (i = l.steps-1; i >= 0; --i) {
146  copy_cpu(l.hidden * l.batch, input_layer.output, 1, l.state, 1);
147  axpy_cpu(l.hidden * l.batch, 1, self_layer.output, 1, l.state, 1);
148 
149  s.input = l.state;
150  s.delta = self_layer.delta;
151  backward_convolutional_layer(output_layer, s);
152 
153  l.state -= l.hidden*l.batch;
154  /*
155  if(i > 0){
156  copy_cpu(l.hidden * l.batch, input_layer.output - l.hidden*l.batch, 1, l.state, 1);
157  axpy_cpu(l.hidden * l.batch, 1, self_layer.output - l.hidden*l.batch, 1, l.state, 1);
158  }else{
159  fill_cpu(l.hidden * l.batch, 0, l.state, 1);
160  }
161  */
162 
163  s.input = l.state;
164  s.delta = self_layer.delta - l.hidden*l.batch;
165  if (i == 0) s.delta = 0;
166  backward_convolutional_layer(self_layer, s);
167 
168  copy_cpu(l.hidden*l.batch, self_layer.delta, 1, input_layer.delta, 1);
169  if (i > 0 && l.shortcut) axpy_cpu(l.hidden*l.batch, 1, self_layer.delta, 1, self_layer.delta - l.hidden*l.batch, 1);
170  s.input = net.input + i*l.inputs*l.batch;
171  if(net.delta) s.delta = net.delta + i*l.inputs*l.batch;
172  else s.delta = 0;
173  backward_convolutional_layer(input_layer, s);
174 
175  increment_layer(&input_layer, -1);
176  increment_layer(&self_layer, -1);
177  increment_layer(&output_layer, -1);
178  }
179 }
180 
181 #ifdef GPU
182 
183 void pull_crnn_layer(layer l)
184 {
188 }
189 
190 void push_crnn_layer(layer l)
191 {
195 }
196 
197 void update_crnn_layer_gpu(layer l, update_args a)
198 {
202 }
203 
204 void forward_crnn_layer_gpu(layer l, network net)
205 {
206  network s = net;
207  int i;
208  layer input_layer = *(l.input_layer);
209  layer self_layer = *(l.self_layer);
210  layer output_layer = *(l.output_layer);
211 
212  fill_gpu(l.outputs * l.batch * l.steps, 0, output_layer.delta_gpu, 1);
213  fill_gpu(l.hidden * l.batch * l.steps, 0, self_layer.delta_gpu, 1);
214  fill_gpu(l.hidden * l.batch * l.steps, 0, input_layer.delta_gpu, 1);
215  if(net.train) fill_gpu(l.hidden * l.batch, 0, l.state_gpu, 1);
216 
217  for (i = 0; i < l.steps; ++i) {
218  s.input_gpu = net.input_gpu;
219  forward_convolutional_layer_gpu(input_layer, s);
220 
221  s.input_gpu = l.state_gpu;
222  forward_convolutional_layer_gpu(self_layer, s);
223 
224  float *old_state = l.state_gpu;
225  if(net.train) l.state_gpu += l.hidden*l.batch;
226  if(l.shortcut){
227  copy_gpu(l.hidden * l.batch, old_state, 1, l.state_gpu, 1);
228  }else{
229  fill_gpu(l.hidden * l.batch, 0, l.state_gpu, 1);
230  }
231  axpy_gpu(l.hidden * l.batch, 1, input_layer.output_gpu, 1, l.state_gpu, 1);
232  axpy_gpu(l.hidden * l.batch, 1, self_layer.output_gpu, 1, l.state_gpu, 1);
233 
234  s.input_gpu = l.state_gpu;
235  forward_convolutional_layer_gpu(output_layer, s);
236 
237  net.input_gpu += l.inputs*l.batch;
238  increment_layer(&input_layer, 1);
239  increment_layer(&self_layer, 1);
240  increment_layer(&output_layer, 1);
241  }
242 }
243 
244 void backward_crnn_layer_gpu(layer l, network net)
245 {
246  network s = net;
247  s.train = net.train;
248  int i;
249  layer input_layer = *(l.input_layer);
250  layer self_layer = *(l.self_layer);
251  layer output_layer = *(l.output_layer);
252  increment_layer(&input_layer, l.steps - 1);
253  increment_layer(&self_layer, l.steps - 1);
254  increment_layer(&output_layer, l.steps - 1);
255  l.state_gpu += l.hidden*l.batch*l.steps;
256  for (i = l.steps-1; i >= 0; --i) {
257  copy_gpu(l.hidden * l.batch, input_layer.output_gpu, 1, l.state_gpu, 1);
258  axpy_gpu(l.hidden * l.batch, 1, self_layer.output_gpu, 1, l.state_gpu, 1);
259 
260  s.input_gpu = l.state_gpu;
261  s.delta_gpu = self_layer.delta_gpu;
262  backward_convolutional_layer_gpu(output_layer, s);
263 
264  l.state_gpu -= l.hidden*l.batch;
265 
266  s.input_gpu = l.state_gpu;
267  s.delta_gpu = self_layer.delta_gpu - l.hidden*l.batch;
268  if (i == 0) s.delta_gpu = 0;
269  backward_convolutional_layer_gpu(self_layer, s);
270 
271  copy_gpu(l.hidden*l.batch, self_layer.delta_gpu, 1, input_layer.delta_gpu, 1);
272  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);
273  s.input_gpu = net.input_gpu + i*l.inputs*l.batch;
274  if(net.delta_gpu) s.delta_gpu = net.delta_gpu + i*l.inputs*l.batch;
275  else s.delta_gpu = 0;
276  backward_convolutional_layer_gpu(input_layer, s);
277 
278  increment_layer(&input_layer, -1);
279  increment_layer(&self_layer, -1);
280  increment_layer(&output_layer, -1);
281  }
282 }
283 #endif
int steps
Definition: darknet.h:157
ACTIVATION
Definition: darknet.h:56
void forward_convolutional_layer_gpu(convolutional_layer l, network net)
int w
Definition: darknet.h:140
struct layer * output_layer
Definition: darknet.h:297
void(* update)(struct layer, update_args)
Definition: darknet.h:125
void(* forward_gpu)(struct layer, struct network)
Definition: darknet.h:126
void(* backward_gpu)(struct layer, struct network)
Definition: darknet.h:127
float * x
Definition: darknet.h:261
void axpy_gpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
void(* update_gpu)(struct layer, update_args)
Definition: darknet.h:128
void pull_convolutional_layer(layer l)
void(* forward)(struct layer, struct network)
Definition: darknet.h:123
int out_w
Definition: darknet.h:141
convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int groups, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam)
float * delta
Definition: darknet.h:486
int out_c
Definition: darknet.h:141
void fill_gpu(int N, float ALPHA, float *X, int INCX)
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)
Definition: crnn_layer.c:29
void fill_cpu(int N, float ALPHA, float *X, int INCX)
Definition: blas.c:190
float * state
Definition: darknet.h:223
int train
Definition: darknet.h:488
int shortcut
Definition: darknet.h:130
int h
Definition: darknet.h:140
float * delta
Definition: darknet.h:245
int out_h
Definition: darknet.h:141
int inputs
Definition: darknet.h:134
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
Definition: blas.c:178
void(* backward)(struct layer, struct network)
Definition: darknet.h:124
float * x_norm
Definition: darknet.h:262
void backward_convolutional_layer_gpu(convolutional_layer l, network net)
void forward_convolutional_layer(convolutional_layer l, network net)
int batch
Definition: darknet.h:131
float * output
Definition: darknet.h:246
struct layer * input_layer
Definition: darknet.h:295
void backward_convolutional_layer(convolutional_layer l, network net)
void forward_crnn_layer(layer l, network net)
Definition: crnn_layer.c:91
void copy_gpu(int N, float *X, int INCX, float *Y, int INCY)
Definition: darknet.h:83
void update_crnn_layer(layer l, update_args a)
Definition: crnn_layer.c:84
void push_convolutional_layer(layer l)
int hidden
Definition: darknet.h:158
int c
Definition: darknet.h:140
void copy_cpu(int N, float *X, int INCX, float *Y, int INCY)
Definition: blas.c:226
void update_convolutional_layer(convolutional_layer l, update_args a)
void update_convolutional_layer_gpu(layer l, update_args a)
LAYER_TYPE type
Definition: darknet.h:120
float * input
Definition: darknet.h:484
void backward_crnn_layer(layer l, network net)
Definition: crnn_layer.c:132
int outputs
Definition: darknet.h:135
struct layer * self_layer
Definition: darknet.h:296
Definition: darknet.h:119