This repository has been archived by the owner on May 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
mobilenet.py
54 lines (44 loc) · 1.87 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
'''MobileNet in PyTorch.
Modified based on (https://github.com/kuangliu/pytorch-cifar/blob/master/models/mobilenet.py)
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
class Block(nn.Module):
'''Depthwise conv + Pointwise conv'''
def __init__(self, in_planes, out_planes, stride=1):
super(Block, self).__init__()
self.conv1 = nn.Conv2d(in_planes, in_planes, kernel_size=3,
stride=stride, padding=1, groups=in_planes, bias=False)
self.conv2 = nn.Conv2d(in_planes, out_planes,
kernel_size=1, stride=1, padding=0, bias=False)
def forward(self, x):
out = F.relu(self.conv1(x))
out = F.relu(self.conv2(out))
return out
class MobileNet(nn.Module):
# (128,2) means conv planes=128, conv stride=2, by default conv stride=1
cfg = [64, (128, 2), 128, (256, 2), 256, (512, 2),
512, 512, 512, 512, 512, (1024, 2), 1024]
def __init__(self, num_classes=10):
super(MobileNet, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3,
stride=1, padding=1, bias=False)
self.layers = self._make_layers(in_planes=32)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.linear = nn.Linear(1024, num_classes)
def _make_layers(self, in_planes):
layers = []
for x in self.cfg:
out_planes = x if isinstance(x, int) else x[0]
stride = 1 if isinstance(x, int) else x[1]
layers.append(Block(in_planes, out_planes, stride))
in_planes = out_planes
return nn.Sequential(*layers)
def forward(self, x):
out = F.relu(self.conv1(x))
out = self.layers(out)
out = self.avgpool(out)
out = torch.flatten(out, 1) # out.view(out.size(0), -1)
out = self.linear(out)
return out