Spaces:
Running
on
T4
Running
on
T4
File size: 7,171 Bytes
561c629 |
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 |
"""
EDSR common.py
Since a lot of models are developed on top of EDSR, here we include some common functions from EDSR.
In this repository, the common functions is used by edsr_esa.py and ipt.py
"""
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
def default_conv(in_channels, out_channels, kernel_size, bias=True):
return nn.Conv2d(
in_channels, out_channels, kernel_size, padding=(kernel_size // 2), bias=bias
)
class MeanShift(nn.Conv2d):
def __init__(
self,
rgb_range,
rgb_mean=(0.4488, 0.4371, 0.4040),
rgb_std=(1.0, 1.0, 1.0),
sign=-1,
):
super(MeanShift, self).__init__(3, 3, kernel_size=1)
std = torch.Tensor(rgb_std)
self.weight.data = torch.eye(3).view(3, 3, 1, 1) / std.view(3, 1, 1, 1)
self.bias.data = sign * rgb_range * torch.Tensor(rgb_mean) / std
for p in self.parameters():
p.requires_grad = False
class BasicBlock(nn.Sequential):
def __init__(
self,
conv,
in_channels,
out_channels,
kernel_size,
stride=1,
bias=False,
bn=True,
act=nn.ReLU(True),
):
m = [conv(in_channels, out_channels, kernel_size, bias=bias)]
if bn:
m.append(nn.BatchNorm2d(out_channels))
if act is not None:
m.append(act)
super(BasicBlock, self).__init__(*m)
class ESA(nn.Module):
def __init__(self, esa_channels, n_feats):
super(ESA, self).__init__()
f = esa_channels
self.conv1 = nn.Conv2d(n_feats, f, kernel_size=1)
self.conv_f = nn.Conv2d(f, f, kernel_size=1)
# self.conv_max = conv(f, f, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(f, f, kernel_size=3, stride=2, padding=0)
self.conv3 = nn.Conv2d(f, f, kernel_size=3, padding=1)
# self.conv3_ = conv(f, f, kernel_size=3, padding=1)
self.conv4 = nn.Conv2d(f, n_feats, kernel_size=1)
self.sigmoid = nn.Sigmoid()
# self.relu = nn.ReLU(inplace=True)
def forward(self, x):
c1_ = self.conv1(x)
c1 = self.conv2(c1_)
v_max = F.max_pool2d(c1, kernel_size=7, stride=3)
c3 = self.conv3(v_max)
# v_range = self.relu(self.conv_max(v_max))
# c3 = self.relu(self.conv3(v_range))
# c3 = self.conv3_(c3)
c3 = F.interpolate(
c3, (x.size(2), x.size(3)), mode="bilinear", align_corners=False
)
cf = self.conv_f(c1_)
c4 = self.conv4(c3 + cf)
m = self.sigmoid(c4)
return x * m
# class ESA(nn.Module):
# def __init__(self, esa_channels, n_feats, conv=nn.Conv2d):
# super(ESA, self).__init__()
# f = n_feats // 4
# self.conv1 = conv(n_feats, f, kernel_size=1)
# self.conv_f = conv(f, f, kernel_size=1)
# self.conv_max = conv(f, f, kernel_size=3, padding=1)
# self.conv2 = conv(f, f, kernel_size=3, stride=2, padding=0)
# self.conv3 = conv(f, f, kernel_size=3, padding=1)
# self.conv3_ = conv(f, f, kernel_size=3, padding=1)
# self.conv4 = conv(f, n_feats, kernel_size=1)
# self.sigmoid = nn.Sigmoid()
# self.relu = nn.ReLU(inplace=True)
#
# def forward(self, x):
# c1_ = (self.conv1(x))
# c1 = self.conv2(c1_)
# v_max = F.max_pool2d(c1, kernel_size=7, stride=3)
# v_range = self.relu(self.conv_max(v_max))
# c3 = self.relu(self.conv3(v_range))
# c3 = self.conv3_(c3)
# c3 = F.interpolate(c3, (x.size(2), x.size(3)), mode='bilinear', align_corners=False)
# cf = self.conv_f(c1_)
# c4 = self.conv4(c3 + cf)
# m = self.sigmoid(c4)
#
# return x * m
class ResBlock(nn.Module):
def __init__(
self,
conv,
n_feats,
kernel_size,
bias=True,
bn=False,
act=nn.ReLU(True),
res_scale=1,
esa_block=True,
depth_wise_kernel=7,
):
super(ResBlock, self).__init__()
m = []
for i in range(2):
m.append(conv(n_feats, n_feats, kernel_size, bias=bias))
if bn:
m.append(nn.BatchNorm2d(n_feats))
if i == 0:
m.append(act)
self.body = nn.Sequential(*m)
self.esa_block = esa_block
if self.esa_block:
esa_channels = 16
self.c5 = nn.Conv2d(
n_feats,
n_feats,
depth_wise_kernel,
padding=depth_wise_kernel // 2,
groups=n_feats,
bias=True,
)
self.esa = ESA(esa_channels, n_feats)
self.res_scale = res_scale
def forward(self, x):
res = self.body(x).mul(self.res_scale)
res += x
if self.esa_block:
res = self.esa(self.c5(res))
return res
class Upsampler(nn.Sequential):
def __init__(self, conv, scale, n_feats, bn=False, act=False, bias=True):
m = []
if (scale & (scale - 1)) == 0: # Is scale = 2^n?
for _ in range(int(math.log(scale, 2))):
m.append(conv(n_feats, 4 * n_feats, 3, bias))
m.append(nn.PixelShuffle(2))
if bn:
m.append(nn.BatchNorm2d(n_feats))
if act == "relu":
m.append(nn.ReLU(True))
elif act == "prelu":
m.append(nn.PReLU(n_feats))
elif scale == 3:
m.append(conv(n_feats, 9 * n_feats, 3, bias))
m.append(nn.PixelShuffle(3))
if bn:
m.append(nn.BatchNorm2d(n_feats))
if act == "relu":
m.append(nn.ReLU(True))
elif act == "prelu":
m.append(nn.PReLU(n_feats))
else:
raise NotImplementedError
super(Upsampler, self).__init__(*m)
class LiteUpsampler(nn.Sequential):
def __init__(self, conv, scale, n_feats, n_out=3, bn=False, act=False, bias=True):
m = []
m.append(conv(n_feats, n_out * (scale**2), 3, bias))
m.append(nn.PixelShuffle(scale))
# if (scale & (scale - 1)) == 0: # Is scale = 2^n?
# for _ in range(int(math.log(scale, 2))):
# m.append(conv(n_feats, 4 * n_out, 3, bias))
# m.append(nn.PixelShuffle(2))
# if bn:
# m.append(nn.BatchNorm2d(n_out))
# if act == 'relu':
# m.append(nn.ReLU(True))
# elif act == 'prelu':
# m.append(nn.PReLU(n_out))
# elif scale == 3:
# m.append(conv(n_feats, 9 * n_out, 3, bias))
# m.append(nn.PixelShuffle(3))
# if bn:
# m.append(nn.BatchNorm2d(n_out))
# if act == 'relu':
# m.append(nn.ReLU(True))
# elif act == 'prelu':
# m.append(nn.PReLU(n_out))
# else:
# raise NotImplementedError
super(LiteUpsampler, self).__init__(*m)
|