-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshufflenetv2.py
150 lines (117 loc) · 4.89 KB
/
shufflenetv2.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
142
143
144
145
146
147
148
149
150
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 ShuffleBlockV2(nn.Module):
def __init__(
self,
inp,
oup,
stride: int = 1,
dilation: int = 1
):
super().__init__()
self.inp = inp
self.oup = oup // 2
self.stride = stride if dilation == 1 else 1
self.dilation = max(1, dilation // stride)
self.split = None
if stride == 1:
self.inp = inp // 2
self.split = blocks.ChannelChunk(2)
self.branch1 = nn.Identity()
if stride != 1:
self.branch1 = nn.Sequential(OrderedDict([
('dwconv', blocks.DepthwiseConv2dBN(self.inp, self.inp, stride=self.stride, dilation=self.dilation)),
('1x1', blocks.Conv2d1x1Block(self.inp, self.oup))
]))
self.branch2 = nn.Sequential(OrderedDict([
('1x1-1', blocks.Conv2d1x1Block(self.inp, self.oup)),
('dwconv', blocks.DepthwiseConv2dBN(self.oup, self.oup, stride=self.stride, dilation=self.dilation)),
('1x1-2', blocks.Conv2d1x1Block(self.oup, self.oup))
]))
self.combine = blocks.Combine('CONCAT')
self.shuffle = blocks.ChannelShuffle(groups=2)
def forward(self, x):
if isinstance(self.branch1, nn.Identity):
x1, x2 = self.split(x)
x2 = self.branch2(x2)
else:
x1 = self.branch1(x)
x2 = self.branch2(x)
out = self.combine([x1, x2])
out = self.shuffle(out)
return out
@export
class ShuffleNetV2(nn.Module):
def __init__(
self,
in_channels: int = 3,
num_classes: int = 1000,
repeats: List[int] = [4, 8, 4],
channels: List[int] = [24, 48, 96, 192, 1024],
dropout_rate: float = 0.0,
dilations: List[int] = None,
thumbnail: bool = False,
**kwargs: Any
):
super().__init__()
self.block = ShuffleBlockV2
dilations = dilations or [1, 1, 1, 1]
assert len(dilations) == 4, ''
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(3, stride=2, padding=1) if not thumbnail else nn.Identity()),
('stage2', self.make_layers(repeats[0], channels[0], channels[1], dilations[1])),
('stage3', self.make_layers(repeats[1], channels[1], channels[2], dilations[2])),
('stage4', self.make_layers(repeats[2], channels[2], channels[3], dilations[3])),
]))
self.features[-1].append(
blocks.Conv2d1x1Block(channels[3], channels[4])
)
self.pool = nn.AdaptiveAvgPool2d((1, 1))
self.classifier = nn.Sequential(
nn.Dropout(dropout_rate, inplace=True),
nn.Linear(channels[4], num_classes)
)
self.features[-1].out_channels = channels[-1]
self.features[-2].out_channels = channels[-3]
self.features[-3].out_channels = channels[-4]
def make_layers(self, repeat, inp, oup, dilation):
layers = [self.block(inp, oup, stride=2, dilation=dilation)]
for _ in range(repeat - 1):
layers.append(self.block(oup, oup, dilation=dilation))
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_v2(
repeats: List[int],
channels: List[int],
pretrained: bool = False,
pth: str = None,
progress: bool = True,
**kwargs: Any
):
model = ShuffleNetV2(repeats=repeats, channels=channels, **kwargs)
if pretrained:
load_from_local_or_url(model, pth, kwargs.get('url', None), progress)
return model
@export
def shufflenet_v2_x0_5(pretrained: bool = False, pth: str = None, progress: bool = True, **kwargs: Any):
return _shufflenet_v2([4, 8, 4], [24, 48, 96, 192, 1024], pretrained, pth, progress, **kwargs)
@export
def shufflenet_v2_x1_0(pretrained: bool = False, pth: str = None, progress: bool = True, **kwargs: Any):
return _shufflenet_v2([4, 8, 4], [24, 116, 232, 464, 1024], pretrained, pth, progress, **kwargs)
@export
def shufflenet_v2_x1_5(pretrained: bool = False, pth: str = None, progress: bool = True, **kwargs: Any):
return _shufflenet_v2([4, 8, 4], [24, 176, 352, 704, 1024], pretrained, pth, progress, **kwargs)
@export
@config(url='https://github.com/ffiirree/cv-models/releases/download/v0.0.1-shufflenets-weights/shufflenet_v2_x2_0-35a176a6.pth')
def shufflenet_v2_x2_0(pretrained: bool = False, pth: str = None, progress: bool = True, **kwargs: Any):
return _shufflenet_v2([4, 8, 4], [24, 244, 488, 976, 2048], pretrained, pth, progress, **kwargs)