mobilenet.py
2.04 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class MobileNet1(nn.Module):
def __init__(self, inchannel=3, num_classes=10):
super(MobileNet1, self).__init__()
self.num_classes = num_classes
def conv_bn(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
nn.BatchNorm2d(oup),
nn.Hardswish()
#nn.Hardsigmoid(inplace=True)
# nn.LeakyReLU(negative_slope=0.1, inplace=True)
# nn.ReLU(inplace=True)
)
def conv_dw(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
nn.BatchNorm2d(inp),
nn.Hardswish(),
#nn.Hardsigmoid(inplace=True),
# nn.LeakyReLU(negative_slope=0.1, inplace=True),
# nn.ReLU(inplace=True),
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
nn.Hardswish()
#nn.Hardsigmoid(inplace=True)
# nn.LeakyReLU(negative_slope=0.1, inplace=True)
# nn.ReLU(inplace=True),
)
self.model = nn.Sequential(
conv_bn(inchannel, 32, 1),
conv_dw( 32, 64, 1),
conv_dw( 64, 128, 2),
conv_dw(128, 128, 1),
conv_dw(128, 256, 2),
conv_dw(256, 256, 1),
conv_dw(256, 512, 2),
conv_dw(512, 512, 1),
conv_dw(512, 512, 1),
conv_dw(512, 512, 1),
conv_dw(512, 512, 1),
conv_dw(512, 512, 1),
conv_dw(512, 1024, 2),
conv_dw(1024, 1024, 1),
nn.AdaptiveAvgPool2d(1)
)
self.fc = nn.Linear(1024, self.num_classes)
def forward(self, x):
x, act_scale = self.model(x)
# x = self.model(x)
x = x.view(x.size(0), -1)
# x = self.fc(x)
x = self.fc(x, act_scale)
return x