darknet/src/crop_layer.c
2015-07-13 23:25:08 -07:00

78 lines
2.1 KiB
C

#include "crop_layer.h"
#include "cuda.h"
#include <stdio.h>
image get_crop_image(crop_layer l)
{
int h = l.out_h;
int w = l.out_w;
int c = l.out_c;
return float_to_image(w,h,c,l.output);
}
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)
{
fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h,w,crop_height,crop_width,c);
crop_layer l = {0};
l.type = CROP;
l.batch = batch;
l.h = h;
l.w = w;
l.c = c;
l.flip = flip;
l.angle = angle;
l.saturation = saturation;
l.exposure = exposure;
l.crop_width = crop_width;
l.crop_height = crop_height;
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;
l.output = calloc(crop_width*crop_height * c*batch, sizeof(float));
#ifdef GPU
l.output_gpu = cuda_make_array(l.output, crop_width*crop_height*c*batch);
l.rand_gpu = cuda_make_array(0, l.batch*8);
#endif
return l;
}
void forward_crop_layer(const crop_layer l, network_state state)
{
int i,j,c,b,row,col;
int index;
int count = 0;
int flip = (l.flip && rand()%2);
int dh = rand()%(l.h - l.crop_height + 1);
int dw = rand()%(l.w - l.crop_width + 1);
float scale = 2;
float trans = -1;
if(l.noadjust){
scale = 1;
trans = 0;
}
if(!state.train){
flip = 0;
dh = (l.h - l.crop_height)/2;
dw = (l.w - l.crop_width)/2;
}
for(b = 0; b < l.batch; ++b){
for(c = 0; c < l.c; ++c){
for(i = 0; i < l.crop_height; ++i){
for(j = 0; j < l.crop_width; ++j){
if(flip){
col = l.w - dw - j - 1;
}else{
col = j + dw;
}
row = i + dh;
index = col+l.w*(row+l.h*(c + l.c*b));
l.output[count++] = state.input[index]*scale + trans;
}
}
}
}
}