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

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实