酷站网素材,wordpress多站用户,互动网站开发,wordpress媒体库在哪个文件夹倒残差结构#xff1a; 倒残差结构是MobileNetV2中引入的一种设计#xff0c;用于增强网络的表达能力和特征提取能力#xff0c;同时保持轻量级的特点。它的核心思想是在每个瓶颈块中#xff0c;先使用一个扩张卷积#xff08;Dilated Convolution#xff09;#x…倒残差结构 倒残差结构是MobileNetV2中引入的一种设计用于增强网络的表达能力和特征提取能力同时保持轻量级的特点。它的核心思想是在每个瓶颈块中先使用一个扩张卷积Dilated Convolution然后再应用一个融合卷积Pointwise Convolution以增加非线性性和跨通道的特征表达。
扩张卷积Dilated Convolution在瓶颈块的中间层应用了一个扩张卷积。扩张卷积通过在卷积核中引入一定的空洞dilation扩大了卷积核的感受野。这有助于网络捕捉更广阔的上下文信息从而提高了特征的丰富性。融合卷积Pointwise Convolution扩张卷积后使用1x1的融合卷积来进行特征的融合和压缩。这个融合卷积将扩张卷积得到的特征进行通道的线性组合从而加强了特征之间的交互。 以下是一个更详细的PyTorch代码示例
import torch
import torch.nn as nn
from torchsummary import summary# 3、倒残差结构
class ConvBNReLU(nn.Sequential):def __init__(self, in_channel, out_channel, kernel_size3, stride1, groups1):padding (kernel_size - 1) // 2super(ConvBNReLU, self).__init__(nn.Conv2d(in_channel, out_channel, kernel_size, stride, padding, groupsgroups, biasFalse),nn.BatchNorm2d(out_channel),nn.ReLU(inplaceTrue))class InvertedResidual(nn.Module):def __init__(self, in_channel, out_channel, stride, expand_ratio):super(InvertedResidual, self).__init__()hidden_channel in_channel * expand_ratio#expand_ratio:扩展因子self.use_shortcut stride 1 and in_channel out_channellayers []if expand_ratio ! 1:layers.append(ConvBNReLU(in_channel, hidden_channel, kernel_size1))#hxwxk--hxwx(tk)layers.extend([#layers.extend() 是 Python 中的列表方法用于在一个列表的末尾一次性添加另一个可迭代对象中的所有元素到该列表中。ConvBNReLU(hidden_channel, hidden_channel, kernel_sizestride, groupshidden_channel),#hxwx(tk)--(h/s)x(w/s)x(tk)nn.Conv2d(hidden_channel, out_channel, kernel_size1, biasFalse),#(h/s)x(w/s)x(tk)--(h/s)x(w/s)xknn.BatchNorm2d(out_channel)])self.conv nn.Sequential(*layers)def forward(self, x):if self.use_shortcut:x x self.conv(x)return xelse:x self.conv(x)return xif __name__ __main__:modelInvertedResidual(3,64,1,6)device torch.device(cuda if torch.cuda.is_available() else cpu)model.to(device)input_tensortorch.randn(1,3,224,224).to(device)input_tensor1 (3, 224, 224)output_tensormodel(input_tensor)print(output_tensor.shape)print(InvertedResidual:)summary(model, input_tensor1)以上代码详细展示了如何使用PyTorch构建一个倒残差结构的MobileNetV2模型。您可以根据实际需要进行调整和扩展。