resnet34和resnet50结构图和区别
时间: 2025-01-05 16:34:02 浏览: 9
### ResNet34与ResNet50的网络结构及其差异
#### ResNet34架构特点
ResNet34属于较浅版本的残差网络,采用了基本的残差块设计。该模型由多个堆叠的基础模块组成,每个基础模块通常包含两个3×3的卷积层[^4]。
```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架构特点
相比之下,ResNet50则运用了更复杂的瓶颈(Bottleneck)结构来替代简单的双层卷积单元。这种改进不仅提高了计算资源利用率,还增强了特征表达能力。具体来说,每一个Bottleneck block包含了三个不同尺寸的卷积操作——首先是压缩通道数至四分之一的一维卷积;其次是标准的空间感知三维卷积;最后再扩展回原始宽度的一维卷积[^2]。
```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
```
#### 主要区别总结
- **层数**:ResNet34总共拥有34个权重层,而ResNet50有50个。
- **block类型**:前者采用Basic Block作为构建单位,后者则是更为高效的Bottleneck Block。
- **参数量和计算成本**:由于使用了更多层次以及更加精细的设计,ResNet50虽然更深但是却能在一定程度上减少整体参数数目并提高运行效率。
阅读全文