Spaces:
Sleeping
Sleeping
Update archs/network.py
Browse files- archs/network.py +58 -30
archs/network.py
CHANGED
|
@@ -3,10 +3,10 @@ import torch.nn as nn
|
|
| 3 |
import torch.nn.functional as F
|
| 4 |
import functools
|
| 5 |
try:
|
| 6 |
-
from .arch_util import EBlock
|
| 7 |
from .arch_util_freq import EBlock_freq
|
| 8 |
except:
|
| 9 |
-
from arch_util import EBlock
|
| 10 |
from arch_util_freq import EBlock_freq
|
| 11 |
|
| 12 |
|
|
@@ -14,11 +14,13 @@ class Network(nn.Module):
|
|
| 14 |
|
| 15 |
def __init__(self, img_channel=3,
|
| 16 |
width=16,
|
| 17 |
-
|
|
|
|
| 18 |
enc_blk_nums=[],
|
| 19 |
dec_blk_nums=[],
|
| 20 |
dilations = [1],
|
| 21 |
-
extra_depth_wise = False
|
|
|
|
| 22 |
super(Network, self).__init__()
|
| 23 |
|
| 24 |
self.intro = nn.Conv2d(in_channels=img_channel, out_channels=width, kernel_size=3, padding=1, stride=1, groups=1,
|
|
@@ -36,7 +38,7 @@ class Network(nn.Module):
|
|
| 36 |
for num in enc_blk_nums:
|
| 37 |
self.encoders.append(
|
| 38 |
nn.Sequential(
|
| 39 |
-
*[
|
| 40 |
)
|
| 41 |
)
|
| 42 |
self.downs.append(
|
|
@@ -44,9 +46,13 @@ class Network(nn.Module):
|
|
| 44 |
)
|
| 45 |
chan = chan * 2
|
| 46 |
|
| 47 |
-
self.
|
| 48 |
nn.Sequential(
|
| 49 |
-
*[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
for num in dec_blk_nums:
|
|
@@ -59,21 +65,16 @@ class Network(nn.Module):
|
|
| 59 |
chan = chan // 2
|
| 60 |
self.decoders.append(
|
| 61 |
nn.Sequential(
|
| 62 |
-
*[EBlock(chan, extra_depth_wise=extra_depth_wise) for _ in range(num)]
|
| 63 |
)
|
| 64 |
)
|
| 65 |
|
| 66 |
self.padder_size = 2 ** len(self.encoders)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
#
|
| 71 |
-
#
|
| 72 |
-
# extra_depth_wise = False) for i in range(residual_layers)])
|
| 73 |
-
|
| 74 |
-
# ResidualBlock_noBN_f = functools.partial(ResidualBlock_noBN, nf = width * self.padder_size)
|
| 75 |
-
# self.recon_trunk_light = make_layer(ResidualBlock_noBN_f, residual_layers)
|
| 76 |
-
|
| 77 |
|
| 78 |
|
| 79 |
def forward(self, input):
|
|
@@ -83,26 +84,43 @@ class Network(nn.Module):
|
|
| 83 |
input = self.check_image_size(input)
|
| 84 |
x = self.intro(input)
|
| 85 |
|
| 86 |
-
encs = []
|
|
|
|
| 87 |
# i = 0
|
| 88 |
for encoder, down in zip(self.encoders, self.downs):
|
| 89 |
x = encoder(x)
|
|
|
|
|
|
|
| 90 |
# print(i, x.shape)
|
| 91 |
-
encs.append(x)
|
| 92 |
x = down(x)
|
| 93 |
# i += 1
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# print('3', x.shape)
|
| 97 |
# apply the mask
|
| 98 |
# x = x * mask
|
| 99 |
|
| 100 |
# x = self.recon_trunk_light(x)
|
| 101 |
-
|
| 102 |
-
for decoder, up,
|
| 103 |
x = up(x)
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
x = self.ending(x)
|
| 108 |
x = x + input
|
|
@@ -121,19 +139,29 @@ if __name__ == '__main__':
|
|
| 121 |
img_channel = 3
|
| 122 |
width = 32
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
enc_blks = [1, 2, 3]
|
| 125 |
-
|
|
|
|
| 126 |
dec_blks = [3, 1, 1]
|
| 127 |
-
residual_layers =
|
| 128 |
-
dilations = [1, 4]
|
|
|
|
|
|
|
| 129 |
|
| 130 |
net = Network(img_channel=img_channel,
|
| 131 |
width=width,
|
| 132 |
-
|
|
|
|
| 133 |
enc_blk_nums=enc_blks,
|
| 134 |
dec_blk_nums=dec_blks,
|
| 135 |
-
dilations = dilations
|
| 136 |
-
|
|
|
|
|
|
|
| 137 |
# NAF = NAFNet(img_channel=img_channel, width=width, middle_blk_num=middle_blk_num,
|
| 138 |
# enc_blk_nums=enc_blks, dec_blk_nums=dec_blks)
|
| 139 |
|
|
|
|
| 3 |
import torch.nn.functional as F
|
| 4 |
import functools
|
| 5 |
try:
|
| 6 |
+
from .arch_util import EBlock
|
| 7 |
from .arch_util_freq import EBlock_freq
|
| 8 |
except:
|
| 9 |
+
from arch_util import EBlock
|
| 10 |
from arch_util_freq import EBlock_freq
|
| 11 |
|
| 12 |
|
|
|
|
| 14 |
|
| 15 |
def __init__(self, img_channel=3,
|
| 16 |
width=16,
|
| 17 |
+
middle_blk_num_enc=1,
|
| 18 |
+
middle_blk_num_dec=1,
|
| 19 |
enc_blk_nums=[],
|
| 20 |
dec_blk_nums=[],
|
| 21 |
dilations = [1],
|
| 22 |
+
extra_depth_wise = False,
|
| 23 |
+
ksize = 5):
|
| 24 |
super(Network, self).__init__()
|
| 25 |
|
| 26 |
self.intro = nn.Conv2d(in_channels=img_channel, out_channels=width, kernel_size=3, padding=1, stride=1, groups=1,
|
|
|
|
| 38 |
for num in enc_blk_nums:
|
| 39 |
self.encoders.append(
|
| 40 |
nn.Sequential(
|
| 41 |
+
*[EBlock_freq(chan, extra_depth_wise=extra_depth_wise) for _ in range(num)]
|
| 42 |
)
|
| 43 |
)
|
| 44 |
self.downs.append(
|
|
|
|
| 46 |
)
|
| 47 |
chan = chan * 2
|
| 48 |
|
| 49 |
+
self.middle_blks_enc = \
|
| 50 |
nn.Sequential(
|
| 51 |
+
*[EBlock_freq(chan, extra_depth_wise=extra_depth_wise) for _ in range(middle_blk_num_enc)]
|
| 52 |
+
)
|
| 53 |
+
self.middle_blks_dec = \
|
| 54 |
+
nn.Sequential(
|
| 55 |
+
*[EBlock(chan, dilations = dilations, extra_depth_wise=extra_depth_wise) for _ in range(middle_blk_num_dec)]
|
| 56 |
)
|
| 57 |
|
| 58 |
for num in dec_blk_nums:
|
|
|
|
| 65 |
chan = chan // 2
|
| 66 |
self.decoders.append(
|
| 67 |
nn.Sequential(
|
| 68 |
+
*[EBlock(chan,dilations = dilations, extra_depth_wise=extra_depth_wise) for _ in range(num)]
|
| 69 |
)
|
| 70 |
)
|
| 71 |
|
| 72 |
self.padder_size = 2 ** len(self.encoders)
|
| 73 |
|
| 74 |
+
# self.facs = nn.ModuleList([nn.Identity(), nn.Identity(),
|
| 75 |
+
# nn.Identity(),
|
| 76 |
+
# nn.Identity())
|
| 77 |
+
# self.kconv_deblur = KernelConv2D(ksize=ksize, act = True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
def forward(self, input):
|
|
|
|
| 84 |
input = self.check_image_size(input)
|
| 85 |
x = self.intro(input)
|
| 86 |
|
| 87 |
+
# encs = []
|
| 88 |
+
facs = []
|
| 89 |
# i = 0
|
| 90 |
for encoder, down in zip(self.encoders, self.downs):
|
| 91 |
x = encoder(x)
|
| 92 |
+
# x_fac = fac(x)
|
| 93 |
+
facs.append(x)
|
| 94 |
# print(i, x.shape)
|
| 95 |
+
# encs.append(x)
|
| 96 |
x = down(x)
|
| 97 |
# i += 1
|
| 98 |
|
| 99 |
+
# we apply the encoder transforms
|
| 100 |
+
x_light = self.middle_blks_enc(x)
|
| 101 |
+
# calculate the fac at this level
|
| 102 |
+
# x_fac = self.facs[-1](x)
|
| 103 |
+
# facs.append(x_fac)
|
| 104 |
+
# apply the decoder transforms
|
| 105 |
+
x = self.middle_blks_dec(x_light)
|
| 106 |
+
# apply the fac transform over this step
|
| 107 |
+
x = x + x_light
|
| 108 |
+
|
| 109 |
# print('3', x.shape)
|
| 110 |
# apply the mask
|
| 111 |
# x = x * mask
|
| 112 |
|
| 113 |
# x = self.recon_trunk_light(x)
|
| 114 |
+
i = 0
|
| 115 |
+
for decoder, up, fac_skip in zip(self.decoders, self.ups, facs[::-1]):
|
| 116 |
x = up(x)
|
| 117 |
+
if i == 2: # in the toppest decoder step
|
| 118 |
+
x = x + fac_skip
|
| 119 |
+
x = decoder(x)
|
| 120 |
+
else:
|
| 121 |
+
x = x + fac_skip
|
| 122 |
+
x = decoder(x)
|
| 123 |
+
i+=1
|
| 124 |
|
| 125 |
x = self.ending(x)
|
| 126 |
x = x + input
|
|
|
|
| 139 |
img_channel = 3
|
| 140 |
width = 32
|
| 141 |
|
| 142 |
+
# enc_blks = [1, 1, 1, 3]
|
| 143 |
+
# middle_blk_num = 3
|
| 144 |
+
# dec_blks = [2, 1, 1, 1]
|
| 145 |
+
|
| 146 |
enc_blks = [1, 2, 3]
|
| 147 |
+
middle_blk_num_enc = 2
|
| 148 |
+
middle_blk_num_dec = 2
|
| 149 |
dec_blks = [3, 1, 1]
|
| 150 |
+
residual_layers = None
|
| 151 |
+
dilations = [1, 4, 9]
|
| 152 |
+
extra_depth_wise = True
|
| 153 |
+
ksize = 5
|
| 154 |
|
| 155 |
net = Network(img_channel=img_channel,
|
| 156 |
width=width,
|
| 157 |
+
middle_blk_num_enc=middle_blk_num_enc,
|
| 158 |
+
middle_blk_num_dec= middle_blk_num_dec,
|
| 159 |
enc_blk_nums=enc_blks,
|
| 160 |
dec_blk_nums=dec_blks,
|
| 161 |
+
dilations = dilations,
|
| 162 |
+
extra_depth_wise = extra_depth_wise,
|
| 163 |
+
ksize = ksize)
|
| 164 |
+
|
| 165 |
# NAF = NAFNet(img_channel=img_channel, width=width, middle_blk_num=middle_blk_num,
|
| 166 |
# enc_blk_nums=enc_blks, dec_blk_nums=dec_blks)
|
| 167 |
|