当前位置:网站首页>Integration of revolution and batch normalization
Integration of revolution and batch normalization
2022-07-02 23:26:00 【Point PY】
Theoretical calculation
At present CNN The basic composition unit of convolution layer is standard :Conv + BN +ReLU Three sub modules . But in fact, in the reasoning stage of the network , Can be BN The operation of layer is integrated into Conv Layer , Reduce the amount of computation , Speed up reasoning . In essence, the parameters of convolution kernel are modified , Without adding Conv While calculating the amount of layers , Omit BN Calculation amount of layers . The formula is derived as follows .
conv Parameters of the layer 
BN Parameters of the layer 
Suppose the input is x, be x->Conv->BN The output of is :
Make a simple formula deformation :

Code implementation
In actual use , First of all, we need to position conv and bn The location of , Replace or delete according to the actual situation BN layer . In this embodiment , Split the model base with open source https://github.com/qubvel/segmentation_models.pytorch Conduct a fusion experiment for the case , Yes BN Layer has been replaced .
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
边栏推荐
- 2022 latest and complete interview questions for software testing
- 解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
- Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
- 实现BottomNavigationView和Navigation联动
- Catalogue of digital image processing experiments
- What experience is there only one test in the company? Listen to what they say
- Go basic anonymous variable
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
- 理想汽车×OceanBase:当造车新势力遇上数据库新势力
- Win11系统explorer频繁卡死无响应的三种解决方法
猜你喜欢

Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决

Deep analysis of data storage in memory - C language

I've been interviewed. The starting salary is 16K
![[npuctf2020]ezlogin XPath injection](/img/6e/dac4dfa0970829775084bada740542.png)
[npuctf2020]ezlogin XPath injection

The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear

聊聊内存模型与内存序

Print out mode of go

购买完域名之后能干什么事儿?

Why does RTOS system use MPU?

Set right click to select vs code to open the file
随机推荐
Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
@BindsInstance在Dagger2中怎么使用
@How to use bindsinstance in dagger2
【Redis笔记】压缩列表(ziplist)
密码技术---密钥和SSL/TLS
[live broadcast appointment] database obcp certification comprehensive upgrade open class
【STL源码剖析】仿函数(待补充)
Arduino - character judgment function
Application of containerization technology in embedded field
C# MVC创建一个视图摆脱布局的影响
Detailed explanation of 'viewpager' in compose | developer said · dtalk
抖音实战~点赞数量弹框
[Yangcheng cup 2020] easyphp
跨境电商如何通过打好数据底座,实现低成本稳步增长
Eight bit responder [51 single chip microcomputer]
Quantitative analysis of PSNR, SSIM and RMSE
阿里云有奖体验:如何使用 PolarDB-X
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
程序员版本的八荣八耻~
Connexion à distance de la tarte aux framboises en mode visionneur VNC