给出一个mobilenetv3net代码示例
时间: 2023-03-30 10:01:20 浏览: 100
以下是一个使用 PyTorch 实现的 MobileNetV3 网络的代码示例:
import torch
import torch.nn as nn
class SqueezeExcitation(nn.Module):
def __init__(self, in_channels, se_ratio=.25):
super(SqueezeExcitation, self).__init__()
self.pool = nn.AdaptiveAvgPool2d(1)
self.fc1 = nn.Conv2d(in_channels, in_channels // 4, kernel_size=1, bias=False)
self.relu = nn.ReLU(inplace=True)
self.fc2 = nn.Conv2d(in_channels // 4, in_channels, kernel_size=1, bias=False)
self.sigmoid = nn.Sigmoid()
self.se_ratio = se_ratio
def forward(self, x):
out = self.pool(x)
out = self.fc1(out)
out = self.relu(out)
out = self.fc2(out)
out = self.sigmoid(out)
return x * out
class MobileNetV3Block(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, use_se=True, nl='HS'):
super(MobileNetV3Block, self).__init__()
self.use_se = use_se
self.stride = stride
self.in_channels = in_channels
self.out_channels = out_channels
assert nl in ['HS', 'RE']
self.nl = nl
padding = (kernel_size - 1) // 2
self.conv1 = nn.Conv2d(in_channels, in_channels, kernel_size, stride, padding, groups=in_channels, bias=False)
self.bn1 = nn.BatchNorm2d(in_channels)
self.relu1 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(in_channels, out_channels, 1, 1, , bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
if use_se:
self.se = SqueezeExcitation(out_channels)
self.relu2 = nn.ReLU(inplace=True)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
if self.nl == 'HS':
out = self.relu1(out)
out = self.conv2(out)
out = self.bn2(out)
if self.use_se:
out = self.se(out)
if self.nl == 'RE':
out = self.relu1(out)
if self.stride == 1 and self.in_channels == self.out_channels:
out = x + out
return out
class MobileNetV3(nn.Module):
def __init__(self, num_classes=100, mode='large', multiplier=1.):
super(MobileNetV3, self).__init__()
assert mode in ['large', 'small']
if mode == 'large':
layers = [
# in_channels, out_channels, kernel_size, stride, use_se, nl
MobileNetV3Block(3, int(16 * multiplier), 3, 1, False, 'RE'),
MobileNetV3Block(int(16 * multiplier), int(16 * multiplier), 3, 2, False, 'RE'),
MobileNetV3Block(int(16 * multiplier), int(24 * multiplier), 3, 1, False, 'RE'),
MobileNetV3Block(int(24 * multiplier), int(24 * multiplier), 3, 1, False, 'RE'),
MobileNetV3Block(int(24 * multiplier), int(40 * multiplier), 5, 2, True, 'RE'),
MobileNetV3Block(int(40 * multiplier), int(40 * multiplier), 5, 1, True, 'RE'),
MobileNetV3Block(int(40 * multiplier), int(40 * multiplier), 5, 1, True, 'RE'),
MobileNetV3Block(int(40 * multiplier), int(80 * multiplier), 3, 2, False, 'HS'),
MobileNetV3Block(int(80 * multiplier), int(80 * multiplier), 3, 1, False, 'HS'),
MobileNetV3Block(int(80 * multiplier), int(80 * multiplier), 3, 1, False, 'HS'),
MobileNetV3Block(int(80 * multiplier), int(112 * multiplier), 3, 1, True, 'HS'),
MobileNetV3Block(int(112 * multiplier), int(112 * multiplier), 3, 1, True, 'HS'),
MobileNetV3Block(int(112 * multiplier), int(160 * multiplier), 5, 2, True, 'HS'),
MobileNetV3Block(int(160 * multiplier), int(160 * multiplier), 5, 1, True, 'HS'),
MobileNetV3Block(int(160 * multiplier), int(160 * multiplier), 5, 1, True, 'HS'),
MobileNetV3Block(int(160 * multiplier), int(320 * multiplier), 3, 1, False, 'HS'),
nn.Conv2d(int(320 * multiplier), int(128 * multiplier), 1, 1, , bias=False),
nn.BatchNorm2d(int(128 * multiplier)),
nn.ReLU(inplace=True),
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(int(128 * multiplier), num_classes, 1, 1, , bias=True),
]
else:
layers = [
MobileNetV3Block(3, int(16 * multiplier), 3, 2, True, 'RE'),
MobileNetV3Block(int(16 * multiplier), int(24 * multiplier), 3, 2, False, 'RE'),
MobileNetV3Block(int(24 * multiplier), int(24 * multiplier), 3, 1, False, 'RE'),
MobileNetV3Block(int(24 * multiplier), int(40 * multiplier), 5, 2, True, 'RE'),
MobileNetV3Block(int(40 * multiplier), int(40 * multiplier), 5, 1, True, 'RE'),
MobileNetV3Block(int(40 * multiplier), int(40 * multiplier), 5, 1, True, 'RE'),
MobileNetV3Block(int(40 * multiplier), int(48 * multiplier), 5, 1, True, 'HS'),
MobileNetV3Block(int(48 * multiplier), int(48 * multiplier), 5, 1, True, 'HS'),
MobileNetV3Block(int(48 * multiplier), int(96 * multiplier), 5, 2, True, 'HS'),
MobileNetV3Block(int(96 * multiplier), int(96 * multiplier), 5, 1, True, 'HS'),
MobileNetV3Block(int(96 * multiplier), int(96 * multiplier), 5, 1, True, 'HS'),
nn.Conv2d(int(96 * multiplier), int(576 * multiplier), 1, 1, , bias=False),
nn.BatchNorm2d(int(576 * multiplier)),
nn.ReLU(inplace=True),
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(int(576 * multiplier), num_classes, 1, 1, , bias=True),
]
self.layers = nn.Sequential(*layers)
def forward(self, x):
out = self.layers(x)
out = out.view(out.size(), -1)
return out
希望这个代码示例能对你有所帮助!
相关推荐















