forked from R-JunmingChen/NEMG-IQA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_block.py
254 lines (207 loc) · 8.69 KB
/
basic_block.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import torch
import torch.nn as nn
import numpy as np
from util import Context
import torch
import torch.nn.functional as F
from torch.autograd import Variable
from math import exp
try:
from torch.hub import load_state_dict_from_url
except ImportError:
from torch.utils.model_zoo import load_url as load_state_dict_from_url
config = Context().get_config()
logger = Context().get_logger()
def get_log_diff_fn_2(exponent=0.2):
def log_diff_fn(in_a, in_b):
diff = torch.abs(in_a - in_b)
val = torch.pow(diff, exponent)
return val
return log_diff_fn
def double_value(x):
return x*x;
def identity_mutiply(x,y):
indentity=x
out=x*y
out=torch.cat([out,indentity],dim=1)
return out
def identity_add(x,y):
indentity=x
out=x+y
out=torch.cat([out,indentity],dim=1)
return out
class SEBlock(nn.Module):
def __init__(self, channel, reduction=6):
super(SEBlock, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, int(channel/reduction), bias=False),
nn.ReLU(inplace=True),
nn.Linear(int(channel/reduction), channel, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.shape
y = self.avg_pool(x).reshape(b, c)
y = self.fc(y).reshape(b, c, 1, 1)
return x * y.expand_as(x)
class ConcatSEBlock(nn.Module):
def __init__(self, channel, reduction=6):
super(ConcatSEBlock, self).__init__()
self.se_block = SEBlock(channel,reduction)
def forward(self, x,concat_list):
out = x
if isinstance(concat_list, list):
for ele in concat_list:
out = torch.cat([out, ele], dim=1)
else:
out = torch.cat([out, concat_list], dim=1)
out = self.se_block(out)
return out
class MutiplyConvTrans(nn.Module):
def __init__(self, inplanes, planes, kernel_size, stride=1, padding=1, bias=False, groups=1):
super(MutiplyConvTrans, self).__init__()
self.convtrans = nn.ConvTranspose2d(inplanes, planes, kernel_size=kernel_size, stride=stride,
padding=padding, groups=groups, bias=bias)
def forward(self, x, concat_list):
out = x
if isinstance(concat_list, list):
for ele in concat_list:
out= out*ele
else:
out = out*concat_list
out = self.convtrans(out)
return out
class MutiplyConv(nn.Module):
def __init__(self, inplanes, planes, kernel_size, stride=1, padding=1, bias=False, groups=1):
super(MutiplyConv, self).__init__()
self.conv = nn.Conv2d(inplanes, planes, kernel_size=kernel_size, stride=stride,
padding=padding, groups=groups, bias=bias)
def forward(self, x, concat_list):
out = x
if isinstance(concat_list, list):
for ele in concat_list:
out= out*ele
else:
out = out*concat_list
out = self.convtrans(out)
return out
class ConcatConvTrans(nn.Module):
def __init__(self, inplanes, planes, kernel_size, stride=1, padding=1, bias=False, groups=1):
super(ConcatConvTrans, self).__init__()
self.convtrans = nn.ConvTranspose2d(inplanes, planes, kernel_size=kernel_size, stride=stride,
padding=padding, groups=groups, bias=bias)
def forward(self, x, concat_list):
out = x
if isinstance(concat_list, list):
for ele in concat_list:
out = torch.cat([out, ele], dim=1)
else:
out = torch.cat([out, concat_list], dim=1)
out = self.convtrans(out)
return out
class ConcatConv(nn.Module):
def __init__(self, inplanes, planes, kernel_size, stride=1, padding=1, bias=False, groups=1):
super(ConcatConv, self).__init__()
self.conv = nn.Conv2d(inplanes, planes, kernel_size=kernel_size, stride=stride,
padding=padding, groups=groups, bias=bias)
def forward(self, x, concat_list):
out = x
if isinstance(concat_list, list):
for ele in concat_list:
out = torch.cat([out, ele], dim=1)
else:
out = torch.cat([out, concat_list], dim=1)
out = self.conv(out)
return out
class UpSampleFilter(nn.Module):
def __init__(self):
super(UpSampleFilter, self).__init__()
upsample_filter = nn.ConvTranspose2d(1, 1, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2),
output_padding=(1, 1), bias=False)
k = np.float32([1, 4, 6, 4, 1])
k = np.outer(k, k)
k5x5 = (k / k.sum()).reshape((1, 1, 5, 5))
k5x5 *= 4
upsample_weight = torch.from_numpy(k5x5)
upsample_filter.weight = torch.nn.Parameter(upsample_weight)
upsample_filter.weight.requires_grad = False
self.filter = upsample_filter
def forward(self, x):
if x.shape[1] > 1:
batch_size, channel, height, width = x.shape
out = self.filter(x.reshape(batch_size * channel, 1, height, width))
height, width = out.shape[2], out.shape[3]
out = out.reshape(batch_size, channel, height, width)
else:
out = self.filter(x)
return out
class DownSampleFilter(nn.Module):
def __init__(self):
super(DownSampleFilter, self).__init__()
downsample_filter = nn.Conv2d(1, 1, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2))
k = np.float32([1, 4, 6, 4, 1])
k = np.outer(k, k)
k5x5 = (k / k.sum()).reshape((1, 1, 5, 5))
downsample_weight = torch.from_numpy(k5x5)
downsample_filter.weight = torch.nn.Parameter(downsample_weight)
downsample_filter.bias.data.fill_(0)
downsample_filter.weight.requires_grad = False
downsample_filter.bias.requires_grad = False
self.filter = downsample_filter
def forward(self, x):
if x.shape[1] > 1:
batch_size, channel, height, width = x.shape
out = self.filter(x.reshape(batch_size * channel, 1, height, width))
height, width = out.shape[2], out.shape[3]
out = out.reshape(batch_size, channel, height, width)
else:
out = self.filter(x)
return out
class SobelX(nn.Module):
def __init__(self):
super(SobelX, self).__init__()
sobel_x_filter = nn.Conv2d(1, 1, kernel_size=(3, 3), stride=1, padding=(1, 1))
sobel_x_val = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]],
dtype='float32').reshape((1, 1, 3, 3))
sobel_x_filter_weight = torch.from_numpy(sobel_x_val)
sobel_x_filter.weight = torch.nn.Parameter(sobel_x_filter_weight)
sobel_x_filter.bias.data.fill_(0)
sobel_x_filter.weight.requires_grad = False
sobel_x_filter.bias.requires_grad = False
self.filter = sobel_x_filter
def forward(self, x):
if x.shape[1] > 1:
batch_size, channel, height, width = x.shape
out = self.filter(x.reshape(batch_size * channel, 1, height, width))
height, width = out.shape[2], out.shape[3]
out = out.reshape(batch_size, channel, height, width)
else:
out = self.filter(x)
return out
class SobelY(nn.Module):
def __init__(self):
super(SobelY, self).__init__()
sobel_y_filter = nn.Conv2d(1, 1, kernel_size=(3, 3), stride=1, padding=(1, 1))
sobel_y_val = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]],
dtype='float32').reshape((1, 1, 3, 3))
sobel_y_filter_weight = torch.from_numpy(sobel_y_val)
sobel_y_filter.weight = torch.nn.Parameter(sobel_y_filter_weight)
sobel_y_filter.bias.data.fill_(0)
sobel_y_filter.weight.requires_grad = False
sobel_y_filter.bias.requires_grad = False
self.filter = sobel_y_filter
def forward(self, x):
if x.shape[1] > 1:
batch_size, channel, height, width = x.shape
out = self.filter(x.reshape(batch_size * channel, 1, height, width))
height, width = out.shape[2], out.shape[3]
out = out.reshape(batch_size, channel, height, width)
else:
out = self.filter(x)
return out
def shave_border( feat_map,ign=4):
if ign > 0:
return feat_map[:, :, ign:-ign, ign:-ign]
else:
return feat_map