From 16f6a6731d80fcc04dcdb693d74fc5c21e753c10 Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Sun, 11 Sep 2022 12:47:26 -0400 Subject: [PATCH] install GFPGAN inside SD repository in order to fix 'dark cast' issue #169 --- environment-mac.yaml | 2 ++ environment.yaml | 5 +++-- ldm/gfpgan/gfpgan_tools.py | 13 ++++++++----- scripts/dream.py | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/environment-mac.yaml b/environment-mac.yaml index 07d7d6a0f0..8338d63238 100644 --- a/environment-mac.yaml +++ b/environment-mac.yaml @@ -35,6 +35,7 @@ dependencies: - opencv==4.6.0 - pudb==2022.1 - pytorch-lightning==1.6.5 + - realesrgan==0.2.5.0 - scipy==1.9.1 - streamlit==1.12.2 - sympy==1.10.1 @@ -47,6 +48,7 @@ dependencies: - -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers - -e git+https://github.com/openai/CLIP.git@main#egg=clip - -e git+https://github.com/Birch-san/k-diffusion.git@mps#egg=k_diffusion + - -e git+https://github.com/lstein/GFPGAN@fix-dark-cast-images#egg=gfpgan - -e . variables: PYTORCH_ENABLE_MPS_FALLBACK: 1 diff --git a/environment.yaml b/environment.yaml index 7d5b4fe9e3..2a8a6290be 100644 --- a/environment.yaml +++ b/environment.yaml @@ -1,4 +1,4 @@ -name: ldm +name: sd-ldm channels: - pytorch - defaults @@ -11,7 +11,7 @@ dependencies: - numpy=1.19.2 - pip: - albumentations==0.4.3 - - opencv-python==4.1.2.30 + - opencv-python==4.5.5.64 - pudb==2019.2 - imageio==2.9.0 - imageio-ffmpeg==0.4.2 @@ -28,4 +28,5 @@ dependencies: - -e git+https://github.com/openai/CLIP.git@main#egg=clip - -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers - -e git+https://github.com/lstein/k-diffusion.git@master#egg=k-diffusion + - -e git+https://github.com/lstein/GFPGAN@fix-dark-cast-images#egg=gfpgan - -e . diff --git a/ldm/gfpgan/gfpgan_tools.py b/ldm/gfpgan/gfpgan_tools.py index 0de706ae42..bbef1bca38 100644 --- a/ldm/gfpgan/gfpgan_tools.py +++ b/ldm/gfpgan/gfpgan_tools.py @@ -8,10 +8,7 @@ from PIL import Image from scripts.dream import create_argv_parser arg_parser = create_argv_parser() -opt = arg_parser.parse_args() - -model_path = os.path.join(opt.gfpgan_dir, opt.gfpgan_model_path) -gfpgan_model_exists = os.path.isfile(model_path) +opt = arg_parser.parse_args() def run_gfpgan(image, strength, seed, upsampler_scale=4): print(f'>> GFPGAN - Restoring Faces for image seed:{seed}') @@ -19,6 +16,9 @@ def run_gfpgan(image, strength, seed, upsampler_scale=4): with warnings.catch_warnings(): warnings.filterwarnings('ignore', category=DeprecationWarning) warnings.filterwarnings('ignore', category=UserWarning) + + model_path = os.path.join(opt.gfpgan_dir, opt.gfpgan_model_path) + gfpgan_model_exists = os.path.isfile(model_path) try: if not gfpgan_model_exists: @@ -46,7 +46,10 @@ def run_gfpgan(image, strength, seed, upsampler_scale=4): if gfpgan is None: print( - f'>> GFPGAN not initialized. Their packages must be installed as siblings to the "stable-diffusion" folder, or set explicitly using the --gfpgan_dir option.' + f'>> WARNING: GFPGAN not initialized.' + ) + print( + f'>> Download https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth to {model_path}, \nor change GFPGAN directory with --gfpgan_dir.' ) return image diff --git a/scripts/dream.py b/scripts/dream.py index f9fbabe696..8534b41ea4 100755 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -183,7 +183,7 @@ def main_loop(t2i, outdir, prompt_as_dir, parser, infile): if len(opt.prompt) == 0: print('Try again with a prompt!') continue - if opt.init_img is not None and re.match('^-\d+$',opt.init_img): # retrieve previous value! + if opt.init_img is not None and re.match('^-\\d+$',opt.init_img): # retrieve previous value! try: opt.init_img = last_results[int(opt.init_img)][0] print(f'>> Reusing previous image {opt.init_img}') @@ -478,7 +478,7 @@ def create_argv_parser(): parser.add_argument( '--gfpgan_dir', type=str, - default='../GFPGAN', + default='./src/gfpgan', help='Indicates the directory containing the GFPGAN code.', ) parser.add_argument(