zhangap commited on
Commit
4384dd9
1 Parent(s): e3f0e80

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -0
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import sys
4
+ import math
5
+ from typing import List
6
+
7
+ import numpy as np
8
+ from PIL import Image
9
+
10
+ import torch
11
+ import torch.nn.functional as F
12
+ import torch.utils.checkpoint
13
+ from diffusers.utils.import_utils import is_xformers_available
14
+
15
+ from my_utils.testing_utils import parse_args_paired_testing
16
+ from de_net import DEResNet
17
+ from s3diff_tile import S3Diff
18
+ from torchvision import transforms
19
+ from utils.wavelet_color import wavelet_color_fix, adain_color_fix
20
+
21
+ tensor_transforms = transforms.Compose([
22
+ transforms.ToTensor(),
23
+ ])
24
+
25
+ args = parse_args_paired_testing()
26
+
27
+ # Load scheduler, tokenizer and models.
28
+ pretrained_model_path = 'checkpoint-path/s3diff.pkl'
29
+ t2i_path = 'sd-turbo-path'
30
+ de_net_path = 'assets/mm-realsr/de_net.pth'
31
+
32
+ # initialize net_sr
33
+ net_sr = S3Diff(lora_rank_unet=args.lora_rank_unet, lora_rank_vae=args.lora_rank_vae, sd_path=t2i_path, pretrained_path=pretrained_model_path, args=args)
34
+ net_sr.set_eval()
35
+
36
+ # initalize degradation estimation network
37
+ net_de = DEResNet(num_in_ch=3, num_degradation=2)
38
+ net_de.load_model(de_net_path)
39
+ net_de = net_de.cuda()
40
+ net_de.eval()
41
+
42
+ if args.enable_xformers_memory_efficient_attention:
43
+ if is_xformers_available():
44
+ net_sr.unet.enable_xformers_memory_efficient_attention()
45
+ else:
46
+ raise ValueError("xformers is not available. Make sure it is installed correctly")
47
+
48
+ if args.gradient_checkpointing:
49
+ net_sr.unet.enable_gradient_checkpointing()
50
+
51
+ weight_dtype = torch.float32
52
+ device = "cuda"
53
+
54
+ # Move text_encode and vae to gpu and cast to weight_dtype
55
+ net_sr.to(device, dtype=weight_dtype)
56
+ net_de.to(device, dtype=weight_dtype)
57
+
58
+ @torch.no_grad()
59
+ def process(
60
+ input_image: Image.Image,
61
+ scale_factor: float,
62
+ cfg_scale: float,
63
+ latent_tiled_size: int,
64
+ latent_tiled_overlap: int,
65
+ align_method: str,
66
+ ) -> List[np.ndarray]:
67
+
68
+ # positive_prompt = ""
69
+ # negative_prompt = ""
70
+
71
+ net_sr._set_latent_tile(latent_tiled_size = latent_tiled_size, latent_tiled_overlap = latent_tiled_overlap)
72
+
73
+ im_lr = tensor_transforms(input_image).unsqueeze(0).to(device)
74
+ ori_h, ori_w = im_lr.shape[2:]
75
+ im_lr_resize = F.interpolate(
76
+ im_lr,
77
+ size=(int(ori_h * scale_factor),
78
+ int(ori_w * scale_factor)),
79
+ mode='bicubic',
80
+ )
81
+ im_lr_resize = im_lr_resize.contiguous()
82
+ im_lr_resize_norm = im_lr_resize * 2 - 1.0
83
+ im_lr_resize_norm = torch.clamp(im_lr_resize_norm, -1.0, 1.0)
84
+ resize_h, resize_w = im_lr_resize_norm.shape[2:]
85
+
86
+ pad_h = (math.ceil(resize_h / 64)) * 64 - resize_h
87
+ pad_w = (math.ceil(resize_w / 64)) * 64 - resize_w
88
+ im_lr_resize_norm = F.pad(im_lr_resize_norm, pad=(0, pad_w, 0, pad_h), mode='reflect')
89
+
90
+ try:
91
+ with torch.autocast("cuda"):
92
+ deg_score = net_de(im_lr)
93
+
94
+ pos_tag_prompt = [args.pos_prompt]
95
+ neg_tag_prompt = [args.neg_prompt]
96
+
97
+ x_tgt_pred = net_sr(im_lr_resize_norm, deg_score, pos_prompt=pos_tag_prompt, neg_prompt=neg_tag_prompt)
98
+ x_tgt_pred = x_tgt_pred[:, :, :resize_h, :resize_w]
99
+ out_img = (x_tgt_pred * 0.5 + 0.5).cpu().detach()
100
+
101
+ output_pil = transforms.ToPILImage()(out_img[0])
102
+
103
+ if align_method == 'no fix':
104
+ image = output_pil
105
+ else:
106
+ im_lr_resize = transforms.ToPILImage()(im_lr_resize[0])
107
+ if align_method == 'wavelet':
108
+ image = wavelet_color_fix(output_pil, im_lr_resize)
109
+ elif align_method == 'adain':
110
+ image = adain_color_fix(output_pil, im_lr_resize)
111
+
112
+ except Exception as e:
113
+ print(e)
114
+ image = Image.new(mode="RGB", size=(512, 512))
115
+
116
+ return image
117
+
118
+
119
+ #
120
+ MARKDOWN = \
121
+ """
122
+ ## Degradation-Guided One-Step Image Super-Resolution with Diffusion Priors
123
+
124
+ [GitHub](https://github.com/ArcticHare105/S3Diff) | [Paper](https://arxiv.org/abs/2409.17058)
125
+
126
+ If S3Diff is helpful for you, please help star the GitHub Repo. Thanks!
127
+ """
128
+
129
+ block = gr.Blocks().queue()
130
+ with block:
131
+ with gr.Row():
132
+ gr.Markdown(MARKDOWN)
133
+ with gr.Row():
134
+ with gr.Column():
135
+ input_image = gr.Image(source="upload", type="pil")
136
+ run_button = gr.Button(label="Run")
137
+ with gr.Accordion("Options", open=True):
138
+ cfg_scale = gr.Slider(label="Classifier Free Guidance Scale (Set a value larger than 1 to enable it!)", minimum=1.0, maximum=1.1, value=1.07, step=0.01)
139
+ scale_factor = gr.Number(label="SR Scale", value=4)
140
+ latent_tiled_size = gr.Slider(label="Tile Size", minimum=64, maximum=160, value=96, step=1)
141
+ latent_tiled_overlap = gr.Slider(label="Tile Overlap", minimum=16, maximum=48, value=32, step=1)
142
+ align_method = gr.Dropdown(label="Color Correction", choices=["wavelet", "adain", "no fix"], value="wavelet")
143
+ with gr.Column():
144
+ result_image = gr.Image(label="Output", show_label=False, elem_id="result_image", source="canvas", width="100%", height="auto")
145
+
146
+ inputs = [
147
+ input_image,
148
+ scale_factor,
149
+ cfg_scale,
150
+ latent_tiled_size,
151
+ latent_tiled_overlap,
152
+ align_method
153
+ ]
154
+ run_button.click(fn=process, inputs=inputs, outputs=[result_image])
155
+
156
+ block.launch()
157
+