mirror of https://github.com/AlexeyAB/darknet.git
parent
cd8d53df21
commit
70d622ea54
20 changed files with 427 additions and 133 deletions
@ -0,0 +1,47 @@ |
||||
inline void col2im_set_pixel(float *im, int height, int width, int channels, |
||||
int row, int col, int channel, int pad, float val) |
||||
{ |
||||
row -= pad; |
||||
col -= pad; |
||||
|
||||
if (row < 0 || col < 0 || |
||||
row >= height || col >= width) return; |
||||
im[col + width*(row + channel*height)] = val; |
||||
} |
||||
//This one might be too, can't remember.
|
||||
void col2im_cpu(float* data_col, |
||||
const int batch, const int channels, const int height, const int width, |
||||
const int ksize, const int stride, int pad, float* data_im)
|
||||
{ |
||||
int c,h,w,b; |
||||
int height_col = (height - ksize) / stride + 1; |
||||
int width_col = (width - ksize) / stride + 1; |
||||
if (pad){ |
||||
height_col = 1 + (height-1) / stride; |
||||
width_col = 1 + (width-1) / stride; |
||||
pad = ksize/2; |
||||
} |
||||
int channels_col = channels * ksize * ksize; |
||||
int im_size = height*width*channels; |
||||
int col_size = height_col*width_col*channels_col; |
||||
for (b = 0; b < batch; ++b) { |
||||
for (c = 0; c < channels_col; ++c) { |
||||
int w_offset = c % ksize; |
||||
int h_offset = (c / ksize) % ksize; |
||||
int c_im = c / ksize / ksize; |
||||
for (h = 0; h < height_col; ++h) { |
||||
for (w = 0; w < width_col; ++w) { |
||||
int im_row = h_offset + h * stride; |
||||
int im_col = w_offset + w * stride; |
||||
double val = data_col[(c * height_col + h) * width_col + w]; |
||||
col2im_set_pixel(data_im, height, width, channels, |
||||
im_row, im_col, c_im, pad, val); |
||||
} |
||||
} |
||||
} |
||||
data_im += im_size; |
||||
data_col+= col_size; |
||||
} |
||||
} |
||||
|
||||
|
@ -0,0 +1,72 @@ |
||||
int detection_out_height(detection_layer layer) |
||||
{ |
||||
return layer.size + layer.h*layer.stride; |
||||
} |
||||
|
||||
int detection_out_width(detection_layer layer) |
||||
{ |
||||
return layer.size + layer.w*layer.stride; |
||||
} |
||||
|
||||
detection_layer *make_detection_layer(int batch, int h, int w, int c, int n, int size, int stride, ACTIVATION activation) |
||||
{ |
||||
int i; |
||||
size = 2*(size/2)+1; //HA! And you thought you'd use an even sized filter...
|
||||
detection_layer *layer = calloc(1, sizeof(detection_layer)); |
||||
layer->h = h; |
||||
layer->w = w; |
||||
layer->c = c; |
||||
layer->n = n; |
||||
layer->batch = batch; |
||||
layer->stride = stride; |
||||
layer->size = size; |
||||
assert(c%n == 0); |
||||
|
||||
layer->filters = calloc(c*size*size, sizeof(float)); |
||||
layer->filter_updates = calloc(c*size*size, sizeof(float)); |
||||
layer->filter_momentum = calloc(c*size*size, sizeof(float)); |
||||
|
||||
float scale = 1./(size*size*c); |
||||
for(i = 0; i < c*n*size*size; ++i) layer->filters[i] = scale*(rand_uniform()); |
||||
|
||||
int out_h = detection_out_height(*layer); |
||||
int out_w = detection_out_width(*layer); |
||||
|
||||
layer->output = calloc(layer->batch * out_h * out_w * n, sizeof(float)); |
||||
layer->delta = calloc(layer->batch * out_h * out_w * n, sizeof(float)); |
||||
|
||||
layer->activation = activation; |
||||
|
||||
fprintf(stderr, "Convolutional Layer: %d x %d x %d image, %d filters -> %d x %d x %d image\n", h,w,c,n, out_h, out_w, n); |
||||
srand(0); |
||||
|
||||
return layer; |
||||
} |
||||
|
||||
void forward_detection_layer(const detection_layer layer, float *in) |
||||
{ |
||||
int out_h = detection_out_height(layer); |
||||
int out_w = detection_out_width(layer); |
||||
int i,j,fh, fw,c; |
||||
memset(layer.output, 0, layer->batch*layer->n*out_h*out_w*sizeof(float)); |
||||
for(c = 0; c < layer.c; ++c){ |
||||
for(i = 0; i < layer.h; ++i){ |
||||
for(j = 0; j < layer.w; ++j){ |
||||
float val = layer->input[j+(i + c*layer.h)*layer.w]; |
||||
for(fh = 0; fh < layer.size; ++fh){ |
||||
for(fw = 0; fw < layer.size; ++fw){ |
||||
int h = i*layer.stride + fh; |
||||
int w = j*layer.stride + fw; |
||||
layer.output[w+(h+c/n*out_h)*out_w] += val*layer->filters[fw+(fh+c*layer.size)*layer.size]; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
void backward_detection_layer(const detection_layer layer, float *delta) |
||||
{ |
||||
} |
||||
|
||||
|
@ -0,0 +1,40 @@ |
||||
#ifndef DETECTION_LAYER_H |
||||
#define DETECTION_LAYER_H |
||||
|
||||
typedef struct { |
||||
int batch; |
||||
int h,w,c; |
||||
int n; |
||||
int size; |
||||
int stride; |
||||
|
||||
float *filters; |
||||
float *filter_updates; |
||||
float *filter_momentum; |
||||
|
||||
float *biases; |
||||
float *bias_updates; |
||||
float *bias_momentum; |
||||
|
||||
float *col_image; |
||||
float *delta; |
||||
float *output; |
||||
|
||||
#ifdef GPU |
||||
cl_mem filters_cl; |
||||
cl_mem filter_updates_cl; |
||||
cl_mem filter_momentum_cl; |
||||
|
||||
cl_mem biases_cl; |
||||
cl_mem bias_updates_cl; |
||||
cl_mem bias_momentum_cl; |
||||
|
||||
cl_mem col_image_cl; |
||||
cl_mem delta_cl; |
||||
cl_mem output_cl; |
||||
#endif |
||||
|
||||
ACTIVATION activation; |
||||
} convolutional_layer; |
||||
|
||||
#endif |
Loading…
Reference in new issue