很多博主引用了错误的MBV3代码,在结构与论文不一致。尤其注意SE的位置,在此修正如下。
#注意力机制
class SeModule(nn.Module):
def __init__(self, channel, reduction=4):
super(SeModule, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
hsigmoid()
# nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
#线性瓶颈和反向残差结构
class Block(nn.Module):
def __init__(self, kernel_size, in_channel, expand_size, out_channel, nolinear, semodule, stride):
super(Block, self).__init__()
self.stride = stride
self.se = semodule
#1*1展开卷积
self.conv1 = nn.Conv2d(in_channel, expand_size, kernel_size=1, stride=1, padding=0, bias=False)
self.bn1 = nn.BatchNorm2d(expand_size)
self.nolinear1 = nolinear
#3*3(或5*5)深度可分离卷积
self.conv2 = nn.Conv2d(expand_size, expand_size, kernel_size=kernel_size, stride=stride, padding=kernel_size//2, groups=expand_size, bias=False)
self.bn2 = nn.BatchNorm2d(expand_size)
self.nolinear2 = nolinear
#1*1投影卷积
self.conv3 = nn.Conv2d(expand_size, out_channel, kernel_size=1, stride=1, padding=0, bias=False)
self.bn3 = nn.BatchNorm2d(out_channel)
self.shortcut = (in_channel==out_channel and stride==1)
def forward(self, x):
out = self.nolinear1(self.bn1(self.conv1(x)))
out = self.nolinear2(self.bn2(self.conv2(out)))
#注意力模块 在3*3DW Conv 后
if self.se != None:
out = self.se(out)
out = self.bn3(self.conv3(out))#注意点积在最后
#残差链接
if (self.shortcut):
out = out + x
return out
class MobileNetV3_Large(nn.Module):
def __init__(self, num_classes=10):
super(MobileNetV3_Large, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.hs1 = hswish()
self.num_classes = num_classes
self.bneck=nn.Sequential(
Block(3, 16, 16, 16, nn.ReLU(inplace=True), None, 1),
Block(3, 16, 64, 24, nn.ReLU(inplace=True), None, 2),
Block(3, 24, 72, 24, nn.ReLU(inplace=True), None, 1),
Block(5, 24, 72, 40, nn.ReLU(inplace=True), SeModule(72), 2),
Block(5, 40, 120, 40, nn.ReLU(inplace=True), SeModule(120), 1),
Block(5, 40, 120, 40, nn.ReLU(inplace=True), SeModule(120), 1),
Block(3, 40, 240, 80, hswish(), None, 2),
Block(3, 80, 200, 80, hswish(), None, 1),
Block(3, 80, 184, 80, hswish(), None, 1),
Block(3, 80, 184, 80, hswish(), None, 1),
Block(3, 80, 480, 112, hswish(), SeModule(480), 1),
Block(3, 112, 672, 112, hswish(), SeModule(672), 1),
Block(5, 112, 672, 160, hswish(), SeModule(672), 2),
Block(5, 160, 960, 160, hswish(), SeModule(960), 1),
Block(5, 160, 960, 160, hswish(), SeModule(960), 1))
self.conv2 = nn.Conv2d(160, 960, kernel_size=1, stride=1, padding=0, bias=False)
self.bn2 = nn.BatchNorm2d(960)
self.hs2 = hswish()
self.avgpool=nn.AdaptiveAvgPool2d(1)
self.linear3 = nn.Linear(960, 1280)
self.hs3 = hswish()
self.linear4 = nn.Linear(1280, num_classes)
self.init_params()
def init_params(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant_(m.weight, 1)
init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
init.normal_(m.weight, std=0.001)
if m.bias is not None:
init.constant_(m.bias, 0)
def forward(self, x):
out = self.hs1(self.bn1(self.conv1(x)))
out = self.bneck(out)
out = self.hs2(self.bn2(self.conv2(out)))
out = self.avgpool(out)
out = out.view(out.size(0), -1)
out = self.hs3(self.linear3(out))
#out = self.conv4(out)
out = self.linear4(out)
return out