-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFCRSN.py
More file actions
142 lines (123 loc) · 5.96 KB
/
Copy pathFCRSN.py
File metadata and controls
142 lines (123 loc) · 5.96 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
import torch
from torch import nn
def conv3x1(in_planes, out_planes, stride=1, groups=1):
"""3x3 convolution with padding"""
return nn.Conv1d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, groups=groups, bias=False)
def conv1x1(in_planes, out_planes, stride=1):
"""1x1 convolution"""
return nn.Conv1d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
def conv5x1(in_planes,out_planes,stride=1):
return nn.Conv1d(in_planes,out_planes,kernel_size=5,stride=stride,bias=False)
class SEModule(nn.Module):
def __init__(self, channels, reduction=16):
super(SEModule, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1, padding=0)
self.relu = nn.ReLU(inplace=True)
self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1, padding=0)
self.sigmoid = nn.Sigmoid()
def forward(self, input):
x = self.avg_pool(input)
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.sigmoid(x)
return input * x
class Res2NetBottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, downsample=None,
gate=False,stride=1, scales=4, groups=1, se=False, norm_layer=None):
super(Res2NetBottleneck, self).__init__()
if planes % scales != 0:
raise ValueError('Planes must be divisible by scales')
if norm_layer is None:
norm_layer = nn.BatchNorm2d
bottleneck_planes = groups * planes
self.conv1 = conv1x1(inplanes, bottleneck_planes, stride)
self.bn1 = norm_layer(bottleneck_planes)
self.conv2 = nn.ModuleList([conv3x1(bottleneck_planes // scales, bottleneck_planes // scales, groups=groups) for _ in range(scales-1)])
self.bn2 = nn.ModuleList([norm_layer(bottleneck_planes // scales) for _ in range(scales-1)])
self.conv3 = conv1x1(bottleneck_planes, planes * self.expansion)
self.bn3 = norm_layer(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.gate= conv1x1(bottleneck_planes,bottleneck_planes//scales) if gate else None
self.se = SEModule(planes * self.expansion) if se else None
self.downsample = downsample
self.stride = stride
self.scales = scales
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
xs = torch.chunk(out, self.scales, 1)
ys = []
gate=[]
for s in range(self.scales):
if s == 0:
ys.append(xs[s])
elif s == 1:
ys.append(self.relu(self.bn2[s-1](self.conv2[s-1](xs[s]))))
else:
if gate:
value=self.gate(x)
ys.append(self.relu(self.bn2[s - 1](self.conv2[s - 1](xs[s] + value *ys[-1]))))
else:
ys.append(self.relu(self.bn2[s-1](self.conv2[s-1](xs[s] + ys[-1]))))
out = torch.cat(ys, 1)
out = self.conv3(out)
out = self.bn3(out)
if self.se is not None:
out = self.se(out)
if self.downsample is not None:
identity = self.downsample(identity)
out += identity
out = self.relu(out)
return out
class Res2RNN(nn.Module):
def __init__(self, layers, maxlength, groups=1,
gate=False,width=8,scales=4, se=False, norm_layer=None,inplanes=26):
super(Res2RNN, self).__init__()
planes = [int(width * scales * 2 ** i) for i in range(4)]
self.gate=gate
self.pre=inplanes
self.inplanes=planes[0]
self.maxlength=maxlength
self.pre=conv1x1(inplanes,planes[0])
self.layer1 = self._make_layer(Res2NetBottleneck, planes[0], layers[0], scales=scales, groups=groups, gate=gate,
se=se,
norm_layer=norm_layer)
self.layer2 = self._make_layer(Res2NetBottleneck, planes[1], layers[1], stride=2, scales=scales, groups=groups,
gate=gate,se=se, norm_layer=norm_layer)
self.layer3 = self._make_layer(Res2NetBottleneck, planes[2], layers[2], stride=2, scales=scales, groups=groups,
gate=gate,se=se, norm_layer=norm_layer)
self.layer4 = self._make_layer(Res2NetBottleneck, planes[3], layers[3], stride=2, scales=scales, groups=groups,
gate=gate,se=se, norm_layer=norm_layer)
self.presslayer=conv1x1(in_planes=1024,out_planes=maxlength)
self.roi=nn.AdaptiveMaxPool1d(output_size=1)
def _make_layer(self, block, planes, blocks, stride=1, scales=4, groups=1, gate=False,se=False, norm_layer=None):
if norm_layer is None:
norm_layer = nn.BatchNorm1d
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride),
norm_layer(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, downsample, stride=stride, gate=gate,scales=scales, groups=groups, se=se,
norm_layer=norm_layer))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, scales=scales, groups=groups,gate=gate, se=se, norm_layer=norm_layer))
return nn.Sequential(*layers)
def forward(self,x):
x= self.pre(x)
x= self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.roi(x)
output= self.presslayer(x).squeeze()
return output