File size: 2,987 Bytes
f5bf8e5
 
 
 
 
 
 
 
 
 
 
 
 
4dc12ee
 
f5bf8e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5670d74
 
 
 
 
 
 
 
 
f5bf8e5
 
5670d74
f5bf8e5
 
230f266
f5bf8e5
 
 
 
5670d74
 
 
 
f5bf8e5
 
 
 
 
 
 
 
 
 
5670d74
 
f5bf8e5
5670d74
f5bf8e5
 
 
5670d74
 
 
 
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
## Restormer: Efficient Transformer for High-Resolution Image Restoration
## Syed Waqas Zamir, Aditya Arora, Salman Khan, Munawar Hayat, Fahad Shahbaz Khan, and Ming-Hsuan Yang
## https://arxiv.org/abs/2111.09881


import torch
import torch.nn.functional as F
import os
from skimage import img_as_ubyte
import cv2
import argparse

parser = argparse.ArgumentParser(description='Test Restormer on your own images')
parser.add_argument('--input_path', default='./temp/image.jpg', type=str, help='Directory of input images or path of single image')
parser.add_argument('--result_dir', default='./temp/', type=str, help='Directory for restored results')
parser.add_argument('--task', required=True, type=str, help='Task to run', choices=['Motion_Deblurring',
                                                                                    'Single_Image_Defocus_Deblurring',
                                                                                    'Deraining',
                                                                                    'Real_Denoising',
                                                                                    'Gaussian_Gray_Denoising',
                                                                                    'Gaussian_Color_Denoising'])

args = parser.parse_args()


task    = args.task
out_dir = os.path.join(args.result_dir, task)

os.makedirs(out_dir, exist_ok=True)


if task == 'Motion_Deblurring':
    model = torch.jit.load('motion_deblurring.pt')
elif task == 'Single_Image_Defocus_Deblurring':
    model = torch.jit.load('single_image_defocus_deblurring.pt')
elif task == 'Deraining':    
    model = torch.jit.load('deraining.pt')
elif task == 'Real_Denoising':
    model = torch.jit.load('real_denoising.pt')
    
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# device = torch.device('cpu')
# stx()

model = model.to(device)
model.eval()

img_multiple_of = 8

with torch.inference_mode():
    if torch.cuda.is_available():
        torch.cuda.ipc_collect()
        torch.cuda.empty_cache()
    
    img = cv2.cvtColor(cv2.imread(args.input_path), cv2.COLOR_BGR2RGB)

    input_ = torch.from_numpy(img).float().div(255.).permute(2,0,1).unsqueeze(0).to(device)

    # Pad the input if not_multiple_of 8
    h,w = input_.shape[2], input_.shape[3]
    H,W = ((h+img_multiple_of)//img_multiple_of)*img_multiple_of, ((w+img_multiple_of)//img_multiple_of)*img_multiple_of
    padh = H-h if h%img_multiple_of!=0 else 0
    padw = W-w if w%img_multiple_of!=0 else 0
    input_ = F.pad(input_, (0,padw,0,padh), 'reflect')

    # print(h,w)
    restored = torch.clamp(model(input_),0,1)

    # Unpad the output
    restored = img_as_ubyte(restored[:,:,:h,:w].permute(0, 2, 3, 1).cpu().detach().numpy()[0])

    out_path = os.path.join(out_dir, os.path.split(args.input_path)[-1])
    cv2.imwrite(out_path,cv2.cvtColor(restored, cv2.COLOR_RGB2BGR))

    # print(f"\nRestored images are saved at {out_dir}")