在线绘制流程图的网站,全景网站开发多少钱,买一台服务器需要多少钱,显示网站运行时间代码一、要点
ResNeXt是ResNet的小幅升级#xff0c;更新了block 左边#xff08;ResNet的block/50/101/152层#xff09;#xff1a;
对于输入通道为256的特征矩阵#xff0c;首先使用64个11的卷积核进行降维#xff0c;再通过64个33的卷积核处理#xff0c;再通过256个1…一、要点
ResNeXt是ResNet的小幅升级更新了block 左边ResNet的block/50/101/152层
对于输入通道为256的特征矩阵首先使用64个1×1的卷积核进行降维再通过64个3×3的卷积核处理再通过256个1×1的卷积核升维输出将输出与输入进行相加得到最终的输出。
使用右边的结构替代左边的结构下面解释。
一论文中的性能参数指标 二关于ResNet和ResNeXt在ImageNet上top-1 的错误率计算量相同 三组卷积 Group Convolution 当分组的个数与输入特征矩阵的channel是一致的并且输入特征矩阵的channel也和输出特征矩阵的channel一致的话就相当于对我们输入特征矩阵的每一个channel分配了一个channel为1的卷积核进行卷积。即DW卷积。
四ResNeXt的block结构 c最简形式:输入通道为256维首先通过128个1×1的卷积核降维处理再通过group卷积卷积核3×3group数为32得到的特征矩阵的通道是128维再通过256个1×1的卷积核升维得到输出。再将输出和输入的特征矩阵进行相加得到最终的输出。
b和c等价 a和b等价 举例假设path为2对每个path采用1×1的卷积核来进行卷积 五 网络结构 二、使用pytorch搭建
代码是包括ResNet和ResNeXt的
import torch.nn as nn
import torch# 18层/34层 对应的残差结构既要有实线残差结构的功能又要有虚线残差结构的功能
class BasicBlock(nn.Module):expansion 1 #残差结构的主分支卷积核的个数有无发生变化def __init__(self, in_channel, out_channel, stride1, downsampleNone, **kwargs): # downsample对应残差结构的虚线shortcut分支super(BasicBlock, self).__init__()# stride1,对应着实线的残差结构因为并没有改变输入特征矩阵的高和宽# output (input -3 2 * 1) / 1 1 input# stride2,对应着虚线的残差结构在第一个卷积层需要将特征矩阵的高和宽缩减为原来的一半# output (input -3 2 * 1) / 2 1 input / 2 0.5 input / 2 向下取整self.conv1 nn.Conv2d(in_channelsin_channel, out_channelsout_channel,kernel_size3, stridestride, padding1, biasFalse) # biasFalse,不使用偏置项因为下面用到BatchNormalizationself.bn1 nn.BatchNorm2d(out_channel)self.relu nn.ReLU()self.conv2 nn.Conv2d(in_channelsout_channel, out_channelsout_channel,kernel_size3, stride1, padding1, biasFalse)self.bn2 nn.BatchNorm2d(out_channel)self.downsample downsampledef forward(self, x):identity x # shorcut上的输出值if self.downsample is not None:identity self.downsample(x)out self.conv1(x)out self.bn1(out)out self.relu(out)out self.conv2(out)out self.bn2(out)out identityout self.relu(out)return out# 50层/101层/152层 对应的残差结构
class Bottleneck(nn.Module):注意原论文中在虚线残差结构的主分支上第一个1x1卷积层的步距是2第二个3x3卷积层步距是1。但在pytorch官方实现过程中是第一个1x1卷积层的步距是1第二个3x3卷积层步距是2这么做的好处是能够在top1上提升大概0.5%的准确率。可参考Resnet v1.5 https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorchexpansion 4 #每个残差结构的最后一层卷积核的个数都是前两层的4倍def __init__(self, in_channel, out_channel, stride1, downsampleNone,groups1, width_per_group64):super(Bottleneck, self).__init__()width int(out_channel * (width_per_group / 64.)) * groupsself.conv1 nn.Conv2d(in_channelsin_channel, out_channelswidth,kernel_size1, stride1, biasFalse) # squeeze channelsself.bn1 nn.BatchNorm2d(width)# -----------------------------------------self.conv2 nn.Conv2d(in_channelswidth, out_channelswidth, groupsgroups,kernel_size3, stridestride, biasFalse, padding1)self.bn2 nn.BatchNorm2d(width)# -----------------------------------------self.conv3 nn.Conv2d(in_channelswidth, out_channelsout_channel*self.expansion,kernel_size1, stride1, biasFalse) # unsqueeze channelsself.bn3 nn.BatchNorm2d(out_channel*self.expansion)self.relu nn.ReLU(inplaceTrue)self.downsample downsampledef forward(self, x):identity xif self.downsample is not None:identity self.downsample(x)out self.conv1(x)out self.bn1(out)out self.relu(out)out self.conv2(out)out self.bn2(out)out self.relu(out)out self.conv3(out)out self.bn3(out)out identityout self.relu(out)return outclass ResNet(nn.Module):def __init__(self,block, # 对应18/34层或者50/101/152层的残差结构blocks_num, # 残差结构的个数是个列表以34层为例就是[3,4,6,3]num_classes1000, # 训练集的分类个数include_topTrue, # 方便以后在ResNet网络上去搭建更复杂的网络groups1,width_per_group64):super(ResNet, self).__init__()self.include_top include_topself.in_channel 64self.groups groupsself.width_per_group width_per_groupself.conv1 nn.Conv2d(3, self.in_channel, kernel_size7, stride2,padding3, biasFalse)# 为了让特征矩阵的宽和高缩减为原来的一半所以这里padding3self.bn1 nn.BatchNorm2d(self.in_channel)self.relu nn.ReLU(inplaceTrue)self.maxpool nn.MaxPool2d(kernel_size3, stride2, padding1)# 为了让特征矩阵的宽和高缩减为原来的一半所以这里padding1self.layer1 self._make_layer(block, 64, blocks_num[0]) # conv2_x 对于50/101/152层来说第一个残差结构的第一层只改变特征矩阵的深度没有改变宽高所以没有传入stride默认为1self.layer2 self._make_layer(block, 128, blocks_num[1], stride2) # conv3_xself.layer3 self._make_layer(block, 256, blocks_num[2], stride2) # conv4_xself.layer4 self._make_layer(block, 512, blocks_num[3], stride2) # conv5_xif self.include_top:self.avgpool nn.AdaptiveAvgPool2d((1, 1)) # output size (1, 1)self.fc nn.Linear(512 * block.expansion, num_classes)for m in self.modules():if isinstance(m, nn.Conv2d):nn.init.kaiming_normal_(m.weight, modefan_out, nonlinearityrelu)def _make_layer(self, block, channel, block_num, stride1):downsample None# 对于18/34层第一个残差结构跳过这句if stride ! 1 or self.in_channel ! channel * block.expansion:downsample nn.Sequential(nn.Conv2d(self.in_channel, channel * block.expansion, kernel_size1, stridestride, biasFalse),nn.BatchNorm2d(channel * block.expansion))layers []# 残差结构的第一层虚线残差结构layers.append(block(self.in_channel,channel,downsampledownsample,stridestride,groupsself.groups,width_per_groupself.width_per_group))self.in_channel channel * block.expansionfor _ in range(1, block_num):layers.append(block(self.in_channel,channel,groupsself.groups,width_per_groupself.width_per_group))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)if self.include_top:x self.avgpool(x)x torch.flatten(x, 1)x self.fc(x)return xdef resnet34(num_classes1000, include_topTrue):# https://download.pytorch.org/models/resnet34-333f7ec4.pthreturn ResNet(BasicBlock, [3, 4, 6, 3], num_classesnum_classes, include_topinclude_top)def resnet50(num_classes1000, include_topTrue):# https://download.pytorch.org/models/resnet50-19c8e357.pthreturn ResNet(Bottleneck, [3, 4, 6, 3], num_classesnum_classes, include_topinclude_top)def resnet101(num_classes1000, include_topTrue):# https://download.pytorch.org/models/resnet101-5d3b4d8f.pthreturn ResNet(Bottleneck, [3, 4, 23, 3], num_classesnum_classes, include_topinclude_top)def resnext50_32x4d(num_classes1000, include_topTrue):# https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pthgroups 32width_per_group 4return ResNet(Bottleneck, [3, 4, 6, 3],num_classesnum_classes,include_topinclude_top,groupsgroups,width_per_groupwidth_per_group)def resnext101_32x8d(num_classes1000, include_topTrue):# https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pthgroups 32width_per_group 8return ResNet(Bottleneck, [3, 4, 23, 3],num_classesnum_classes,include_topinclude_top,groupsgroups,width_per_groupwidth_per_group)