Joseph Redmon
2015-05-06 e7688a05a194e3c8baf3c11fbf09b7f5e8640a77
src/crop_layer.c
@@ -1,4 +1,5 @@
#include "crop_layer.h"
#include "cuda.h"
#include <stdio.h>
image get_crop_image(crop_layer layer)
@@ -6,10 +7,10 @@
    int h = layer.crop_height;
    int w = layer.crop_width;
    int c = layer.c;
    return float_to_image(h,w,c,layer.output);
    return float_to_image(w,h,c,layer.output);
}
crop_layer *make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip)
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 *layer = calloc(1, sizeof(crop_layer));
@@ -18,37 +19,46 @@
    layer->w = w;
    layer->c = c;
    layer->flip = flip;
    layer->angle = angle;
    layer->saturation = saturation;
    layer->exposure = exposure;
    layer->crop_width = crop_width;
    layer->crop_height = crop_height;
    layer->output = calloc(crop_width*crop_height * c*batch, sizeof(float));
    layer->delta = calloc(crop_width*crop_height * c*batch, sizeof(float));
    #ifdef GPU
    layer->output_gpu = cuda_make_array(layer->output, crop_width*crop_height*c*batch);
    layer->rand_gpu = cuda_make_array(0, layer->batch*8);
    #endif
    return layer;
}
void forward_crop_layer(const crop_layer layer, float *input)
void forward_crop_layer(const crop_layer layer, network_state state)
{
    int i,j,c,b;
    int dh = rand()%(layer.h - layer.crop_height);
    int dw = rand()%(layer.w - layer.crop_width);
    int i,j,c,b,row,col;
    int index;
    int count = 0;
    if(layer.flip && rand()%2){
        for(b = 0; b < layer.batch; ++b){
            for(c = 0; c < layer.c; ++c){
                for(i = dh; i < dh+layer.crop_height; ++i){
                    for(j = dw+layer.crop_width-1; j >= dw; --j){
                        int index = j+layer.w*(i+layer.h*(c + layer.c*b));
                        layer.output[count++] = input[index];
    int flip = (layer.flip && rand()%2);
    int dh = rand()%(layer.h - layer.crop_height + 1);
    int dw = rand()%(layer.w - layer.crop_width + 1);
    float scale = 2;
    float trans = -1;
    if(!state.train){
        flip = 0;
        dh = (layer.h - layer.crop_height)/2;
        dw = (layer.w - layer.crop_width)/2;
    }
    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;
                    }
                }
            }
        }
    }else{
        for(b = 0; b < layer.batch; ++b){
            for(c = 0; c < layer.c; ++c){
                for(i = dh; i < dh+layer.crop_height; ++i){
                    for(j = dw; j < dw+layer.crop_width; ++j){
                        int index = j+layer.w*(i+layer.h*(c + layer.c*b));
                        layer.output[count++] = input[index];
                    }
                    row = i + dh;
                    index = col+layer.w*(row+layer.h*(c + layer.c*b));
                    layer.output[count++] = state.input[index]*scale + trans;
                }
            }
        }