img2col

在这里插入图片描述

#include <iostream>
using namespace std;

namespace test_caffe {

inline bool is_a_ge_zero_and_a_lt_b(int a, int b) {
  return static_cast<unsigned>(a) < static_cast<unsigned>(b);
  //验证只有 0<=a<b 才返回真
  //若  0<=a<b 显然返回真
  //若 a=0,b=0 返回假
  //若 a<0,b>=0 
  //由于static_cast<unsigned>(a)最高位为1,static_cast<unsigned>(b)最高位为0
  //所以返回假
  //若 a>=0,b<0 返回真,不符合预期
  //若 a<0 ,b<0 也可能返回真(a=-2,b=-1),不符合预期

  //综上,此函数只有在确定b>=0 等情况下才能使用,否则可能出bug

}
/*
im2col_cpu函数调用
im2col_cpu(data, conv_in_channels_, conv_input_shape_.cpu_data()[1],
                 conv_input_shape_.cpu_data()[2], kernel_shape_.cpu_data()[0],
                 kernel_shape_.cpu_data()[1], pad_.cpu_data()[0],
                 pad_.cpu_data()[1], stride_.cpu_data()[0],
                 stride_.cpu_data()[1], dilation_.cpu_data()[0],
                 dilation_.cpu_data()[1], col_buff);
*/
template <typename Dtype>
void im2col_cpu(const Dtype* data_im, const int channels, const int height,
                const int width, const int kernel_h, const int kernel_w,
                const int pad_h, const int pad_w, const int stride_h,
                const int stride_w, const int dilation_h, const int dilation_w,
                Dtype* data_col) {
  const int output_h =
      (height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
  const int output_w =
      (width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
  const int channel_size = height * width;  //一个通道图的大小
  // data_im是输入数据的指针,每遍历一次就移动channel_size的位移
  // data_im每次增加一个通道的数据
  for (int channel = channels; channel--; data_im += channel_size) {
    //然后遍历原图像中一个通道里卷积核大小
    for (int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
      for (int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
        // dilation_h这个变量是每隔多少个像素取值,比如dilation_h=2
        //那就是每隔2个像素取值,默认dilation_h=1,所以不必考虑
        //逐行遍历卷积窗口的输入数据
        int input_row = -pad_h + kernel_row * dilation_h;
        for (int output_rows = output_h; output_rows; output_rows--) {
          if (!is_a_ge_zero_and_a_lt_b(input_row, height)) {
            for (int output_cols = output_w; output_cols; output_cols--) {
              *(data_col++) = 0;
            }
          } else {
            int input_col = -pad_w + kernel_col * dilation_w;
            for (int output_col = output_w; output_col; output_col--) {
              if (is_a_ge_zero_and_a_lt_b(input_col, width)) {
                *(data_col++) = data_im[input_row * width + input_col];
              } else {
                *(data_col++) = 0;
              }
              input_col += stride_w;
            }
          }
          input_row += stride_h;
        }
      }
    }
  }
}
}  // namespace test_caffe

int dataim[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

int datacol[1000];
int outim[50];

int main() {
  test_caffe::im2col_cpu(dataim, 1, 4, 4, 3, 3, 0, 0, 1, 1, 1, 1, datacol);
  std::cout << "im2col_cpu:" << std::endl;
  for (int i = 0; i < 36; i++) {
    std::cout << datacol[i] << " ";
  }
  std::cout << std::endl;
  //按行存储
  /*
  im2col_cpu
  0 1 4 5 1 2 5 6 2 3 6 7 4 5 8 9 5 6 9 10 6 7 10 11 8 9 12 13 9 10 13 14 10 11
  14 15
  */
  return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海洋2416

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值