darknet  v3
shortcut_layer.c
Go to the documentation of this file.
1 #include "shortcut_layer.h"
2 #include "cuda.h"
3 #include "blas.h"
4 #include "activations.h"
5 
6 #include <stdio.h>
7 #include <assert.h>
8 
9 layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2)
10 {
11  fprintf(stderr, "res %3d %4d x%4d x%4d -> %4d x%4d x%4d\n",index, w2,h2,c2, w,h,c);
12  layer l = {0};
13  l.type = SHORTCUT;
14  l.batch = batch;
15  l.w = w2;
16  l.h = h2;
17  l.c = c2;
18  l.out_w = w;
19  l.out_h = h;
20  l.out_c = c;
21  l.outputs = w*h*c;
22  l.inputs = l.outputs;
23 
24  l.index = index;
25 
26  l.delta = calloc(l.outputs*batch, sizeof(float));
27  l.output = calloc(l.outputs*batch, sizeof(float));;
28 
31  #ifdef GPU
32  l.forward_gpu = forward_shortcut_layer_gpu;
33  l.backward_gpu = backward_shortcut_layer_gpu;
34 
35  l.delta_gpu = cuda_make_array(l.delta, l.outputs*batch);
36  l.output_gpu = cuda_make_array(l.output, l.outputs*batch);
37  #endif
38  return l;
39 }
40 
41 void resize_shortcut_layer(layer *l, int w, int h)
42 {
43  assert(l->w == l->out_w);
44  assert(l->h == l->out_h);
45  l->w = l->out_w = w;
46  l->h = l->out_h = h;
47  l->outputs = w*h*l->out_c;
48  l->inputs = l->outputs;
49  l->delta = realloc(l->delta, l->outputs*l->batch*sizeof(float));
50  l->output = realloc(l->output, l->outputs*l->batch*sizeof(float));
51 
52 #ifdef GPU
53  cuda_free(l->output_gpu);
54  cuda_free(l->delta_gpu);
55  l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch);
56  l->delta_gpu = cuda_make_array(l->delta, l->outputs*l->batch);
57 #endif
58 
59 }
60 
61 
63 {
64  copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1);
65  shortcut_cpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output, l.out_w, l.out_h, l.out_c, l.alpha, l.beta, l.output);
67 }
68 
70 {
72  axpy_cpu(l.outputs*l.batch, l.alpha, l.delta, 1, net.delta, 1);
73  shortcut_cpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta, l.w, l.h, l.c, 1, l.beta, net.layers[l.index].delta);
74 }
75 
76 #ifdef GPU
77 void forward_shortcut_layer_gpu(const layer l, network net)
78 {
79  copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1);
80  shortcut_gpu(l.batch, l.w, l.h, l.c, net.layers[l.index].output_gpu, l.out_w, l.out_h, l.out_c, l.alpha, l.beta, l.output_gpu);
81  activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation);
82 }
83 
84 void backward_shortcut_layer_gpu(const layer l, network net)
85 {
86  gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu);
87  axpy_gpu(l.outputs*l.batch, l.alpha, l.delta_gpu, 1, net.delta_gpu, 1);
88  shortcut_gpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta_gpu, l.w, l.h, l.c, 1, l.beta, net.layers[l.index].delta_gpu);
89 }
90 #endif
ACTIVATION activation
Definition: darknet.h:121
int w
Definition: darknet.h:140
void backward_shortcut_layer(const layer l, network net)
void resize_shortcut_layer(layer *l, int w, int h)
void(* forward_gpu)(struct layer, struct network)
Definition: darknet.h:126
void(* backward_gpu)(struct layer, struct network)
Definition: darknet.h:127
void axpy_gpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
void(* forward)(struct layer, struct network)
Definition: darknet.h:123
int out_w
Definition: darknet.h:141
void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta)
Definition: activations.c:143
float * delta
Definition: darknet.h:486
int out_c
Definition: darknet.h:141
void shortcut_cpu(int batch, int w1, int h1, int c1, float *add, int w2, int h2, int c2, float s1, float s2, float *out)
Definition: blas.c:68
layer * layers
Definition: darknet.h:441
int h
Definition: darknet.h:140
float * delta
Definition: darknet.h:245
int out_h
Definition: darknet.h:141
float beta
Definition: darknet.h:186
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
int batch
Definition: darknet.h:131
float * output
Definition: darknet.h:246
void copy_gpu(int N, float *X, int INCX, float *Y, int INCY)
void forward_shortcut_layer(const layer l, network net)
void shortcut_gpu(int batch, int w1, int h1, int c1, float *add, int w2, int h2, int c2, float s1, float s2, float *out)
float alpha
Definition: darknet.h:185
int c
Definition: darknet.h:140
void activate_array(float *x, const int n, const ACTIVATION a)
Definition: activations.c:100
void copy_cpu(int N, float *X, int INCX, float *Y, int INCY)
Definition: blas.c:226
LAYER_TYPE type
Definition: darknet.h:120
float * input
Definition: darknet.h:484
void activate_array_gpu(float *x, int n, ACTIVATION a)
int outputs
Definition: darknet.h:135
layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2)
Definition: shortcut_layer.c:9
int index
Definition: darknet.h:154
void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta)
Definition: darknet.h:119