|
|
@ -7,9 +7,9 @@ from common import lut
|
|
|
|
from common import layers
|
|
|
|
from common import layers
|
|
|
|
from pathlib import Path
|
|
|
|
from pathlib import Path
|
|
|
|
from . import sdylut
|
|
|
|
from . import sdylut
|
|
|
|
|
|
|
|
from models.base import SRNetBase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SDYNetx1(SRNetBase):
|
|
|
|
class SDYNetx1(nn.Module):
|
|
|
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
super(SDYNetx1, self).__init__()
|
|
|
|
super(SDYNetx1, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.scale = scale
|
|
|
@ -20,16 +20,6 @@ class SDYNetx1(nn.Module):
|
|
|
|
self.stage1_D = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage1_D = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage1_Y = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage1_Y = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
|
|
|
|
|
|
|
|
def forward_stage(self, x, scale, percieve_pattern, stage):
|
|
|
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
|
|
|
x = percieve_pattern(x)
|
|
|
|
|
|
|
|
x = stage(x)
|
|
|
|
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h, w, scale, scale)
|
|
|
|
|
|
|
|
x = x.permute(0,1,2,4,3,5)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*scale, w*scale)
|
|
|
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
@ -39,7 +29,6 @@ class SDYNetx1(nn.Module):
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_Y, self.stage1_Y)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_Y, self.stage1_Y)
|
|
|
|
output /= 3
|
|
|
|
output /= 3
|
|
|
|
x = output
|
|
|
|
x = output
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
return x
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
@ -56,7 +45,7 @@ class SDYNetx1(nn.Module):
|
|
|
|
return loss_fn
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SDYNetx2(nn.Module):
|
|
|
|
class SDYNetx2(SRNetBase):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
super(SDYNetx2, self).__init__()
|
|
|
|
super(SDYNetx2, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.scale = scale
|
|
|
@ -70,16 +59,6 @@ class SDYNetx2(nn.Module):
|
|
|
|
self.stage2_D = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage2_D = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage2_Y = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage2_Y = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
|
|
|
|
|
|
|
|
def forward_stage(self, x, scale, percieve_pattern, stage):
|
|
|
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
|
|
|
x = percieve_pattern(x)
|
|
|
|
|
|
|
|
x = stage(x)
|
|
|
|
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h, w, scale, scale)
|
|
|
|
|
|
|
|
x = x.permute(0,1,2,4,3,5)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*scale, w*scale)
|
|
|
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
@ -89,14 +68,12 @@ class SDYNetx2(nn.Module):
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_Y, self.stage1_Y)
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_Y, self.stage1_Y)
|
|
|
|
output /= 3
|
|
|
|
output /= 3
|
|
|
|
x = output
|
|
|
|
x = output
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
output = torch.zeros([b*c, 1, h*self.scale, w*self.scale], dtype=x.dtype, device=x.device)
|
|
|
|
output = torch.zeros([b*c, 1, h*self.scale, w*self.scale], dtype=x.dtype, device=x.device)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_S, self.stage2_S)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_S, self.stage2_S)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_D, self.stage2_D)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_D, self.stage2_D)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_Y, self.stage2_Y)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_Y, self.stage2_Y)
|
|
|
|
output /= 3
|
|
|
|
output /= 3
|
|
|
|
x = output
|
|
|
|
x = output
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
return x
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
@ -115,7 +92,7 @@ class SDYNetx2(nn.Module):
|
|
|
|
return F.mse_loss(pred/255, target/255)
|
|
|
|
return F.mse_loss(pred/255, target/255)
|
|
|
|
return loss_fn
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
class SDYNetx3(nn.Module):
|
|
|
|
class SDYNetx3(SRNetBase):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
super(SDYNetx3, self).__init__()
|
|
|
|
super(SDYNetx3, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.scale = scale
|
|
|
@ -132,16 +109,6 @@ class SDYNetx3(nn.Module):
|
|
|
|
self.stage3_D = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage3_D = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage3_Y = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage3_Y = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
|
|
|
|
|
|
|
|
def forward_stage(self, x, scale, percieve_pattern, stage):
|
|
|
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
|
|
|
x = percieve_pattern(x)
|
|
|
|
|
|
|
|
x = stage(x)
|
|
|
|
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h, w, scale, scale)
|
|
|
|
|
|
|
|
x = x.permute(0,1,2,4,3,5)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*scale, w*scale)
|
|
|
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
@ -151,21 +118,18 @@ class SDYNetx3(nn.Module):
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_Y, self.stage1_Y)
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_Y, self.stage1_Y)
|
|
|
|
output /= 3
|
|
|
|
output /= 3
|
|
|
|
x = output
|
|
|
|
x = output
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
output = torch.zeros([b*c, 1, h, w], dtype=x.dtype, device=x.device)
|
|
|
|
output = torch.zeros([b*c, 1, h, w], dtype=x.dtype, device=x.device)
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_S, self.stage2_S)
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_S, self.stage2_S)
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_D, self.stage2_D)
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_D, self.stage2_D)
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_Y, self.stage2_Y)
|
|
|
|
output += self.forward_stage(x, 1, self._extract_pattern_Y, self.stage2_Y)
|
|
|
|
output /= 3
|
|
|
|
output /= 3
|
|
|
|
x = output
|
|
|
|
x = output
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
output = torch.zeros([b*c, 1, h*self.scale, w*self.scale], dtype=x.dtype, device=x.device)
|
|
|
|
output = torch.zeros([b*c, 1, h*self.scale, w*self.scale], dtype=x.dtype, device=x.device)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_S, self.stage3_S)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_S, self.stage3_S)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_D, self.stage3_D)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_D, self.stage3_D)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_Y, self.stage3_Y)
|
|
|
|
output += self.forward_stage(x, self.scale, self._extract_pattern_Y, self.stage3_Y)
|
|
|
|
output /= 3
|
|
|
|
output /= 3
|
|
|
|
x = output
|
|
|
|
x = output
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
return x
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
@ -222,7 +186,6 @@ class SDYNetR90x1(nn.Module):
|
|
|
|
output += torch.rot90(self.forward_stage(rotated, self.scale, self._extract_pattern_Y, self.stage1_Y), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output += torch.rot90(self.forward_stage(rotated, self.scale, self._extract_pattern_Y, self.stage1_Y), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output /= 4*3
|
|
|
|
output /= 4*3
|
|
|
|
x = output
|
|
|
|
x = output
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
return x
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
@ -238,7 +201,7 @@ class SDYNetR90x1(nn.Module):
|
|
|
|
return F.mse_loss(pred/255, target/255)
|
|
|
|
return F.mse_loss(pred/255, target/255)
|
|
|
|
return loss_fn
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
class SDYNetR90x2(nn.Module):
|
|
|
|
class SDYNetR90x2(SRNetBase):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
super(SDYNetR90x2, self).__init__()
|
|
|
|
super(SDYNetR90x2, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.scale = scale
|
|
|
@ -252,16 +215,6 @@ class SDYNetR90x2(nn.Module):
|
|
|
|
self.stage2_D = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage2_D = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage2_Y = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
self.stage2_Y = layers.UpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
|
|
|
|
|
|
|
|
def forward_stage(self, x, scale, percieve_pattern, stage):
|
|
|
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
|
|
|
x = percieve_pattern(x)
|
|
|
|
|
|
|
|
x = stage(x)
|
|
|
|
|
|
|
|
x = round_func(x)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h, w, scale, scale)
|
|
|
|
|
|
|
|
x = x.permute(0,1,2,4,3,5)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*scale, w*scale)
|
|
|
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
x = x.view(b*c, 1, h, w)
|
|
|
|
x = x.view(b*c, 1, h, w)
|
|
|
@ -275,7 +228,7 @@ class SDYNetR90x2(nn.Module):
|
|
|
|
output_1 += torch.rot90(self.forward_stage(rotated, 1, self._extract_pattern_D, self.stage1_D), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output_1 += torch.rot90(self.forward_stage(rotated, 1, self._extract_pattern_D, self.stage1_D), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output_1 += torch.rot90(self.forward_stage(rotated, 1, self._extract_pattern_Y, self.stage1_Y), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output_1 += torch.rot90(self.forward_stage(rotated, 1, self._extract_pattern_Y, self.stage1_Y), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output_1 /= 4*3
|
|
|
|
output_1 /= 4*3
|
|
|
|
x = round_func(output_1)
|
|
|
|
x = output_1
|
|
|
|
output_2 = torch.zeros([b*c, 1, h*self.scale, w*self.scale], dtype=x.dtype, device=x.device)
|
|
|
|
output_2 = torch.zeros([b*c, 1, h*self.scale, w*self.scale], dtype=x.dtype, device=x.device)
|
|
|
|
output_2 += self.forward_stage(x, self.scale, self._extract_pattern_S, self.stage2_S)
|
|
|
|
output_2 += self.forward_stage(x, self.scale, self._extract_pattern_S, self.stage2_S)
|
|
|
|
output_2 += self.forward_stage(x, self.scale, self._extract_pattern_D, self.stage2_D)
|
|
|
|
output_2 += self.forward_stage(x, self.scale, self._extract_pattern_D, self.stage2_D)
|
|
|
@ -286,7 +239,7 @@ class SDYNetR90x2(nn.Module):
|
|
|
|
output_2 += torch.rot90(self.forward_stage(rotated, self.scale, self._extract_pattern_D, self.stage2_D), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output_2 += torch.rot90(self.forward_stage(rotated, self.scale, self._extract_pattern_D, self.stage2_D), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output_2 += torch.rot90(self.forward_stage(rotated, self.scale, self._extract_pattern_Y, self.stage2_Y), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output_2 += torch.rot90(self.forward_stage(rotated, self.scale, self._extract_pattern_Y, self.stage2_Y), k=-rotations_count, dims=[-2, -1])
|
|
|
|
output_2 /= 4*3
|
|
|
|
output_2 /= 4*3
|
|
|
|
x = round_func(output_2)
|
|
|
|
x = output_2
|
|
|
|
x = x.view(b, c, h*self.scale, w*self.scale)
|
|
|
|
x = x.view(b, c, h*self.scale, w*self.scale)
|
|
|
|
return x
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|