-
Notifications
You must be signed in to change notification settings - Fork 59
/
FairNAS_A.py
132 lines (117 loc) · 4.14 KB
/
FairNAS_A.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
import math
import torch.nn as nn
import torch.nn.functional as F
def stem(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
nn.BatchNorm2d(oup),
nn.ReLU6(inplace=True)
)
def separable_conv(inp, oup):
return nn.Sequential(
nn.Conv2d(inp, inp, 3, 1, 1, groups=inp, bias=False),
nn.BatchNorm2d(inp),
nn.ReLU6(inplace=True),
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
def conv_before_pooling(inp, oup):
return nn.Sequential(
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
nn.ReLU6(inplace=True)
)
class InvertedResidual(nn.Module):
def __init__(self, inp, oup, kernel_size, stride, expand_ratio):
super(InvertedResidual, self).__init__()
assert stride in [1, 2]
self.stride = stride
padding = kernel_size // 2
hidden_dim = round(inp * expand_ratio)
self.use_res_connect = self.stride == 1 and inp == oup
self.conv1 = nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False)
self.bn1 = nn.BatchNorm2d(hidden_dim)
self.conv2 = nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, padding, groups=hidden_dim, bias=False)
self.bn2 = nn.BatchNorm2d(hidden_dim)
self.conv3 = nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False)
self.bn3 = nn.BatchNorm2d(oup)
def forward(self, x):
inputs = x
x = self.conv1(x)
x = self.bn1(x)
x = F.relu6(x, inplace=True)
x = self.conv2(x)
x = self.bn2(x)
x = F.relu6(x, inplace=True)
x = self.conv3(x)
x = self.bn3(x)
if self.use_res_connect:
return inputs + x
else:
return x
class FairNasA(nn.Module):
def __init__(self, n_class=1000, input_size=224):
super(FairNasA, self).__init__()
assert input_size % 32 == 0
mb_config = [
# expansion, out_channel, kernel_size, stride,
[3, 32, 7, 2],
[3, 32, 3, 1],
[3, 40, 7, 2],
[6, 40, 3, 1],
[6, 40, 7, 1],
[3, 40, 3, 1],
[3, 80, 3, 2],
[6, 80, 7, 1],
[6, 80, 7, 1],
[3, 80, 5, 1],
[6, 96, 3, 1],
[3, 96, 5, 1],
[3, 96, 5, 1],
[6, 96, 3, 1],
[6, 192, 3, 2],
[6, 192, 7, 1],
[6, 192, 3, 1],
[6, 192, 7, 1],
[6, 320, 5, 1],
]
input_channel = 16
last_channel = 1280
self.last_channel = last_channel
self.stem = stem(3, 32, 2)
self.separable_conv = separable_conv(32, 16)
self.mb_module = list()
for t, c, k, s in mb_config:
output_channel = c
self.mb_module.append(InvertedResidual(input_channel, output_channel, k, s, expand_ratio=t))
input_channel = output_channel
self.mb_module = nn.Sequential(*self.mb_module)
self.conv_before_pooling = conv_before_pooling(input_channel, self.last_channel)
self.classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(self.last_channel, n_class),
)
self._initialize_weights()
def forward(self, x):
x = self.stem(x)
x = self.separable_conv(x)
x = self.mb_module(x)
x = self.conv_before_pooling(x)
x = x.mean(3).mean(2)
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):
n = m.weight.size(0) # fan-out
init_range = 1.0 / math.sqrt(n)
m.weight.data.uniform_(-init_range, init_range)
m.bias.data.zero_()