Remove gfpgan_dir

+ Update GFPGAN Model Path Defaults
>  Update them to match the new file heirarchy
This commit is contained in:
blessedcoolant 2022-11-11 11:55:03 +13:00 committed by Lincoln Stein
parent 45e5053d06
commit fd67df9447
8 changed files with 45 additions and 71 deletions

View File

@ -99,8 +99,7 @@ overridden on a per-prompt basis (see
| `--sampler <sampler>` | `-A<sampler>` | `k_lms` | Sampler to use. Use `-h` to get list of available samplers. | | `--sampler <sampler>` | `-A<sampler>` | `k_lms` | Sampler to use. Use `-h` to get list of available samplers. |
| `--seamless` | | `False` | Create interesting effects by tiling elements of the image. | | `--seamless` | | `False` | Create interesting effects by tiling elements of the image. |
| `--embedding_path <path>` | | `None` | Path to pre-trained embedding manager checkpoints, for custom models | | `--embedding_path <path>` | | `None` | Path to pre-trained embedding manager checkpoints, for custom models |
| `--gfpgan_dir` | | `src/gfpgan` | Path to where GFPGAN is installed. | | `--gfpgan_model_path` | | `experiments/pretrained_models/GFPGANv1.4.pth` | Path to GFPGAN model file. |
| `--gfpgan_model_path` | | `experiments/pretrained_models/GFPGANv1.4.pth` | Path to GFPGAN model file, relative to `--gfpgan_dir`. |
| `--free_gpu_mem` | | `False` | Free GPU memory after sampling, to allow image decoding and saving in low VRAM conditions | | `--free_gpu_mem` | | `False` | Free GPU memory after sampling, to allow image decoding and saving in low VRAM conditions |
| `--precision` | | `auto` | Set model precision, default is selected by device. Options: auto, float32, float16, autocast | | `--precision` | | `auto` | Set model precision, default is selected by device. Options: auto, float32, float16, autocast |

View File

@ -6,49 +6,39 @@ title: Postprocessing
## Intro ## Intro
This extension provides the ability to restore faces and upscale This extension provides the ability to restore faces and upscale images.
images.
Face restoration and upscaling can be applied at the time you generate Face restoration and upscaling can be applied at the time you generate the
the images, or at any later time against a previously-generated PNG images, or at any later time against a previously-generated PNG file, using the
file, using the [!fix](#fixing-previously-generated-images) [!fix](#fixing-previously-generated-images) command.
command. [Outpainting and outcropping](OUTPAINTING.md) can only be [Outpainting and outcropping](OUTPAINTING.md) can only be applied after the
applied after the fact. fact.
## Face Fixing ## Face Fixing
The default face restoration module is GFPGAN. The default upscale is The default face restoration module is GFPGAN. The default upscale is
Real-ESRGAN. For an alternative face restoration module, see [CodeFormer Real-ESRGAN. For an alternative face restoration module, see
Support](#codeformer-support) below. [CodeFormer Support](#codeformer-support) below.
As of version 1.14, environment.yaml will install the Real-ESRGAN As of version 1.14, environment.yaml will install the Real-ESRGAN package into
package into the standard install location for python packages, and the standard install location for python packages, and will put GFPGAN into a
will put GFPGAN into a subdirectory of "src" in the InvokeAI subdirectory of "src" in the InvokeAI directory. Upscaling with Real-ESRGAN
directory. Upscaling with Real-ESRGAN should "just work" without should "just work" without further intervention. Simply pass the `--upscale`
further intervention. Simply pass the `--upscale` (`-U`) option on the (`-U`) option on the `invoke>` command line, or indicate the desired scale on
`invoke>` command line, or indicate the desired scale on the popup in the popup in the Web GUI.
the Web GUI.
**GFPGAN** requires a series of downloadable model files to **GFPGAN** requires a series of downloadable model files to work. These are
work. These are loaded when you run `scripts/preload_models.py`. If loaded when you run `scripts/preload_models.py`. If GFPAN is failing with an
GFPAN is failing with an error, please run the following from the error, please run the following from the InvokeAI directory:
InvokeAI directory:
```bash ```bash
python scripts/preload_models.py python scripts/preload_models.py
``` ```
If you do not run this script in advance, the GFPGAN module will attempt If you do not run this script in advance, the GFPGAN module will attempt to
to download the models files the first time you try to perform facial download the models files the first time you try to perform facial
reconstruction. reconstruction.
Alternatively, if you have GFPGAN installed elsewhere, or if you are
using an earlier version of this package which asked you to install
GFPGAN in a sibling directory, you may use the `--gfpgan_dir` argument
with `invoke.py` to set a custom path to your GFPGAN directory. _There
are other GFPGAN related boot arguments if you wish to customize
further._
## Usage ## Usage
You will now have access to two new prompt arguments. You will now have access to two new prompt arguments.
@ -119,15 +109,15 @@ actions.
This repo also allows you to perform face restoration using This repo also allows you to perform face restoration using
[CodeFormer](https://github.com/sczhou/CodeFormer). [CodeFormer](https://github.com/sczhou/CodeFormer).
In order to setup CodeFormer to work, you need to download the models In order to setup CodeFormer to work, you need to download the models like with
like with GFPGAN. You can do this either by running GFPGAN. You can do this either by running `preload_models.py` or by manually
`preload_models.py` or by manually downloading the [model downloading the
file](https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth) [model file](https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth)
and saving it to `ldm/invoke/restoration/codeformer/weights` folder. and saving it to `ldm/invoke/restoration/codeformer/weights` folder.
You can use `-ft` prompt argument to swap between CodeFormer and the You can use `-ft` prompt argument to swap between CodeFormer and the default
default GFPGAN. The above mentioned `-G` prompt argument will allow GFPGAN. The above mentioned `-G` prompt argument will allow you to control the
you to control the strength of the restoration effect. strength of the restoration effect.
### Usage ### Usage
@ -157,9 +147,9 @@ situations when there is very little facial data to work with.
## Fixing Previously-Generated Images ## Fixing Previously-Generated Images
It is easy to apply face restoration and/or upscaling to any It is easy to apply face restoration and/or upscaling to any
previously-generated file. Just use the syntax `!fix path/to/file.png previously-generated file. Just use the syntax
<options>`. For example, to apply GFPGAN at strength 0.8 and upscale `!fix path/to/file.png <options>`. For example, to apply GFPGAN at strength 0.8
2X for a file named `./outputs/img-samples/000044.2945021133.png`, and upscale 2X for a file named `./outputs/img-samples/000044.2945021133.png`,
just run: just run:
```bash ```bash

View File

@ -552,14 +552,8 @@ class Args(object):
postprocessing_group.add_argument( postprocessing_group.add_argument(
'--gfpgan_model_path', '--gfpgan_model_path',
type=str, type=str,
default='./GFPGANv1.4.pth', default='./models/gfpgan/GFPGANv1.4.pth',
help='Indicates the path to the GFPGAN model, relative to --gfpgan_dir.', help='Indicates the path to the GFPGAN model',
)
postprocessing_group.add_argument(
'--gfpgan_dir',
type=str,
default='./models/gfpgan',
help='Indicates the directory containing the GFPGAN code.',
) )
web_server_group.add_argument( web_server_group.add_argument(
'--web', '--web',

View File

@ -2,9 +2,9 @@ class Restoration():
def __init__(self) -> None: def __init__(self) -> None:
pass pass
def load_face_restore_models(self, gfpgan_dir='./src/gfpgan', gfpgan_model_path='experiments/pretrained_models/GFPGANv1.4.pth'): def load_face_restore_models(self, gfpgan_model_path='./models/gfpgan/GFPGANv1.4.pth'):
# Load GFPGAN # Load GFPGAN
gfpgan = self.load_gfpgan(gfpgan_dir, gfpgan_model_path) gfpgan = self.load_gfpgan(gfpgan_model_path)
if gfpgan.gfpgan_model_exists: if gfpgan.gfpgan_model_exists:
print('>> GFPGAN Initialized') print('>> GFPGAN Initialized')
else: else:
@ -22,9 +22,9 @@ class Restoration():
return gfpgan, codeformer return gfpgan, codeformer
# Face Restore Models # Face Restore Models
def load_gfpgan(self, gfpgan_dir, gfpgan_model_path): def load_gfpgan(self, gfpgan_model_path):
from ldm.invoke.restoration.gfpgan import GFPGAN from ldm.invoke.restoration.gfpgan import GFPGAN
return GFPGAN(gfpgan_dir, gfpgan_model_path) return GFPGAN(gfpgan_model_path)
def load_codeformer(self): def load_codeformer(self):
from ldm.invoke.restoration.codeformer import CodeFormerRestoration from ldm.invoke.restoration.codeformer import CodeFormerRestoration

View File

@ -10,17 +10,14 @@ from PIL import Image
class GFPGAN(): class GFPGAN():
def __init__( def __init__(
self, self,
gfpgan_dir='models/gfpgan', gfpgan_model_path='./models/gfpgan/GFPGANv1.4.pth') -> None:
gfpgan_model_path='GFPGANv1.4.pth'
) -> None:
self.model_path = os.path.join(gfpgan_dir, gfpgan_model_path) self.model_path = os.path.join(gfpgan_model_path)
self.gfpgan_model_exists = os.path.isfile(self.model_path) self.gfpgan_model_exists = os.path.isfile(self.model_path)
if not self.gfpgan_model_exists: if not self.gfpgan_model_exists:
print('## NOT FOUND: GFPGAN model not found at ' + self.model_path) print('## NOT FOUND: GFPGAN model not found at ' + self.model_path)
return None return None
sys.path.append(os.path.abspath(gfpgan_dir))
def model_exists(self): def model_exists(self):
return os.path.isfile(self.model_path) return os.path.isfile(self.model_path)
@ -51,7 +48,7 @@ class GFPGAN():
f'>> WARNING: GFPGAN not initialized.' f'>> WARNING: GFPGAN not initialized.'
) )
print( print(
f'>> Download https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth to {self.model_path}, \nor change GFPGAN directory with --gfpgan_dir.' f'>> Download https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth to {self.model_path}'
) )
image = image.convert('RGB') image = image.convert('RGB')

View File

@ -809,7 +809,7 @@ def load_face_restoration(opt):
from ldm.invoke.restoration import Restoration from ldm.invoke.restoration import Restoration
restoration = Restoration() restoration = Restoration()
if opt.restore: if opt.restore:
gfpgan, codeformer = restoration.load_face_restore_models(opt.gfpgan_dir, opt.gfpgan_model_path) gfpgan, codeformer = restoration.load_face_restore_models(opt.gfpgan_model_path)
else: else:
print('>> Face restoration disabled') print('>> Face restoration disabled')
if opt.esrgan: if opt.esrgan:

View File

@ -487,14 +487,8 @@ def create_argv_parser():
parser.add_argument( parser.add_argument(
'--gfpgan_model_path', '--gfpgan_model_path',
type=str, type=str,
default='experiments/pretrained_models/GFPGANv1.3.pth', default='./models/gfpgan/GFPGANv1.4.pth',
help='Indicates the path to the GFPGAN model, relative to --gfpgan_dir.', help='Indicates the path to the GFPGAN model.',
)
parser.add_argument(
'--gfpgan_dir',
type=str,
default='./src/gfpgan',
help='Indicates the directory containing the GFPGAN code.',
) )
parser.add_argument( parser.add_argument(
'--web', '--web',

View File

@ -448,15 +448,15 @@ def download_gfpgan():
for model in ( for model in (
[ [
'https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth', 'https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth',
'models/gfpgan/GFPGANv1.4.pth' './models/gfpgan/GFPGANv1.4.pth'
], ],
[ [
'https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_Resnet50_Final.pth', 'https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_Resnet50_Final.pth',
'models/gfpgan/weights/detection_Resnet50_Final.pth' './models/gfpgan/weights/detection_Resnet50_Final.pth'
], ],
[ [
'https://github.com/xinntao/facexlib/releases/download/v0.2.2/parsing_parsenet.pth', 'https://github.com/xinntao/facexlib/releases/download/v0.2.2/parsing_parsenet.pth',
'models/gfpgan/weights/parsing_parsenet.pth' './models/gfpgan/weights/parsing_parsenet.pth'
], ],
): ):
model_url,model_dest = model model_url,model_dest = model