当前位置:网站首页>Convolution和Batch normalization的融合
Convolution和Batch normalization的融合
2022-07-02 23:25:00 【點PY】
理論推算
當前CNN卷積層的基本組成單元標配:Conv + BN +ReLU 三個子模塊。但其實在網絡的推理階段,可以將BN層的運算融合到Conv層中,减少運算量,加速推理。本質上是修改了卷積核的參數,在不增加Conv層計算量的同時,略去了BN層的計算量。公式推導如下。
conv層的參數
BN層的參數
假設輸入為x,則x->Conv->BN的輸出便是:
做個簡單的公式變形:

代碼實現
在實際使用時,首先要定比特conv和bn的比特置,根據實際情况進行替換或者删除BN層。在本次實施例中,以開源分割模型庫https://github.com/qubvel/segmentation_models.pytorch為案例進行融合實驗,對BN層進行了替換。
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
边栏推荐
- Win11系统explorer频繁卡死无响应的三种解决方法
- What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
- The difference between new and make in golang
- @BindsInstance在Dagger2中怎么使用
- SQL advanced syntax
- Golang common settings - modify background
- RuntimeError: no valid convolution algorithms available in CuDNN
- ServletContext learning diary 1
- Print out mode of go
- ADC of stm32
猜你喜欢

Potplayer set minimized shortcut keys

Print out mode of go

RuntimeError: no valid convolution algorithms available in CuDNN

golang入门:for...range修改切片中元素的值的另类方法

MySQL queries nearby data And sort by distance

解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错

What experience is there only one test in the company? Listen to what they say

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

(毒刺)利用Pystinger Socks4上线不出网主机

Yolox enhanced feature extraction network panet analysis
随机推荐
“一个优秀程序员可抵五个普通程序员!”
Go basic data type
SQL advanced syntax
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
Getting started with golang: for Range an alternative method of modifying the values of elements in slices
Where is the win11 microphone test? Win11 method of testing microphone
Eight bit responder [51 single chip microcomputer]
Brief introduction of emotional dialogue recognition and generation
[npuctf2020]ezlogin XPath injection
Interface switching based on pyqt5 toolbar button -2
4 special cases! Schools in area a adopt the re examination score line in area B!
2022 latest and complete interview questions for software testing
Detailed explanation of 'viewpager' in compose | developer said · dtalk
VIM interval deletion note
Quantitative analysis of PSNR, SSIM and RMSE
【STL源码剖析】仿函数(待补充)
跨境电商如何通过打好数据底座,实现低成本稳步增长
Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!
Go basic anonymous variable
海思调用接口之Makefile配置