im2col.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. }
  37. // Function uses casting from int to unsigned to compare if value of
  38. // parameter a is greater or equal to zero and lower than value of
  39. // parameter b. The b parameter is of type signed and is always positive,
  40. // therefore its value is always lower than 0x800... where casting
  41. // negative value of a parameter converts it to value higher than 0x800...
  42. // The casting allows to use one condition instead of two.
  43. inline static int is_a_ge_zero_and_a_lt_b(int a, int b) {
  44. return (unsigned)(a) < (unsigned)(b);
  45. }
  46. // https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cpp
  47. void im2col_cpu_ext(const float* data_im, const int channels,
  48. const int height, const int width, const int kernel_h, const int kernel_w,
  49. const int pad_h, const int pad_w,
  50. const int stride_h, const int stride_w,
  51. const int dilation_h, const int dilation_w,
  52. float* data_col)
  53. {
  54. const int output_h = (height + 2 * pad_h -
  55. (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
  56. const int output_w = (width + 2 * pad_w -
  57. (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
  58. const int channel_size = height * width;
  59. int channel, kernel_row, kernel_col, output_rows, output_col;
  60. for (channel = channels; channel--; data_im += channel_size) {
  61. for (kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
  62. for (kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
  63. int input_row = -pad_h + kernel_row * dilation_h;
  64. for (output_rows = output_h; output_rows; output_rows--) {
  65. if (!is_a_ge_zero_and_a_lt_b(input_row, height)) {
  66. for (output_col = output_w; output_col; output_col--) {
  67. *(data_col++) = 0;
  68. }
  69. }
  70. else {
  71. int input_col = -pad_w + kernel_col * dilation_w;
  72. for (output_col = output_w; output_col; output_col--) {
  73. if (is_a_ge_zero_and_a_lt_b(input_col, width)) {
  74. *(data_col++) = data_im[input_row * width + input_col];
  75. }
  76. else {
  77. *(data_col++) = 0;
  78. }
  79. input_col += stride_w;
  80. }
  81. }
  82. input_row += stride_h;
  83. }
  84. }
  85. }
  86. }
  87. }