|
|
|
@ -33,6 +33,510 @@ class SRNet(SRNetBase):
|
|
|
|
|
lut_model = srlut.SRLut.init_from_numpy(stage_lut)
|
|
|
|
|
return lut_model
|
|
|
|
|
|
|
|
|
|
class SRNetMixer(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixer, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1_1 = layers.UpscaleBlock(in_features=1, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage1_2 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage1_3 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage1_4 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
self.stage1_5 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage1_6 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage1_7 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage1_8 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
self.stage1_9 = layers.UpscaleBlock(in_features=1, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage1_10 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage1_11 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage1_12 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
self.stage1_13 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S1 = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1)
|
|
|
|
|
self._extract_pattern_S2 = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,2],[2,0],[2,2]], center=[1,1], window_size=3)
|
|
|
|
|
self._extract_pattern_S3 = layers.PercievePattern(receptive_field_idxes=[[0,1],[1,2],[2,1],[1,0]], center=[1,1], window_size=3)
|
|
|
|
|
self._extract_pattern_S4 = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,4],[4,4],[4,0]], center=[1,1], window_size=5)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S1, self.stage1_1)
|
|
|
|
|
output = torch.cat([output, self.forward_stage(x, self._extract_pattern_S2, self.stage1_2)], dim=1)
|
|
|
|
|
output = torch.cat([output, self.forward_stage(x, self._extract_pattern_S3, self.stage1_3)], dim=1)
|
|
|
|
|
output = torch.cat([output, self.forward_stage(x, self._extract_pattern_S4, self.stage1_4)], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_mix, self.stage1_5)
|
|
|
|
|
output = torch.cat([output, self.forward_stage(x, self._extract_pattern_mix, self.stage1_6)], dim=1)
|
|
|
|
|
output = torch.cat([output, self.forward_stage(x, self._extract_pattern_mix, self.stage1_7)], dim=1)
|
|
|
|
|
output = torch.cat([output, self.forward_stage(x, self._extract_pattern_mix, self.stage1_8)], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S1, self.stage1_9)
|
|
|
|
|
output = torch.cat([output, self.forward_stage(x, self._extract_pattern_S2, self.stage1_10)], dim=1)
|
|
|
|
|
output = torch.cat([output, self.forward_stage(x, self._extract_pattern_S3, self.stage1_11)], dim=1)
|
|
|
|
|
output = torch.cat([output, self.forward_stage(x, self._extract_pattern_S4, self.stage1_12)], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_mix, self.stage1_13)
|
|
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1) for _ in range(4)])
|
|
|
|
|
self.stage_mix = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage1[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output = torch.cat([output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage1[rotations_count]), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v2(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v2, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale) for _ in range(4)])
|
|
|
|
|
self.stage_mix = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage1[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output = torch.cat([output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage1[rotations_count]), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v3(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v3, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale//2) for _ in range(4)])
|
|
|
|
|
self.stage_mix = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale//2)
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage1[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output = torch.cat([output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage1[rotations_count]), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v5(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v5, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale) for _ in range(4)])
|
|
|
|
|
self.stage_mix = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage2 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1) for _ in range(4)])
|
|
|
|
|
self.stage_mix2 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage1[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output = torch.cat([output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage1[rotations_count]), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage2[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output = torch.cat([output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage2[rotations_count]), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_mix, self.stage_mix2)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v9(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v9, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage_mix = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage2 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
self.stage_mix2 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage1)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output = torch.cat([output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage1), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage2)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output = torch.cat([output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage2), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_mix, self.stage_mix2)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v10(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v10, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1S = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage1D = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage1Y = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage_mix = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage2S = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
self.stage_mix2 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_D = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,2],[2,0],[2,2]], center=[0,0], window_size=3)
|
|
|
|
|
self._extract_pattern_Y = layers.PercievePattern(receptive_field_idxes=[[0,0],[1,2],[2,1],[2,2]], center=[0,0], window_size=3)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def stage(self, x, pattern, stage_net, mix_pattern, stage_mix_net):
|
|
|
|
|
tmp_output = self.forward_stage(x, pattern, stage_net)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
tmp_output = torch.cat([tmp_output, torch.rot90(self.forward_stage(rotated, pattern, stage_net), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
output = self.forward_stage(tmp_output, mix_pattern, stage_mix_net)
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.stage(x, self._extract_pattern_S, self.stage1S, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
output += self.stage(x, self._extract_pattern_D, self.stage1D, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
output += self.stage(x, self._extract_pattern_Y, self.stage1Y, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
x = output / 3
|
|
|
|
|
x = x.clamp(0, 255)
|
|
|
|
|
x = self.stage(x, self._extract_pattern_S, self.stage2S, self._extract_pattern_mix, self.stage_mix2)
|
|
|
|
|
x = x.clamp(0, 255)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v10Lsb(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v10Lsb, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1S = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage1D = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage1Y = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage_mix = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage2S = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
self.stage_mix2 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
self.stage1S_lsb = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale, input_max_value=15, output_max_value=15)
|
|
|
|
|
self.stage1D_lsb = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale, input_max_value=15, output_max_value=15)
|
|
|
|
|
self.stage1Y_lsb = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale, input_max_value=15, output_max_value=15)
|
|
|
|
|
self.stage_mix_lsb = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1, input_max_value=15, output_max_value=255)
|
|
|
|
|
self.stage2S_lsb = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1, input_max_value=15, output_max_value=15)
|
|
|
|
|
self.stage_mix2_lsb = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1, input_max_value=15, output_max_value=255)
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_D = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,2],[2,0],[2,2]], center=[0,0], window_size=3)
|
|
|
|
|
self._extract_pattern_Y = layers.PercievePattern(receptive_field_idxes=[[0,0],[1,2],[2,1],[2,2]], center=[0,0], window_size=3)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def stage(self, x, pattern, stage_net, mix_pattern, stage_mix_net):
|
|
|
|
|
tmp_output = self.forward_stage(x, pattern, stage_net)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
tmp_output = torch.cat([tmp_output, torch.rot90(self.forward_stage(rotated, pattern, stage_net), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
output = self.forward_stage(tmp_output, mix_pattern, stage_mix_net)
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
lsb = x % 16
|
|
|
|
|
msb = x - lsb
|
|
|
|
|
output_msb = self.stage(msb, self._extract_pattern_S, self.stage1S, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
output_msb += self.stage(msb, self._extract_pattern_D, self.stage1D, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
output_msb += self.stage(msb, self._extract_pattern_Y, self.stage1Y, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
output_lsb = self.stage(lsb, self._extract_pattern_S, self.stage1S_lsb, self._extract_pattern_mix, self.stage_mix_lsb)
|
|
|
|
|
output_lsb += self.stage(lsb, self._extract_pattern_D, self.stage1D_lsb, self._extract_pattern_mix, self.stage_mix_lsb)
|
|
|
|
|
output_lsb += self.stage(lsb, self._extract_pattern_Y, self.stage1Y_lsb, self._extract_pattern_mix, self.stage_mix_lsb)
|
|
|
|
|
x = output_msb / 3 + output_lsb / 3
|
|
|
|
|
x = x.clamp(0, 255)
|
|
|
|
|
lsb = x % 16
|
|
|
|
|
msb = x - lsb
|
|
|
|
|
output_msb = self.stage(msb, self._extract_pattern_S, self.stage2S, self._extract_pattern_mix, self.stage_mix2)
|
|
|
|
|
output_lsb = self.stage(lsb, self._extract_pattern_S, self.stage2S_lsb, self._extract_pattern_mix, self.stage_mix2_lsb)
|
|
|
|
|
x = output_msb + output_lsb
|
|
|
|
|
x = x.clamp(0, 255)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v4(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v4, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale) for _ in range(4)])
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage1[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output += torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage1[rotations_count]), k=-rotations_count, dims=[2, 3])
|
|
|
|
|
x = output
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v6(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v6, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale) for _ in range(4)])
|
|
|
|
|
self.stage2 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1) for _ in range(4)])
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage1[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output += torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage1[rotations_count]), k=-rotations_count, dims=[2, 3])
|
|
|
|
|
x = output
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage2[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output += torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage2[rotations_count]), k=-rotations_count, dims=[2, 3])
|
|
|
|
|
x = output
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(channel=1, data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v7(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v7, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale) for _ in range(4)])
|
|
|
|
|
self.stage2 = nn.ModuleList([layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1) for _ in range(4)])
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage1[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output += torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage1[rotations_count]), k=-rotations_count, dims=[2, 3])
|
|
|
|
|
x = output
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage2[0])
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output += torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage2[rotations_count]), k=-rotations_count, dims=[2, 3])
|
|
|
|
|
x = output
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
class HDBNetv4(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4, rotations = 4):
|
|
|
|
|
super(HDBNetv4, self).__init__()
|
|
|
|
|
assert scale == 4
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.rotations = rotations
|
|
|
|
|
self.stage1_3H = layers.UpscaleBlock(in_features=3, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale, input_max_value=255, output_max_value=255)
|
|
|
|
|
self.stage1_3D = layers.UpscaleBlock(in_features=3, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale, input_max_value=255, output_max_value=255)
|
|
|
|
|
self.stage1_3B = layers.UpscaleBlock(in_features=3, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale, input_max_value=255, output_max_value=255)
|
|
|
|
|
self.stage1_2H = layers.UpscaleBlock(in_features=2, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale, input_max_value=15, output_max_value=255)
|
|
|
|
|
self.stage1_2D = layers.UpscaleBlock(in_features=2, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale, input_max_value=15, output_max_value=255)
|
|
|
|
|
|
|
|
|
|
self.stage2_3H = layers.UpscaleBlock(in_features=3, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1, input_max_value=255, output_max_value=255)
|
|
|
|
|
self.stage2_3D = layers.UpscaleBlock(in_features=3, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1, input_max_value=255, output_max_value=255)
|
|
|
|
|
self.stage2_3B = layers.UpscaleBlock(in_features=3, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1, input_max_value=255, output_max_value=255)
|
|
|
|
|
self.stage2_2H = layers.UpscaleBlock(in_features=2, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1, input_max_value=15, output_max_value=255)
|
|
|
|
|
self.stage2_2D = layers.UpscaleBlock(in_features=2, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1, input_max_value=15, output_max_value=255)
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_3H = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[0,2]], center=[0,0], window_size=3)
|
|
|
|
|
self._extract_pattern_3D = layers.PercievePattern(receptive_field_idxes=[[0,0],[1,1],[2,2]], center=[0,0], window_size=3)
|
|
|
|
|
self._extract_pattern_3B = layers.PercievePattern(receptive_field_idxes=[[0,0],[1,2],[2,1]], center=[0,0], window_size=3)
|
|
|
|
|
self._extract_pattern_2H = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_2D = layers.PercievePattern(receptive_field_idxes=[[0,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
upsampled = nn.Upsample(scale_factor=self.scale, mode='nearest')(x)
|
|
|
|
|
lsb = x % 16
|
|
|
|
|
msb = x - lsb
|
|
|
|
|
output_msb = torch.zeros([b*c, 1, h*self.scale, w*self.scale], dtype=x.dtype, device=x.device)
|
|
|
|
|
output_lsb = torch.zeros([b*c, 1, h*self.scale, w*self.scale], dtype=x.dtype, device=x.device)
|
|
|
|
|
for rotations_count in range(self.rotations):
|
|
|
|
|
rotated_msb = torch.rot90(msb, k=rotations_count, dims=[2, 3])
|
|
|
|
|
rotated_lsb = torch.rot90(lsb, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output_msbt = self.forward_stage(rotated_msb, self._extract_pattern_3H, self.stage1_3H) + \
|
|
|
|
|
self.forward_stage(rotated_msb, self._extract_pattern_3D, self.stage1_3D) + \
|
|
|
|
|
self.forward_stage(rotated_msb, self._extract_pattern_3B, self.stage1_3B)
|
|
|
|
|
output_lsbt = self.forward_stage(rotated_lsb, self._extract_pattern_2H, self.stage1_2H) + \
|
|
|
|
|
self.forward_stage(rotated_lsb, self._extract_pattern_2D, self.stage1_2D)
|
|
|
|
|
if not config is None and config.current_iter % config.display_step == 0:
|
|
|
|
|
config.writer.add_histogram('s1_output_lsb', output_lsb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
|
config.writer.add_histogram('s1_output_msb', output_msb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
|
output_msb += torch.rot90(output_msbt, k=-rotations_count, dims=[2, 3])
|
|
|
|
|
output_lsb += torch.rot90(output_lsbt, k=-rotations_count, dims=[2, 3])
|
|
|
|
|
output_msb /= self.rotations*3
|
|
|
|
|
output_lsb /= self.rotations*2
|
|
|
|
|
output = output_msb + output_lsb
|
|
|
|
|
x = output.clamp(0, 255)
|
|
|
|
|
lsb = x % 16
|
|
|
|
|
msb = x - lsb
|
|
|
|
|
output_msb = torch.zeros([b*c, 1, h*self.scale, w*self.scale,], dtype=x.dtype, device=x.device)
|
|
|
|
|
output_lsb = torch.zeros([b*c, 1, h*self.scale, w*self.scale,], dtype=x.dtype, device=x.device)
|
|
|
|
|
for rotations_count in range(self.rotations):
|
|
|
|
|
rotated_msb = torch.rot90(msb, k=rotations_count, dims=[2, 3])
|
|
|
|
|
rotated_lsb = torch.rot90(lsb, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output_msbt = self.forward_stage(rotated_msb, self._extract_pattern_3H, self.stage2_3H) + \
|
|
|
|
|
self.forward_stage(rotated_msb, self._extract_pattern_3D, self.stage2_3D) + \
|
|
|
|
|
self.forward_stage(rotated_msb, self._extract_pattern_3B, self.stage2_3B)
|
|
|
|
|
output_lsbt = self.forward_stage(rotated_lsb, self._extract_pattern_2H, self.stage2_2H) + \
|
|
|
|
|
self.forward_stage(rotated_lsb, self._extract_pattern_2D, self.stage2_2D)
|
|
|
|
|
if not config is None and config.current_iter % config.display_step == 0:
|
|
|
|
|
config.writer.add_histogram('s2_output_lsb', output_lsb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
|
config.writer.add_histogram('s2_output_msb', output_msb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
|
output_msb += torch.rot90(output_msbt, k=-rotations_count, dims=[2, 3])
|
|
|
|
|
output_lsb += torch.rot90(output_lsbt, k=-rotations_count, dims=[2, 3])
|
|
|
|
|
output_msb /= self.rotations*3
|
|
|
|
|
output_lsb /= self.rotations*2
|
|
|
|
|
output_msb -= 127.5
|
|
|
|
|
output_msb -= 127.5
|
|
|
|
|
output = output_msb + output_lsb + upsampled
|
|
|
|
|
x = output.clamp(0, 255)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
class SRNetGabor(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetGabor, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1_S = layers.UpscaleBlockGabor(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_S, self.stage1_S)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
|
stage_lut = lut.transfer_4_input_SxS_output(self.stage1_S, quantization_interval=quantization_interval, batch_size=batch_size)
|
|
|
|
|
lut_model = srlut.SRLut.init_from_numpy(stage_lut)
|
|
|
|
|
return lut_model
|
|
|
|
|
|
|
|
|
|
class SRNetChebyKan(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
@ -626,4 +1130,104 @@ class SRMsbLsb4R90Net(SRNetBase):
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRNetMixerR90v11(SRNetBase):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetMixerR90v11, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self.stage1S = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage1D = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage1Y = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage2S = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
self.stage_mix = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=1)
|
|
|
|
|
self.stage_mix2 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
self.stage_mix3 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=2, upscale_factor=1)
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self._extract_pattern_D = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,2],[2,0],[2,2]], center=[0,0], window_size=3)
|
|
|
|
|
self._extract_pattern_Y = layers.PercievePattern(receptive_field_idxes=[[0,0],[1,2],[2,1],[2,2]], center=[0,0], window_size=3)
|
|
|
|
|
self._extract_pattern_mix = layers.PercievePattern(receptive_field_idxes=[[0,0]], center=[0,0], window_size=1, channels=4)
|
|
|
|
|
|
|
|
|
|
self._extract_pattern_1 = layers.PercievePattern(receptive_field_idxes=[[3,0],[3,1],[3,2],[3,3]], center=[0,0], window_size=4)
|
|
|
|
|
self._extract_pattern_2 = layers.PercievePattern(receptive_field_idxes=[[0,3],[1,3],[2,3],[3,3]], center=[0,0], window_size=4)
|
|
|
|
|
self._extract_pattern_3 = layers.PercievePattern(receptive_field_idxes=[[0,0],[1,1],[2,2],[3,2]], center=[0,0], window_size=4)
|
|
|
|
|
self._extract_pattern_4 = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,4],[4,0],[4,4]], center=[0,0], window_size=5)
|
|
|
|
|
|
|
|
|
|
self.stage_pattern_1 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage_pattern_2 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage_pattern_3 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
self.stage_pattern_4 = layers.UpscaleBlock(in_features=4, hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=self.scale)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
|
|
|
|
|
|
tmp_output = self.forward_stage(x, self._extract_pattern_S, self.stage1S)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
tmp_output = torch.cat([tmp_output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage1S), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
output = self.forward_stage(tmp_output, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
|
|
|
|
|
tmp_output = self.forward_stage(x, self._extract_pattern_D, self.stage1D)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
tmp_output = torch.cat([tmp_output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_D, self.stage1D), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
output += self.forward_stage(tmp_output, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
|
|
|
|
|
tmp_output = self.forward_stage(x, self._extract_pattern_Y, self.stage1Y)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
tmp_output = torch.cat([tmp_output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_Y, self.stage1Y), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
output += self.forward_stage(tmp_output, self._extract_pattern_mix, self.stage_mix)
|
|
|
|
|
|
|
|
|
|
########################################
|
|
|
|
|
tmp_output = self.forward_stage(x, self._extract_pattern_1, self.stage_pattern_1)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
tmp_output = torch.cat([tmp_output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_1, self.stage_pattern_1), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
output += self.forward_stage(tmp_output, self._extract_pattern_mix, self.stage_mix2)
|
|
|
|
|
|
|
|
|
|
tmp_output = self.forward_stage(x, self._extract_pattern_2, self.stage_pattern_2)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
tmp_output = torch.cat([tmp_output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_2, self.stage_pattern_2), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
output += self.forward_stage(tmp_output, self._extract_pattern_mix, self.stage_mix2)
|
|
|
|
|
|
|
|
|
|
tmp_output = self.forward_stage(x, self._extract_pattern_3, self.stage_pattern_3)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
tmp_output = torch.cat([tmp_output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_3, self.stage_pattern_3), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
output += self.forward_stage(tmp_output, self._extract_pattern_mix, self.stage_mix2)
|
|
|
|
|
|
|
|
|
|
tmp_output = self.forward_stage(x, self._extract_pattern_4, self.stage_pattern_4)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
tmp_output = torch.cat([tmp_output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_4, self.stage_pattern_4), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
output += self.forward_stage(tmp_output, self._extract_pattern_mix, self.stage_mix2)
|
|
|
|
|
|
|
|
|
|
output /= 7
|
|
|
|
|
x = output
|
|
|
|
|
x = x.clamp(0, 255)
|
|
|
|
|
|
|
|
|
|
output = self.forward_stage(x, self._extract_pattern_S, self.stage2S)
|
|
|
|
|
for rotations_count in range(1,4):
|
|
|
|
|
rotated = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
output = torch.cat([output, torch.rot90(self.forward_stage(rotated, self._extract_pattern_S, self.stage2S), k=-rotations_count, dims=[2, 3])], dim=1)
|
|
|
|
|
x = output
|
|
|
|
|
x = self.forward_stage(x, self._extract_pattern_mix, self.stage_mix3)
|
|
|
|
|
|
|
|
|
|
x = x.clamp(0, 255)
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
ssim_loss = losses.SSIM(data_range=255)
|
|
|
|
|
l1_loss = losses.CharbonnierLoss()
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
return ssim_loss(pred, target) + l1_loss(pred, target)
|
|
|
|
|
return loss_fn
|