当前位置:网站首页>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
边栏推荐
- Interface switching based on pyqt5 toolbar button -1
- C# MVC创建一个视图摆脱布局的影响
- 富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
- @BindsInstance在Dagger2中怎么使用
- 【STL源码剖析】仿函数(待补充)
- SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
- Application of containerization technology in embedded field
- Writing of head and bottom components of non routing components
- RecyclerView结合ViewBinding的使用
- VIM interval deletion note
猜你喜欢
ADC of stm32
实现BottomNavigationView和Navigation联动
Three solutions to frequent sticking and no response of explorer in win11 system
Explain promise usage in detail
Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!
Li Kou brush questions (2022-6-28)
【Redis笔记】压缩列表(ziplist)
C MVC creates a view to get rid of the influence of layout
高数有多难?AI 卷到数学圈,高数考试正确率 81%!
【STL源码剖析】仿函数(待补充)
随机推荐
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
Cryptography -- the mode of block cipher
[proteus simulation] 51 MCU +lcd12864 push box game
Quantitative analysis of PSNR, SSIM and RMSE
门牌制作 C语言
实现BottomNavigationView和Navigation联动
Remote connection of raspberry pie by VNC viewer
All things work together, and I will review oceanbase's practice in government and enterprise industry
2016. maximum difference between incremental elements
Win11如何开启目视控制?Win11开启目视控制的方法
Sword finger offer II 099 Sum of minimum paths - double hundred code
SQL advanced syntax
Application of containerization technology in embedded field
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
Yolox enhanced feature extraction network panet analysis
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - packaging based on thinkphp5 - detailed guidance of the author - July 1, 2022 08:43:
面试过了,起薪16k
Cryptographic technology -- key and ssl/tls
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base