Go to the documentation of this file.00001 #include "crop_layer.h"
00002 #include "cuda.h"
00003 #include <stdio.h>
00004
00005 image get_crop_image(crop_layer l)
00006 {
00007 int h = l.out_h;
00008 int w = l.out_w;
00009 int c = l.out_c;
00010 return float_to_image(w,h,c,l.output);
00011 }
00012
00013 void backward_crop_layer(const crop_layer l, network_state state){}
00014 void backward_crop_layer_gpu(const crop_layer l, network_state state){}
00015
00016 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)
00017 {
00018 fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h,w,crop_height,crop_width,c);
00019 crop_layer l = {0};
00020 l.type = CROP;
00021 l.batch = batch;
00022 l.h = h;
00023 l.w = w;
00024 l.c = c;
00025 l.scale = (float)crop_height / h;
00026 l.flip = flip;
00027 l.angle = angle;
00028 l.saturation = saturation;
00029 l.exposure = exposure;
00030 l.out_w = crop_width;
00031 l.out_h = crop_height;
00032 l.out_c = c;
00033 l.inputs = l.w * l.h * l.c;
00034 l.outputs = l.out_w * l.out_h * l.out_c;
00035 l.output = calloc(l.outputs*batch, sizeof(float));
00036 l.forward = forward_crop_layer;
00037 l.backward = backward_crop_layer;
00038
00039 #ifdef GPU
00040 l.forward_gpu = forward_crop_layer_gpu;
00041 l.backward_gpu = backward_crop_layer_gpu;
00042 l.output_gpu = cuda_make_array(l.output, l.outputs*batch);
00043 l.rand_gpu = cuda_make_array(0, l.batch*8);
00044 #endif
00045 return l;
00046 }
00047
00048 void resize_crop_layer(layer *l, int w, int h)
00049 {
00050 l->w = w;
00051 l->h = h;
00052
00053 l->out_w = l->scale*w;
00054 l->out_h = l->scale*h;
00055
00056 l->inputs = l->w * l->h * l->c;
00057 l->outputs = l->out_h * l->out_w * l->out_c;
00058
00059 l->output = realloc(l->output, l->batch*l->outputs*sizeof(float));
00060 #ifdef GPU
00061 cuda_free(l->output_gpu);
00062 l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch);
00063 #endif
00064 }
00065
00066
00067 void forward_crop_layer(const crop_layer l, network_state state)
00068 {
00069 int i,j,c,b,row,col;
00070 int index;
00071 int count = 0;
00072 int flip = (l.flip && rand()%2);
00073 int dh = rand()%(l.h - l.out_h + 1);
00074 int dw = rand()%(l.w - l.out_w + 1);
00075 float scale = 2;
00076 float trans = -1;
00077 if(l.noadjust){
00078 scale = 1;
00079 trans = 0;
00080 }
00081 if(!state.train){
00082 flip = 0;
00083 dh = (l.h - l.out_h)/2;
00084 dw = (l.w - l.out_w)/2;
00085 }
00086 for(b = 0; b < l.batch; ++b){
00087 for(c = 0; c < l.c; ++c){
00088 for(i = 0; i < l.out_h; ++i){
00089 for(j = 0; j < l.out_w; ++j){
00090 if(flip){
00091 col = l.w - dw - j - 1;
00092 }else{
00093 col = j + dw;
00094 }
00095 row = i + dh;
00096 index = col+l.w*(row+l.h*(c + l.c*b));
00097 l.output[count++] = state.input[index]*scale + trans;
00098 }
00099 }
00100 }
00101 }
00102 }
00103