darknet  v3
cost_layer.c
Go to the documentation of this file.
1 #include "cost_layer.h"
2 #include "utils.h"
3 #include "cuda.h"
4 #include "blas.h"
5 #include <math.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 
11 {
12  if (strcmp(s, "seg")==0) return SEG;
13  if (strcmp(s, "sse")==0) return SSE;
14  if (strcmp(s, "masked")==0) return MASKED;
15  if (strcmp(s, "smooth")==0) return SMOOTH;
16  if (strcmp(s, "L1")==0) return L1;
17  if (strcmp(s, "wgan")==0) return WGAN;
18  fprintf(stderr, "Couldn't find cost type %s, going with SSE\n", s);
19  return SSE;
20 }
21 
23 {
24  switch(a){
25  case SEG:
26  return "seg";
27  case SSE:
28  return "sse";
29  case MASKED:
30  return "masked";
31  case SMOOTH:
32  return "smooth";
33  case L1:
34  return "L1";
35  case WGAN:
36  return "wgan";
37  }
38  return "sse";
39 }
40 
41 cost_layer make_cost_layer(int batch, int inputs, COST_TYPE cost_type, float scale)
42 {
43  fprintf(stderr, "cost %4d\n", inputs);
44  cost_layer l = {0};
45  l.type = COST;
46 
47  l.scale = scale;
48  l.batch = batch;
49  l.inputs = inputs;
50  l.outputs = inputs;
51  l.cost_type = cost_type;
52  l.delta = calloc(inputs*batch, sizeof(float));
53  l.output = calloc(inputs*batch, sizeof(float));
54  l.cost = calloc(1, sizeof(float));
55 
58  #ifdef GPU
59  l.forward_gpu = forward_cost_layer_gpu;
60  l.backward_gpu = backward_cost_layer_gpu;
61 
62  l.delta_gpu = cuda_make_array(l.output, inputs*batch);
63  l.output_gpu = cuda_make_array(l.delta, inputs*batch);
64  #endif
65  return l;
66 }
67 
68 void resize_cost_layer(cost_layer *l, int inputs)
69 {
70  l->inputs = inputs;
71  l->outputs = inputs;
72  l->delta = realloc(l->delta, inputs*l->batch*sizeof(float));
73  l->output = realloc(l->output, inputs*l->batch*sizeof(float));
74 #ifdef GPU
75  cuda_free(l->delta_gpu);
76  cuda_free(l->output_gpu);
77  l->delta_gpu = cuda_make_array(l->delta, inputs*l->batch);
78  l->output_gpu = cuda_make_array(l->output, inputs*l->batch);
79 #endif
80 }
81 
83 {
84  if (!net.truth) return;
85  if(l.cost_type == MASKED){
86  int i;
87  for(i = 0; i < l.batch*l.inputs; ++i){
88  if(net.truth[i] == SECRET_NUM) net.input[i] = SECRET_NUM;
89  }
90  }
91  if(l.cost_type == SMOOTH){
92  smooth_l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
93  }else if(l.cost_type == L1){
94  l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
95  } else {
96  l2_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
97  }
98  l.cost[0] = sum_array(l.output, l.batch*l.inputs);
99 }
100 
102 {
103  axpy_cpu(l.batch*l.inputs, l.scale, l.delta, 1, net.delta, 1);
104 }
105 
106 #ifdef GPU
107 
108 void pull_cost_layer(cost_layer l)
109 {
110  cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
111 }
112 
113 void push_cost_layer(cost_layer l)
114 {
115  cuda_push_array(l.delta_gpu, l.delta, l.batch*l.inputs);
116 }
117 
118 int float_abs_compare (const void * a, const void * b)
119 {
120  float fa = *(const float*) a;
121  if(fa < 0) fa = -fa;
122  float fb = *(const float*) b;
123  if(fb < 0) fb = -fb;
124  return (fa > fb) - (fa < fb);
125 }
126 
127 void forward_cost_layer_gpu(cost_layer l, network net)
128 {
129  if (!net.truth) return;
130  if(l.smooth){
131  scal_gpu(l.batch*l.inputs, (1-l.smooth), net.truth_gpu, 1);
132  add_gpu(l.batch*l.inputs, l.smooth * 1./l.inputs, net.truth_gpu, 1);
133  }
134 
135  if(l.cost_type == SMOOTH){
136  smooth_l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
137  } else if (l.cost_type == L1){
138  l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
139  } else if (l.cost_type == WGAN){
140  wgan_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
141  } else {
142  l2_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
143  }
144 
145  if (l.cost_type == SEG && l.noobject_scale != 1) {
146  scale_mask_gpu(l.batch*l.inputs, l.delta_gpu, 0, net.truth_gpu, l.noobject_scale);
147  scale_mask_gpu(l.batch*l.inputs, l.output_gpu, 0, net.truth_gpu, l.noobject_scale);
148  }
149  if (l.cost_type == MASKED) {
150  mask_gpu(l.batch*l.inputs, net.delta_gpu, SECRET_NUM, net.truth_gpu, 0);
151  }
152 
153  if(l.ratio){
154  cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
155  qsort(l.delta, l.batch*l.inputs, sizeof(float), float_abs_compare);
156  int n = (1-l.ratio) * l.batch*l.inputs;
157  float thresh = l.delta[n];
158  thresh = 0;
159  printf("%f\n", thresh);
160  supp_gpu(l.batch*l.inputs, thresh, l.delta_gpu, 1);
161  }
162 
163  if(l.thresh){
164  supp_gpu(l.batch*l.inputs, l.thresh*1./l.inputs, l.delta_gpu, 1);
165  }
166 
167  cuda_pull_array(l.output_gpu, l.output, l.batch*l.inputs);
168  l.cost[0] = sum_array(l.output, l.batch*l.inputs);
169 }
170 
171 void backward_cost_layer_gpu(const cost_layer l, network net)
172 {
173  axpy_gpu(l.batch*l.inputs, l.scale, l.delta_gpu, 1, net.delta_gpu, 1);
174 }
175 #endif
176 
float thresh
Definition: darknet.h:198
void backward_cost_layer(const cost_layer l, network net)
Definition: cost_layer.c:101
float scale
Definition: darknet.h:212
void(* forward_gpu)(struct layer, struct network)
Definition: darknet.h:126
void l2_cpu(int n, float *pred, float *truth, float *delta, float *error)
Definition: blas.c:287
float * truth
Definition: darknet.h:485
float smooth
Definition: darknet.h:160
Definition: darknet.h:98
void(* backward_gpu)(struct layer, struct network)
Definition: darknet.h:127
Definition: darknet.h:98
void axpy_gpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
char * get_cost_string(COST_TYPE a)
Definition: cost_layer.c:22
void smooth_l1_cpu(int n, float *pred, float *truth, float *delta, float *error)
Definition: blas.c:238
void(* forward)(struct layer, struct network)
Definition: darknet.h:123
float * delta
Definition: darknet.h:486
void supp_gpu(int N, float ALPHA, float *X, int INCX)
Definition: darknet.h:74
Definition: darknet.h:98
Definition: darknet.h:98
void smooth_l1_gpu(int n, float *pred, float *truth, float *delta, float *error)
void scale_mask_gpu(int N, float *X, float mask_num, float *mask, float scale)
void scal_gpu(int N, float ALPHA, float *X, int INCX)
float * delta
Definition: darknet.h:245
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
void resize_cost_layer(cost_layer *l, int inputs)
Definition: cost_layer.c:68
void l1_cpu(int n, float *pred, float *truth, float *delta, float *error)
Definition: blas.c:255
int batch
Definition: darknet.h:131
float * output
Definition: darknet.h:246
void add_gpu(int N, float ALPHA, float *X, int INCX)
float sum_array(float *a, int n)
Definition: utils.c:479
void l1_gpu(int n, float *pred, float *truth, float *delta, float *error)
cost_layer make_cost_layer(int batch, int inputs, COST_TYPE cost_type, float scale)
Definition: cost_layer.c:41
COST_TYPE
Definition: darknet.h:97
void wgan_gpu(int n, float *pred, float *truth, float *delta, float *error)
void mask_gpu(int N, float *X, float mask_num, float *mask, float val)
Definition: darknet.h:98
COST_TYPE get_cost_type(char *s)
Definition: cost_layer.c:10
Definition: darknet.h:98
LAYER_TYPE type
Definition: darknet.h:120
#define SECRET_NUM
Definition: darknet.h:8
float * input
Definition: darknet.h:484
void l2_gpu(int n, float *pred, float *truth, float *delta, float *error)
int outputs
Definition: darknet.h:135
void forward_cost_layer(cost_layer l, network net)
Definition: cost_layer.c:82
COST_TYPE cost_type
Definition: darknet.h:122
float ratio
Definition: darknet.h:167
float * cost
Definition: darknet.h:222
float noobject_scale
Definition: darknet.h:191
Definition: darknet.h:119