Spaces:
Running
Running
File size: 3,227 Bytes
6f9115d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import torch
import torchvision.transforms.functional as TF
import torch.nn.functional as F
from PIL import Image
import os
from skimage import img_as_ubyte
from tqdm import tqdm
from natsort import natsorted
from glob import glob
from utils.image_utils import save_img
from utils.model_utils import load_checkpoint
import argparse
from model_arch.SRMNet_SWFF import SRMNet_SWFF
from model_arch.SRMNet import SRMNet
tasks = ['Deblurring_motionblur',
'Dehaze_realworld',
'Denoise_gaussian',
'Denoise_realworld',
'Deraining_raindrop',
'Deraining_rainstreak',
'LLEnhancement',
'Retouching']
def main():
parser = argparse.ArgumentParser(description='Quick demo Image Restoration')
parser.add_argument('--input_dir', default='test/', type=str, help='Input images root')
parser.add_argument('--result_dir', default='result/', type=str, help='Results images root')
parser.add_argument('--weights_root', default='pretrained_model', type=str, help='Weights root')
parser.add_argument('--task', default='Retouching', type=str, help='Restoration task (Above task list)')
args = parser.parse_args()
# Prepare testing data
inp_dir = os.path.join(args.input_dir, args.task)
files = natsorted(glob.glob(os.path.join(inp_dir, '*')))
if len(files) == 0:
raise Exception("\nNo images in {} \nPlease enter the following tasks: \n\n{}".format(inp_dir, '\n'.join(tasks)))
out_dir = os.path.join(args.result_dir, args.task)
os.makedirs(out_dir, exist_ok=True)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Build model
model = define_model(args)
model.eval()
model = model.to(device)
print('restoring images......')
mul = 16
for i, file_ in enumerate(tqdm(files)):
img = Image.open(file_).convert('RGB')
input_ = TF.to_tensor(img).unsqueeze(0).cuda()
# Pad the input if not_multiple_of 8
h, w = input_.shape[2], input_.shape[3]
H, W = ((h + mul) // mul) * mul, ((w + mul) // mul) * mul
padh = H - h if h % mul != 0 else 0
padw = W - w if w % mul != 0 else 0
input_ = F.pad(input_, (0, padw, 0, padh), 'reflect')
with torch.no_grad():
restored = model(input_)
restored = torch.clamp(restored, 0, 1)
restored = restored[:, :, :h, :w]
restored = restored.permute(0, 2, 3, 1).cpu().detach().numpy()
restored = img_as_ubyte(restored[0])
f = os.path.splitext(os.path.split(file_)[-1])[0]
save_img((os.path.join(out_dir, f + '.png')), restored)
print(f"Files saved at {out_dir}")
print('finish !')
def define_model(args):
# Enhance models
if args.task in ['LLEnhancement', 'Retouching']:
model = SRMNet(in_chn=3, wf=96, depth=4)
weight_path = os.path.join(args.weights_root, args.task + '.pth')
load_checkpoint(model, weight_path)
# Restored models
else:
model = SRMNet_SWFF(in_chn=3, wf=96, depth=4)
weight_path = os.path.join(args.weights_root, args.task + '.pth')
load_checkpoint(model, weight_path)
return model
if __name__ == '__main__':
main()
|