mobilenetv3的block
时间: 2025-02-19 09:00:00 浏览: 69
### MobileNetV3 Block 结构和实现详解
#### 倒残差瓶颈 (Inverted Residual Bottleneck)
MobileNetV3 的基础构建单元是倒残差瓶颈(Inverted Residuals),这种设计继承自 MobileNetV2 并进行了改进。在每个 bottleneck 中,输入特征图首先通过逐点卷积(point-wise convolution)扩展到更高的维度,接着经过深度可分离卷积(depthwise separable convolution),最后再压缩回较低的维度。
```python
def inverted_residual_block(inputs, expansion, filters, kernel_size, stride, se_ratio=None):
shortcut = inputs
# Pointwise Convolution to expand channels
x = tf.keras.layers.Conv2D(expansion * int(inputs.shape[-1]), 1, padding='same')(inputs)
# Depthwise Separable Convolution
x = tf.keras.layers.DepthwiseConv2D(kernel_size=kernel_size, strides=stride, padding='same')(x)
# Squeeze-and-Excitation Module if specified
if se_ratio is not None:
x = squeeze_excite(x, ratio=se_ratio)
# Linear pointwise convolution
x = tf.keras.layers.Conv2D(filters=filters, kernel_size=(1, 1), activation=None)(x)
if all(s == 1 for s in stride) and int(shortcut.shape[-1]) == filters:
return tf.keras.layers.Add()([shortcut, x])
else:
return x
```
#### 挤压激励(Squeeze-and-Excitation, SE)模块
SE 模块被引入来增强模型的感受野并改善信息流动效率[^1]。具体来说,在完成一次完整的bottleneck操作之后,会计算全局平均池化(global average pooling),从而获得各个通道上的统计特性;随后利用两个全连接层分别降低和恢复维度,最终产生的权重用于重新加权原始特征图中的每一通道。
```python
def squeeze_excite(tensor_input, ratio=0.25):
num_channels = tensor_input.shape[-1]
# Global Average Pooling across spatial dimensions
pooled_features = tf.reduce_mean(tensor_input, axis=[1, 2], keepdims=True)
# Fully connected layers with reduced number of units
squeezed = tf.keras.layers.Dense(int(num_channels*ratio))(pooled_features)
excited = tf.keras.layers.ReLU()(squeezed)
expanded = tf.keras.layers.Dense(num_channels)(excited)
scaled = tf.nn.sigmoid(expanded)*tensor_input
return scaled
```
#### Hard-Swish 激活函数
为了进一步提升性能表现,MobileNetV3采用了新的激活函数——Hard-Swish替代传统的ReLU6作为默认选项之一[^3]。相比于后者,前者能够在保持轻量化的同时提供更好的表达能力。
```python
import tensorflow as tf
@tf.function
def hard_swish(x):
return x * tf.nn.relu6(x + 3.) / 6.
```
阅读全文
相关推荐


















