Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,35 @@
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# -*- coding:utf-8 -*-
|
| 3 |
-
# Power by Zongsheng Yue 2024-12-11 17:17:41
|
| 4 |
-
# Modified to run on CPU
|
| 5 |
|
| 6 |
import os
|
| 7 |
-
|
| 8 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 9 |
|
|
|
|
| 10 |
import warnings
|
| 11 |
warnings.filterwarnings("ignore")
|
|
|
|
| 12 |
import argparse
|
| 13 |
import numpy as np
|
| 14 |
import gradio as gr
|
| 15 |
from pathlib import Path
|
| 16 |
from omegaconf import OmegaConf
|
| 17 |
from sampler_invsr import InvSamplerSR
|
|
|
|
| 18 |
from utils import util_common
|
| 19 |
from utils import util_image
|
| 20 |
from basicsr.utils.download_util import load_file_from_url
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def get_configs(num_steps=1, chopping_size=128, seed=12345):
|
| 23 |
configs = OmegaConf.load("./configs/sample-sd-turbo.yaml")
|
|
|
|
| 24 |
if num_steps == 1:
|
| 25 |
configs.timesteps = [200,]
|
| 26 |
elif num_steps == 2:
|
|
@@ -36,12 +45,13 @@ def get_configs(num_steps=1, chopping_size=128, seed=12345):
|
|
| 36 |
configs.timesteps = np.linspace(
|
| 37 |
start=250, stop=0, num=num_steps, endpoint=False, dtype=np.int64()
|
| 38 |
).tolist()
|
|
|
|
| 39 |
print(f'Setting timesteps for inference: {configs.timesteps}')
|
| 40 |
-
|
| 41 |
-
# path to save noise predictor
|
| 42 |
started_ckpt_name = "noise_predictor_sd_turbo_v5.pth"
|
| 43 |
started_ckpt_dir = "./weights"
|
| 44 |
util_common.mkdir(started_ckpt_dir, delete=False, parents=True)
|
|
|
|
| 45 |
started_ckpt_path = Path(started_ckpt_dir) / started_ckpt_name
|
| 46 |
if not started_ckpt_path.exists():
|
| 47 |
load_file_from_url(
|
|
@@ -50,100 +60,63 @@ def get_configs(num_steps=1, chopping_size=128, seed=12345):
|
|
| 50 |
progress=True,
|
| 51 |
file_name=started_ckpt_name,
|
| 52 |
)
|
|
|
|
| 53 |
configs.model_start.ckpt_path = str(started_ckpt_path)
|
| 54 |
configs.bs = 1
|
| 55 |
-
configs.seed = seed
|
| 56 |
configs.basesr.chopping.pch_size = chopping_size
|
|
|
|
| 57 |
if chopping_size == 128:
|
| 58 |
configs.basesr.chopping.extra_bs = 8
|
| 59 |
elif chopping_size == 256:
|
| 60 |
configs.basesr.chopping.extra_bs = 4
|
| 61 |
else:
|
| 62 |
configs.basesr.chopping.extra_bs = 1
|
| 63 |
-
|
| 64 |
-
# Explicitly set device to CPU if the config schema supports it
|
| 65 |
-
if OmegaConf.is_struct(configs):
|
| 66 |
-
OmegaConf.update(configs, 'device', 'cpu')
|
| 67 |
-
else:
|
| 68 |
-
configs.device = 'cpu'
|
| 69 |
-
|
| 70 |
return configs
|
| 71 |
|
| 72 |
-
|
|
|
|
| 73 |
def predict(in_path, num_steps=1, chopping_size=128, seed=12345):
|
| 74 |
configs = get_configs(num_steps=num_steps, chopping_size=chopping_size, seed=seed)
|
|
|
|
| 75 |
sampler = InvSamplerSR(configs)
|
|
|
|
| 76 |
out_dir = Path('invsr_output')
|
| 77 |
-
|
| 78 |
-
|
| 79 |
sampler.inference(in_path, out_path=out_dir, bs=1)
|
|
|
|
| 80 |
out_path = out_dir / f"{Path(in_path).stem}.png"
|
| 81 |
assert out_path.exists(), 'Super-resolution failed!'
|
|
|
|
| 82 |
im_sr = util_image.imread(out_path, chn="rgb", dtype="uint8")
|
|
|
|
| 83 |
return im_sr, str(out_path)
|
| 84 |
|
|
|
|
| 85 |
title = "Arbitrary-steps Image Super-resolution via Diffusion Inversion"
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"""
|
| 91 |
-
article = r"""
|
| 92 |
-
If you've found InvSR useful for your research or projects, please show your support by ⭐ the <a href='https://github.com/zsyOAOA/InvSR' target='_blank'>Github Repo</a>. Thanks!
|
| 93 |
-
[](https://github.com/zsyOAOA/InvSR)
|
| 94 |
-
---
|
| 95 |
-
If our work is useful for your research, please consider citing:
|
| 96 |
-
```bibtex
|
| 97 |
-
@article{yue2024InvSR,
|
| 98 |
-
title={Arbitrary-steps Image Super-resolution via Diffusion Inversion},
|
| 99 |
-
author={Yue, Zongsheng and Kang, Liao and Loy, Chen Change},
|
| 100 |
-
journal = {arXiv preprint arXiv:2412.09013},
|
| 101 |
-
year={2024},
|
| 102 |
-
}
|
| 103 |
-
```
|
| 104 |
-
|
| 105 |
-
📋 **License**
|
| 106 |
-
|
| 107 |
-
This project is licensed under <a rel="license" href="https://github.com/zsyOAOA/InvSR/blob/master/LICENSE">S-Lab License 1.0</a>.
|
| 108 |
-
Redistribution and use for non-commercial purposes should follow this license.
|
| 109 |
-
|
| 110 |
-
📧 **Contact**
|
| 111 |
-
|
| 112 |
-
If you have any questions, please feel free to contact me via <b>zsyzam@gmail.com</b>.
|
| 113 |
-

|
| 114 |
"""
|
|
|
|
| 115 |
demo = gr.Interface(
|
| 116 |
fn=predict,
|
| 117 |
inputs=[
|
| 118 |
-
gr.Image(type="filepath", label="Input
|
| 119 |
-
gr.Dropdown(
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
label="Number of steps",
|
| 123 |
-
),
|
| 124 |
-
gr.Dropdown(
|
| 125 |
-
choices=[128, 256, 512],
|
| 126 |
-
value=128,
|
| 127 |
-
label="Chopping size",
|
| 128 |
-
),
|
| 129 |
-
gr.Number(value=12345, precision=0, label="Ranom seed")
|
| 130 |
],
|
| 131 |
outputs=[
|
| 132 |
-
gr.Image(type="numpy", label="Output
|
| 133 |
-
gr.File(label="Download
|
| 134 |
],
|
| 135 |
title=title,
|
| 136 |
-
description=description
|
| 137 |
-
|
| 138 |
-
examples=[
|
| 139 |
-
['./testdata/RealSet80/29.jpg', 3, 128, 12345],
|
| 140 |
-
['./testdata/RealSet80/32.jpg', 1, 128, 12345],
|
| 141 |
-
['./testdata/RealSet80/0030.jpg', 1, 128, 12345],
|
| 142 |
-
['./testdata/RealSet80/2684538-PH.jpg', 1, 128, 12345],
|
| 143 |
-
['./testdata/RealSet80/oldphoto6.png', 1, 128, 12345],
|
| 144 |
-
]
|
| 145 |
-
)
|
| 146 |
|
| 147 |
demo.queue(max_size=5)
|
| 148 |
-
demo.launch(
|
| 149 |
-
|
|
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# -*- coding:utf-8 -*-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import os
|
| 5 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "" # 🔥 Force CPU
|
|
|
|
| 6 |
|
| 7 |
+
import spaces
|
| 8 |
import warnings
|
| 9 |
warnings.filterwarnings("ignore")
|
| 10 |
+
|
| 11 |
import argparse
|
| 12 |
import numpy as np
|
| 13 |
import gradio as gr
|
| 14 |
from pathlib import Path
|
| 15 |
from omegaconf import OmegaConf
|
| 16 |
from sampler_invsr import InvSamplerSR
|
| 17 |
+
|
| 18 |
from utils import util_common
|
| 19 |
from utils import util_image
|
| 20 |
from basicsr.utils.download_util import load_file_from_url
|
| 21 |
|
| 22 |
+
# Optional: enforce CPU in torch if used internally
|
| 23 |
+
try:
|
| 24 |
+
import torch
|
| 25 |
+
torch.set_default_device("cpu")
|
| 26 |
+
except:
|
| 27 |
+
pass
|
| 28 |
+
|
| 29 |
+
|
| 30 |
def get_configs(num_steps=1, chopping_size=128, seed=12345):
|
| 31 |
configs = OmegaConf.load("./configs/sample-sd-turbo.yaml")
|
| 32 |
+
|
| 33 |
if num_steps == 1:
|
| 34 |
configs.timesteps = [200,]
|
| 35 |
elif num_steps == 2:
|
|
|
|
| 45 |
configs.timesteps = np.linspace(
|
| 46 |
start=250, stop=0, num=num_steps, endpoint=False, dtype=np.int64()
|
| 47 |
).tolist()
|
| 48 |
+
|
| 49 |
print(f'Setting timesteps for inference: {configs.timesteps}')
|
| 50 |
+
|
|
|
|
| 51 |
started_ckpt_name = "noise_predictor_sd_turbo_v5.pth"
|
| 52 |
started_ckpt_dir = "./weights"
|
| 53 |
util_common.mkdir(started_ckpt_dir, delete=False, parents=True)
|
| 54 |
+
|
| 55 |
started_ckpt_path = Path(started_ckpt_dir) / started_ckpt_name
|
| 56 |
if not started_ckpt_path.exists():
|
| 57 |
load_file_from_url(
|
|
|
|
| 60 |
progress=True,
|
| 61 |
file_name=started_ckpt_name,
|
| 62 |
)
|
| 63 |
+
|
| 64 |
configs.model_start.ckpt_path = str(started_ckpt_path)
|
| 65 |
configs.bs = 1
|
| 66 |
+
configs.seed = seed
|
| 67 |
configs.basesr.chopping.pch_size = chopping_size
|
| 68 |
+
|
| 69 |
if chopping_size == 128:
|
| 70 |
configs.basesr.chopping.extra_bs = 8
|
| 71 |
elif chopping_size == 256:
|
| 72 |
configs.basesr.chopping.extra_bs = 4
|
| 73 |
else:
|
| 74 |
configs.basesr.chopping.extra_bs = 1
|
| 75 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
return configs
|
| 77 |
|
| 78 |
+
|
| 79 |
+
# ❌ Removed @spaces.GPU
|
| 80 |
def predict(in_path, num_steps=1, chopping_size=128, seed=12345):
|
| 81 |
configs = get_configs(num_steps=num_steps, chopping_size=chopping_size, seed=seed)
|
| 82 |
+
|
| 83 |
sampler = InvSamplerSR(configs)
|
| 84 |
+
|
| 85 |
out_dir = Path('invsr_output')
|
| 86 |
+
out_dir.mkdir(exist_ok=True)
|
| 87 |
+
|
| 88 |
sampler.inference(in_path, out_path=out_dir, bs=1)
|
| 89 |
+
|
| 90 |
out_path = out_dir / f"{Path(in_path).stem}.png"
|
| 91 |
assert out_path.exists(), 'Super-resolution failed!'
|
| 92 |
+
|
| 93 |
im_sr = util_image.imread(out_path, chn="rgb", dtype="uint8")
|
| 94 |
+
|
| 95 |
return im_sr, str(out_path)
|
| 96 |
|
| 97 |
+
|
| 98 |
title = "Arbitrary-steps Image Super-resolution via Diffusion Inversion"
|
| 99 |
+
|
| 100 |
+
description = """
|
| 101 |
+
<b>CPU version</b> of InvSR demo.<br>
|
| 102 |
+
⚠️ Note: Running on CPU will be significantly slower than GPU.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
"""
|
| 104 |
+
|
| 105 |
demo = gr.Interface(
|
| 106 |
fn=predict,
|
| 107 |
inputs=[
|
| 108 |
+
gr.Image(type="filepath", label="Input Image"),
|
| 109 |
+
gr.Dropdown([1,2,3,4,5], value=1, label="Steps"),
|
| 110 |
+
gr.Dropdown([128, 256, 512], value=128, label="Chopping size"),
|
| 111 |
+
gr.Number(value=12345, precision=0, label="Seed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
],
|
| 113 |
outputs=[
|
| 114 |
+
gr.Image(type="numpy", label="Output Image"),
|
| 115 |
+
gr.File(label="Download")
|
| 116 |
],
|
| 117 |
title=title,
|
| 118 |
+
description=description
|
| 119 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
demo.queue(max_size=5)
|
| 122 |
+
demo.launch()
|
|
|