caffe源码解析:卷积乘法中用到的im2col及col2im

2023-11-07 05:59

本文主要是介绍caffe源码解析:卷积乘法中用到的im2col及col2im,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这两个函数其实完成的功能比较简单,im2col就是把矩阵按卷积乘法所需,变换成列向量,col2im是一个逆过程

从下面这张图你一眼就能看明白im2col的操作(caffe中卷积计算都是Matrix_Kernel * Matrix_Col),因为都列出来太长了,我只列出了前4个,注意这是四周围完全没有填充0的情况,

 

col2im是一个反过来的过程,那么你可能会好奇,这两个操作能完全可逆吗?

事实上,结构是可逆的,结果不是,下面这个图很好地说明了展开的计算过程(图片比较大,可下载到电脑上看),

下面是一个可单独运行的测试源码,你可以随便编译跑一跑

#include <iostream>
using namespace std;inline bool is_a_ge_zero_and_a_lt_b(int a, int b) {return static_cast<unsigned>(a) < static_cast<unsigned>(b);
}template <typename Dtype>
void caffe_set(const int N, const Dtype alpha, Dtype* Y) {if (alpha == 0) {memset(Y, 0, sizeof(Dtype) * N);  // NOLINT(caffe/alt_fn)return;}for (int i = 0; i < N; ++i) {Y[i] = alpha;}
}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;}}}}
}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;}}}}
}// 如果想运行6x6的矩阵,请取消下面的注释,并把5X5那段注释掉
int dataim[] = {1,2,3,4,5,6,5,6,7,8,9,10,6,5,4,3,2,1,10,9,8,7,6,5,4,3,2,1,5,6,3,2,1,6,5,4,
};int datacol[1000];
int outim[50];int main()
{im2col_cpu(dataim, 1, 6, 6, 3, 3, 0, 0, 1, 1, 1, 1, datacol);col2im_cpu(datacol, 1, 6, 6, 3, 3, 0, 0, 1, 1, 1, 1, outim);return 0;
}// 如果想运行5x5的矩阵,请取消下面的注释, 并把上面那段注释掉
/* 
int dataim[] = {1,2,3,4,5,6,7,8,9,10,5,4,3,2,1,10,9,8,7,6,4,3,2,1,5,
};int datacol[1000];
int outim[50];int main()
{im2col_cpu(dataim, 1, 5, 5, 3, 3, 0, 0, 1, 1, 1, 1, datacol);col2im_cpu(datacol, 1, 5, 5, 3, 3, 0, 0, 1, 1, 1, 1, outim);return 0;
}*/

按上面源码的操作,先运行im2col,再运行col2im,结果就很有意思了,相当于每个元素都乘了一个放大系数,只是不同的位置的放大系数是不一样的,看下面的图

仔细看那个放大系数矩阵,非常有规律,有木有?

AI视像算法学习群:824991413

这篇关于caffe源码解析:卷积乘法中用到的im2col及col2im的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/361692

相关文章

IDEA与JDK、Maven安装配置完整步骤解析

《IDEA与JDK、Maven安装配置完整步骤解析》:本文主要介绍如何安装和配置IDE(IntelliJIDEA),包括IDE的安装步骤、JDK的下载与配置、Maven的安装与配置,以及如何在I... 目录1. IDE安装步骤2.配置操作步骤3. JDK配置下载JDK配置JDK环境变量4. Maven配置下

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC