2014-08-11 23:52:07 +04:00
|
|
|
#include "crop_layer.h"
|
2015-01-23 03:38:24 +03:00
|
|
|
#include "cuda.h"
|
2014-08-11 23:52:07 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
image get_crop_image(crop_layer l)
|
2014-08-11 23:52:07 +04:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
int h = l.out_h;
|
|
|
|
int w = l.out_w;
|
|
|
|
int c = l.out_c;
|
|
|
|
return float_to_image(w,h,c,l.output);
|
2014-08-11 23:52:07 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
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)
|
2014-08-11 23:52:07 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h,w,crop_height,crop_width,c);
|
2015-05-11 23:46:49 +03:00
|
|
|
crop_layer l = {0};
|
|
|
|
l.type = CROP;
|
|
|
|
l.batch = batch;
|
|
|
|
l.h = h;
|
|
|
|
l.w = w;
|
|
|
|
l.c = c;
|
2016-01-19 02:40:14 +03:00
|
|
|
l.scale = (float)crop_height / h;
|
2015-05-11 23:46:49 +03:00
|
|
|
l.flip = flip;
|
|
|
|
l.angle = angle;
|
|
|
|
l.saturation = saturation;
|
|
|
|
l.exposure = exposure;
|
|
|
|
l.out_w = crop_width;
|
|
|
|
l.out_h = crop_height;
|
|
|
|
l.out_c = c;
|
|
|
|
l.inputs = l.w * l.h * l.c;
|
|
|
|
l.outputs = l.out_w * l.out_h * l.out_c;
|
2016-01-19 02:40:14 +03:00
|
|
|
l.output = calloc(l.outputs*batch, sizeof(float));
|
2014-12-16 22:40:05 +03:00
|
|
|
#ifdef GPU
|
2016-01-19 02:40:14 +03:00
|
|
|
l.output_gpu = cuda_make_array(l.output, l.outputs*batch);
|
2015-09-01 21:21:01 +03:00
|
|
|
l.rand_gpu = cuda_make_array(0, l.batch*8);
|
2014-12-16 22:40:05 +03:00
|
|
|
#endif
|
2015-05-11 23:46:49 +03:00
|
|
|
return l;
|
2014-08-11 23:52:07 +04:00
|
|
|
}
|
2014-12-16 22:40:05 +03:00
|
|
|
|
2016-01-19 02:40:14 +03:00
|
|
|
void resize_crop_layer(layer *l, int w, int h)
|
|
|
|
{
|
|
|
|
l->w = w;
|
|
|
|
l->h = h;
|
|
|
|
|
|
|
|
l->out_w = l->scale*w;
|
|
|
|
l->out_h = l->scale*h;
|
|
|
|
|
|
|
|
l->inputs = l->w * l->h * l->c;
|
|
|
|
l->outputs = l->out_h * l->out_w * l->out_c;
|
|
|
|
|
|
|
|
l->output = realloc(l->output, l->batch*l->outputs*sizeof(float));
|
|
|
|
#ifdef GPU
|
|
|
|
cuda_free(l->output_gpu);
|
|
|
|
l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void forward_crop_layer(const crop_layer l, network_state state)
|
2014-08-11 23:52:07 +04:00
|
|
|
{
|
2014-12-16 22:40:05 +03:00
|
|
|
int i,j,c,b,row,col;
|
|
|
|
int index;
|
|
|
|
int count = 0;
|
2015-05-11 23:46:49 +03:00
|
|
|
int flip = (l.flip && rand()%2);
|
2016-01-19 02:40:14 +03:00
|
|
|
int dh = rand()%(l.h - l.out_h + 1);
|
|
|
|
int dw = rand()%(l.w - l.out_w + 1);
|
2015-04-15 11:04:38 +03:00
|
|
|
float scale = 2;
|
|
|
|
float trans = -1;
|
2015-07-14 09:25:08 +03:00
|
|
|
if(l.noadjust){
|
|
|
|
scale = 1;
|
|
|
|
trans = 0;
|
|
|
|
}
|
2015-03-12 08:20:15 +03:00
|
|
|
if(!state.train){
|
2015-01-31 09:05:23 +03:00
|
|
|
flip = 0;
|
2016-01-19 02:40:14 +03:00
|
|
|
dh = (l.h - l.out_h)/2;
|
|
|
|
dw = (l.w - l.out_w)/2;
|
2015-01-31 09:05:23 +03:00
|
|
|
}
|
2015-05-11 23:46:49 +03:00
|
|
|
for(b = 0; b < l.batch; ++b){
|
|
|
|
for(c = 0; c < l.c; ++c){
|
2016-01-19 02:40:14 +03:00
|
|
|
for(i = 0; i < l.out_h; ++i){
|
|
|
|
for(j = 0; j < l.out_w; ++j){
|
2014-12-16 22:40:05 +03:00
|
|
|
if(flip){
|
2015-05-11 23:46:49 +03:00
|
|
|
col = l.w - dw - j - 1;
|
2014-12-16 22:40:05 +03:00
|
|
|
}else{
|
|
|
|
col = j + dw;
|
2014-08-11 23:52:07 +04:00
|
|
|
}
|
2014-12-16 22:40:05 +03:00
|
|
|
row = i + dh;
|
2015-05-11 23:46:49 +03:00
|
|
|
index = col+l.w*(row+l.h*(c + l.c*b));
|
|
|
|
l.output[count++] = state.input[index]*scale + trans;
|
2014-08-11 23:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|