support denoise strength for realesr-general-x4v3
This commit is contained in:
@@ -3,6 +3,7 @@ import cv2
|
|||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
from basicsr.archs.rrdbnet_arch import RRDBNet
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
||||||
|
from basicsr.utils.download_util import load_file_from_url
|
||||||
|
|
||||||
from realesrgan import RealESRGANer
|
from realesrgan import RealESRGANer
|
||||||
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
||||||
@@ -19,10 +20,18 @@ def main():
|
|||||||
type=str,
|
type=str,
|
||||||
default='RealESRGAN_x4plus',
|
default='RealESRGAN_x4plus',
|
||||||
help=('Model names: RealESRGAN_x4plus | RealESRNet_x4plus | RealESRGAN_x4plus_anime_6B | RealESRGAN_x2plus | '
|
help=('Model names: RealESRGAN_x4plus | RealESRNet_x4plus | RealESRGAN_x4plus_anime_6B | RealESRGAN_x2plus | '
|
||||||
'realesr-animevideov3 | realesr-general-x4v3 | realesr-general-wdn-x4v3'))
|
'realesr-animevideov3 | realesr-general-x4v3'))
|
||||||
parser.add_argument('-o', '--output', type=str, default='results', help='Output folder')
|
parser.add_argument('-o', '--output', type=str, default='results', help='Output folder')
|
||||||
parser.add_argument('--model_path', type=str, default=None, help='Model path')
|
parser.add_argument(
|
||||||
|
'-dn',
|
||||||
|
'--denoise_strength',
|
||||||
|
type=float,
|
||||||
|
default=0.5,
|
||||||
|
help=('Denoise strength. 0 for weak denoise (keep noise), 1 for strong denoise ability. '
|
||||||
|
'Only used for the realesr-general-x4v3 model'))
|
||||||
parser.add_argument('-s', '--outscale', type=float, default=4, help='The final upsampling scale of the image')
|
parser.add_argument('-s', '--outscale', type=float, default=4, help='The final upsampling scale of the image')
|
||||||
|
parser.add_argument(
|
||||||
|
'--model_path', type=str, default=None, help='[Option] Model path. Usually, you do not need to specify it')
|
||||||
parser.add_argument('--suffix', type=str, default='out', help='Suffix of the restored image')
|
parser.add_argument('--suffix', type=str, default='out', help='Suffix of the restored image')
|
||||||
parser.add_argument('-t', '--tile', type=int, default=0, help='Tile size, 0 for no tile during testing')
|
parser.add_argument('-t', '--tile', type=int, default=0, help='Tile size, 0 for no tile during testing')
|
||||||
parser.add_argument('--tile_pad', type=int, default=10, help='Tile padding')
|
parser.add_argument('--tile_pad', type=int, default=10, help='Tile padding')
|
||||||
@@ -47,36 +56,58 @@ def main():
|
|||||||
|
|
||||||
# determine models according to model names
|
# determine models according to model names
|
||||||
args.model_name = args.model_name.split('.')[0]
|
args.model_name = args.model_name.split('.')[0]
|
||||||
if args.model_name in ['RealESRGAN_x4plus', 'RealESRNet_x4plus']: # x4 RRDBNet model
|
if args.model_name == 'RealESRGAN_x4plus': # x4 RRDBNet model
|
||||||
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
||||||
netscale = 4
|
netscale = 4
|
||||||
elif args.model_name in ['RealESRGAN_x4plus_anime_6B']: # x4 RRDBNet model with 6 blocks
|
file_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth'
|
||||||
|
elif args.model_name == 'RealESRNet_x4plus': # x4 RRDBNet model
|
||||||
|
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
||||||
|
netscale = 4
|
||||||
|
file_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth'
|
||||||
|
elif args.model_name == 'RealESRGAN_x4plus_anime_6B': # x4 RRDBNet model with 6 blocks
|
||||||
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
|
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
|
||||||
netscale = 4
|
netscale = 4
|
||||||
elif args.model_name in ['RealESRGAN_x2plus']: # x2 RRDBNet model
|
file_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth'
|
||||||
|
elif args.model_name == 'RealESRGAN_x2plus': # x2 RRDBNet model
|
||||||
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
||||||
netscale = 2
|
netscale = 2
|
||||||
elif args.model_name in ['realesr-animevideov3']: # x4 VGG-style model (XS size)
|
file_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth'
|
||||||
|
elif args.model_name == 'realesr-animevideov3': # x4 VGG-style model (XS size)
|
||||||
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu')
|
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu')
|
||||||
netscale = 4
|
netscale = 4
|
||||||
elif args.model_name in ['realesr-general-x4v3', 'realesr-general-wdn-x4v3']: # x4 VGG-style model (S size)
|
file_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth'
|
||||||
|
elif args.model_name == 'realesr-general-x4v3': # x4 VGG-style model (S size)
|
||||||
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
||||||
netscale = 4
|
netscale = 4
|
||||||
|
file_url = [
|
||||||
|
'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth',
|
||||||
|
'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth'
|
||||||
|
]
|
||||||
|
|
||||||
# determine model paths
|
# determine model paths
|
||||||
if args.model_path is not None:
|
if args.model_path is not None:
|
||||||
model_path = args.model_path
|
model_path = args.model_path
|
||||||
else:
|
else:
|
||||||
model_path = os.path.join('experiments/pretrained_models', args.model_name + '.pth')
|
|
||||||
if not os.path.isfile(model_path):
|
|
||||||
model_path = os.path.join('realesrgan/weights', args.model_name + '.pth')
|
model_path = os.path.join('realesrgan/weights', args.model_name + '.pth')
|
||||||
if not os.path.isfile(model_path):
|
if not os.path.isfile(model_path):
|
||||||
raise ValueError(f'Model {args.model_name} does not exist.')
|
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
for url in file_url:
|
||||||
|
# model_path will be updated
|
||||||
|
model_path = load_file_from_url(
|
||||||
|
url=url, model_dir=os.path.join(ROOT_DIR, 'realesrgan/weights'), progress=True, file_name=None)
|
||||||
|
|
||||||
|
# use dni to control the denoise strength
|
||||||
|
dni_weight = None
|
||||||
|
if args.model_name == 'realesr-general-x4v3' and args.denoise_strength != 1:
|
||||||
|
wdn_model_path = model_path.replace('realesr-general-x4v3', 'realesr-general-wdn-x4v3')
|
||||||
|
model_path = [model_path, wdn_model_path]
|
||||||
|
dni_weight = [args.denoise_strength, 1 - args.denoise_strength]
|
||||||
|
|
||||||
# restorer
|
# restorer
|
||||||
upsampler = RealESRGANer(
|
upsampler = RealESRGANer(
|
||||||
scale=netscale,
|
scale=netscale,
|
||||||
model_path=model_path,
|
model_path=model_path,
|
||||||
|
dni_weight=dni_weight,
|
||||||
model=model,
|
model=model,
|
||||||
tile=args.tile,
|
tile=args.tile,
|
||||||
tile_pad=args.tile_pad,
|
tile_pad=args.tile_pad,
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ class RealESRGANer():
|
|||||||
def __init__(self,
|
def __init__(self,
|
||||||
scale,
|
scale,
|
||||||
model_path,
|
model_path,
|
||||||
|
dni_weight=None,
|
||||||
model=None,
|
model=None,
|
||||||
tile=0,
|
tile=0,
|
||||||
tile_pad=10,
|
tile_pad=10,
|
||||||
@@ -49,22 +50,44 @@ class RealESRGANer():
|
|||||||
f'cuda:{gpu_id}' if torch.cuda.is_available() else 'cpu') if device is None else device
|
f'cuda:{gpu_id}' if torch.cuda.is_available() else 'cpu') if device is None else device
|
||||||
else:
|
else:
|
||||||
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') if device is None else device
|
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') if device is None else device
|
||||||
|
|
||||||
|
if isinstance(model_path, list):
|
||||||
|
# dni
|
||||||
|
assert len(model_path) == len(dni_weight), 'model_path and dni_weight should have the save length.'
|
||||||
|
loadnet = self.dni(model_path[0], model_path[1], dni_weight)
|
||||||
|
else:
|
||||||
# if the model_path starts with https, it will first download models to the folder: realesrgan/weights
|
# if the model_path starts with https, it will first download models to the folder: realesrgan/weights
|
||||||
if model_path.startswith('https://'):
|
if model_path.startswith('https://'):
|
||||||
model_path = load_file_from_url(
|
model_path = load_file_from_url(
|
||||||
url=model_path, model_dir=os.path.join(ROOT_DIR, 'realesrgan/weights'), progress=True, file_name=None)
|
url=model_path,
|
||||||
|
model_dir=os.path.join(ROOT_DIR, 'realesrgan/weights'),
|
||||||
|
progress=True,
|
||||||
|
file_name=None)
|
||||||
loadnet = torch.load(model_path, map_location=torch.device('cpu'))
|
loadnet = torch.load(model_path, map_location=torch.device('cpu'))
|
||||||
|
|
||||||
# prefer to use params_ema
|
# prefer to use params_ema
|
||||||
if 'params_ema' in loadnet:
|
if 'params_ema' in loadnet:
|
||||||
keyname = 'params_ema'
|
keyname = 'params_ema'
|
||||||
else:
|
else:
|
||||||
keyname = 'params'
|
keyname = 'params'
|
||||||
model.load_state_dict(loadnet[keyname], strict=True)
|
model.load_state_dict(loadnet[keyname], strict=True)
|
||||||
|
|
||||||
model.eval()
|
model.eval()
|
||||||
self.model = model.to(self.device)
|
self.model = model.to(self.device)
|
||||||
if self.half:
|
if self.half:
|
||||||
self.model = self.model.half()
|
self.model = self.model.half()
|
||||||
|
|
||||||
|
def dni(self, net_a, net_b, dni_weight, key='params', loc='cpu'):
|
||||||
|
"""Deep network interpolation.
|
||||||
|
|
||||||
|
``Paper: Deep Network Interpolation for Continuous Imagery Effect Transition``
|
||||||
|
"""
|
||||||
|
net_a = torch.load(net_a, map_location=torch.device(loc))
|
||||||
|
net_b = torch.load(net_b, map_location=torch.device(loc))
|
||||||
|
for k, v_a in net_a[key].items():
|
||||||
|
net_a[key][k] = dni_weight[0] * v_a + dni_weight[1] * net_b[key][k]
|
||||||
|
return net_a
|
||||||
|
|
||||||
def pre_process(self, img):
|
def pre_process(self, img):
|
||||||
"""Pre-process, such as pre-pad and mod pad, so that the images can be divisible
|
"""Pre-process, such as pre-pad and mod pad, so that the images can be divisible
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user