做视频网站 视频放在哪里,射击官网,培训前端网站开发,网址注册了怎么做网站#x1f368; 本文为#x1f517;365天深度学习训练营 中的学习记录博客#x1f356; 原作者#xff1a;K同学啊 一、ResNetV1和ResNetV2的区别 
ResNetV2 和 ResNetV1 都是深度残差网络#xff08;ResNet#xff09;的变体#xff0c;它们的主要区别在于残差块的设计和…  本文为365天深度学习训练营 中的学习记录博客 原作者K同学啊 一、ResNetV1和ResNetV2的区别 
ResNetV2 和 ResNetV1 都是深度残差网络ResNet的变体它们的主要区别在于残差块的设计和批归一化Batch Normalization, BN的使用方式。ResNetV2 是在 ResNetV1 的基础上进行改进的一种版本旨在提高模型的性能和稳定性。以下是它们之间的一些关键区别 
1. 残差块中的批归一化位置 
ResNetV1 在 ResNetV1 中批归一化层位于每个卷积层的后面ReLU 激活函数在批归一化之后。具体来说每个残差块的顺序是 卷积层 - 批归一化 - ReLU - 卷积层 - 批归一化 - 加和 - ReLU ResNetV2 在 ResNetV2 中批归一化和 ReLU 激活函数在每个卷积层之前进行。这种改变使得信息在模型中传播得更加顺畅减轻了梯度消失的问题。具体来说每个残差块的顺序是 批归一化 - ReLU - 卷积层 - 批归一化 - ReLU - 卷积层 - 加和 2. 预激活残差块 
ResNetV2 引入了“预激活残差块”Pre-activation Residual Block的概念即在每个残差块中的卷积操作之前进行批归一化和激活。这种设计有助于信息流动特别是在深层网络中。 3. 全局平均池化和分类层 
在 ResNetV1 中最后一个残差模块的输出经过批归一化和 ReLU 激活之后再通过全局平均池化层和全连接层进行分类。 
在 ResNetV2 中全局平均池化层和分类层之间没有额外的激活函数和归一化操作直接对预激活的输出进行池化和分类。 
4. 网络的深度和参数数量 
ResNetV2 通常会采用更多的参数以提高性能这包括在更深层次上引入更多的卷积层和更复杂的架构设计。 
二、ResNetV2代码实现(PyTorch) 
import torch
import torch .nn as nn
import torch.nn.functional as F# BasicBlock用于ResNet-18和ResNet-34
class BasicBlockV2(nn.Module):expansion  1def __init__(self, in_channels, out_channels, stride1, downsampleNone):super(BasicBlockV2, self).__init__()# 在卷积层之前进行批归一化和 ReLU 激活这是 ResNetV2 的主要区别之一self.bn1  nn.BatchNorm2d(in_channels)self.relu  nn.ReLU(inplaceTrue)self.conv1  nn.Conv2d(in_channels, out_channels, kernel_size3, stridestride, padding1, biasFalse)self.bn2  nn.BatchNorm2d(out_channels)self.relu  nn.ReLU(inplaceTrue)self.conv2  nn.Conv2d(out_channels, out_channels, kernel_size3, padding1, biasFalse)self.downsample  downsampledef forward(self, x):identity  x# 在卷积层之前进行批归一化和 ReLU 激活out  self.bn1(x)out  self.relu(out)out  self.conv1(out)out  self.bn2(out)out  self.relu(out)out  self.conv2(out)if self.downsample is not None:identity  self.downsample(x)out  identityreturn out# Bottleneck用于ResNet-50, ResNet-101和ResNet-152
class BottleneckV2(nn.Module):expansion  4   # 定义扩展因子def __init__(self, in_channels, out_channels, stride1, downsampleNone):super(BottleneckV2, self).__init__()# 在卷积层之前进行批归一化和 ReLU 激活这是 ResNetV2 的主要区别之一self.bn1  nn.BatchNorm2d(in_channels)self.relu  nn.ReLU(inplaceTrue)self.conv1  nn.Conv2d(in_channels, out_channels, kernel_size1, biasFalse)self.bn2  nn.BatchNorm2d(out_channels)self.conv2  nn.Conv2d(out_channels, out_channels, kernel_size3, stridestride, padding1, biasFalse)self.bn3  nn.BatchNorm2d(out_channels)self.conv3  nn.Conv2d(out_channels, out_channels * self.expansion, kernel_size1, biasFalse)self.downsample  downsampledef forward(self, x):identity  x# 在卷积层之前进行批归一化和 ReLU 激活out  self.bn1(x)out  self.relu(out)out  self.conv1(out)out  self.bn2(out)out  self.relu(out)out  self.conv2(out)out  self.bn3(out)out  self.relu(out)out  self.conv3(out)if self.downsample is not None:identity  self.downsample(x)out  identityreturn outclass ResNetV2(nn.Module):def __init__(self, block, layers, num_classes1000):初始化ResNetV2模型参数:block: 使用的残差块类型(BasicBlockV2 或 BottleneckV2)layers: 每个残差模块中的残差块数量列表例如[3, 4, 6, 3]num_classes: 分类任务的类别数, 默认为1000(适用于ImageNet数据集)super(ResNetV2, self).__init__()self.in_channels  64# 初始卷积层7x7卷积步幅2填充3self.conv1  nn.Conv2d(3, 64, kernel_size7, stride2, padding3, biasFalse)self.bn1  nn.BatchNorm2d(64)self.relu  nn.ReLU(inplaceTrue)self.maxpool  nn.MaxPool2d(kernel_size3, stride2, padding1)# 创建四个残差模块self.layer1  self._make_layer(block, 64, layers[0])self.layer2  self._make_layer(block, 128, layers[1], stride2)self.layer3  self._make_layer(block, 256, layers[2], stride2)self.layer4  self._make_layer(block, 512, layers[3], stride2)# 最后一个批归一化层ResNetV2 的特点self.bn_last  nn.BatchNorm2d(512 * block.expansion)# 平均池化层和全连接层self.avgpool  nn.AdaptiveAvgPool2d((1, 1))self.fc  nn.Linear(512 * block.expansion, num_classes)def _make_layer(self, block, out_channels, blocks, stride1):构建残差模块参数:block: 使用的残差块类型(BasicBlockV2 或 BottleneckV2)out_channels: 残差块的输出通道数blocks: 残差块数量stride: 第一个残差块的步幅, 默认为1返回: 残差模块序列downsample  None# 如果步幅不为1或输入通道数不匹配则进行下采样if stride ! 1 or self.in_channels ! out_channels * block.expansion:downsample  nn.Sequential(nn.Conv2d(self.in_channels, out_channels * block.expansion, kernel_size1, stridestride, biasFalse),nn.BatchNorm2d(out_channels * block.expansion),)layers  []# 第一个残差块可能需要下采样layers.append(block(self.in_channels, out_channels, stride, downsample))self.in_channels  out_channels * block.expansion# 其余残差块for _ in range(1, blocks):layers.append(block(self.in_channels, out_channels))return nn.Sequential(*layers)def forward(self, x):x  self.conv1(x)x  self.bn1(x)x  self.relu(x)x  self.maxpool(x)x  self.layer1(x)x  self.layer2(x)x  self.layer3(x)x  self.layer4(x)# 最后的批归一化和 ReLU 激活x  self.bn_last(x)x  self.relu(x)# 全局平均池化和全连接层x  self.avgpool(x)x  torch.flatten(x, 1)x  self.fc(x)return xdef resnet18_v2(num_classes1000):构建ResNet-18模型return ResNetV2(BasicBlockV2, [2, 2, 2, 2], num_classes)def resnet34_v2(num_classes1000):构建ResNet-34模型return ResNetV2(BasicBlockV2, [3, 4, 6, 3], num_classes)def resnet50_v2(num_classes1000):构建ResNet-50模型return ResNetV2(BottleneckV2, [3, 4, 6, 3], num_classes)def resnet101_v2(num_classes1000):构建ResNet-101模型return ResNetV2(BottleneckV2, [3, 4, 23, 3], num_classes)def resnet152_v2(num_classes1000):构建ResNet-152模型return ResNetV2(BottleneckV2, [3, 8, 36, 3], num_classes)from torchinfo import summarymodel  resnet50_v2(num_classes1000)
summary(model)三、个人小结 
通过对比 ResNetV1 和 ResNetV2我们可以看出 ResNetV2 通过将批归一化和 ReLU 激活函数移动到卷积层之前提出了预激活残差块的概念。这一改进不仅简化了梯度流动减轻了梯度消失的问题还提高了模型的训练稳定性和性能。本文还通过具体的代码实现展示了如何在 PyTorch 中构建和训练 ResNetV2 模型包括 ResNet-18, ResNet-34, ResNet-50, ResNet-101, 和 ResNet-152 各种变体。 文章转载自: http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.hhqtq.cn.gov.cn.hhqtq.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.nxdqz.cn.gov.cn.nxdqz.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.wncb.cn.gov.cn.wncb.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.zzaxr.cn.gov.cn.zzaxr.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.dnls.cn.gov.cn.dnls.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.bctr.cn.gov.cn.bctr.cn http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.crqpl.cn.gov.cn.crqpl.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.wmhqd.cn.gov.cn.wmhqd.cn http://www.morning.5-73.com.gov.cn.5-73.com http://www.morning.bpmdr.cn.gov.cn.bpmdr.cn http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.mbrbg.cn.gov.cn.mbrbg.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.drswd.cn.gov.cn.drswd.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.krjrb.cn.gov.cn.krjrb.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.kdbcx.cn.gov.cn.kdbcx.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.mbzlg.cn.gov.cn.mbzlg.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn