darknet  v3
crop_layer.c
Go to the documentation of this file.
1 #include "crop_layer.h"
2 #include "cuda.h"
3 #include <stdio.h>
4 
6 {
7  int h = l.out_h;
8  int w = l.out_w;
9  int c = l.out_c;
10  return float_to_image(w,h,c,l.output);
11 }
12 
15 
16 crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure)
17 {
18  fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h,w,crop_height,crop_width,c);
19  crop_layer l = {0};
20  l.type = CROP;
21  l.batch = batch;
22  l.h = h;
23  l.w = w;
24  l.c = c;
25  l.scale = (float)crop_height / h;
26  l.flip = flip;
27  l.angle = angle;
28  l.saturation = saturation;
29  l.exposure = exposure;
30  l.out_w = crop_width;
31  l.out_h = crop_height;
32  l.out_c = c;
33  l.inputs = l.w * l.h * l.c;
34  l.outputs = l.out_w * l.out_h * l.out_c;
35  l.output = calloc(l.outputs*batch, sizeof(float));
38 
39  #ifdef GPU
42  l.output_gpu = cuda_make_array(l.output, l.outputs*batch);
43  l.rand_gpu = cuda_make_array(0, l.batch*8);
44  #endif
45  return l;
46 }
47 
48 void resize_crop_layer(layer *l, int w, int h)
49 {
50  l->w = w;
51  l->h = h;
52 
53  l->out_w = l->scale*w;
54  l->out_h = l->scale*h;
55 
56  l->inputs = l->w * l->h * l->c;
57  l->outputs = l->out_h * l->out_w * l->out_c;
58 
59  l->output = realloc(l->output, l->batch*l->outputs*sizeof(float));
60  #ifdef GPU
61  cuda_free(l->output_gpu);
62  l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch);
63  #endif
64 }
65 
66 
68 {
69  int i,j,c,b,row,col;
70  int index;
71  int count = 0;
72  int flip = (l.flip && rand()%2);
73  int dh = rand()%(l.h - l.out_h + 1);
74  int dw = rand()%(l.w - l.out_w + 1);
75  float scale = 2;
76  float trans = -1;
77  if(l.noadjust){
78  scale = 1;
79  trans = 0;
80  }
81  if(!net.train){
82  flip = 0;
83  dh = (l.h - l.out_h)/2;
84  dw = (l.w - l.out_w)/2;
85  }
86  for(b = 0; b < l.batch; ++b){
87  for(c = 0; c < l.c; ++c){
88  for(i = 0; i < l.out_h; ++i){
89  for(j = 0; j < l.out_w; ++j){
90  if(flip){
91  col = l.w - dw - j - 1;
92  }else{
93  col = j + dw;
94  }
95  row = i + dh;
96  index = col+l.w*(row+l.h*(c + l.c*b));
97  l.output[count++] = net.input[index]*scale + trans;
98  }
99  }
100  }
101  }
102 }
103 
int flip
Definition: darknet.h:153
image get_crop_image(crop_layer l)
Definition: crop_layer.c:5
int w
Definition: darknet.h:140
float scale
Definition: darknet.h:212
void forward_crop_layer_gpu(crop_layer layer, network net)
void(* forward_gpu)(struct layer, struct network)
Definition: darknet.h:126
void(* backward_gpu)(struct layer, struct network)
Definition: darknet.h:127
Definition: darknet.h:512
void forward_crop_layer(const crop_layer l, network net)
Definition: crop_layer.c:67
void(* forward)(struct layer, struct network)
Definition: darknet.h:123
int out_w
Definition: darknet.h:141
image float_to_image(int w, int h, int c, float *data)
Definition: image.c:774
int out_c
Definition: darknet.h:141
int train
Definition: darknet.h:488
float exposure
Definition: darknet.h:165
int h
Definition: darknet.h:140
int out_h
Definition: darknet.h:141
int inputs
Definition: darknet.h:134
void(* backward)(struct layer, struct network)
Definition: darknet.h:124
int noadjust
Definition: darknet.h:178
int batch
Definition: darknet.h:131
float * output
Definition: darknet.h:246
void resize_crop_layer(layer *l, int w, int h)
Definition: crop_layer.c:48
float angle
Definition: darknet.h:162
int c
Definition: darknet.h:140
float saturation
Definition: darknet.h:164
LAYER_TYPE type
Definition: darknet.h:120
float * input
Definition: darknet.h:484
void backward_crop_layer_gpu(const crop_layer l, network net)
Definition: crop_layer.c:14
void backward_crop_layer(const crop_layer l, network net)
Definition: crop_layer.c:13
int outputs
Definition: darknet.h:135
Definition: darknet.h:72
crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure)
Definition: crop_layer.c:16
Definition: darknet.h:119