Linear Bottlenecks
- ReLU or Linear
- X' = T^-1(ReLU(T*X)) | 将input数据通过随机矩阵嵌入到高维manifold中使用ReLU作为激活函数进行变换,然后再通过逆变换T^-1将变换后的数据ReLU(T*X)映射到原始的低维manifold空间.
- 若映射manifold的维度较小,通过ReLU作用后返回到原始空间中的数据信息丢失较多[原始的流行结构基本被破坏]!
- 若映射manifold的维度较大,通过ReLU作用后返回到原始空间中的数据依旧能够较好地保留原始数据的流行结构[信息]!
Inverted Residuals
- Residual Block | Inverted Residual Block
- 在传统的ResBlock[Bottleneck]结构中,feature_map经历:降维、卷积、升维,期间都要按照ReLU(BN(Conv))顺序进行变换,按照Linear Bottlenecks环节的分析,这样的组合拳容易导致feature_map信息的丢失!
- 为了解决Bottleneck存在的信息丢失的问题,Inverted-Bottleneck应运而生。feature_map经历:升维、卷积[DSC]、降维.
Mobilev2-Block
class ConvBNReLU(nn.Sequential):
def __init__(self, in_planes, out_planes, kernel_size=3, stride=1, groups=1):
padding = (kernel_size - 1) // 2
super(ConvBNReLU, self).__init__(
nn.Conv2d(in_planes, out_planes, kernel_size, stride, padding, groups=groups, bias=False),
nn.BatchNorm2d(out_planes),
nn.ReLU6(inplace=True))
class InvertedResidual(nn.Module):
def __init__(self, inp, oup, stride, expand_ratio):
super(InvertedResidual, self).__init__()
self.stride = stride
assert stride in [1, 2]
hidden_dim = int(round(inp * expand_ratio))
self.use_res_connect = self.stride == 1 and inp == oup
layers = []
if expand_ratio != 1:
layers.append(ConvBNReLU(inp, hidden_dim, kernel_size=1)) # pw
layers.extend([
ConvBNReLU(hidden_dim, hidden_dim, stride=stride, groups=hidden_dim), # dw
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), # # pw-linear
nn.BatchNorm2d(oup),
])
self.conv = nn.Sequential(*layers)
def forward(self, x):
if self.use_res_connect:
return x + self.conv(x)
else:
return self.conv(x)