Spaces:
Running on Zero
Running on Zero
Commit ·
4b96e06
1
Parent(s): 5b5a563
Muliple files updated
Browse files- CodeFormer/basicsr/{VERSION → _VERSION} +0 -0
- CodeFormer/basicsr/train.py +1 -2
- _res/_custom.css +1 -1
- app.py +46 -59
- modules/__init__.py +0 -0
- modules/weights_downloads.py +20 -0
- requirements.txt +4 -1
CodeFormer/basicsr/{VERSION → _VERSION}
RENAMED
|
File without changes
|
CodeFormer/basicsr/train.py
CHANGED
|
@@ -12,8 +12,7 @@ from basicsr.data import build_dataloader, build_dataset
|
|
| 12 |
from basicsr.data.data_sampler import EnlargedSampler
|
| 13 |
from basicsr.data.prefetch_dataloader import CPUPrefetcher, CUDAPrefetcher
|
| 14 |
from basicsr.models import build_model
|
| 15 |
-
from basicsr.utils import (MessageLogger, check_resume, get_env_info, get_root_logger, init_tb_logger,
|
| 16 |
-
init_wandb_logger, make_exp_dirs, mkdir_and_rename, set_random_seed)
|
| 17 |
from basicsr.utils.dist_util import get_dist_info, init_dist
|
| 18 |
from basicsr.utils.options import dict2str, parse
|
| 19 |
|
|
|
|
| 12 |
from basicsr.data.data_sampler import EnlargedSampler
|
| 13 |
from basicsr.data.prefetch_dataloader import CPUPrefetcher, CUDAPrefetcher
|
| 14 |
from basicsr.models import build_model
|
| 15 |
+
from basicsr.utils import (MessageLogger, check_resume, get_env_info, get_root_logger, init_tb_logger,init_wandb_logger, make_exp_dirs, mkdir_and_rename, set_random_seed)
|
|
|
|
| 16 |
from basicsr.utils.dist_util import get_dist_info, init_dist
|
| 17 |
from basicsr.utils.options import dict2str, parse
|
| 18 |
|
_res/_custom.css
CHANGED
|
@@ -86,7 +86,7 @@ body>gradio-app.has-bg-image::before {
|
|
| 86 |
gradio-app .gradio-container {
|
| 87 |
max-width: 80% !important;
|
| 88 |
|
| 89 |
-
& div.
|
| 90 |
background-color: var(--neutral-900) !important;
|
| 91 |
border-right-width: 0px !important;
|
| 92 |
border-bottom-width: 0px !important;
|
|
|
|
| 86 |
gradio-app .gradio-container {
|
| 87 |
max-width: 80% !important;
|
| 88 |
|
| 89 |
+
& div.row-main {
|
| 90 |
background-color: var(--neutral-900) !important;
|
| 91 |
border-right-width: 0px !important;
|
| 92 |
border-bottom-width: 0px !important;
|
app.py
CHANGED
|
@@ -16,6 +16,8 @@ from basicsr.utils.realesrgan_utils import RealESRGANer
|
|
| 16 |
|
| 17 |
from basicsr.utils.registry import ARCH_REGISTRY
|
| 18 |
|
|
|
|
|
|
|
| 19 |
# Get torch device
|
| 20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
|
|
@@ -38,41 +40,23 @@ theme = gr.themes.Soft(
|
|
| 38 |
|
| 39 |
os.system("pip freeze")
|
| 40 |
|
| 41 |
-
pretrain_model_url = {
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
}
|
| 47 |
-
# download weights
|
| 48 |
-
if not os.path.exists('CodeFormer/weights/CodeFormer/codeformer.pth'):
|
| 49 |
-
|
| 50 |
-
if not os.path.exists('CodeFormer/weights/facelib/detection_Resnet50_Final.pth'):
|
| 51 |
-
|
| 52 |
-
if not os.path.exists('CodeFormer/weights/facelib/parsing_parsenet.pth'):
|
| 53 |
-
|
| 54 |
-
if not os.path.exists('CodeFormer/weights/realesrgan/RealESRGAN_x2plus.pth'):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
torch.hub.download_url_to_file(
|
| 59 |
-
'https://replicate.com/api/models/sczhou/codeformer/files/fa3fe3d1-76b0-4ca8-ac0d-0a925cb0ff54/06.png',
|
| 60 |
-
'01.png')
|
| 61 |
-
torch.hub.download_url_to_file(
|
| 62 |
-
'https://replicate.com/api/models/sczhou/codeformer/files/a1daba8e-af14-4b00-86a4-69cec9619b53/04.jpg',
|
| 63 |
-
'02.jpg')
|
| 64 |
-
torch.hub.download_url_to_file(
|
| 65 |
-
'https://replicate.com/api/models/sczhou/codeformer/files/542d64f9-1712-4de7-85f7-3863009a7c3d/03.jpg',
|
| 66 |
-
'03.jpg')
|
| 67 |
-
torch.hub.download_url_to_file(
|
| 68 |
-
'https://replicate.com/api/models/sczhou/codeformer/files/a11098b0-a18a-4c02-a19a-9a7045d68426/010.jpg',
|
| 69 |
-
'04.jpg')
|
| 70 |
-
torch.hub.download_url_to_file(
|
| 71 |
-
'https://replicate.com/api/models/sczhou/codeformer/files/7cf19c2c-e0cf-4712-9af8-cf5bdbb8d0ee/012.jpg',
|
| 72 |
-
'05.jpg')
|
| 73 |
-
torch.hub.download_url_to_file(
|
| 74 |
-
'https://raw.githubusercontent.com/sczhou/CodeFormer/master/inputs/cropped_faces/0729.png',
|
| 75 |
-
'06.png')
|
| 76 |
|
| 77 |
def imread(img_path):
|
| 78 |
img = cv2.imread(img_path)
|
|
@@ -117,18 +101,20 @@ codeformer_net.eval()
|
|
| 117 |
|
| 118 |
os.makedirs('output', exist_ok=True)
|
| 119 |
|
| 120 |
-
def inference(image,
|
| 121 |
"""Run a single prediction on the model"""
|
| 122 |
try: # global try
|
| 123 |
# take the default setting for the demo
|
|
|
|
| 124 |
only_center_face = False
|
| 125 |
draw_box = False
|
| 126 |
detection_model = "retinaface_resnet50"
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
| 132 |
upscale = upscale if (upscale is not None and upscale > 0) else 2
|
| 133 |
|
| 134 |
has_aligned = not face_align
|
|
@@ -240,22 +226,23 @@ title = "Photo restoration and enhancement by CodeFormer"
|
|
| 240 |
description = ""
|
| 241 |
article = ""
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
gr.
|
| 246 |
-
|
| 247 |
-
gr.
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
)
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
| 16 |
|
| 17 |
from basicsr.utils.registry import ARCH_REGISTRY
|
| 18 |
|
| 19 |
+
from modules import weights_downloads
|
| 20 |
+
|
| 21 |
# Get torch device
|
| 22 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
|
|
|
|
| 40 |
|
| 41 |
os.system("pip freeze")
|
| 42 |
|
| 43 |
+
# pretrain_model_url = {
|
| 44 |
+
# 'codeformer': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth',
|
| 45 |
+
# 'detection': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/detection_Resnet50_Final.pth',
|
| 46 |
+
# 'parsing': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/parsing_parsenet.pth',
|
| 47 |
+
# 'realesrgan': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth'
|
| 48 |
+
# }
|
| 49 |
+
# # download weights
|
| 50 |
+
# if not os.path.exists('CodeFormer/weights/CodeFormer/codeformer.pth'):
|
| 51 |
+
# load_file_from_url(url=pretrain_model_url['codeformer'], model_dir='CodeFormer/weights/CodeFormer', progress=True, file_name=None)
|
| 52 |
+
# if not os.path.exists('CodeFormer/weights/facelib/detection_Resnet50_Final.pth'):
|
| 53 |
+
# load_file_from_url(url=pretrain_model_url['detection'], model_dir='CodeFormer/weights/facelib', progress=True, file_name=None)
|
| 54 |
+
# if not os.path.exists('CodeFormer/weights/facelib/parsing_parsenet.pth'):
|
| 55 |
+
# load_file_from_url(url=pretrain_model_url['parsing'], model_dir='CodeFormer/weights/facelib', progress=True, file_name=None)
|
| 56 |
+
# if not os.path.exists('CodeFormer/weights/realesrgan/RealESRGAN_x2plus.pth'):
|
| 57 |
+
# load_file_from_url(url=pretrain_model_url['realesrgan'], model_dir='CodeFormer/weights/realesrgan', progress=True, file_name=None)
|
| 58 |
+
|
| 59 |
+
weights_downloads()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def imread(img_path):
|
| 62 |
img = cv2.imread(img_path)
|
|
|
|
| 101 |
|
| 102 |
os.makedirs('output', exist_ok=True)
|
| 103 |
|
| 104 |
+
def inference(image, inf_options, upscale, codeformer_fidelity):
|
| 105 |
"""Run a single prediction on the model"""
|
| 106 |
try: # global try
|
| 107 |
# take the default setting for the demo
|
| 108 |
+
# face_align, background_enhance, face_upsample,
|
| 109 |
only_center_face = False
|
| 110 |
draw_box = False
|
| 111 |
detection_model = "retinaface_resnet50"
|
| 112 |
|
| 113 |
+
# "Gesicht ausrichten", "Hintergrund verbessern", "Gesicht Hochskalieren"
|
| 114 |
+
print('Inp:', image, inf_options, upscale, codeformer_fidelity)
|
| 115 |
+
face_align = False if "Gesicht ausrichten" not in inf_options else True
|
| 116 |
+
background_enhance = False if "Hintergrund verbessern" not in inf_options else True
|
| 117 |
+
face_upsample = face_upsample if "Gesicht Hochskalieren" not in inf_options else True
|
| 118 |
upscale = upscale if (upscale is not None and upscale > 0) else 2
|
| 119 |
|
| 120 |
has_aligned = not face_align
|
|
|
|
| 226 |
description = ""
|
| 227 |
article = ""
|
| 228 |
|
| 229 |
+
with gr.Blocks(theme=theme, title=title, css=custom_css, js=custom_js) as demo_photo_enhance:
|
| 230 |
+
with gr.Row(elem_classes="row-header"):
|
| 231 |
+
gr.Markdown("""""")
|
| 232 |
+
with gr.Row(elem_classes="row-main"):
|
| 233 |
+
with gr.Column(scale=1):
|
| 234 |
+
inp_image = gr.Image(type="filepath", label="Input")
|
| 235 |
+
inp_lang = gr.Dropdown(["English", "Deutsch"], value="Deutsch", label="Sprache", interactive=True)
|
| 236 |
+
inf_options = gr.CheckboxGroup(["Gesicht ausrichten", "Hintergrund verbessern", "Gesicht Hochskalieren",], label="Optionen", info="Where are they from?", interactive=True)
|
| 237 |
+
inp_scale = gr.Slider(0, 4, value=2, step=1, label="Foto Hochskalieren", info="Du kannst das Foto bis zum Faktor 4 hochskalieren")
|
| 238 |
+
inp_factor = gr.Slider(0, 1, value=0.5, step=0.01, label='Verbesserungsfaktor', info='zu 0 verstärkt die Ausgabe, zu 1 erhält die Identität')
|
| 239 |
+
run_btn = gr.Button("Los")
|
| 240 |
+
with gr.Column(scale=2):
|
| 241 |
+
output_image = gr.Image(type="numpy", label="Ergebnis")
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
run_btn.click(fn=inference, inputs=[inp_image, inp_lang, inp_scale], outputs=[out_audio])
|
| 245 |
+
|
| 246 |
+
if __name__ == "__main__":
|
| 247 |
+
DEBUG = os.getenv('DEBUG') == '1'
|
| 248 |
+
demo_photo_enhance.launch(debug=DEBUG)
|
modules/__init__.py
ADDED
|
File without changes
|
modules/weights_downloads.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
def download_wights():
|
| 3 |
+
|
| 4 |
+
# MARK: PRETRAINED MODEL URLS
|
| 5 |
+
pretrain_model_url = {
|
| 6 |
+
'codeformer': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth',
|
| 7 |
+
'detection': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/detection_Resnet50_Final.pth',
|
| 8 |
+
'parsing': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/parsing_parsenet.pth',
|
| 9 |
+
'realesrgan': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth'
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
# MARK: WEIGHT URLS
|
| 13 |
+
if not os.path.exists('CodeFormer/weights/CodeFormer/codeformer.pth'):
|
| 14 |
+
load_file_from_url(url=pretrain_model_url['codeformer'], model_dir='CodeFormer/weights/CodeFormer', progress=True, file_name=None)
|
| 15 |
+
if not os.path.exists('CodeFormer/weights/facelib/detection_Resnet50_Final.pth'):
|
| 16 |
+
load_file_from_url(url=pretrain_model_url['detection'], model_dir='CodeFormer/weights/facelib', progress=True, file_name=None)
|
| 17 |
+
if not os.path.exists('CodeFormer/weights/facelib/parsing_parsenet.pth'):
|
| 18 |
+
load_file_from_url(url=pretrain_model_url['parsing'], model_dir='CodeFormer/weights/facelib', progress=True, file_name=None)
|
| 19 |
+
if not os.path.exists('CodeFormer/weights/realesrgan/RealESRGAN_x2plus.pth'):
|
| 20 |
+
load_file_from_url(url=pretrain_model_url['realesrgan'], model_dir='CodeFormer/weights/realesrgan', progress=True, file_name=None)
|
requirements.txt
CHANGED
|
@@ -14,7 +14,10 @@ torchvision
|
|
| 14 |
tqdm
|
| 15 |
yapf
|
| 16 |
lpips
|
| 17 |
-
gdown
|
|
|
|
|
|
|
|
|
|
| 18 |
# cmake
|
| 19 |
# dlib
|
| 20 |
# conda install -c conda-forge dlib
|
|
|
|
| 14 |
tqdm
|
| 15 |
yapf
|
| 16 |
lpips
|
| 17 |
+
gdown
|
| 18 |
+
gradio_imageslider
|
| 19 |
+
gradio_modal
|
| 20 |
+
# supports downloading the large file from Google Drive
|
| 21 |
# cmake
|
| 22 |
# dlib
|
| 23 |
# conda install -c conda-forge dlib
|