|
|
|
@ -5,8 +5,9 @@ import numpy as np
|
|
|
|
|
from common.utils import round_func
|
|
|
|
|
from common import lut
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from .srlut import SRLut, SRLutRot90
|
|
|
|
|
from common.layers import PercievePattern, DenseConvUpscaleBlock, ConvUpscaleBlock
|
|
|
|
|
from . import srlut
|
|
|
|
|
from common.layers import PercievePattern, DenseConvUpscaleBlock, ConvUpscaleBlock, RgbToYcbcr, YcbcrToRgb
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SRNet(nn.Module):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
@ -26,7 +27,7 @@ class SRNet(nn.Module):
|
|
|
|
|
|
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
|
stage_lut = lut.transfer_2x2_input_SxS_output(self.stage, quantization_interval=quantization_interval, batch_size=batch_size)
|
|
|
|
|
lut_model = SRLut.init_from_lut(stage_lut)
|
|
|
|
|
lut_model = srlut.SRLut.init_from_lut(stage_lut)
|
|
|
|
|
return lut_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -48,7 +49,7 @@ class SRNetDense(nn.Module):
|
|
|
|
|
|
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
|
stage_lut = lut.transfer_2x2_input_SxS_output(self.stage, quantization_interval=quantization_interval, batch_size=batch_size)
|
|
|
|
|
lut_model = SRLut.init_from_lut(stage_lut)
|
|
|
|
|
lut_model = srlut.SRLut.init_from_lut(stage_lut)
|
|
|
|
|
return lut_model
|
|
|
|
|
|
|
|
|
|
class SRNetDenseRot90(nn.Module):
|
|
|
|
@ -75,5 +76,40 @@ class SRNetDenseRot90(nn.Module):
|
|
|
|
|
|
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
|
stage_lut = lut.transfer_2x2_input_SxS_output(self.stage, quantization_interval=quantization_interval, batch_size=batch_size)
|
|
|
|
|
lut_model = SRLutRot90.init_from_lut(stage_lut)
|
|
|
|
|
lut_model = srlut.SRLutRot90.init_from_lut(stage_lut)
|
|
|
|
|
return lut_model
|
|
|
|
|
|
|
|
|
|
class SRNetDenseRot90Y(nn.Module):
|
|
|
|
|
def __init__(self, hidden_dim = 64, layers_count = 4, scale = 4):
|
|
|
|
|
super(SRNetDenseRot90Y, self).__init__()
|
|
|
|
|
self.scale = scale
|
|
|
|
|
self._extract_pattern_S = PercievePattern(receptive_field_idxes=[[0,0],[0,1],[1,0],[1,1]], center=[0,0], window_size=2)
|
|
|
|
|
self.stage = DenseConvUpscaleBlock(hidden_dim=hidden_dim, layers_count=layers_count, upscale_factor=scale)
|
|
|
|
|
self.rgb_to_ycbcr = RgbToYcbcr()
|
|
|
|
|
self.ycbcr_to_rgb = YcbcrToRgb()
|
|
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
|
b,c,h,w = x.shape
|
|
|
|
|
x = self.rgb_to_ycbcr(x)
|
|
|
|
|
y = x[:,0:1,:,:]
|
|
|
|
|
cbcr = x[:,1:,:,:]
|
|
|
|
|
cbcr_scaled = F.interpolate(cbcr, size=[h*self.scale, w*self.scale], mode='bilinear')
|
|
|
|
|
|
|
|
|
|
x = y.view(b, 1, h, w)
|
|
|
|
|
output = torch.zeros([b, 1, h*self.scale, w*self.scale], dtype=x.dtype, device=x.device)
|
|
|
|
|
for rotations_count in range(4):
|
|
|
|
|
rx = torch.rot90(x, k=rotations_count, dims=[2, 3])
|
|
|
|
|
_,_,rh,rw = rx.shape
|
|
|
|
|
rx = self._extract_pattern_S(rx)
|
|
|
|
|
rx = self.stage(rx)
|
|
|
|
|
rx = rx.view(b, 1, rh, rw, self.scale, self.scale).permute(0,1,2,4,3,5).reshape(b, 1, rh*self.scale, rw*self.scale)
|
|
|
|
|
output += torch.rot90(rx, k=-rotations_count, dims=[2, 3])
|
|
|
|
|
output /= 4
|
|
|
|
|
output = torch.cat([output, cbcr_scaled], dim=1)
|
|
|
|
|
output = self.ycbcr_to_rgb(output)
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
def get_lut_model(self, quantization_interval=16, batch_size=2**10):
|
|
|
|
|
stage_lut = lut.transfer_2x2_input_SxS_output(self.stage, quantization_interval=quantization_interval, batch_size=batch_size)
|
|
|
|
|
lut_model = srlut.SRLutRot90Y.init_from_lut(stage_lut)
|
|
|
|
|
return lut_model
|