Open Voyagerlemon opened 1 year ago
你这个代码……有点乱,看不太懂啊,为什么有些事代码格式有些不是
MobileNetV3网络
导入火炬.nn 作为 NN 导入数学导入火炬
all = ['build_mobilenetv3_small', 'build_mobilenetv3_large']
def _make_divisible(v, divisor, min_value=None): 如果min_value为 None: min_value = 除数 new_v = max(min_value, int(v + 除数 / 2) // 除数 除数) #--------------------------------------# # 确保调整后的值不会比原始值下降超过10% #--------------------------------------# 如果new_v < 0.9 v: new_v += 除数返回new_v
类 Sigmoid(nn.模块): def init(self, inplace=True): super(Sigmoid, self).init() self.relu6 = nn.ReLU6(就地=就地)
def forward(self, x): return self.relu6(x + 3) / 6
类硬斯威什(nn.模块):def init(self, inplace=True): super(HardSwish, self)。init() self.sigmoid = Sigmoid(inplace=inplace)
def forward(self, x): return x * self.sigmoid(x)
类 SEInvertedBottleneck(nn.模块): def init(self, channel, reduction=4): super(SEInvertedBottleneck, self).init() self.avg_pool = nn。AdaptiveAvgPool2d(1) self.SEblock = nn。顺序( nn.线性(通道, _make_divisible(通道 // 缩减, 8)), # nn.ReLU(inplace=True), nn.ReLU6(inplace=True), nn.线性(_make_divisible(channel // reduction, 8), channel), # Sigmoid() HardSwish(inplace=True) )
def forward(self, x): b, c, _, _ = x.size() y = self.avg_pool(x).view(b, c) y = self.SEblock(y).view(b, c, 1, 1) return x * y
--------------------------------
带有批归一化和激活函数的卷积层
--------------------------------#def conv_3x3_bn(inp, oup, stride): return nn.顺序( nn.Conv2d(inp, oup, 3, stride, 1, bias=False), nn.BatchNorm2d(oup), HardSwish() )
----------------------------------
带有批归一化和激活函数的1×1卷积层
----------------------------------#def conv_1x1_bn(inp, oup): return nn.顺序( nn.Conv2d(inp, oup, 1, 1, 0, bias=False), nn.BatchNorm2d(oup), HardSwish() )
-------------------------
MobileNetV3的倒残差网络
-------------------------#类 倒置残差(nn.模块):def init(self, inp, hidden_dim, oup, kernel_size, stride, use_se, use_hs): super(InvertedResidual, self).init() 断言在 [1, 2] 中的步幅
self.identity = stride == 1 and inp == oup if inp == hidden_dim: self.conv = nn.Sequential( #----------------------------------------------# # 进行3x3的逐层卷积, 进行跨特征点的特征提取(dw) #----------------------------------------------# nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False), nn.BatchNorm2d(hidden_dim), # HardSwish() if use_hs else nn.ReLU(inplace=True) HardSwish() if use_hs else nn.ReLU6(inplace=True), #---------------# # 使用SE模块 #---------------# SEInvertedBottleneck(hidden_dim) if use_se else nn.Identity(), #---------------------------------------# # 利用1x1卷积进行通道数的调整(pw-linear) #---------------------------------------# nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), nn.BatchNorm2d(oup), ) else: self.conv = nn.Sequential( #---------------------------------# # 利用1x1卷积进行通道数的调整(pw) #---------------------------------# nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False), nn.BatchNorm2d(hidden_dim), HardSwish() if use_hs else nn.ReLU(inplace=True), #----------------------------------------------# # 进行3x3的逐层卷积, 进行跨特征点的特征提取(dw) #----------------------------------------------# nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False), nn.BatchNorm2d(hidden_dim), #---------------# # 使用SE模块 #---------------# SEInvertedBottleneck(hidden_dim) if use_se else nn.Identity(), HardSwish() if use_hs else nn.ReLU(inplace=True), #----------------------------------------# # 利用1x1卷积进行通道数的调整(pw-linear) #----------------------------------------# nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), nn.BatchNorm2d(oup), ) def forward(self, x): if self.identity: return x + self.conv(x) else: return self.conv(x)
类 MobileNetV3(nn.模块): def init(self, cfgs, mode, num_classes=1000, width_mult=1.): super(MobileNetV3, self).初始化()
#------------------# # 设置倒残差网络块 #------------------# self.cfgs = cfgs assert mode in ['large', 'small'] #--------------# # 构建第一层 #--------------# input_channel = _make_divisible(16 * width_mult, 8) #------------------------------# # 对特征层进行高和宽的压缩 # 1024×2048×3-->512×1024×3 #------------------------------# self.features = [conv_3x3_bn(3, input_channel, 2)] #------------------# # 构建倒残差网络块 #------------------# block = InvertedResidual exp_size = None output_channel = None for i in range(4): for k, t, c, use_se, use_hs, s in self.cfgs[i]: output_channel = _make_divisible(c * width_mult, 8) exp_size = _make_divisible(input_channel * t, 8) self.features.append(block(input_channel, exp_size, output_channel, k, s, use_se, use_hs)) input_channel = output_channel #------------------# # 构建最后几层网络 #------------------# self.features = nn.Sequential(*self.features) self.conv = conv_1x1_bn(input_channel, exp_size) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) output_channel = {'large': 1280, 'small': 1024} output_channel = _make_divisible(output_channel[mode] * width_mult, 8) if width_mult > 1.0 else output_channel[ mode] self.classifier = nn.Sequential( nn.Linear(exp_size, output_channel), HardSwish(), nn.Dropout(0.2), nn.Linear(output_channel, num_classes), ) self._initialize_weights() def forward(self, x): #--------------------------------------# # downsample: [2 4 8 16 32] #--------------------------------------# x = self.features(x) x = self.conv(x) x = self.avgpool(x) x = x.view(x.size(0), -1) x = self.classifier(x) return x def _initialize_weights(self): for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.Linear): m.weight.data.normal_(0, 0.01) m.bias.data.zero_()
def mobilenetv3_large(**kwargs): “”“ 构造一个 MobileNetV3-Large 模型
8x download factor """ cfgs = [ # k, t, c, SE, HS, s [[3, 1, 16, 0, 0, 1], [3, 4, 24, 0, 0, 2], [3, 3, 24, 0, 0, 1]], [[5, 3, 40, 1, 0, 2], [5, 3, 40, 1, 0, 1], [5, 3, 40, 1, 0, 1]], [[3, 6, 80, 0, 1, 1], [3, 2.5, 80, 0, 1, 1], [3, 2.3, 80, 0, 1, 1], [3, 2.3, 80, 0, 1, 1], [3, 6, 112, 1, 1, 1], [3, 6, 112, 1, 1, 1]], [[5, 6, 160, 1, 1, 1], [5, 6, 160, 1, 1, 1], [5, 6, 160, 1, 1, 1]] ] return MobileNetV3(cfgs, mode='large', **kwargs)
def mobilenetv3_small(**kwargs): “”“ 构造一个 MobileNetV3-Small 模型 8x 下载因子 ”“” cfgs = [ # k, t, c, SE, HS, s [[3, 1, 16, 1, 0, 2]], [[3, 4.5, 24, 0, 0, 2], [3, 3.67, 24, 0, 0, 1]], [[5, 4, 40, 1, 1, 1], [5, 6, 40, 1, 1, 1], [5, 6, 40, 1, 1, 1, 5, 3, 48, 1, 1, 1, 5, 3], [48, 1, 1, 1, 5, 6]], [[96, 1, 1, 1, 5, 6], [96, 1, 1, 1, 5, 6], [96, 1, 1, 1, <>, <>]], ]
return MobileNetV3(cfgs, mode='small', **kwargs)
def load_and_convert(net, state_dict): net_dict = net.state_dict().copy() net_list = list(net_dict.keys()) trained_list = list(state_dict.keys()) assert len(net_list) == len(trained_list), 'Learning parameters not match, check net and train state_dict' for i in range(len(net_list)): net_dict[net_list[i]] = state_dict[trained_list[i]] net.load_state_dict(net_dict)
def build_mobilenetv3_large(pretrained=True, width_mult=1.): net = mobilenetv3_large(width_mult=width_mult) 如果预训练: eps = 1e-5 如果 abs(1.0 - width_mult) < EPS: weights = './initmodel/mobilenetv3-large-1cd25616.pth' state_dict = Torch.load(weights) elif abs(0.75 - width_mult) < EPS: weights = './initmodel/mobilenetv3-large-0.75-9632d2a8.pth' state_dict = torch.load(weights) else: raise RuntimeError(“Not support width_mult: {}”.format(width_mult)) load_and_convert(net, state_dict) return net
def build_mobilenetv3_small(pretrained=True, width_mult=1.): net = mobilenetv3_small(width_mult=width_mult) 如果预训练: eps = 1e-5 如果 abs(1.0 - width_mult) < EPS: weights = './initmodel/mobilenetv3-small-55df8e1f.pth' state_dict = Torch.load(weights) elif abs(0.75 - width_mult) < EPS: weights = './initmodel/mobilenetv3-small-0.75-86c972c3.pth' state_dict = torch.load(weights) else: raise RuntimeError(“Not support width_mult: {}”.format(width_mult)) load_and_convert(net, state_dict) return net
如果名称 == “主”:导入割炬
def params(net): return sum(param.numel() for param in net.parameters()) net = build_mobilenetv3_large(pretrained=False, width_mult=1.) input = torch.randn((1, 3, 224, 224)) out = net(input) print('Out shape ', out.size())
#######################################################################请问各位大佬这样构建有问题吗,在deeplabv3_plus.py中,这样的写法如何更改呢??请up住及各位大佬解答一下!!!! class MobileNetV3(nn.模块): def init(self, pretrained=True): super(MobileNetV3, self).init() 从 functools 导入部分
model = build_mobilenetv3_large(pretrained) self.features = model.features[:4] def forward(self, x): #-----------------------------------# # 获得DeepLabv3+的深层特征和浅层特征 #-----------------------------------# low_level_features = self.features[:4](x) x = self.features[4:](low_level_features) return low_level_features, x
在类 DeepLab(nn.模块)中: elif 骨干 == “mobilenetv3”: self.backbone = MobileNetV3(pretrained=pretrained) in_channels = 320 low_level_channels = 24
你好,请问你解决了吗?我也想更换一下主干网络,但是一直会出错,想请教你一下,谢谢
你这个代码……有点乱,看不太懂啊,为什么有些事代码格式有些不是 重新粘贴一下代码,确实太乱了:
import torch.nn as nn import math import torch
all = ['build_mobilenetv3_small', 'build_mobilenetv3_large']
def _make_divisible(v, divisor, min_value=None): if min_value is None: min_value = divisor new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
# 确保调整后的值不会比原始值下降超过10%
#--------------------------------------#
if new_v < 0.9 * v:
new_v += divisor
return new_v
class Sigmoid(nn.Module): def init(self, inplace=True): super(Sigmoid, self).init() self.relu6 = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu6(x + 3) / 6
class HardSwish(nn.Module): def init(self, inplace=True): super(HardSwish, self).init() self.sigmoid = Sigmoid(inplace=inplace)
def forward(self, x):
return x * self.sigmoid(x)
class SEInvertedBottleneck(nn.Module): def init(self, channel, reduction=4): super(SEInvertedBottleneck, self).init() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.SEblock = nn.Sequential( nn.Linear(channel, _make_divisible(channel // reduction, 8)),
nn.ReLU6(inplace=True),
nn.Linear(_make_divisible(channel // reduction, 8), channel),
# Sigmoid()
HardSwish(inplace=True)
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.SEblock(y).view(b, c, 1, 1)
return x * y
def conv_3x3_bn(inp, oup, stride): return nn.Sequential( nn.Conv2d(inp, oup, 3, stride, 1, bias=False), nn.BatchNorm2d(oup), HardSwish() )
def conv_1x1_bn(inp, oup): return nn.Sequential( nn.Conv2d(inp, oup, 1, 1, 0, bias=False), nn.BatchNorm2d(oup), HardSwish() )
class InvertedResidual(nn.Module): def init(self, inp, hidden_dim, oup, kernel_size, stride, use_se, use_hs): super(InvertedResidual, self).init() assert stride in [1, 2]
self.identity = stride == 1 and inp == oup
if inp == hidden_dim:
self.conv = nn.Sequential(
#----------------------------------------------#
# 进行3x3的逐层卷积, 进行跨特征点的特征提取(dw)
#----------------------------------------------#
nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
bias=False),
nn.BatchNorm2d(hidden_dim),
# HardSwish() if use_hs else nn.ReLU(inplace=True)
HardSwish() if use_hs else nn.ReLU6(inplace=True),
#---------------#
# 使用SE模块
#---------------#
SEInvertedBottleneck(hidden_dim) if use_se else nn.Identity(),
#---------------------------------------#
# 利用1x1卷积进行通道数的调整(pw-linear)
#---------------------------------------#
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
else:
self.conv = nn.Sequential(
#---------------------------------#
# 利用1x1卷积进行通道数的调整(pw)
#---------------------------------#
nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
nn.BatchNorm2d(hidden_dim),
HardSwish() if use_hs else nn.ReLU(inplace=True),
#----------------------------------------------#
# 进行3x3的逐层卷积, 进行跨特征点的特征提取(dw)
#----------------------------------------------#
nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
bias=False),
nn.BatchNorm2d(hidden_dim),
#---------------#
# 使用SE模块
#---------------#
SEInvertedBottleneck(hidden_dim) if use_se else nn.Identity(),
HardSwish() if use_hs else nn.ReLU(inplace=True),
#----------------------------------------#
# 利用1x1卷积进行通道数的调整(pw-linear)
#----------------------------------------#
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
def forward(self, x):
if self.identity:
return x + self.conv(x)
else:
return self.conv(x)
class MobileNetV3(nn.Module): def init(self, cfgs, mode, num_classes=1000, width_mult=1.): super(MobileNetV3, self).init()
#------------------#
# 设置倒残差网络块
#------------------#
self.cfgs = cfgs
assert mode in ['large', 'small']
#--------------#
# 构建第一层
#--------------#
input_channel = _make_divisible(16 * width_mult, 8)
#------------------------------#
# 对特征层进行高和宽的压缩
# 1024×2048×3-->512×1024×3
#------------------------------#
self.features = [conv_3x3_bn(3, input_channel, 2)]
#------------------#
# 构建倒残差网络块
#------------------#
block = InvertedResidual
exp_size = None
output_channel = None
for i in range(4):
for k, t, c, use_se, use_hs, s in self.cfgs[i]:
output_channel = _make_divisible(c * width_mult, 8)
exp_size = _make_divisible(input_channel * t, 8)
self.features.append(block(input_channel, exp_size, output_channel, k, s, use_se, use_hs))
input_channel = output_channel
#------------------#
# 构建最后几层网络
#------------------#
self.features = nn.Sequential(*self.features)
self.conv = conv_1x1_bn(input_channel, exp_size)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
output_channel = {'large': 1280, 'small': 1024}
output_channel = _make_divisible(output_channel[mode] * width_mult, 8) if width_mult > 1.0 else output_channel[
mode]
self.classifier = nn.Sequential(
nn.Linear(exp_size, output_channel),
HardSwish(),
nn.Dropout(0.2),
nn.Linear(output_channel, num_classes),
)
self._initialize_weights()
def forward(self, x):
#--------------------------------------#
# downsample: [2 4 8 16 32]
#--------------------------------------#
x = self.features(x)
x = self.conv(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()
def mobilenetv3_large(**kwargs): cfgs = [
[[3, 1, 16, 0, 0, 1],
[3, 4, 24, 0, 0, 2],
[3, 3, 24, 0, 0, 1]],
[[5, 3, 40, 1, 0, 2],
[5, 3, 40, 1, 0, 1],
[5, 3, 40, 1, 0, 1]],
[[3, 6, 80, 0, 1, 1],
[3, 2.5, 80, 0, 1, 1],
[3, 2.3, 80, 0, 1, 1],
[3, 2.3, 80, 0, 1, 1],
[3, 6, 112, 1, 1, 1],
[3, 6, 112, 1, 1, 1]],
[[5, 6, 160, 1, 1, 1],
[5, 6, 160, 1, 1, 1],
[5, 6, 160, 1, 1, 1]]
]
return MobileNetV3(cfgs, mode='large', **kwargs)
def mobilenetv3_small(**kwargs): cfgs = [
[[3, 1, 16, 1, 0, 2]],
[[3, 4.5, 24, 0, 0, 2],
[3, 3.67, 24, 0, 0, 1]],
[[5, 4, 40, 1, 1, 1],
[5, 6, 40, 1, 1, 1],
[5, 6, 40, 1, 1, 1],
[5, 3, 48, 1, 1, 1],
[5, 3, 48, 1, 1, 1]],
[[5, 6, 96, 1, 1, 1],
[5, 6, 96, 1, 1, 1],
[5, 6, 96, 1, 1, 1]],
]
return MobileNetV3(cfgs, mode='small', **kwargs)
def load_and_convert(net, state_dict): net_dict = net.state_dict().copy() net_list = list(net_dict.keys()) trained_list = list(state_dict.keys()) assert len(net_list) == len(trained_list), 'Learning parameters do not match, check net and trained state_dict' for i in range(len(net_list)): net_dict[net_list[i]] = state_dict[trained_list[i]] net.load_state_dict(net_dict)
def build_mobilenetv3_large(pretrained=True, width_mult=1.): net = mobilenetv3_large(width_mult=width_mult) if pretrained: eps = 1e-5 if abs(1.0 - width_mult) < eps: weights = './model_pretrain/mobilenetv3-large-1cd25616.pth' state_dict = torch.load(weights) elif abs(0.75 - width_mult) < eps: weights = './model_pretrain/mobilenetv3-large-0.75-9632d2a8.pth' state_dict = torch.load(weights) else: raise RuntimeError("Not support width_mult: {}".format(width_mult)) load_and_convert(net, state_dict) return net
def build_mobilenetv3_small(pretrained=True, width_mult=1.): net = mobilenetv3_small(width_mult=width_mult) if pretrained: eps = 1e-5 if abs(1.0 - width_mult) < eps: weights = './model_pretrain/mobilenetv3-small-55df8e1f.pth' state_dict = torch.load(weights) elif abs(0.75 - width_mult) < eps: weights = './model_pretrain/mobilenetv3-small-0.75-86c972c3.pth' state_dict = torch.load(weights) else: raise RuntimeError("Not support width_mult: {}".format(width_mult)) load_and_convert(net, state_dict) return net
if name == 'main':
def params(net):
return sum(param.numel() for param in net.parameters())
net = build_mobilenetv3_large(pretrained=False, width_mult=1.)
input = torch.randn((1, 3, 224, 224))
out = net(input)
print('Out shape ', out.size())
这是构建的mobilenetv3.py的代码,请问在deeplab_plus.py中如何使用这个模块呢?
@Leochen9 目前还没有呢
额,你这个貌似只是作为backbone的mobilenetv3 你有看mobilenetv2的调用方式吗
额,你这个貌似只是作为backbone的mobilenetv3 你有看mobilenetv2的调用方式吗
佬,可以发一个怎么把mobilenetv3做backbone的代码吗?还有就是我要训练自己数据集的话,是不是最好先用vooc训练个模型作为预训练模型
额,你这个貌似只是作为backbone的mobilenetv3 你有看mobilenetv2的调用方式吗
佬,可以发一个怎么把mobilenetv3做backbone的代码吗?还有就是我要训练自己数据集的话,是不是最好先用vooc训练个模型作为预训练模型
imagenet就行,我周末有空可能会弄弄
额,你这个貌似只是作为backbone的mobilenetv3 你有看mobilenetv2的调用方式吗
佬,可以发一个怎么把mobilenetv3做backbone的代码吗?还有就是我要训练自己数据集的话,是不是最好先用vooc训练个模型作为预训练模型
imagenet就行,我周末有空可能会弄弄
大佬,最后有弄mobilenetv3作为backbone的版本吗
额,你这个貌似只是作为backbone的mobilenetv3 你有看mobilenetv2的调用方式吗
佬,可以发一个怎么把mobilenetv3做backbone的代码吗?还有就是我要训练自己数据集的话,是不是最好先用vooc训练个模型作为预训练模型
imagenet就行,我周末有空可能会弄弄
大佬,最后有弄mobilenetv3作为backbone的版本吗
同求
额,你这个貌似只是作为backbone的mobilenetv3 你有看mobilenetv2的调用方式吗
佬,可以发一个怎么把mobilenetv3做backbone的代码吗?还有就是我要训练自己数据集的话,是不是最好先用vooc训练个模型作为预训练模型
imagenet就行,我周末有空可能会弄弄
大佬,最后有弄mobilenetv3作为backbone的版本吗
同求
MobileNetV3网络
import torch.nn as nn import math import torch
all = ['build_mobilenetv3_small', 'build_mobilenetv3_large']
def _make_divisible(v, divisor, min_value=None): if min_value is None: min_value = divisor new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
--------------------------------------
class Sigmoid(nn.Module): def init(self, inplace=True): super(Sigmoid, self).init() self.relu6 = nn.ReLU6(inplace=inplace)
class HardSwish(nn.Module): def init(self, inplace=True): super(HardSwish, self).init() self.sigmoid = Sigmoid(inplace=inplace)
class SEInvertedBottleneck(nn.Module): def init(self, channel, reduction=4): super(SEInvertedBottleneck, self).init() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.SEblock = nn.Sequential( nn.Linear(channel, _make_divisible(channel // reduction, 8)),
nn.ReLU(inplace=True),
--------------------------------
带有批归一化和激活函数的卷积层
--------------------------------
def conv_3x3_bn(inp, oup, stride): return nn.Sequential( nn.Conv2d(inp, oup, 3, stride, 1, bias=False), nn.BatchNorm2d(oup), HardSwish() )
----------------------------------
带有批归一化和激活函数的1×1卷积层
----------------------------------
def conv_1x1_bn(inp, oup): return nn.Sequential( nn.Conv2d(inp, oup, 1, 1, 0, bias=False), nn.BatchNorm2d(oup), HardSwish() )
-------------------------
MobileNetV3的倒残差网络
-------------------------
class InvertedResidual(nn.Module): def init(self, inp, hidden_dim, oup, kernel_size, stride, use_se, use_hs): super(InvertedResidual, self).init() assert stride in [1, 2]
class MobileNetV3(nn.Module): def init(self, cfgs, mode, num_classes=1000, width_mult=1.): super(MobileNetV3, self).init()
def mobilenetv3_large(**kwargs): """ Constructs a MobileNetV3-Large model
def mobilenetv3_small(**kwargs): """ Constructs a MobileNetV3-Small model 8x download factor """ cfgs = [
k, t, c, SE, HS, s
def load_and_convert(net, state_dict): net_dict = net.state_dict().copy() net_list = list(net_dict.keys()) trained_list = list(state_dict.keys()) assert len(net_list) == len(trained_list), 'Learning parameters do not match, check net and trained state_dict' for i in range(len(net_list)): net_dict[net_list[i]] = state_dict[trained_list[i]] net.load_state_dict(net_dict)
def build_mobilenetv3_large(pretrained=True, width_mult=1.): net = mobilenetv3_large(width_mult=width_mult) if pretrained: eps = 1e-5 if abs(1.0 - width_mult) < eps: weights = './initmodel/mobilenetv3-large-1cd25616.pth' state_dict = torch.load(weights) elif abs(0.75 - width_mult) < eps: weights = './initmodel/mobilenetv3-large-0.75-9632d2a8.pth' state_dict = torch.load(weights) else: raise RuntimeError("Not support width_mult: {}".format(width_mult)) load_and_convert(net, state_dict) return net
def build_mobilenetv3_small(pretrained=True, width_mult=1.): net = mobilenetv3_small(width_mult=width_mult) if pretrained: eps = 1e-5 if abs(1.0 - width_mult) < eps: weights = './initmodel/mobilenetv3-small-55df8e1f.pth' state_dict = torch.load(weights) elif abs(0.75 - width_mult) < eps: weights = './initmodel/mobilenetv3-small-0.75-86c972c3.pth' state_dict = torch.load(weights) else: raise RuntimeError("Not support width_mult: {}".format(width_mult)) load_and_convert(net, state_dict) return net
if name == 'main': import torch
####################################################################### 请问各位大佬这样构建有问题吗,在deeplabv3_plus.py中,这样的写法如何更改呢??请up住及各位大佬解答一下!!!!! class MobileNetV3(nn.Module): def init(self, pretrained=True): super(MobileNetV3, self).init() from functools import partial
在class DeepLab(nn.Module)中: elif backbone == "mobilenetv3": self.backbone = MobileNetV3(pretrained=pretrained) in_channels = 320 low_level_channels = 24