You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.3 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "im2col.h"
  2. #include <stdio.h>
  3. float im2col_get_pixel(float *im, int height, int width, int channels,
  4. int row, int col, int channel, int pad)
  5. {
  6. row -= pad;
  7. col -= pad;
  8. if (row < 0 || col < 0 ||
  9. row >= height || col >= width) return 0;
  10. return im[col + width*(row + height*channel)];
  11. }
  12. //From Berkeley Vision's Caffe!
  13. //https://github.com/BVLC/caffe/blob/master/LICENSE
  14. void im2col_cpu(float* data_im,
  15. int channels, int height, int width,
  16. int ksize, int stride, int pad, float* data_col)
  17. {
  18. int c,h,w;
  19. int height_col = (height + 2*pad - ksize) / stride + 1;
  20. int width_col = (width + 2*pad - ksize) / stride + 1;
  21. int channels_col = channels * ksize * ksize;
  22. for (c = 0; c < channels_col; ++c) {
  23. int w_offset = c % ksize;
  24. int h_offset = (c / ksize) % ksize;
  25. int c_im = c / ksize / ksize;
  26. for (h = 0; h < height_col; ++h) {
  27. for (w = 0; w < width_col; ++w) {
  28. int im_row = h_offset + h * stride;
  29. int im_col = w_offset + w * stride;
  30. int col_index = (c * height_col + h) * width_col + w;
  31. data_col[col_index] = im2col_get_pixel(data_im, height, width, channels,
  32. im_row, im_col, c_im, pad);
  33. }
  34. }
  35. }
  36. }