darknet  v3
upsample_layer.c
Go to the documentation of this file.
1 #include "upsample_layer.h"
2 #include "cuda.h"
3 #include "blas.h"
4 
5 #include <stdio.h>
6 
7 layer make_upsample_layer(int batch, int w, int h, int c, int stride)
8 {
9  layer l = {0};
10  l.type = UPSAMPLE;
11  l.batch = batch;
12  l.w = w;
13  l.h = h;
14  l.c = c;
15  l.out_w = w*stride;
16  l.out_h = h*stride;
17  l.out_c = c;
18  if(stride < 0){
19  stride = -stride;
20  l.reverse=1;
21  l.out_w = w/stride;
22  l.out_h = h/stride;
23  }
24  l.stride = stride;
25  l.outputs = l.out_w*l.out_h*l.out_c;
26  l.inputs = l.w*l.h*l.c;
27  l.delta = calloc(l.outputs*batch, sizeof(float));
28  l.output = calloc(l.outputs*batch, sizeof(float));;
29 
32  #ifdef GPU
33  l.forward_gpu = forward_upsample_layer_gpu;
34  l.backward_gpu = backward_upsample_layer_gpu;
35 
36  l.delta_gpu = cuda_make_array(l.delta, l.outputs*batch);
37  l.output_gpu = cuda_make_array(l.output, l.outputs*batch);
38  #endif
39  if(l.reverse) fprintf(stderr, "downsample %2dx %4d x%4d x%4d -> %4d x%4d x%4d\n", stride, w, h, c, l.out_w, l.out_h, l.out_c);
40  else fprintf(stderr, "upsample %2dx %4d x%4d x%4d -> %4d x%4d x%4d\n", stride, w, h, c, l.out_w, l.out_h, l.out_c);
41  return l;
42 }
43 
44 void resize_upsample_layer(layer *l, int w, int h)
45 {
46  l->w = w;
47  l->h = h;
48  l->out_w = w*l->stride;
49  l->out_h = h*l->stride;
50  if(l->reverse){
51  l->out_w = w/l->stride;
52  l->out_h = h/l->stride;
53  }
54  l->outputs = l->out_w*l->out_h*l->out_c;
55  l->inputs = l->h*l->w*l->c;
56  l->delta = realloc(l->delta, l->outputs*l->batch*sizeof(float));
57  l->output = realloc(l->output, l->outputs*l->batch*sizeof(float));
58 
59 #ifdef GPU
60  cuda_free(l->output_gpu);
61  cuda_free(l->delta_gpu);
62  l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch);
63  l->delta_gpu = cuda_make_array(l->delta, l->outputs*l->batch);
64 #endif
65 
66 }
67 
69 {
70  fill_cpu(l.outputs*l.batch, 0, l.output, 1);
71  if(l.reverse){
72  upsample_cpu(l.output, l.out_w, l.out_h, l.c, l.batch, l.stride, 0, l.scale, net.input);
73  }else{
74  upsample_cpu(net.input, l.w, l.h, l.c, l.batch, l.stride, 1, l.scale, l.output);
75  }
76 }
77 
79 {
80  if(l.reverse){
81  upsample_cpu(l.delta, l.out_w, l.out_h, l.c, l.batch, l.stride, 1, l.scale, net.delta);
82  }else{
83  upsample_cpu(net.delta, l.w, l.h, l.c, l.batch, l.stride, 0, l.scale, l.delta);
84  }
85 }
86 
87 #ifdef GPU
88 void forward_upsample_layer_gpu(const layer l, network net)
89 {
90  fill_gpu(l.outputs*l.batch, 0, l.output_gpu, 1);
91  if(l.reverse){
92  upsample_gpu(l.output_gpu, l.out_w, l.out_h, l.c, l.batch, l.stride, 0, l.scale, net.input_gpu);
93  }else{
94  upsample_gpu(net.input_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, l.scale, l.output_gpu);
95  }
96 }
97 
98 void backward_upsample_layer_gpu(const layer l, network net)
99 {
100  if(l.reverse){
101  upsample_gpu(l.delta_gpu, l.out_w, l.out_h, l.c, l.batch, l.stride, 1, l.scale, net.delta_gpu);
102  }else{
103  upsample_gpu(net.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, l.scale, l.delta_gpu);
104  }
105 }
106 #endif
void forward_upsample_layer(const layer l, network net)
int w
Definition: darknet.h:140
float scale
Definition: darknet.h:212
void(* forward_gpu)(struct layer, struct network)
Definition: darknet.h:126
void(* backward_gpu)(struct layer, struct network)
Definition: darknet.h:127
void(* forward)(struct layer, struct network)
Definition: darknet.h:123
int out_w
Definition: darknet.h:141
float * delta
Definition: darknet.h:486
int out_c
Definition: darknet.h:141
void upsample_gpu(float *in, int w, int h, int c, int batch, int stride, int forward, float scale, float *out)
void fill_gpu(int N, float ALPHA, float *X, int INCX)
void fill_cpu(int N, float ALPHA, float *X, int INCX)
Definition: blas.c:190
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 backward_upsample_layer(const layer l, network net)
void(* backward)(struct layer, struct network)
Definition: darknet.h:124
int batch
Definition: darknet.h:131
float * output
Definition: darknet.h:246
void upsample_cpu(float *in, int w, int h, int c, int batch, int stride, int forward, float scale, float *out)
Definition: blas.c:334
int stride
Definition: darknet.h:147
int c
Definition: darknet.h:140
void resize_upsample_layer(layer *l, int w, int h)
int reverse
Definition: darknet.h:148
LAYER_TYPE type
Definition: darknet.h:120
float * input
Definition: darknet.h:484
int outputs
Definition: darknet.h:135
layer make_upsample_layer(int batch, int w, int h, int c, int stride)
Definition: upsample_layer.c:7
Definition: darknet.h:119