当前位置:网站首页>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
边栏推荐
- Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
- 可知论与熟能生巧
- 聊聊内存模型与内存序
- 数字图像处理实验目录
- 【Redis笔记】压缩列表(ziplist)
- Eight bit responder [51 single chip microcomputer]
- 提交代码流程
- Interface switching based on pyqt5 toolbar button -2
- 基于Pyqt5工具栏按钮可实现界面切换-1
- RuntimeError: no valid convolution algorithms available in CuDNN
猜你喜欢
Print out mode of go
抖音实战~点赞数量弹框
Tiktok actual combat ~ number of likes pop-up box
Temperature measurement and display of 51 single chip microcomputer [simulation]
Value sequence < detailed explanation of daily question >
The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
Application of containerization technology in embedded field
实现BottomNavigationView和Navigation联动
The use of 8255 interface chip and ADC0809
内网渗透 | 手把手教你如何进行内网渗透
随机推荐
Print out mode of go
LINQ usage collection in C #
Makefile configuration of Hisilicon calling interface
I've been interviewed. The starting salary is 16K
Interface switching based on pyqt5 toolbar button -2
阿里云有奖体验:如何使用 PolarDB-X
面试过了,起薪16k
PHP get real IP
All things work together, and I will review oceanbase's practice in government and enterprise industry
提交代码流程
Three solutions to frequent sticking and no response of explorer in win11 system
php 获取真实ip
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Solution to boost library link error
海思调用接口之Makefile配置
力扣刷题(2022-6-28)
详解Promise使用
用matlab调用vs2015来编译vs工程
密码技术---密钥和SSL/TLS
Connexion à distance de la tarte aux framboises en mode visionneur VNC