当前位置:网站首页>Convolution和Batch normalization的融合
Convolution和Batch normalization的融合
2022-07-02 22:14:00 【点PY】
理论推算
当前CNN卷积层的基本组成单元标配:Conv + BN +ReLU 三个子模块。但其实在网络的推理阶段,可以将BN层的运算融合到Conv层中,减少运算量,加速推理。本质上是修改了卷积核的参数,在不增加Conv层计算量的同时,略去了BN层的计算量。公式推导如下。
conv层的参数
BN层的参数
假设输入为x,则x->Conv->BN的输出便是:
做个简单的公式变形:

代码实现
在实际使用时,首先要定位conv和bn的位置,根据实际情况进行替换或者删除BN层。在本次实施例中,以开源分割模型库https://github.com/qubvel/segmentation_models.pytorch为案例进行融合实验,对BN层进行了替换。
class Conv2dReLU(nn.Sequential):
def __init__(
self,
in_channels,
out_channels,
kernel_size,
padding=0,
stride=1,
use_batchnorm=True,
):
if use_batchnorm == "inplace" and InPlaceABN is None:
raise RuntimeError(
"In order to use `use_batchnorm='inplace'` inplace_abn package must be installed. "
+ "To install see: https://github.com/mapillary/inplace_abn"
)
conv = nn.Conv2d(
in_channels,
out_channels,
kernel_size,
stride=stride,
padding=padding,
bias=not (use_batchnorm),
)
relu = nn.ReLU(inplace=True)
if use_batchnorm == "inplace":
bn = InPlaceABN(out_channels, activation="leaky_relu", activation_param=0.0)
relu = nn.Identity()
elif use_batchnorm and use_batchnorm != "inplace":
bn = nn.BatchNorm2d(out_channels)
else:
bn = nn.Identity()
super(Conv2dReLU, self).__init__(conv, bn, relu)
from turtle import forward
from torch.fx.experimental.optimization import fuse
import torch
import torch.nn as nn
import time
import segmentation_models_pytorch.base.modules as md
from utils.torchUtils import fuse_conv_and_bn
def fuseModel(model): # fuse model Conv2d() + BatchNorm2d() layers
for m in model.modules():
if isinstance(m, (md.Conv2dReLU)) and isinstance(m[1], (nn.BatchNorm2d)):
m[0] = fuse_conv_and_bn(m[0], m[1]) # update conv
m[1] = nn.Identity()
count += 1
return model
边栏推荐
- 非路由组件之头部组件和底部组件书写
- Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
- Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
- 提交代码流程
- 4 special cases! Schools in area a adopt the re examination score line in area B!
- Markdown basic grammar
- Boost库链接错误解决方案
- PotPlayer设置最小化的快捷键
- Explain promise usage in detail
- Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
猜你喜欢
![Temperature measurement and display of 51 single chip microcomputer [simulation]](/img/83/73ee7f87787690aef7f0a9dab2c192.jpg)
Temperature measurement and display of 51 single chip microcomputer [simulation]

Cryptography -- the mode of block cipher

PotPlayer设置最小化的快捷键

FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚

LINQ usage collection in C #

公司里只有一个测试是什么体验?听听他们怎么说吧

Looking at Ctrip's toughness and vision from the Q1 financial report in 2022

Brief introduction of emotional dialogue recognition and generation

Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!

Win11自动关机设置在哪?Win11设置自动关机的两种方法
随机推荐
购买完域名之后能干什么事儿?
Li Kou brush questions (2022-6-28)
How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
golang中new与make的区别
【Redis笔记】压缩列表(ziplist)
Getting started with golang: for Range an alternative method of modifying the values of elements in slices
[hardware] origin of standard resistance value
Redis expiration policy +conf record
抖音实战~点赞数量弹框
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
Go basic constant definition and use
基于Pyqt5工具栏按钮可实现界面切换-1
SharedPreferences 保存List<Bean> 到本地并解决com.google.gson.internal.LinkedTreeMap cannot be cast to异常
Use the scroll bar of souI when using the real window in souI
Which common ports should the server open
用matlab调用vs2015来编译vs工程
STM32之ADC
SQL进阶语法
公司里只有一个测试是什么体验?听听他们怎么说吧
基于FPGA的VGA协议实现