darknet/src/crop_layer.c

68 lines
2.1 KiB
C
Raw Normal View History

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>
image get_crop_image(crop_layer layer)
{
int h = layer.crop_height;
int w = layer.crop_width;
int c = layer.c;
2015-04-10 01:18:54 +03:00
return float_to_image(w,h,c,layer.output);
2014-08-11 23:52:07 +04:00
}
2015-04-17 22:32:54 +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);
crop_layer *layer = calloc(1, sizeof(crop_layer));
layer->batch = batch;
layer->h = h;
layer->w = w;
layer->c = c;
layer->flip = flip;
2015-04-14 00:09:55 +03:00
layer->angle = angle;
2015-04-17 22:32:54 +03:00
layer->saturation = saturation;
layer->exposure = exposure;
2014-08-11 23:52:07 +04:00
layer->crop_width = crop_width;
layer->crop_height = crop_height;
layer->output = calloc(crop_width*crop_height * c*batch, sizeof(float));
2014-12-16 22:40:05 +03:00
#ifdef GPU
2015-01-23 03:38:24 +03:00
layer->output_gpu = cuda_make_array(layer->output, crop_width*crop_height*c*batch);
2015-04-17 22:32:54 +03:00
layer->rand_gpu = cuda_make_array(0, layer->batch*8);
2014-12-16 22:40:05 +03:00
#endif
2014-08-11 23:52:07 +04:00
return layer;
}
2014-12-16 22:40:05 +03:00
2015-03-12 08:20:15 +03:00
void forward_crop_layer(const crop_layer layer, 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;
int flip = (layer.flip && rand()%2);
2015-01-31 09:05:23 +03:00
int dh = rand()%(layer.h - layer.crop_height + 1);
int dw = rand()%(layer.w - layer.crop_width + 1);
2015-04-15 11:04:38 +03:00
float scale = 2;
float trans = -1;
2015-03-12 08:20:15 +03:00
if(!state.train){
2015-01-31 09:05:23 +03:00
flip = 0;
dh = (layer.h - layer.crop_height)/2;
dw = (layer.w - layer.crop_width)/2;
}
2014-12-16 22:40:05 +03:00
for(b = 0; b < layer.batch; ++b){
for(c = 0; c < layer.c; ++c){
for(i = 0; i < layer.crop_height; ++i){
for(j = 0; j < layer.crop_width; ++j){
if(flip){
col = layer.w - dw - j - 1;
}else{
col = j + dw;
2014-08-11 23:52:07 +04:00
}
2014-12-16 22:40:05 +03:00
row = i + dh;
index = col+layer.w*(row+layer.h*(c + layer.c*b));
2015-04-15 11:04:38 +03:00
layer.output[count++] = state.input[index]*scale + trans;
2014-08-11 23:52:07 +04:00
}
}
}
}
}