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

相关文章

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷