当前位置:网站首页>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
边栏推荐
- Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
- Eight bit responder [51 single chip microcomputer]
- Loss function~
- [Yangcheng cup 2020] easyphp
- 可知论与熟能生巧
- (毒刺)利用Pystinger Socks4上线不出网主机
- 抖音实战~点赞数量弹框
- Markdown basic grammar
- Win11如何开启目视控制?Win11开启目视控制的方法
- FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
猜你喜欢
实现BottomNavigationView和Navigation联动
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
MarkDown基本语法
Set right click to select vs code to open the file
[live broadcast appointment] database obcp certification comprehensive upgrade open class
What experience is there only one test in the company? Listen to what they say
ADC of stm32
How does win11 turn on visual control? Win11 method of turning on visual control
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
Remote connection of raspberry pie by VNC viewer
随机推荐
用matlab调用vs2015来编译vs工程
SQL advanced syntax
Editor Caton
BBR encounters cubic
Submit code process
Eight bit responder [51 single chip microcomputer]
Markdown basic grammar
YOLOX加强特征提取网络Panet分析
golang中new与make的区别
The difference between new and make in golang
简述中台的常识
Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
“一个优秀程序员可抵五个普通程序员!”
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
Prometheus deployment
跨境电商如何通过打好数据底座,实现低成本稳步增长
非路由组件之头部组件和底部组件书写
2016. maximum difference between incremental elements
[redis notes] compressed list (ziplist)
Go project operation method