ResNet18-50
时间: 2024-12-26 09:23:19 浏览: 10
### ResNet18 和 ResNet50 的架构对比
ResNet18 使用的是 BasicBlock 结构,而 ResNet50 则采用了 Bottleneck 结构。这两种结构的主要区别在于卷积层的数量和配置不同。
对于 **ResNet18** 而言,其网络设计较为简单,主要由两个卷积层加上一个全连接层构成,每个阶段重复应用 BasicBlock[^1]:
```python
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
```
相比之下,**ResNet50** 更加复杂,它引入了更深层次的 Bottleneck 设计来提升性能,在每一个残差单元内部增加了更多的特征提取能力:
```python
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = conv1x1(inplanes, planes)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = conv3x3(planes, planes, stride)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = conv1x1(planes, planes * self.expansion)
self.bn3 = nn.BatchNorm2d(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = 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)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
```
当提到 "ResNet18-50" 这样的组合时,并不存在官方定义的标准模型。通常情况下,这可能是指希望融合两者特点的一种自定义变体版本。如果想要创建这样一个混合模型,则可以考虑在某些部分采用 ResNet18 的轻量化特性,而在其他地方利用 ResNet50 提供的强大表达力。
阅读全文