-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResNet_models_Custom.py
More file actions
208 lines (169 loc) · 7.73 KB
/
ResNet_models_Custom.py
File metadata and controls
208 lines (169 loc) · 7.73 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import torch
import torchvision.models as models
import numpy as np
from ResNet import *
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
from torch.nn import Parameter, Softmax
import torch.nn.functional as F
from Multi_head import MHSA
class BasicConv2d(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1):
super(BasicConv2d, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes,
kernel_size=kernel_size, stride=stride,
padding=padding, dilation=dilation, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
return x
class Classifier_Module(nn.Module):
def __init__(self,dilation_series,padding_series,NoLabels, input_channel):
super(Classifier_Module, self).__init__()
self.conv2d_list = nn.ModuleList()
for dilation,padding in zip(dilation_series,padding_series):
self.conv2d_list.append(nn.Conv2d(input_channel,NoLabels,kernel_size=3,stride=1, padding =padding, dilation = dilation,bias = True))
for m in self.conv2d_list:
m.weight.data.normal_(0, 0.01)
def forward(self, x):
out = self.conv2d_list[0](x)
for i in range(len(self.conv2d_list)-1):
out += self.conv2d_list[i+1](x)
return out
class CAM_Module(nn.Module):
""" Channel attention module"""
def __init__(self):
super(CAM_Module, self).__init__()
self.gamma = Parameter(torch.zeros(1))
self.softmax = Softmax(dim=-1)
def forward(self,x):
"""
inputs :
x : input feature maps( B X C X H X W)
returns :
out : attention value + input feature
attention: B X C X C
"""
m_batchsize, C, height, width = x.size()
proj_query = x.view(m_batchsize, C, -1)
proj_key = x.view(m_batchsize, C, -1).permute(0, 2, 1)
energy = torch.bmm(proj_query, proj_key)
energy_new = torch.max(energy, -1, keepdim=True)[0].expand_as(energy)-energy
attention = self.softmax(energy_new)
proj_value = x.view(m_batchsize, C, -1)
out = torch.bmm(attention, proj_value)
out = out.view(m_batchsize, C, height, width)
out = self.gamma*out + x
return out
## Channel Attention (CA) Layer
class CALayer(nn.Module):
def __init__(self, channel, reduction=16):
super(CALayer, self).__init__()
# global average pooling: feature --> point
self.avg_pool = nn.AdaptiveAvgPool2d(1)
# feature channel downscale and upscale --> channel weight
self.conv_du = nn.Sequential(
nn.Conv2d(channel, channel // reduction, 1, padding=0, bias=True),
nn.ReLU(inplace=True),
nn.Conv2d(channel // reduction, channel, 1, padding=0, bias=True),
nn.Sigmoid()
)
def forward(self, x):
y = self.avg_pool(x)
y = self.conv_du(y)
return x * y
## Residual Channel Attention Block (RCAB)
class RCAB(nn.Module):
def __init__(
self, n_feat, kernel_size=3, reduction=16,
bias=True, bn=False, act=nn.ReLU(True), res_scale=1):
super(RCAB, self).__init__()
modules_body = []
for i in range(2):
modules_body.append(self.default_conv(n_feat, n_feat, kernel_size, bias=bias))
if bn: modules_body.append(nn.BatchNorm2d(n_feat))
if i == 0: modules_body.append(act)
modules_body.append(CALayer(n_feat, reduction))
self.body = nn.Sequential(*modules_body)
self.res_scale = res_scale
def default_conv(self, in_channels, out_channels, kernel_size, bias=True):
return nn.Conv2d(in_channels, out_channels, kernel_size,padding=(kernel_size // 2), bias=bias)
def forward(self, x):
res = self.body(x)
#res = self.body(x).mul(self.res_scale)
res += x
return res
class Triple_Conv(nn.Module):
def __init__(self, in_channel, out_channel):
super(Triple_Conv, self).__init__()
self.reduce = nn.Sequential(
BasicConv2d(in_channel, out_channel, 1),
BasicConv2d(out_channel, out_channel, 3, padding=1),
BasicConv2d(out_channel, out_channel, 3, padding=1)
)
def forward(self, x):
return self.reduce(x)
class _DenseAsppBlock(nn.Sequential):
""" ConvNet block for building DenseASPP. """
def __init__(self, input_num, num1, num2, dilation_rate, drop_out, bn_start=True):
super(_DenseAsppBlock, self).__init__()
self.asppconv = torch.nn.Sequential()
if bn_start:
self.asppconv = nn.Sequential(
nn.BatchNorm2d(input_num),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=input_num, out_channels=num1, kernel_size=1),
nn.BatchNorm2d(num1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=num1, out_channels=num2, kernel_size=3,
dilation=dilation_rate, padding=dilation_rate)
)
else:
self.asppconv = nn.Sequential(
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=input_num, out_channels=num1, kernel_size=1),
nn.BatchNorm2d(num1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=num1, out_channels=num2, kernel_size=3,
dilation=dilation_rate, padding=dilation_rate)
)
self.drop_rate = drop_out
def forward(self, _input):
#feature = super(_DenseAsppBlock, self).forward(_input)
feature = self.asppconv(_input)
if self.drop_rate > 0:
feature = F.dropout2d(feature, p=self.drop_rate, training=self.training)
return feature
class multi_scale_aspp(nn.Sequential):
""" ConvNet block for building DenseASPP. """
def __init__(self, channel):
super(multi_scale_aspp, self).__init__()
self.ASPP_3 = _DenseAsppBlock(input_num=channel, num1=channel * 2, num2=channel, dilation_rate=3,
drop_out=0.1, bn_start=False)
self.ASPP_6 = _DenseAsppBlock(input_num=channel * 2, num1=channel * 2, num2=channel,
dilation_rate=6, drop_out=0.1, bn_start=True)
self.ASPP_12 = _DenseAsppBlock(input_num=channel * 3, num1=channel * 2, num2=channel,
dilation_rate=12, drop_out=0.1, bn_start=True)
self.ASPP_18 = _DenseAsppBlock(input_num=channel * 4, num1=channel * 2, num2=channel,
dilation_rate=18, drop_out=0.1, bn_start=True)
self.ASPP_24 = _DenseAsppBlock(input_num=channel * 5, num1=channel * 2, num2=channel,
dilation_rate=24, drop_out=0.1, bn_start=True)
self.classification = nn.Sequential(
nn.Dropout2d(p=0.1),
nn.Conv2d(in_channels=channel * 6, out_channels=channel, kernel_size=1, padding=0)
)
def forward(self, _input):
#feature = super(_DenseAsppBlock, self).forward(_input)
aspp3 = self.ASPP_3(_input)
feature = torch.cat((aspp3, _input), dim=1)
aspp6 = self.ASPP_6(feature)
feature = torch.cat((aspp6, feature), dim=1)
aspp12 = self.ASPP_12(feature)
feature = torch.cat((aspp12, feature), dim=1)
aspp18 = self.ASPP_18(feature)
feature = torch.cat((aspp18, feature), dim=1)
aspp24 = self.ASPP_24(feature)
feature = torch.cat((aspp24, feature), dim=1)
aspp_feat = self.classification(feature)
return aspp_feat