一、 CBR模块
卷积层(Conv)+ 批归一化层(BN)+ 激活函数层(ReLU或其他激活函数)
- 特点:通过ReLU函数引入非线性,使得模型能够学习到复杂的映射关系。
二、CBL模块
Conv(卷积层)+ Bn(批归一化层)+ Leaky_relu(激活函数)
-
Leaky_relu(激活函数):引入非线性因素,使得网络能够学习到复杂的模式。Leaky_relu激活函数允许小梯度值的通过,避免了ReLU激活函数在输入小于0时梯度为0的问题,从而提高了网络的训练效果。
-
Leaky_relu在ReLU的基础上进行了改进,允许小的负梯度通过,从而解决了ReLU在输入为负值时导致的神经元死亡问题
三、CBM模块
Conv(卷积)+ BN(批归一化)+ Mish激活函数
-
Mish激活函数:一种非线性激活函数,用于引入非线性因素,增强网络的表达能力。Mish激活函数的公式为f(x)=x⋅tanh(log(1+ex)),它相比其他激活函数(如ReLU、Leaky ReLU等)在保持梯度稳定性的同时,能够更好地捕捉输入数据的微小变化。
四、CBS模块
Conv(卷积层)+ BN(批归一化层)+SiLU激活函数
SiLU(激活函数)
-
功能:引入非线性因素,使模型能够学习更复杂的映射关系。
-
公式:SiLU激活函数的公式为silu(x)=x⋅sigmoid(x),它是swish激活函数的一个变体,具有平滑、非单调的特性,有助于缓解梯度消失问题。
对比:
各种激活函数图像
pytorch实现CBM模块和MiSH模块示例:
import torch
import torch.nn as nn
import torch.nn.functional as F
# 定义Mish激活函数
class Mish(nn.Module):
def __init__(self):
super(Mish, self).__init__()
def forward(self, x):
return x * torch.tanh(F.softplus(x))
# 定义CBM模块
class CBM(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0):
super(CBM, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, bias=False)
self.bn = nn.BatchNorm2d(out_channels)
self.mish = Mish()
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.mish(x)
return x
# 示例使用
if __name__ == "__main__":
# 假设输入张量的形状为 [batch_size, in_channels, height, width]
# 这里我们使用随机数据来模拟输入
batch_size, in_channels, height, width = 1, 3, 224, 224
x = torch.randn(batch_size, in_channels, height, width)
# 创建一个CBM模块实例,参数根据实际情况调整
cbm = CBM(in_channels=in_channels, out_channels=64, kernel_size=3, stride=1, padding=1)
# 通过CBM模块
output = cbm(x)
# 打印输出张量的形状
print(output.shape) # 输出应为 [batch_size, 64, height, width],其中height和width可能因padding和stride而改变