MobileNetV2 的 block 结 构 图
时间: 2025-03-13 10:11:52 浏览: 42
### MobileNetV2 Block Structure Diagram
MobileNetV2采用了一种称为倒置残差块(Inverted Residual Block)的设计[^1]。这种设计与传统的残差块不同,在传统残差块中,特征维度先被压缩再放大;而在倒置残差块中,这一过程正好相反——输入首先通过一个逐点卷积层扩展通道数,接着经过深度可分离卷积处理,最后再次通过逐点卷积减少通道数。
以下是MobileNetV2中的倒置残差块的主要组成部分及其功能描述:
- **Pointwise Expansion Convolution**: 输入张量的通道数量增加到更高的维度。
- **Depthwise Separable Convolution**: 对每个通道单独应用卷积操作以降低计算复杂度。
- **Linear Pointwise Projection Convolution**: 将高维数据投影回较低维度空间,并移除激活函数来保持线性瓶颈特性。
#### 倒置残差块结构图说明
虽然无法直接提供图片形式的block structure diagram,但可以文字化描述其架构如下:
1. 扩展阶段 (Expansion Layer): 使用1×1卷积核增大输入feature maps的数量;
2. 深度方向上的卷积 (Depthwise Convolution): 应用于每一个channel上独立执行标准的空间滤波器运算;
3. 投影降维 (Projection Layer): 利用另一个1×1大小的kernel缩小output channels数目回到原始规模或者更少一些的状态下完成整个流程循环闭合形成shortcut连接部分.
下面是基于PyTorch框架实现的一个简化版代码片段展示如何构建这样的模块:
```python
import torch.nn as nn
class InvertedResidualBlock(nn.Module):
def __init__(self, inp, oup, stride, expand_ratio):
super(InvertedResidualBlock, self).__init__()
hidden_dim = round(inp * expand_ratio)
layers = []
if expand_ratio != 1:
# point-wise convolution for expansion phase.
layers.append(ConvBNReLU(inp, hidden_dim, kernel_size=1))
# depth-wise separable convolutions here...
layers.extend([
# dw
nn.Conv2d(hidden_dim, hidden_dim, 3, stride=stride, padding=1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True),
# pw-linear projection without non-linearity at end.
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup)
])
self.conv = nn.Sequential(*layers)
self.use_res_connect = stride == 1 and inp == oup
def forward(self, x):
out = self.conv(x)
if self.use_res_connect:
return x + out
else:
return out
```
上述代码定义了一个通用型`InvertedResidualBlock`, 它接受四个参数:输入channels (`inp`)、输出channels(`oup`)、步幅(strides),以及扩张比例(expand ratios).
阅读全文
相关推荐


















