im2col.cpp

主要是两个函数,一个是im2col_cpu,一个是col2im_cpu函数。

im2col函数功能的是将原始的图像数据转化为一个矩阵,用于卷积操作,转换的目的是为了方便矩阵乘法。在进行了矩阵乘法之后,还要转换为图像形式,所以还需要col2im函数。

矩阵乘法参考: caffe im2col 详解

im2col 具体例子参考: caffe源码深入学习6:超级详细的im2col绘图解析,分析caffe卷积操作的底层实现

要弄懂caffe源码里面im2col具体实现,还需要知道 dilated convolution

我不完整的写出整个过程,只做一定笔记,或者关注其中的某些细节。


1.dilated细节

这里写图片描述

上图所示的
(a)是没有进行dilate的卷积核,
(b)图中dilation_h=dilation_w=2,
(c)图中dilation_h=dilation_w=3。

关注图中的红点,在dilation=1时,使用的就是原始的卷积核,否则9个红点继承原来的权值,其余部位填充为0。


2.矩阵乘法细节(卷积实现)

左边的矩阵.shape=(卷积核数目=输出通道数,图像输入通道数 * 卷积核权值数=图像输入通道数 * 卷积核高*卷积核宽)

右边的矩阵.shape=(图像输入通道数 * 卷积核权值数=图像输入通道数 * 卷积核高 * 卷积核宽 , 输出图像每层通道的数据 = 输出图像高 * 输出图像宽)

结果矩阵.shape=(图像输出通道数=卷积核数目,输出图像高 * 输出图像宽)


3.输出图像高、宽的计算

输出图像的高、宽的计算是根据stride、pad、dilation等计算出来的

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;


4.is_a_ge_zero_and_a_lt_b(int a, int b)

判断 0<=a & & a<b

// Function uses casting from int to unsigned to compare if value of
// parameter a is greater or equal to zero and lower than value of
// parameter b. The b parameter is of type signed and is always positive,
// therefore its value is always lower than 0x800... where casting
// negative value of a parameter converts it to value higher than 0x800...
// The casting allows to use one condition instead of two.
inline bool is_a_ge_zero_and_a_lt_b(int a, int b) {
  return static_cast<unsigned>(a) < static_cast<unsigned>(b);
}

im2col_cpu


图像单通道的转化情形

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;
  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++)
        {
            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;

          }
       }
    }
  }
}

col2im

针对单通道的情形


template <typename Dtype>
void col2im_cpu(const Dtype* data_col, 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_im)
    {
  caffe_set(height * width * channels, Dtype(0), data_im);
  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;

  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++)
            {
                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))
                        data_col += output_w;

                    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_im[input_row * width + input_col] += *data_col;
                            }
                            data_col++;
                            input_col += stride_w;
                        }
                    }
                    input_row += stride_h;
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值