-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshufflenet.py
141 lines (108 loc) · 4.42 KB
/
shufflenet.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
import torch
import torch.nn as nn
from .ops import blocks
from .utils import export, config, load_from_local_or_url
from typing import Any, OrderedDict, List
class ShuffleAddBlock(nn.Module):
def __init__(self, channels, g: int = 2):
super().__init__()
self.branch1 = nn.Sequential(OrderedDict([
('gconv1', blocks.Conv2d1x1Block(channels, channels, groups=g)),
('shuffle', blocks.ChannelShuffle(groups=g)),
('dwconv', blocks.DepthwiseConv2dBN(channels, channels, 3)),
('gconv2', blocks.Conv2d1x1BN(channels, channels, groups=g))
]))
self.branch2 = nn.Identity()
self.combine = blocks.Combine('ADD')
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.combine([self.branch1(x), self.branch2(x)])
x = self.relu(x)
return x
class ShuffleCatBlock(nn.Module):
def __init__(
self,
inp,
oup,
g: int = 2,
stride: int = 2,
apply_first: bool = True
):
super().__init__()
g_1st = g if apply_first else 1
self.branch1 = nn.Sequential(OrderedDict([
('gconv1', blocks.Conv2d1x1Block(inp, oup, groups=g_1st)),
('shuffle', blocks.ChannelShuffle(groups=g)),
('dwconv', blocks.DepthwiseConv2dBN(oup, oup, stride=stride)),
('gconv2', blocks.Conv2d1x1BN(oup, oup, groups=g))
]))
self.branch2 = nn.AvgPool2d(kernel_size=3, stride=stride, padding=1)
self.combine = blocks.Combine('CONCAT')
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.combine([self.branch1(x), self.branch2(x)])
x = self.relu(x)
return x
@export
class ShuffleNet(nn.Module):
def __init__(
self,
in_channels: int = 3,
num_classes: int = 1000,
repeats: List[int] = [4, 84, 4],
channels: List[int] = [],
g: int = 3,
thumbnail: bool = False,
**kwargs: Any
):
super().__init__()
FRONT_S = 1 if thumbnail else 2
self.features = nn.Sequential(OrderedDict([
('stem', blocks.Conv2dBlock(in_channels, channels[0], 3, FRONT_S)),
('stage1', nn.MaxPool2d(kernel_size=3, stride=2, padding=1) if not thumbnail else nn.Identity()),
('stage2', self.make_layers(repeats[0], channels[0], channels[1], g)),
('stage3', self.make_layers(repeats[1], channels[1], channels[2], g)),
('stage4', self.make_layers(repeats[2], channels[2], channels[3], g))
]))
self.pool = nn.AdaptiveAvgPool2d((1, 1))
self.classifier = nn.Linear(channels[3], num_classes)
@staticmethod
def make_layers(repeat, inp, oup, g):
layers = [ShuffleCatBlock(inp, oup - inp, stride=2, g=g)]
for _ in range(repeat - 1):
layers.append(ShuffleAddBlock(oup, g=g))
return blocks.Stage(layers)
def forward(self, x):
x = self.features(x)
x = self.pool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
def _shufflenet(
repeats: List[int],
channels: List[int],
g: int,
pretrained: bool = False,
pth: str = None,
progress: bool = True,
**kwargs: Any
):
model = ShuffleNet(repeats=repeats, channels=channels, g=g, **kwargs)
if pretrained:
load_from_local_or_url(model, pth, kwargs.get('url', None), progress)
return model
@export
def shufflenet_g1(pretrained: bool = False, pth: str = None, progress: bool = True, **kwargs: Any):
return _shufflenet([4, 8, 4], [24, 144, 288, 576], 1, pretrained, pth, progress, **kwargs)
@export
def shufflenet_g2(pretrained: bool = False, pth: str = None, progress: bool = True, **kwargs: Any):
return _shufflenet([4, 8, 4], [24, 200, 400, 800], 2, pretrained, pth, progress, **kwargs)
@export
def shufflenet_g3(pretrained: bool = False, pth: str = None, progress: bool = True, **kwargs: Any):
return _shufflenet([4, 8, 4], [24, 240, 480, 960], 3, pretrained, pth, progress, **kwargs)
@export
def shufflenet_g4(pretrained: bool = False, pth: str = None, progress: bool = True, **kwargs: Any):
return _shufflenet([4, 8, 4], [24, 272, 544, 1088], 4, pretrained, pth, progress, **kwargs)
@export
def shufflenet_g8(pretrained: bool = False, pth: str = None, progress: bool = True, **kwargs: Any):
return _shufflenet([4, 8, 4], [24, 384, 768, 1536], 8, pretrained, pth, progress, **kwargs)