2015-01-23 03:38:24 +03:00
|
|
|
#include "im2col.h"
|
2014-07-17 20:05:07 +04:00
|
|
|
#include <stdio.h>
|
2015-08-02 03:26:53 +03:00
|
|
|
float im2col_get_pixel(float *im, int height, int width, int channels,
|
2014-12-04 10:20:29 +03:00
|
|
|
int row, int col, int channel, int pad)
|
2014-07-14 09:07:51 +04:00
|
|
|
{
|
|
|
|
row -= pad;
|
|
|
|
col -= pad;
|
|
|
|
|
|
|
|
if (row < 0 || col < 0 ||
|
|
|
|
row >= height || col >= width) return 0;
|
2014-12-04 10:20:29 +03:00
|
|
|
return im[col + width*(row + height*channel)];
|
2014-07-14 09:07:51 +04:00
|
|
|
}
|
|
|
|
|
2014-05-10 02:14:52 +04:00
|
|
|
//From Berkeley Vision's Caffe!
|
|
|
|
//https://github.com/BVLC/caffe/blob/master/LICENSE
|
2014-12-04 10:20:29 +03:00
|
|
|
void im2col_cpu(float* data_im,
|
2014-08-28 06:11:46 +04:00
|
|
|
int channels, int height, int width,
|
|
|
|
int ksize, int stride, int pad, float* data_col)
|
2014-05-10 02:14:52 +04:00
|
|
|
{
|
2014-12-04 10:20:29 +03:00
|
|
|
int c,h,w;
|
2016-09-02 02:48:41 +03:00
|
|
|
int height_col = (height + 2*pad - ksize) / stride + 1;
|
|
|
|
int width_col = (width + 2*pad - ksize) / stride + 1;
|
|
|
|
|
2014-05-10 02:14:52 +04:00
|
|
|
int channels_col = channels * ksize * ksize;
|
2014-12-04 10:20:29 +03:00
|
|
|
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;
|
|
|
|
int col_index = (c * height_col + h) * width_col + w;
|
|
|
|
data_col[col_index] = im2col_get_pixel(data_im, height, width, channels,
|
|
|
|
im_row, im_col, c_im, pad);
|
2014-07-17 21:14:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|