Spaces:
mikitona
/
Running on Zero

mikitona commited on
Commit
ea10013
·
verified ·
1 Parent(s): 0d6e78f

Upload gradio_s3diff.py

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