-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobilenet.py
67 lines (47 loc) · 1.72 KB
/
mobilenet.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class mobilenet(nn.Module):
def con_dw(self,in_channel,out_channel,stride):
return nn.Sequential(
nn.Conv2d(in_channel,in_channel,kernel_size=3,stride=stride,
padding=1,groups=in_channel,bias=False),
nn.BatchNorm2d(in_channel),
nn.ReLU(),
nn.Conv2d(in_channel,out_channel,kernel_size=1,
padding=0,bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU()
)
def __init__(self):
super(mobilenet, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(32),
nn.ReLU()
)
self.conv_dw2 = self.con_dw(32,32,1)
self.conv_dw3 = self.con_dw(32,64,2)
self.conv_dw4 = self.con_dw(64,64,1)
self.conv_dw5 = self.con_dw(64,128,2)
self.conv_dw6 = self.con_dw(128,128,1)
self.conv_dw7 = self.con_dw(128,256,2)
self.conv_dw8 = self.con_dw(256, 256, 1)
self.conv_dw9 = self.con_dw(256, 512, 2)
self.fc = nn.Linear(512,10)
def forward(self,x):
out = self.conv1(x)
out = self.conv_dw2(out)
out = self.conv_dw3(out)
out = self.conv_dw4(out)
out = self.conv_dw5(out)
out = self.conv_dw6(out)
out = self.conv_dw7(out)
out = self.conv_dw8(out)
out = self.conv_dw9(out)
out = F.avg_pool2d(out,2)
out = out.view(out.size(0),-1)
out = self.fc(out)
return out
def mobilenetv1_small():
return mobilenet()