-
Notifications
You must be signed in to change notification settings - Fork 34
/
ghostnet.py
148 lines (121 loc) · 5.34 KB
/
ghostnet.py
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
# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File : ghostnet.py
# Author :CodeCat
# version :python 3.7
# Software :Pycharm
"""
import torch
import torch.nn as nn
class DWConv3x3BNReLU(nn.Sequential):
def __init__(self, in_channel, out_channel, stride, groups):
super(DWConv3x3BNReLU, self).__init__(
nn.Conv2d(in_channels=in_channel, out_channels=out_channel, kernel_size=3, stride=stride, padding=1, groups=groups, bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU6(inplace=True),
)
class SqueezeAndExcite(nn.Module):
def __init__(self, in_channel, out_channel, divide=4):
super(SqueezeAndExcite, self).__init__()
mid_channel = in_channel // divide
self.pool = nn.AdaptiveAvgPool2d((1, 1))
self.SEblock = nn.Sequential(
nn.Linear(in_features=in_channel, out_features=mid_channel),
nn.ReLU6(inplace=True),
nn.Linear(in_features=mid_channel, out_features=out_channel),
nn.ReLU6(inplace=True),
)
def forward(self, x):
b, c, h, w = x.size()
out = self.pool(x)
out = torch.flatten(out, start_dim=1)
out = self.SEblock(out)
out = out.view(b, c, 1, 1)
return out * x
class GhostModule(nn.Module):
def __init__(self, in_channel, out_channel, s=2, kernel_size=1, stride=1, use_relu=True):
super(GhostModule, self).__init__()
intrinsic_channel = out_channel // s
ghost_channel = intrinsic_channel * (s - 1)
self.primary_conv = nn.Sequential(
nn.Conv2d(in_channels=in_channel, out_channels=intrinsic_channel, kernel_size=kernel_size, stride=stride, padding=(kernel_size-1)//2, bias=False),
nn.BatchNorm2d(intrinsic_channel),
nn.ReLU(inplace=True) if use_relu else nn.Sequential()
)
self.cheap_op = DWConv3x3BNReLU(in_channel=intrinsic_channel, out_channel=ghost_channel, stride=stride, groups=intrinsic_channel)
def forward(self, x):
x1 = self.primary_conv(x)
x2 = self.cheap_op(x1)
out = torch.cat([x1, x2], dim=1)
return out
class GhostBottleneck(nn.Module):
def __init__(self, in_channel, mid_channel, out_channel, kernel_size, stride, use_se):
super(GhostBottleneck, self).__init__()
self.stride = stride
self.bottleneck = nn.Sequential(
GhostModule(in_channel=in_channel, out_channel=mid_channel, use_relu=True),
DWConv3x3BNReLU(in_channel=mid_channel, out_channel=mid_channel, stride=stride, groups=mid_channel) if self.stride > 1 else nn.Sequential(),
SqueezeAndExcite(in_channel=mid_channel, out_channel=mid_channel) if use_se else nn.Sequential(),
GhostModule(in_channel=mid_channel, out_channel=out_channel, use_relu=False)
)
if self.stride > 1:
self.shortcut = DWConv3x3BNReLU(in_channel=in_channel, out_channel=out_channel, stride=stride, groups=1)
else:
self.shortcut = nn.Conv2d(in_channels=in_channel, out_channels=out_channel, kernel_size=1, stride=1)
def forward(self, x):
out = self.bottleneck(x)
residual = self.shortcut(x)
out += residual
return out
class GhostNet(nn.Module):
def __init__(self, num_classes=1000):
super(GhostNet, self).__init__()
self.first_conv = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(16),
nn.ReLU6(inplace=True),
)
ghost_model_setting = [
# in, mid, out, kernel, stride, use_se
[16, 16, 16, 3, 1, False],
[16, 48, 24, 3, 2, False],
[24, 72, 24, 3, 1, False],
[24, 72, 40, 5, 2, True],
[40, 120, 40, 5, 1, True],
[40, 240, 80, 3, 2, False],
[80, 200, 80, 3, 1, False],
[80, 184, 80, 3, 1, False],
[80, 184, 80, 3, 1, False],
[80, 480, 112, 3, 1, True],
[112, 672, 112, 3, 1, True],
[112, 672, 160, 5, 2, True],
[160, 960, 160, 5, 1, False],
[160, 960, 160, 5, 1, True],
[160, 960, 160, 5, 1, False],
[160, 960, 160, 5, 1, True],
]
layers = []
for in_channel, mid_channel, out_channel, kernel_size, stride, use_se in ghost_model_setting:
layers.append(GhostBottleneck(in_channel=in_channel, mid_channel=mid_channel, out_channel=out_channel, kernel_size=kernel_size, stride=stride, use_se=use_se))
self.features = nn.Sequential(*layers)
self.last_stage = nn.Sequential(
nn.Conv2d(in_channels=160, out_channels=960, kernel_size=1, stride=1),
nn.BatchNorm2d(960),
nn.AdaptiveAvgPool2d((1, 1)),
nn.Conv2d(in_channels=960, out_channels=1280, kernel_size=1, stride=1),
nn.ReLU6(inplace=True)
)
self.classifier = nn.Linear(in_features=1280, out_features=num_classes)
def forward(self, x):
x = self.first_conv(x)
x = self.features(x)
x = self.last_stage(x)
x = torch.flatten(x, start_dim=1)
x = self.classifier(x)
return x
if __name__ == '__main__':
inputs = torch.randn(1, 3, 224, 224)
model = GhostNet(num_classes=10)
out = model(inputs)
print(out.shape)