当前位置:网站首页>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
边栏推荐
- Loss function~
- 阿里云有奖体验:如何使用 PolarDB-X
- YOLOX加强特征提取网络Panet分析
- Implementation of VGA protocol based on FPGA
- 门牌制作 C语言
- 4 special cases! Schools in area a adopt the re examination score line in area B!
- SQL advanced syntax
- 2022 latest and complete interview questions for software testing
- SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
- 万物并作,吾以观复|OceanBase 政企行业实践
猜你喜欢
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
阿里云有奖体验:如何使用 PolarDB-X
内网渗透 | 手把手教你如何进行内网渗透
采用VNC Viewer方式远程连接树莓派
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
面试过了,起薪16k
Temperature measurement and display of 51 single chip microcomputer [simulation]
【Redis笔记】压缩列表(ziplist)
ServletContext learning diary 1
采用VNC Viewer方式遠程連接樹莓派
随机推荐
Connexion à distance de la tarte aux framboises en mode visionneur VNC
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
(毒刺)利用Pystinger Socks4上线不出网主机
潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
[hardware] origin of standard resistance value
PHP get real IP
vim区间删行注释
公司里只有一个测试是什么体验?听听他们怎么说吧
Deep analysis of data storage in memory - C language
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
C#中Linq用法汇集
Hisilicon VI access video process
C# MVC创建一个视图摆脱布局的影响
Detailed explanation and application of merging and sorting
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
Win11自动关机设置在哪?Win11设置自动关机的两种方法
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
(stinger) use pystinger Socks4 to go online and not go out of the network host
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Realize the linkage between bottomnavigationview and navigation