|
|
@ -9,16 +9,9 @@ from . import srlut
|
|
|
|
from common import layers
|
|
|
|
from common import layers
|
|
|
|
from common import losses
|
|
|
|
from common import losses
|
|
|
|
|
|
|
|
|
|
|
|
class SRNet(nn.Module):
|
|
|
|
class SRNetBase(nn.Module):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self):
|
|
|
|
super(SRNet, self).__init__()
|
|
|
|
super(SRNetBase, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
|
|
|
|
self.stage1_S = layers.UpscaleBlock(
|
|
|
|
|
|
|
|
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_stage(self, x, scale, percieve_pattern, stage):
|
|
|
|
def forward_stage(self, x, scale, percieve_pattern, stage):
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
b,c,h,w = x.shape
|
|
|
@ -30,6 +23,26 @@ class SRNet(nn.Module):
|
|
|
|
x = x.reshape(b, c, h*scale, w*scale)
|
|
|
|
x = x.reshape(b, c, h*scale, w*scale)
|
|
|
|
return x
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
|
|
|
return F.mse_loss(pred/255, target/255)
|
|
|
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRNet(SRNetBase):
|
|
|
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
|
|
|
super(SRNet, self).__init__()
|
|
|
|
|
|
|
|
self.scale = scale
|
|
|
|
|
|
|
|
self.stage1_S = layers.UpscaleBlock(
|
|
|
|
|
|
|
|
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):
|
|
|
|
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)
|
|
|
@ -42,12 +55,8 @@ class SRNet(nn.Module):
|
|
|
|
lut_model = srlut.SRLut.init_from_numpy(stage_lut)
|
|
|
|
lut_model = srlut.SRLut.init_from_numpy(stage_lut)
|
|
|
|
return lut_model
|
|
|
|
return lut_model
|
|
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
|
|
|
return F.mse_loss(pred/255, target/255)
|
|
|
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRNetY(nn.Module):
|
|
|
|
class SRNetY(SRNetBase):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
super(SRNetY, self).__init__()
|
|
|
|
super(SRNetY, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.scale = scale
|
|
|
@ -60,16 +69,6 @@ class SRNetY(nn.Module):
|
|
|
|
self.rgb_to_ycbcr = layers.RgbToYcbcr()
|
|
|
|
self.rgb_to_ycbcr = layers.RgbToYcbcr()
|
|
|
|
self.ycbcr_to_rgb = layers.YcbcrToRgb()
|
|
|
|
self.ycbcr_to_rgb = layers.YcbcrToRgb()
|
|
|
|
|
|
|
|
|
|
|
|
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 = self.rgb_to_ycbcr(x)
|
|
|
|
x = self.rgb_to_ycbcr(x)
|
|
|
@ -88,12 +87,8 @@ class SRNetY(nn.Module):
|
|
|
|
lut_model = srlut.SRLutY.init_from_numpy(stage_lut)
|
|
|
|
lut_model = srlut.SRLutY.init_from_numpy(stage_lut)
|
|
|
|
return lut_model
|
|
|
|
return lut_model
|
|
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
|
|
|
return F.mse_loss(pred/255, target/255)
|
|
|
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRNetR90(nn.Module):
|
|
|
|
class SRNetR90(SRNetBase):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
super(SRNetR90, self).__init__()
|
|
|
|
super(SRNetR90, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.scale = scale
|
|
|
@ -104,16 +99,6 @@ class SRNetR90(nn.Module):
|
|
|
|
)
|
|
|
|
)
|
|
|
|
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_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
@ -131,12 +116,8 @@ class SRNetR90(nn.Module):
|
|
|
|
lut_model = srlut.SRLutR90.init_from_numpy(stage_lut)
|
|
|
|
lut_model = srlut.SRLutR90.init_from_numpy(stage_lut)
|
|
|
|
return lut_model
|
|
|
|
return lut_model
|
|
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
|
|
|
return F.mse_loss(pred/255, target/255)
|
|
|
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRNetR90Y(nn.Module):
|
|
|
|
class SRNetR90Y(SRNetBase):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
super(SRNetR90Y, self).__init__()
|
|
|
|
super(SRNetR90Y, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.scale = scale
|
|
|
@ -150,16 +131,6 @@ class SRNetR90Y(nn.Module):
|
|
|
|
self.rgb_to_ycbcr = layers.RgbToYcbcr()
|
|
|
|
self.rgb_to_ycbcr = layers.RgbToYcbcr()
|
|
|
|
self.ycbcr_to_rgb = layers.YcbcrToRgb()
|
|
|
|
self.ycbcr_to_rgb = layers.YcbcrToRgb()
|
|
|
|
|
|
|
|
|
|
|
|
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 = self.rgb_to_ycbcr(x)
|
|
|
|
x = self.rgb_to_ycbcr(x)
|
|
|
@ -183,15 +154,10 @@ class SRNetR90Y(nn.Module):
|
|
|
|
lut_model = srlut.SRLutR90Y.init_from_numpy(stage_lut)
|
|
|
|
lut_model = srlut.SRLutR90Y.init_from_numpy(stage_lut)
|
|
|
|
return lut_model
|
|
|
|
return lut_model
|
|
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
|
|
|
return F.mse_loss(pred/255, target/255)
|
|
|
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRMsbLsbR90Net(nn.Module):
|
|
|
|
class SRMsbLsbNet(SRNetBase):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
super(SRMsbLsbR90Net, self).__init__()
|
|
|
|
super(SRMsbLsbNet, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.scale = scale
|
|
|
|
self.hidden_dim = hidden_dim
|
|
|
|
self.hidden_dim = hidden_dim
|
|
|
|
self.layers_count = layers_count
|
|
|
|
self.layers_count = layers_count
|
|
|
@ -202,7 +168,7 @@ class SRMsbLsbR90Net(nn.Module):
|
|
|
|
layers_count=layers_count,
|
|
|
|
layers_count=layers_count,
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
input_max_value=255,
|
|
|
|
input_max_value=255,
|
|
|
|
output_max_value=15
|
|
|
|
output_max_value=255
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.lsb_fn = layers.UpscaleBlock(
|
|
|
|
self.lsb_fn = layers.UpscaleBlock(
|
|
|
|
in_features=4,
|
|
|
|
in_features=4,
|
|
|
@ -210,20 +176,110 @@ class SRMsbLsbR90Net(nn.Module):
|
|
|
|
layers_count=layers_count,
|
|
|
|
layers_count=layers_count,
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
input_max_value=15,
|
|
|
|
input_max_value=15,
|
|
|
|
output_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_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
|
|
|
|
|
|
|
def forward_stage(self, x, scale, percieve_pattern, stage):
|
|
|
|
def forward(self, x, config=None):
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
x = percieve_pattern(x)
|
|
|
|
x = x.reshape(b*c, 1, h, w)
|
|
|
|
x = stage(x)
|
|
|
|
|
|
|
|
x = round_func(x)
|
|
|
|
lsb = x % 16
|
|
|
|
x = x.reshape(b, c, h, w, scale, scale)
|
|
|
|
msb = x - lsb
|
|
|
|
x = x.permute(0,1,2,4,3,5)
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*scale, w*scale)
|
|
|
|
output_msb = self.forward_stage(msb, self.scale, self._extract_pattern_S, self.msb_fn)
|
|
|
|
|
|
|
|
output_lsb = self.forward_stage(lsb, self.scale, self._extract_pattern_S, self.lsb_fn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not config is None and config.current_iter % config.display_step == 0:
|
|
|
|
|
|
|
|
config.writer.add_histogram('output_lsb', output_lsb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
|
|
|
|
config.writer.add_histogram('output_msb', output_msb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
|
|
|
|
x = output_msb + output_lsb
|
|
|
|
|
|
|
|
x = x.reshape(b, c, h*self.scale, w*self.scale)
|
|
|
|
return x
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRMsbLsbShift2Net(SRNetBase):
|
|
|
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
|
|
|
super(SRMsbLsbShift2Net, self).__init__()
|
|
|
|
|
|
|
|
self.scale = scale
|
|
|
|
|
|
|
|
self.hidden_dim = hidden_dim
|
|
|
|
|
|
|
|
self.layers_count = layers_count
|
|
|
|
|
|
|
|
self.count = 4
|
|
|
|
|
|
|
|
self.msb_fns = nn.ModuleList([layers.UpscaleBlock(
|
|
|
|
|
|
|
|
in_features=4,
|
|
|
|
|
|
|
|
hidden_dim=hidden_dim,
|
|
|
|
|
|
|
|
layers_count=layers_count,
|
|
|
|
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
|
|
|
|
input_max_value=255,
|
|
|
|
|
|
|
|
output_max_value=255
|
|
|
|
|
|
|
|
) for x in range(self.count)])
|
|
|
|
|
|
|
|
self.lsb_fns = nn.ModuleList([layers.UpscaleBlock(
|
|
|
|
|
|
|
|
in_features=4,
|
|
|
|
|
|
|
|
hidden_dim=hidden_dim,
|
|
|
|
|
|
|
|
layers_count=layers_count,
|
|
|
|
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
|
|
|
|
input_max_value=15,
|
|
|
|
|
|
|
|
output_max_value=255
|
|
|
|
|
|
|
|
) for x in range(self.count)])
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 i, msb_fn, lsb_fn in zip(range(self.count), self.msb_fns, self.lsb_fns):
|
|
|
|
|
|
|
|
output_msb_s = self.forward_stage(msb, self.scale, self._extract_pattern_S, msb_fn)
|
|
|
|
|
|
|
|
output_lsb_s = self.forward_stage(lsb, self.scale, self._extract_pattern_S, lsb_fn)
|
|
|
|
|
|
|
|
output_msb += torch.nn.functional.pad(output_msb_s, [i, 0, i, 0], mode='replicate')[:,:,:h*self.scale,:w*self.scale]
|
|
|
|
|
|
|
|
output_lsb += torch.nn.functional.pad(output_lsb_s, [i, 0, i, 0], mode='replicate')[:,:,:h*self.scale,:w*self.scale]
|
|
|
|
|
|
|
|
output_msb /= self.count
|
|
|
|
|
|
|
|
output_lsb /= self.count
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not config is None and config.current_iter % config.display_step == 0:
|
|
|
|
|
|
|
|
config.writer.add_histogram('output_lsb', output_lsb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
|
|
|
|
config.writer.add_histogram('output_msb', output_msb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x = output_msb + output_lsb
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRMsbLsbR90Net(SRNetBase):
|
|
|
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
|
|
|
super(SRMsbLsbR90Net, self).__init__()
|
|
|
|
|
|
|
|
self.scale = scale
|
|
|
|
|
|
|
|
self.hidden_dim = hidden_dim
|
|
|
|
|
|
|
|
self.layers_count = layers_count
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.msb_fn = layers.UpscaleBlock(
|
|
|
|
|
|
|
|
in_features=4,
|
|
|
|
|
|
|
|
hidden_dim=hidden_dim,
|
|
|
|
|
|
|
|
layers_count=layers_count,
|
|
|
|
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
|
|
|
|
input_max_value=255,
|
|
|
|
|
|
|
|
output_max_value=255
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
self.lsb_fn = layers.UpscaleBlock(
|
|
|
|
|
|
|
|
in_features=4,
|
|
|
|
|
|
|
|
hidden_dim=hidden_dim,
|
|
|
|
|
|
|
|
layers_count=layers_count,
|
|
|
|
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
@ -236,10 +292,8 @@ class SRMsbLsbR90Net(nn.Module):
|
|
|
|
for rotations_count in range(4):
|
|
|
|
for rotations_count in range(4):
|
|
|
|
rotated_msb = torch.rot90(msb, k=rotations_count, dims=[2, 3])
|
|
|
|
rotated_msb = torch.rot90(msb, k=rotations_count, dims=[2, 3])
|
|
|
|
rotated_lsb = torch.rot90(lsb, k=rotations_count, dims=[2, 3])
|
|
|
|
rotated_lsb = torch.rot90(lsb, k=rotations_count, dims=[2, 3])
|
|
|
|
output_msb_r = self.forward_stage(rotated_msb, self.scale, self._extract_pattern_S, msb_fn)
|
|
|
|
output_msb_r = self.forward_stage(rotated_msb, self.scale, self._extract_pattern_S, self.msb_fn)
|
|
|
|
output_lsb_r = self.forward_stage(rotated_lsb, self.scale, self._extract_pattern_S, lsb_fn)
|
|
|
|
output_lsb_r = self.forward_stage(rotated_lsb, self.scale, self._extract_pattern_S, self.lsb_fn)
|
|
|
|
output_msb_r = round_func(output_msb_r) * 15
|
|
|
|
|
|
|
|
output_lsb_r = round_func(output_lsb_r)
|
|
|
|
|
|
|
|
output_msb += torch.rot90(output_msb_r, k=-rotations_count, dims=[2, 3])
|
|
|
|
output_msb += torch.rot90(output_msb_r, k=-rotations_count, dims=[2, 3])
|
|
|
|
output_lsb += torch.rot90(output_lsb_r, k=-rotations_count, dims=[2, 3])
|
|
|
|
output_lsb += torch.rot90(output_lsb_r, k=-rotations_count, dims=[2, 3])
|
|
|
|
output_msb /= 4
|
|
|
|
output_msb /= 4
|
|
|
@ -254,14 +308,8 @@ class SRMsbLsbR90Net(nn.Module):
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
raise NotImplementedError
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
|
|
|
fourier_loss_fn = losses.FocalFrequencyLoss()
|
|
|
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
|
|
|
return fourier_loss_fn(pred, target)
|
|
|
|
|
|
|
|
return loss_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRMsbLsb4R90Net(nn.Module):
|
|
|
|
class SRMsbLsb4R90Net(SRNetBase):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
super(SRMsbLsb4R90Net, self).__init__()
|
|
|
|
super(SRMsbLsb4R90Net, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.scale = scale
|
|
|
@ -274,7 +322,7 @@ class SRMsbLsb4R90Net(nn.Module):
|
|
|
|
layers_count=layers_count,
|
|
|
|
layers_count=layers_count,
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
input_max_value=255,
|
|
|
|
input_max_value=255,
|
|
|
|
output_max_value=15
|
|
|
|
output_max_value=255
|
|
|
|
) for x in range(4)])
|
|
|
|
) for x in range(4)])
|
|
|
|
self.lsb_fns = nn.ModuleList([layers.UpscaleBlock(
|
|
|
|
self.lsb_fns = nn.ModuleList([layers.UpscaleBlock(
|
|
|
|
in_features=4,
|
|
|
|
in_features=4,
|
|
|
@ -282,20 +330,10 @@ class SRMsbLsb4R90Net(nn.Module):
|
|
|
|
layers_count=layers_count,
|
|
|
|
layers_count=layers_count,
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
upscale_factor=self.scale,
|
|
|
|
input_max_value=15,
|
|
|
|
input_max_value=15,
|
|
|
|
output_max_value=15
|
|
|
|
output_max_value=255
|
|
|
|
) for x in range(4)])
|
|
|
|
) for x 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_S = layers.PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
@ -310,8 +348,6 @@ class SRMsbLsb4R90Net(nn.Module):
|
|
|
|
rotated_lsb = torch.rot90(lsb, k=rotations_count, dims=[2, 3])
|
|
|
|
rotated_lsb = torch.rot90(lsb, k=rotations_count, dims=[2, 3])
|
|
|
|
output_msb_r = self.forward_stage(rotated_msb, self.scale, self._extract_pattern_S, msb_fn)
|
|
|
|
output_msb_r = self.forward_stage(rotated_msb, self.scale, self._extract_pattern_S, msb_fn)
|
|
|
|
output_lsb_r = self.forward_stage(rotated_lsb, self.scale, self._extract_pattern_S, lsb_fn)
|
|
|
|
output_lsb_r = self.forward_stage(rotated_lsb, self.scale, self._extract_pattern_S, lsb_fn)
|
|
|
|
output_msb_r = round_func(output_msb_r) * 15
|
|
|
|
|
|
|
|
output_lsb_r = round_func(output_lsb_r)
|
|
|
|
|
|
|
|
output_msb += torch.rot90(output_msb_r, k=-rotations_count, dims=[2, 3])
|
|
|
|
output_msb += torch.rot90(output_msb_r, k=-rotations_count, dims=[2, 3])
|
|
|
|
output_lsb += torch.rot90(output_lsb_r, k=-rotations_count, dims=[2, 3])
|
|
|
|
output_lsb += torch.rot90(output_lsb_r, k=-rotations_count, dims=[2, 3])
|
|
|
|
output_msb /= 4
|
|
|
|
output_msb /= 4
|
|
|
@ -320,14 +356,9 @@ class SRMsbLsb4R90Net(nn.Module):
|
|
|
|
config.writer.add_histogram('output_lsb', output_lsb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
config.writer.add_histogram('output_lsb', output_lsb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
config.writer.add_histogram('output_msb', output_msb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
config.writer.add_histogram('output_msb', output_msb.detach().cpu().numpy(), config.current_iter)
|
|
|
|
x = output_msb + output_lsb
|
|
|
|
x = output_msb + output_lsb
|
|
|
|
|
|
|
|
x = x.clamp(0, 255)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
raise NotImplementedError
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
def get_loss_fn(self):
|
|
|
|
|
|
|
|
fourier_loss_fn = losses.FocalFrequencyLoss()
|
|
|
|
|
|
|
|
def loss_fn(pred, target):
|
|
|
|
|
|
|
|
return fourier_loss_fn(pred, target)
|
|
|
|
|
|
|
|
return loss_fn
|
|
|
|
|