MixNet
【摘要】
import torch
import torch.nn as nn
class HardSwish(nn.Module):
def __init__(self, inplace=True):
...
import torch
import torch.nn as nn
class HardSwish(nn.Module):
def __init__(self, inplace=True):
super(HardSwish, self).__init__()
self.relu6 = nn.ReLU6(inplace)
def forward(self, x):
return x*self.relu6(x+3)/6
def ConvBNActivation(in_channels,out_channels,kernel_size,stride,activate):
return nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=(kernel_size-1)//2),
nn.BatchNorm2d(out_channels),
nn.ReLU6(inplace=True) if activate == 'relu' else HardSwish(inplace=True)
)
def Conv1x1BNActivation(in_channels,out_channels,activate):
return nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1),
nn.BatchNorm2d(out_channels),
nn.ReLU6(inplace=True) if activate == 'relu' else HardSwish(inplace=True)
)
def Conv1x1BN(in_channels,out_channels):
return nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1),
nn.BatchNorm2d(out_channels)
)
class MDConv(nn.Module):
def __init__(self, nchannels, kernel_sizes, stride):
super(MDConv,self).__init__()
self.nchannels = nchannels
self.groups = len(kernel_sizes)
self.split_channels = [nchannels // self.groups for _ in range(self.groups)]
self.split_channels[0] += nchannels - sum(self.split_channels)
self.layers = []
for i in range(self.groups):
self.layers.append(nn.Conv2d(in_channels=self.split_channels[i],out_channels=self.split_channels[i],
kernel_size=kernel_sizes[i], stride=stride,padding=int(kernel_sizes[i]//2), groups=self.split_channels[i]))
def forward(self, x):
split_x = torch.split(x, self.split_channels, dim=1)
outputs = [layer(sp_x) for layer,sp_x in zip(self.layers, split_x)]
return torch.cat(outputs, dim=1)
class SqueezeAndExcite(nn.Module):
def __init__(self, nchannels, squeeze_channels, se_ratio=1):
super(SqueezeAndExcite, self).__init__()
squeeze_channels = int(squeeze_channels * se_ratio)
self.SEblock = nn.Sequential(
nn.Conv2d(in_channels=nchannels, out_channels=squeeze_channels, kernel_size=1, stride=1, padding=0),
nn.ReLU6(inplace=True),
nn.Conv2d(in_channels=squeeze_channels, out_channels=nchannels, kernel_size=1, stride=1, padding=0),
nn.Sigmoid(),
)
def forward(self, x):
out = torch.mean(x, (2, 3), keepdim=True)
out = self.SEblock(out)
return out * x
class MDConvBlock(nn.Module):
def __init__(self, in_channels,out_channels, kernel_sizes, stride,expand_ratio, activate='relu', se_ratio=1):
super(MDConvBlock,self).__init__()
self.stride = stride
self.se_ratio = se_ratio
mid_channels = in_channels * expand_ratio
self.expand_conv = Conv1x1BNActivation(in_channels, mid_channels, activate)
self.md_conv = nn.Sequential(
# in_channels,out_channels,groups, kernel_sizes, strides
MDConv(mid_channels, kernel_sizes, stride),
nn.BatchNorm2d(mid_channels),
nn.ReLU6(inplace=True) if activate == 'relu' else HardSwish(inplace=True),
)
if self.se_ratio > 0:
self.squeeze_excite = SqueezeAndExcite(mid_channels, in_channels)
self.projection_conv = Conv1x1BN(mid_channels,out_channels)
def forward(self, x):
x = self.expand_conv(x)
x = self.md_conv(x)
if self.se_ratio > 0:
x = self.squeeze_excite(x)
out = self.projection_conv(x)
return out
class MixNet(nn.Module):
mixnet_s = [(16, 16, [3], 1, 1, 'ReLU', 0.0),
(16, 24, [3], 2, 6, 'ReLU', 0.0),
(24, 24, [3], 1, 3, 'ReLU', 0.0),
(24, 40, [3, 5, 7], 2, 6, 'Swish', 0.5),
(40, 40, [3, 5], 1, 6, 'Swish', 0.5),
(40, 40, [3, 5], 1, 6, 'Swish', 0.5),
(40, 40, [3, 5], 1, 6, 'Swish', 0.5),
(40, 80, [3, 5, 7], 2, 6, 'Swish', 0.25),
(80, 80, [3, 5], 1, 6, 'Swish', 0.25),
(80, 80, [3, 5], 1, 6, 'Swish', 0.25),
(80, 120, [3, 5, 7], 1, 6, 'Swish', 0.5),
(120, 120, [3, 5, 7, 9], 1, 3, 'Swish', 0.5),
(120, 120, [3, 5, 7, 9], 1, 3, 'Swish', 0.5),
(120, 200, [3, 5, 7, 9, 11], 2, 6, 'Swish', 0.5),
(200, 200, [3, 5, 7, 9], 1, 6, 'Swish', 0.5),
(200, 200, [3, 5, 7, 9], 1, 6, 'Swish', 0.5)
]
mixnet_m = [(24, 24, [3], 1, 1, 'ReLU', 0.0),
(24, 32, [3, 5, 7], 2, 6, 'ReLU', 0.0),
(32, 32, [3], 1, 3, 'ReLU', 0.0),
(32, 40, [3, 5, 7, 9], 2, 6, 'Swish', 0.5),
(40, 40, [3, 5], 1, 6, 'Swish', 0.5),
(40, 40, [3, 5], 1, 6, 'Swish', 0.5),
(40, 40, [3, 5], 1, 6, 'Swish', 0.5),
(40, 80, [3, 5, 7], 2, 6, 'Swish', 0.25),
(80, 80, [3, 5, 7, 9], 1, 6, 'Swish', 0.25),
(80, 80, [3, 5, 7, 9], 1, 6, 'Swish', 0.25),
(80, 80, [3, 5, 7, 9], 1, 6, 'Swish', 0.25),
(80, 120, [3], 1, 6, 'Swish', 0.5),
(120, 120, [3, 5, 7, 9], 1, 3, 'Swish', 0.5),
(120, 120, [3, 5, 7, 9], 1, 3, 'Swish', 0.5),
(120, 120, [3, 5, 7, 9], 1, 3, 'Swish', 0.5),
(120, 200, [3, 5, 7, 9], 2, 6, 'Swish', 0.5),
(200, 200, [3, 5, 7, 9], 1, 6, 'Swish', 0.5),
(200, 200, [3, 5, 7, 9], 1, 6, 'Swish', 0.5),
(200, 200, [3, 5, 7, 9], 1, 6, 'Swish', 0.5)]
def __init__(self, type='mixnet_s'):
super(MixNet,self).__init__()
if type == 'mixnet_s':
config = self.mixnet_s
stem_channels = 16
elif type == 'mixnet_m':
config = self.mixnet_m
stem_channels = 24
self.stem = nn.Sequential(
nn.Conv2d(in_channels=3,out_channels=stem_channels,kernel_size=3,stride=2,padding=1),
nn.BatchNorm2d(stem_channels),
HardSwish(inplace=True),
)
layers = []
for in_channels, out_channels, kernel_sizes, stride, expand_ratio, activate, se_ratio in config:
layers.append(MDConvBlock(
in_channels,
out_channels,
kernel_sizes=kernel_sizes,
stride=stride,
expand_ratio=expand_ratio,
activate=activate,
se_ratio=se_ratio
))
self.bottleneck = nn.Sequential(*layers)
def init_params(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.Linear):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
x = self.stem(x)
out = self.bottleneck(x)
return out
if __name__ == '__main__':
model = MixNet(type ='mixnet_m')
print(model)
input = torch.randn(1, 3, 224, 224)
out = model(input)
print(out.shape)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
文章来源: wanghao.blog.csdn.net,作者:AI浩,版权归原作者所有,如需转载,请联系作者。
原文链接:wanghao.blog.csdn.net/article/details/121607274
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)