caffe源码解析-im2col

2024-08-31 21:32
文章标签 源码 解析 caffe im2col

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

im2col这个函数特别棒!为啥?因为它让卷积变得简单,他将卷积操作转变为矩阵乘法,对比发现全连接层的实质就是矩阵乘法,所以这个函数使得卷积层的很多操作只需要仿照全连接层就可以了。下面主要介绍一下这两个函数:

  1. im2col_cpu,将输入feature map转变为矩阵
  2. col2im_cpu,将输出的残差map传递给输入的残差map,具体的残差传递还涉及权重

**

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)
{//计算输出的size,这个公式应该不需要介绍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;//channel_size是每个输入feature map的sizeconst int channel_size = height * width;//data_im是输入数据的指针,每遍历一次就移动channel_size的位移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--){//如果坐标超出输入数据的界限,一般出现这种情况是因为pad!=0if (!is_a_ge_zero_and_a_lt_b(input_row, height)){//逐列遍历输出数据,由于输入数据的行超出界限(补0),对应的输出为0for (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{//如果输入列坐标超过界限,便置0*(data_col++) = 0;}//输出列坐标移动(下一个卷积窗口了)input_col += stride_w;}}//输入行坐标移动(下一个卷积窗口了)input_row += stride_h;}}}}
}

**

col2im_cpu

**

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)){//其他逻辑都是相同的,只是前者置0,这里就是直接跳过什么也不做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;}}}}
}

这篇关于caffe源码解析-im2col的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

Spring MVC使用视图解析的问题解读

《SpringMVC使用视图解析的问题解读》:本文主要介绍SpringMVC使用视图解析的问题解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC使用视图解析1. 会使用视图解析的情况2. 不会使用视图解析的情况总结Spring MVC使用视图

利用Python和C++解析gltf文件的示例详解

《利用Python和C++解析gltf文件的示例详解》gltf,全称是GLTransmissionFormat,是一种开放的3D文件格式,Python和C++是两个非常强大的工具,下面我们就来看看如何... 目录什么是gltf文件选择语言的原因安装必要的库解析gltf文件的步骤1. 读取gltf文件2. 提