-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrouped_resnet.py
More file actions
166 lines (153 loc) · 5.3 KB
/
grouped_resnet.py
File metadata and controls
166 lines (153 loc) · 5.3 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import torch.nn as nn
import torch
from primitives import Conv2d, Reduction_A, Stem, Reduction_B, Inception_ResNet_A, Inception_ResNet_B, Inception_ResNet_C
class Grouped_ResNet_A(nn.Module):
def __init__(self, in_channels, scale=1.0, groups=1):
super(Grouped_ResNet_A, self).__init__()
self.scale = scale
self.branch_0 = Conv2d(
in_channels, 32, 1, stride=1, padding=0,
groups=groups, bias=False
)
self.branch_1 = nn.Sequential(
Conv2d(
in_channels, 32, 1, stride=1, padding=0,
groups=groups, bias=False
),
Conv2d(
32, 32, 3, stride=1, padding=1,
groups=groups, bias=False
)
)
self.branch_2 = nn.Sequential(
Conv2d(
in_channels, 32, 1, stride=1, padding=0,
groups=groups, bias=False
),
Conv2d(
32, 48, 3, stride=1, padding=1,
groups=groups, bias=False
),
Conv2d(
48, 64, 3, stride=1, padding=1,
groups=groups, bias=False
)
)
self.conv = nn.Conv2d(
128, in_channels, 1, stride=1, padding=0,
groups=groups, bias=True
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x0 = self.branch_0(x)
x1 = self.branch_1(x)
x2 = self.branch_2(x)
x_res = torch.cat((x0, x1, x2), dim=1)
x_res = self.conv(x_res)
return self.relu(x + self.scale * x_res)
class Grouped_ResNet_B(nn.Module):
def __init__(self, in_channels, scale=1.0, groups=1):
super(Grouped_ResNet_B, self).__init__()
self.scale = scale
self.branch_0 = Conv2d(
in_channels, 192, 1, stride=1, padding=0,
groups=groups, bias=False
)
self.branch_1 = nn.Sequential(
Conv2d(
in_channels, 128, 1, stride=1, padding=0,
groups=groups, bias=False
),
Conv2d(
128, 160, (1, 7), stride=1, padding=(0, 3),
groups=groups, bias=False
),
Conv2d(
160, 192, (7, 1), stride=1, padding=(3, 0),
groups=groups, bias=False
)
)
self.conv = nn.Conv2d(
384, in_channels, 1, stride=1, padding=0,
groups=groups, bias=True
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x0 = self.branch_0(x)
x1 = self.branch_1(x)
x_res = torch.cat((x0, x1), dim=1)
x_res = self.conv(x_res)
return self.relu(x + self.scale * x_res)
class Grouped_ResNet_C(nn.Module):
def __init__(self, in_channels, scale=1.0, groups=1, activation=True):
super(Grouped_ResNet_C, self).__init__()
self.scale = scale
self.activation = activation
self.branch_0 = Conv2d(
in_channels, 192, 1, stride=1, padding=0,
groups=groups, bias=False
)
self.branch_1 = nn.Sequential(
Conv2d(
in_channels, 192, 1, stride=1, padding=0,
groups=groups, bias=False
),
Conv2d(
192, 224, (1, 3), stride=1, padding=(0, 1),
groups=groups, bias=False
),
Conv2d(
224, 256, (3, 1), stride=1, padding=(1, 0),
groups=groups, bias=False
)
)
self.conv = nn.Conv2d(
448, in_channels, 1, stride=1, padding=0,
groups=groups, bias=True
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x0 = self.branch_0(x)
x1 = self.branch_1(x)
x_res = torch.cat((x0, x1), dim=1)
x_res = self.conv(x_res)
if self.activation:
return self.relu(x + self.scale * x_res)
return x + self.scale * x_res
class GroupedResNet(nn.Module):
def __init__(
self,
in_channels=3,
classes=100,
s0_depth=10,
s1_depth=20,
s2_depth=10,
k=256, l=256, m=384, n=384, groups=1):
super(GroupedResNet, self).__init__()
blocks = []
blocks.append(Stem(in_channels, 320))
for i in range(s0_depth):
blocks.append(Inception_ResNet_A(320, 0.17, groups))
blocks.append(Reduction_A(320, k, l, m, n))
for i in range(s1_depth):
blocks.append(Inception_ResNet_B(1088, 0.10, groups))
blocks.append(Reduction_B(1088))
for i in range(s2_depth - 1):
blocks.append(Inception_ResNet_C(2080, 0.20, groups))
blocks.append(Inception_ResNet_C(2080, scale=0.20, activation=False))
self.features = nn.Sequential(*blocks)
self.conv = Conv2d(
2080, 1536, 1, stride=1, padding=0,
bias=False
)
self.global_average_pooling = nn.AdaptiveAvgPool2d((1, 1))
self.dropout = nn.Dropout(0.2)
self.linear = nn.Linear(1536, classes)
def forward(self, x):
_x = self.features(x)
_x = self.conv(_x)
_x = self.global_average_pooling(_x)
_x = _x.view(_x.size(0), -1)
_x = self.dropout(_x)
_x = self.linear(_x)
return _x