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.

43 lines
1.2 KiB

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