当前位置:网站首页>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
边栏推荐
- php 获取真实ip
- [proteus simulation] 51 MCU +lcd12864 push box game
- Numerical solution of partial differential equations with MATLAB
- Implementation of VGA protocol based on FPGA
- 4 special cases! Schools in area a adopt the re examination score line in area B!
- Ping domain name error unknown host, NSLOOKUP / system d-resolve can be resolved normally, how to Ping the public network address?
- Connexion à distance de la tarte aux framboises en mode visionneur VNC
- golang入门:for...range修改切片中元素的值的另类方法
- RuntimeError: no valid convolution algorithms available in CuDNN
- 【Redis笔记】压缩列表(ziplist)
猜你喜欢

Go basic constant definition and use

2022年最新最全软件测试面试题大全

BBR 遭遇 CUBIC

Prometheus deployment

Why does RTOS system use MPU?

Markdown basic grammar

RuntimeError: no valid convolution algorithms available in CuDNN

The use of 8255 interface chip and ADC0809

Pandora IOT development board learning (HAL Library) - Experiment 4 serial port communication experiment (learning notes)

潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
随机推荐
Three solutions to frequent sticking and no response of explorer in win11 system
【Proteus仿真】51单片机+LCD12864推箱子游戏
【直播预约】数据库OBCP认证全面升级公开课
Getting started with golang: for Range an alternative method of modifying the values of elements in slices
Set right click to select vs code to open the file
Alibaba cloud award winning experience: how to use polardb-x
Temperature measurement and display of 51 single chip microcomputer [simulation]
LINQ usage collection in C #
Print out mode of go
Use the scroll bar of souI when using the real window in souI
Where is the win11 microphone test? Win11 method of testing microphone
Troubleshooting the cause of the crash when STM32 serial port dam receives 253 bytes
Brief introduction to common sense of Zhongtai
PotPlayer设置最小化的快捷键
What experience is there only one test in the company? Listen to what they say
购买完域名之后能干什么事儿?
为什么RTOS系统要使用MPU?
公司里只有一个测试是什么体验?听听他们怎么说吧
内网渗透 | 手把手教你如何进行内网渗透
简述中台的常识