当前位置:网站首页>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
边栏推荐
- Brief introduction to common sense of Zhongtai
- ServletContext learning diary 1
- Ping domain name error unknown host, NSLOOKUP / system d-resolve can be resolved normally, how to Ping the public network address?
- (stinger) use pystinger Socks4 to go online and not go out of the network host
- 数字图像处理实验目录
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
- Win11麦克风测试在哪里?Win11测试麦克风的方法
- 第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
- Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
- Brief introduction of emotional dialogue recognition and generation
猜你喜欢

阿里云有奖体验:如何使用 PolarDB-X

Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11

非路由组件之头部组件和底部组件书写

How does win11 turn on visual control? Win11 method of turning on visual control

CDN acceleration requires the domain name to be filed first

Getting started with golang: for Range an alternative method of modifying the values of elements in slices

YOLOX加强特征提取网络Panet分析

Three solutions to frequent sticking and no response of explorer in win11 system
![[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)](/img/a3/d8421ea1539eba08bf7a5a629d92e6.jpg)
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)

Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
随机推荐
[analysis of STL source code] imitation function (to be supplemented)
20220524_ Database process_ Statement retention
Win11系统explorer频繁卡死无响应的三种解决方法
聊聊内存模型与内存序
在SOUI里使用真窗口时使用SOUI的滚动条
Print out mode of go
Set right click to select vs code to open the file
What experience is there only one test in the company? Listen to what they say
Brief introduction of emotional dialogue recognition and generation
Talk about memory model and memory order
20220527_ Database process_ Statement retention
面试过了,起薪16k
[redis notes] compressed list (ziplist)
【Redis笔记】压缩列表(ziplist)
MarkDown基本语法
【STL源码剖析】仿函数(待补充)
Use of recyclerview with viewbinding
Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
Temperature measurement and display of 51 single chip microcomputer [simulation]