darknet  v3
iseg_layer.c
Go to the documentation of this file.
1 #include "iseg_layer.h"
2 #include "activations.h"
3 #include "blas.h"
4 #include "box.h"
5 #include "cuda.h"
6 #include "utils.h"
7 
8 #include <stdio.h>
9 #include <assert.h>
10 #include <string.h>
11 #include <stdlib.h>
12 
13 layer make_iseg_layer(int batch, int w, int h, int classes, int ids)
14 {
15  layer l = {0};
16  l.type = ISEG;
17 
18  l.h = h;
19  l.w = w;
20  l.c = classes + ids;
21  l.out_w = l.w;
22  l.out_h = l.h;
23  l.out_c = l.c;
24  l.classes = classes;
25  l.batch = batch;
26  l.extra = ids;
27  l.cost = calloc(1, sizeof(float));
28  l.outputs = h*w*l.c;
29  l.inputs = l.outputs;
30  l.truths = 90*(l.w*l.h+1);
31  l.delta = calloc(batch*l.outputs, sizeof(float));
32  l.output = calloc(batch*l.outputs, sizeof(float));
33 
34  l.counts = calloc(90, sizeof(int));
35  l.sums = calloc(90, sizeof(float*));
36  if(ids){
37  int i;
38  for(i = 0; i < 90; ++i){
39  l.sums[i] = calloc(ids, sizeof(float));
40  }
41  }
42 
45 #ifdef GPU
46  l.forward_gpu = forward_iseg_layer_gpu;
47  l.backward_gpu = backward_iseg_layer_gpu;
48  l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
49  l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
50 #endif
51 
52  fprintf(stderr, "iseg\n");
53  srand(0);
54 
55  return l;
56 }
57 
58 void resize_iseg_layer(layer *l, int w, int h)
59 {
60  l->w = w;
61  l->h = h;
62 
63  l->outputs = h*w*l->c;
64  l->inputs = l->outputs;
65 
66  l->output = realloc(l->output, l->batch*l->outputs*sizeof(float));
67  l->delta = realloc(l->delta, l->batch*l->outputs*sizeof(float));
68 
69 #ifdef GPU
70  cuda_free(l->delta_gpu);
71  cuda_free(l->output_gpu);
72 
73  l->delta_gpu = cuda_make_array(l->delta, l->batch*l->outputs);
74  l->output_gpu = cuda_make_array(l->output, l->batch*l->outputs);
75 #endif
76 }
77 
79 {
80 
81  double time = what_time_is_it_now();
82  int i,b,j,k;
83  int ids = l.extra;
84  memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float));
85  memset(l.delta, 0, l.outputs * l.batch * sizeof(float));
86 
87 #ifndef GPU
88  for (b = 0; b < l.batch; ++b){
89  int index = b*l.outputs;
90  activate_array(l.output + index, l.classes*l.w*l.h, LOGISTIC);
91  }
92 #endif
93 
94  for (b = 0; b < l.batch; ++b){
95  // a priori, each pixel has no class
96  for(i = 0; i < l.classes; ++i){
97  for(k = 0; k < l.w*l.h; ++k){
98  int index = b*l.outputs + i*l.w*l.h + k;
99  l.delta[index] = 0 - l.output[index];
100  }
101  }
102 
103  // a priori, embedding should be small magnitude
104  for(i = 0; i < ids; ++i){
105  for(k = 0; k < l.w*l.h; ++k){
106  int index = b*l.outputs + (i+l.classes)*l.w*l.h + k;
107  l.delta[index] = .1 * (0 - l.output[index]);
108  }
109  }
110 
111 
112  memset(l.counts, 0, 90*sizeof(int));
113  for(i = 0; i < 90; ++i){
114  fill_cpu(ids, 0, l.sums[i], 1);
115 
116  int c = net.truth[b*l.truths + i*(l.w*l.h+1)];
117  if(c < 0) break;
118  // add up metric embeddings for each instance
119  for(k = 0; k < l.w*l.h; ++k){
120  int index = b*l.outputs + c*l.w*l.h + k;
121  float v = net.truth[b*l.truths + i*(l.w*l.h + 1) + 1 + k];
122  if(v){
123  l.delta[index] = v - l.output[index];
124  axpy_cpu(ids, 1, l.output + b*l.outputs + l.classes*l.w*l.h + k, l.w*l.h, l.sums[i], 1);
125  ++l.counts[i];
126  }
127  }
128  }
129 
130  float *mse = calloc(90, sizeof(float));
131  for(i = 0; i < 90; ++i){
132  int c = net.truth[b*l.truths + i*(l.w*l.h+1)];
133  if(c < 0) break;
134  for(k = 0; k < l.w*l.h; ++k){
135  float v = net.truth[b*l.truths + i*(l.w*l.h + 1) + 1 + k];
136  if(v){
137  int z;
138  float sum = 0;
139  for(z = 0; z < ids; ++z){
140  int index = b*l.outputs + (l.classes + z)*l.w*l.h + k;
141  sum += pow(l.sums[i][z]/l.counts[i] - l.output[index], 2);
142  }
143  mse[i] += sum;
144  }
145  }
146  mse[i] /= l.counts[i];
147  }
148 
149  // Calculate average embedding
150  for(i = 0; i < 90; ++i){
151  if(!l.counts[i]) continue;
152  scal_cpu(ids, 1.f/l.counts[i], l.sums[i], 1);
153  if(b == 0 && net.gpu_index == 0){
154  printf("%4d, %6.3f, ", l.counts[i], mse[i]);
155  for(j = 0; j < ids; ++j){
156  printf("%6.3f,", l.sums[i][j]);
157  }
158  printf("\n");
159  }
160  }
161  free(mse);
162 
163  // Calculate embedding loss
164  for(i = 0; i < 90; ++i){
165  if(!l.counts[i]) continue;
166  for(k = 0; k < l.w*l.h; ++k){
167  float v = net.truth[b*l.truths + i*(l.w*l.h + 1) + 1 + k];
168  if(v){
169  for(j = 0; j < 90; ++j){
170  if(!l.counts[j])continue;
171  int z;
172  for(z = 0; z < ids; ++z){
173  int index = b*l.outputs + (l.classes + z)*l.w*l.h + k;
174  float diff = l.sums[j][z] - l.output[index];
175  if (j == i) l.delta[index] += diff < 0? -.1 : .1;
176  else l.delta[index] += -(diff < 0? -.1 : .1);
177  }
178  }
179  }
180  }
181  }
182 
183  for(i = 0; i < ids; ++i){
184  for(k = 0; k < l.w*l.h; ++k){
185  int index = b*l.outputs + (i+l.classes)*l.w*l.h + k;
186  l.delta[index] *= .01;
187  }
188  }
189  }
190 
191  *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);
192  printf("took %lf sec\n", what_time_is_it_now() - time);
193 }
194 
196 {
197  axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, net.delta, 1);
198 }
199 
200 #ifdef GPU
201 
202 void forward_iseg_layer_gpu(const layer l, network net)
203 {
204  copy_gpu(l.batch*l.inputs, net.input_gpu, 1, l.output_gpu, 1);
205  int b;
206  for (b = 0; b < l.batch; ++b){
207  activate_array_gpu(l.output_gpu + b*l.outputs, l.classes*l.w*l.h, LOGISTIC);
208  //if(l.extra) activate_array_gpu(l.output_gpu + b*l.outputs + l.classes*l.w*l.h, l.extra*l.w*l.h, LOGISTIC);
209  }
210 
211  cuda_pull_array(l.output_gpu, net.input, l.batch*l.inputs);
212  forward_iseg_layer(l, net);
213  cuda_push_array(l.delta_gpu, l.delta, l.batch*l.outputs);
214 }
215 
216 void backward_iseg_layer_gpu(const layer l, network net)
217 {
218  int b;
219  for (b = 0; b < l.batch; ++b){
220  //if(l.extra) gradient_array_gpu(l.output_gpu + b*l.outputs + l.classes*l.w*l.h, l.extra*l.w*l.h, LOGISTIC, l.delta_gpu + b*l.outputs + l.classes*l.w*l.h);
221  }
222  axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1);
223 }
224 #endif
225 
void forward_iseg_layer(const layer l, network net)
Definition: iseg_layer.c:78
int w
Definition: darknet.h:140
int truths
Definition: darknet.h:139
void(* forward_gpu)(struct layer, struct network)
Definition: darknet.h:126
float * truth
Definition: darknet.h:485
void(* backward_gpu)(struct layer, struct network)
Definition: darknet.h:127
int gpu_index
Definition: darknet.h:481
void axpy_gpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
float ** sums
Definition: darknet.h:220
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 resize_iseg_layer(layer *l, int w, int h)
Definition: iseg_layer.c:58
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 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
layer make_iseg_layer(int batch, int w, int h, int classes, int ids)
Definition: iseg_layer.c:13
int batch
Definition: darknet.h:131
float * output
Definition: darknet.h:246
void scal_cpu(int N, float ALPHA, float *X, int INCX)
Definition: blas.c:184
void copy_gpu(int N, float *X, int INCX, float *Y, int INCY)
int c
Definition: darknet.h:140
void activate_array(float *x, const int n, const ACTIVATION a)
Definition: activations.c:100
int classes
Definition: darknet.h:172
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
float mag_array(float *a, int n)
Definition: utils.c:574
int extra
Definition: darknet.h:138
Definition: darknet.h:89
list classes
Definition: voc_label.py:9
double what_time_is_it_now()
Definition: utils.c:27
int * counts
Definition: darknet.h:219
float * cost
Definition: darknet.h:222
Definition: darknet.h:119
void backward_iseg_layer(const layer l, network net)
Definition: iseg_layer.c:195