Spaces:
Running
on
Zero
Running
on
Zero
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn.functional as F
|
3 |
+
import os
|
4 |
+
from skimage import img_as_ubyte
|
5 |
+
import cv2
|
6 |
+
import argparse
|
7 |
+
|
8 |
+
parser = argparse.ArgumentParser(description='Test Restormer on your own images')
|
9 |
+
parser.add_argument('--input_path', default='./temp/image.jpg', type=str, help='Directory of input images or path of single image')
|
10 |
+
parser.add_argument('--result_dir', default='./temp/', type=str, help='Directory for restored results')
|
11 |
+
parser.add_argument('--task', required=True, type=str, help='Task to run', choices=['Motion_Deblurring',
|
12 |
+
'Single_Image_Defocus_Deblurring',
|
13 |
+
'Deraining',
|
14 |
+
'Real_Denoising',
|
15 |
+
'Gaussian_Gray_Denoising',
|
16 |
+
'Gaussian_Color_Denoising'])
|
17 |
+
|
18 |
+
args = parser.parse_args()
|
19 |
+
|
20 |
+
|
21 |
+
task = args.task
|
22 |
+
out_dir = os.path.join(args.result_dir, task)
|
23 |
+
|
24 |
+
os.makedirs(out_dir, exist_ok=True)
|
25 |
+
|
26 |
+
|
27 |
+
if task == 'Motion_Deblurring':
|
28 |
+
model = torch.jit.load('motion_deblurring.pt')
|
29 |
+
elif task == 'Single_Image_Defocus_Deblurring':
|
30 |
+
model = torch.jit.load('single_image_defocus_deblurring.pt')
|
31 |
+
elif task == 'Deraining':
|
32 |
+
model = torch.jit.load('deraining.pt')
|
33 |
+
elif task == 'Real_Denoising':
|
34 |
+
model = torch.jit.load('real_denoising.pt')
|
35 |
+
|
36 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
37 |
+
# device = torch.device('cpu')
|
38 |
+
# stx()
|
39 |
+
|
40 |
+
model = model.to(device)
|
41 |
+
model.eval()
|
42 |
+
|
43 |
+
img_multiple_of = 8
|
44 |
+
|
45 |
+
with torch.inference_mode():
|
46 |
+
if torch.cuda.is_available():
|
47 |
+
torch.cuda.ipc_collect()
|
48 |
+
torch.cuda.empty_cache()
|
49 |
+
|
50 |
+
img = cv2.cvtColor(cv2.imread(args.input_path), cv2.COLOR_BGR2RGB)
|
51 |
+
|
52 |
+
input_ = torch.from_numpy(img).float().div(255.).permute(2,0,1).unsqueeze(0).to(device)
|
53 |
+
|
54 |
+
# Pad the input if not_multiple_of 8
|
55 |
+
h,w = input_.shape[2], input_.shape[3]
|
56 |
+
H,W = ((h+img_multiple_of)//img_multiple_of)*img_multiple_of, ((w+img_multiple_of)//img_multiple_of)*img_multiple_of
|
57 |
+
padh = H-h if h%img_multiple_of!=0 else 0
|
58 |
+
padw = W-w if w%img_multiple_of!=0 else 0
|
59 |
+
input_ = F.pad(input_, (0,padw,0,padh), 'reflect')
|
60 |
+
|
61 |
+
# print(h,w)
|
62 |
+
restored = torch.clamp(model(input_),0,1)
|
63 |
+
|
64 |
+
# Unpad the output
|
65 |
+
restored = img_as_ubyte(restored[:,:,:h,:w].permute(0, 2, 3, 1).cpu().detach().numpy()[0])
|
66 |
+
|
67 |
+
out_path = os.path.join(out_dir, os.path.split(args.input_path)[-1])
|
68 |
+
cv2.imwrite(out_path,cv2.cvtColor(restored, cv2.COLOR_RGB2BGR))
|
69 |
+
|
70 |
+
# print(f"\nRestored images are saved at {out_dir}")
|