Imadsarvm commited on
Commit
a96fd58
·
1 Parent(s): d9498bf

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import numpy as np
4
+ import torch
5
+ from models.network_swinir import SwinIR
6
+
7
+
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ print("device: %s" % device)
10
+ default_models = {
11
+ "sr": "weights/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.pth",
12
+ "denoise": "weights/005_colorDN_DFWB_s128w8_SwinIR-M_noise25.pth"
13
+ }
14
+ torch.backends.cudnn.enabled = True
15
+ torch.backends.cudnn.benchmark = True
16
+
17
+
18
+ denoise_model = SwinIR(upscale=1, in_chans=3, img_size=128, window_size=8,
19
+ img_range=1., depths=[6, 6, 6, 6, 6, 6], embed_dim=180, num_heads=[6, 6, 6, 6, 6, 6],
20
+ mlp_ratio=2, upsampler='', resi_connection='1conv').to(device)
21
+ param_key_g = 'params'
22
+ try:
23
+ pretrained_model = torch.load(default_models["denoise"])
24
+ denoise_model.load_state_dict(pretrained_model[param_key_g] if param_key_g in pretrained_model.keys() else pretrained_model, strict=True)
25
+ except: print("Loading model failed")
26
+ denoise_model.eval()
27
+
28
+ sr_model = SwinIR(upscale=4, in_chans=3, img_size=64, window_size=8,
29
+ img_range=1., depths=[6, 6, 6, 6, 6, 6], embed_dim=180, num_heads=[6, 6, 6, 6, 6, 6],
30
+ mlp_ratio=2, upsampler='nearest+conv', resi_connection='1conv').to(device)
31
+ param_key_g = 'params_ema'
32
+ try:
33
+ pretrained_model = torch.load(default_models["sr"])
34
+ sr_model.load_state_dict(pretrained_model[param_key_g] if param_key_g in pretrained_model.keys() else pretrained_model, strict=True)
35
+ except: print("Loading model failed")
36
+ sr_model.eval()
37
+
38
+
39
+ def sr(input_img):
40
+
41
+ window_size = 8
42
+ # read image
43
+ img_lq = input_img.astype(np.float32) / 255.
44
+ img_lq = np.transpose(img_lq if img_lq.shape[2] == 1 else img_lq[:, :, [2, 1, 0]], (2, 0, 1)) # HCW-BGR to CHW-RGB
45
+ img_lq = torch.from_numpy(img_lq).float().unsqueeze(0).to(device) # CHW-RGB to NCHW-RGB
46
+
47
+ # inference
48
+ with torch.no_grad():
49
+ # pad input image to be a multiple of window_size
50
+ _, _, h_old, w_old = img_lq.size()
51
+ h_pad = (h_old // window_size + 1) * window_size - h_old
52
+ w_pad = (w_old // window_size + 1) * window_size - w_old
53
+ img_lq = torch.cat([img_lq, torch.flip(img_lq, [2])], 2)[:, :, :h_old + h_pad, :]
54
+ img_lq = torch.cat([img_lq, torch.flip(img_lq, [3])], 3)[:, :, :, :w_old + w_pad]
55
+ output = sr_model(img_lq)
56
+ output = output[..., :h_old * 4, :w_old * 4]
57
+
58
+ # save image
59
+ output = output.data.squeeze().float().cpu().clamp_(0, 1).numpy()
60
+ if output.ndim == 3:
61
+ output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0)) # CHW-RGB to HCW-BGR
62
+ output = (output * 255.0).round().astype(np.uint8) # float32 to uint8
63
+
64
+ return output
65
+
66
+ def denoise(input_img):
67
+
68
+ window_size = 8
69
+ # read image
70
+ img_lq = input_img.astype(np.float32) / 255.
71
+ img_lq = np.transpose(img_lq if img_lq.shape[2] == 1 else img_lq[:, :, [2, 1, 0]], (2, 0, 1)) # HCW-BGR to CHW-RGB
72
+ img_lq = torch.from_numpy(img_lq).float().unsqueeze(0).to(device) # CHW-RGB to NCHW-RGB
73
+
74
+ # inference
75
+ with torch.no_grad():
76
+ # pad input image to be a multiple of window_size
77
+ _, _, h_old, w_old = img_lq.size()
78
+ h_pad = (h_old // window_size + 1) * window_size - h_old
79
+ w_pad = (w_old // window_size + 1) * window_size - w_old
80
+ img_lq = torch.cat([img_lq, torch.flip(img_lq, [2])], 2)[:, :, :h_old + h_pad, :]
81
+ img_lq = torch.cat([img_lq, torch.flip(img_lq, [3])], 3)[:, :, :, :w_old + w_pad]
82
+ output = denoise_model(img_lq)
83
+ output = output[..., :h_old * 4, :w_old * 4]
84
+
85
+ # save image
86
+ output = output.data.squeeze().float().cpu().clamp_(0, 1).numpy()
87
+ if output.ndim == 3:
88
+ output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0)) # CHW-RGB to HCW-BGR
89
+ output = (output * 255.0).round().astype(np.uint8) # float32 to uint8
90
+
91
+ return output
92
+
93
+ title = " AISeed AI Application Demo "
94
+ description = "# A Demo of Deep Learning for Image Restoration"
95
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
96
+
97
+ with gr.Blocks() as demo:
98
+ demo.title = title
99
+ gr.Markdown(description)
100
+ with gr.Row():
101
+ with gr.Column():
102
+ im = gr.Image(label="Input Image")
103
+ im_2 = gr.Image(label="Enhanced Image")
104
+
105
+ with gr.Column():
106
+
107
+ btn1 = gr.Button(value="Enhance Resolution")
108
+ btn1.click(sr, inputs=[im], outputs=[im_2])
109
+ btn2 = gr.Button(value="Denoise")
110
+ btn2.click(denoise, inputs=[im], outputs=[im_2])
111
+ gr.Examples(examples=example_list,
112
+ inputs=[im],
113
+ outputs=[im_2])
114
+
115
+ if __name__ == "__main__":
116
+ demo.launch()