70af4845af
new file: inpaint/__main__.py new file: inpaint/api.py new file: inpaint/batch_processing.py new file: inpaint/benchmark.py new file: inpaint/cli.py new file: inpaint/const.py new file: inpaint/download.py new file: inpaint/file_manager/__init__.py new file: inpaint/file_manager/file_manager.py new file: inpaint/file_manager/storage_backends.py new file: inpaint/file_manager/utils.py new file: inpaint/helper.py new file: inpaint/installer.py new file: inpaint/model/__init__.py new file: inpaint/model/anytext/__init__.py new file: inpaint/model/anytext/anytext_model.py new file: inpaint/model/anytext/anytext_pipeline.py new file: inpaint/model/anytext/anytext_sd15.yaml new file: inpaint/model/anytext/cldm/__init__.py new file: inpaint/model/anytext/cldm/cldm.py new file: inpaint/model/anytext/cldm/ddim_hacked.py new file: inpaint/model/anytext/cldm/embedding_manager.py new file: inpaint/model/anytext/cldm/hack.py new file: inpaint/model/anytext/cldm/model.py new file: inpaint/model/anytext/cldm/recognizer.py new file: inpaint/model/anytext/ldm/__init__.py new file: inpaint/model/anytext/ldm/models/__init__.py new file: inpaint/model/anytext/ldm/models/autoencoder.py new file: inpaint/model/anytext/ldm/models/diffusion/__init__.py new file: inpaint/model/anytext/ldm/models/diffusion/ddim.py new file: inpaint/model/anytext/ldm/models/diffusion/ddpm.py new file: inpaint/model/anytext/ldm/models/diffusion/dpm_solver/__init__.py new file: inpaint/model/anytext/ldm/models/diffusion/dpm_solver/dpm_solver.py new file: inpaint/model/anytext/ldm/models/diffusion/dpm_solver/sampler.py new file: inpaint/model/anytext/ldm/models/diffusion/plms.py new file: inpaint/model/anytext/ldm/models/diffusion/sampling_util.py new file: inpaint/model/anytext/ldm/modules/__init__.py new file: inpaint/model/anytext/ldm/modules/attention.py new file: inpaint/model/anytext/ldm/modules/diffusionmodules/__init__.py new file: inpaint/model/anytext/ldm/modules/diffusionmodules/model.py new file: inpaint/model/anytext/ldm/modules/diffusionmodules/openaimodel.py new file: inpaint/model/anytext/ldm/modules/diffusionmodules/upscaling.py new file: inpaint/model/anytext/ldm/modules/diffusionmodules/util.py new file: inpaint/model/anytext/ldm/modules/distributions/__init__.py new file: inpaint/model/anytext/ldm/modules/distributions/distributions.py new file: inpaint/model/anytext/ldm/modules/ema.py new file: inpaint/model/anytext/ldm/modules/encoders/__init__.py new file: inpaint/model/anytext/ldm/modules/encoders/modules.py new file: inpaint/model/anytext/ldm/util.py new file: inpaint/model/anytext/main.py new file: inpaint/model/anytext/ocr_recog/RNN.py new file: inpaint/model/anytext/ocr_recog/RecCTCHead.py new file: inpaint/model/anytext/ocr_recog/RecModel.py new file: inpaint/model/anytext/ocr_recog/RecMv1_enhance.py new file: inpaint/model/anytext/ocr_recog/RecSVTR.py new file: inpaint/model/anytext/ocr_recog/__init__.py new file: inpaint/model/anytext/ocr_recog/common.py new file: inpaint/model/anytext/ocr_recog/en_dict.txt new file: inpaint/model/anytext/ocr_recog/ppocr_keys_v1.txt new file: inpaint/model/anytext/utils.py new file: inpaint/model/base.py new file: inpaint/model/brushnet/__init__.py new file: inpaint/model/brushnet/brushnet.py new file: inpaint/model/brushnet/brushnet_unet_forward.py new file: inpaint/model/brushnet/brushnet_wrapper.py new file: inpaint/model/brushnet/pipeline_brushnet.py new file: inpaint/model/brushnet/unet_2d_blocks.py new file: inpaint/model/controlnet.py new file: inpaint/model/ddim_sampler.py new file: inpaint/model/fcf.py new file: inpaint/model/helper/__init__.py new file: inpaint/model/helper/controlnet_preprocess.py new file: inpaint/model/helper/cpu_text_encoder.py new file: inpaint/model/helper/g_diffuser_bot.py new file: inpaint/model/instruct_pix2pix.py new file: inpaint/model/kandinsky.py new file: inpaint/model/lama.py new file: inpaint/model/ldm.py new file: inpaint/model/manga.py new file: inpaint/model/mat.py new file: inpaint/model/mi_gan.py new file: inpaint/model/opencv2.py new file: inpaint/model/original_sd_configs/__init__.py new file: inpaint/model/original_sd_configs/sd_xl_base.yaml new file: inpaint/model/original_sd_configs/sd_xl_refiner.yaml new file: inpaint/model/original_sd_configs/v1-inference.yaml new file: inpaint/model/original_sd_configs/v2-inference-v.yaml new file: inpaint/model/paint_by_example.py new file: inpaint/model/plms_sampler.py new file: inpaint/model/power_paint/__init__.py new file: inpaint/model/power_paint/pipeline_powerpaint.py new file: inpaint/model/power_paint/power_paint.py new file: inpaint/model/power_paint/power_paint_v2.py new file: inpaint/model/power_paint/powerpaint_tokenizer.py
157 lines
5.4 KiB
Python
157 lines
5.4 KiB
Python
import os
|
|
|
|
import cv2
|
|
import torch
|
|
from torchvision.transforms.functional import normalize
|
|
from torch.hub import get_dir
|
|
|
|
from .facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
|
from .gfpgan.archs.gfpganv1_clean_arch import GFPGANv1Clean
|
|
from .basicsr.img_util import img2tensor, tensor2img
|
|
|
|
|
|
class MyGFPGANer:
|
|
"""Helper for restoration with GFPGAN.
|
|
|
|
It will detect and crop faces, and then resize the faces to 512x512.
|
|
GFPGAN is used to restored the resized faces.
|
|
The background is upsampled with the bg_upsampler.
|
|
Finally, the faces will be pasted back to the upsample background image.
|
|
|
|
Args:
|
|
model_path (str): The path to the GFPGAN model. It can be urls (will first download it automatically).
|
|
upscale (float): The upscale of the final output. Default: 2.
|
|
arch (str): The GFPGAN architecture. Option: clean | original. Default: clean.
|
|
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
|
|
bg_upsampler (nn.Module): The upsampler for the background. Default: None.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
model_path,
|
|
upscale=2,
|
|
arch="clean",
|
|
channel_multiplier=2,
|
|
bg_upsampler=None,
|
|
device=None,
|
|
):
|
|
self.upscale = upscale
|
|
self.bg_upsampler = bg_upsampler
|
|
|
|
# initialize model
|
|
self.device = (
|
|
torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
if device is None
|
|
else device
|
|
)
|
|
# initialize the GFP-GAN
|
|
if arch == "clean":
|
|
self.gfpgan = GFPGANv1Clean(
|
|
out_size=512,
|
|
num_style_feat=512,
|
|
channel_multiplier=channel_multiplier,
|
|
decoder_load_path=None,
|
|
fix_decoder=False,
|
|
num_mlp=8,
|
|
input_is_latent=True,
|
|
different_w=True,
|
|
narrow=1,
|
|
sft_half=True,
|
|
)
|
|
elif arch == "RestoreFormer":
|
|
from .gfpgan.archs.restoreformer_arch import RestoreFormer
|
|
|
|
self.gfpgan = RestoreFormer()
|
|
|
|
hub_dir = get_dir()
|
|
model_dir = os.path.join(hub_dir, "checkpoints")
|
|
|
|
# initialize face helper
|
|
self.face_helper = FaceRestoreHelper(
|
|
upscale,
|
|
face_size=512,
|
|
crop_ratio=(1, 1),
|
|
det_model="retinaface_resnet50",
|
|
save_ext="png",
|
|
use_parse=True,
|
|
device=self.device,
|
|
model_rootpath=model_dir,
|
|
)
|
|
|
|
loadnet = torch.load(model_path)
|
|
if "params_ema" in loadnet:
|
|
keyname = "params_ema"
|
|
else:
|
|
keyname = "params"
|
|
self.gfpgan.load_state_dict(loadnet[keyname], strict=True)
|
|
self.gfpgan.eval()
|
|
self.gfpgan = self.gfpgan.to(self.device)
|
|
|
|
@torch.no_grad()
|
|
def enhance(
|
|
self,
|
|
img,
|
|
has_aligned=False,
|
|
only_center_face=False,
|
|
paste_back=True,
|
|
weight=0.5,
|
|
):
|
|
self.face_helper.clean_all()
|
|
|
|
if has_aligned: # the inputs are already aligned
|
|
img = cv2.resize(img, (512, 512))
|
|
self.face_helper.cropped_faces = [img]
|
|
else:
|
|
self.face_helper.read_image(img)
|
|
# get face landmarks for each face
|
|
self.face_helper.get_face_landmarks_5(
|
|
only_center_face=only_center_face, eye_dist_threshold=5
|
|
)
|
|
# eye_dist_threshold=5: skip faces whose eye distance is smaller than 5 pixels
|
|
# TODO: even with eye_dist_threshold, it will still introduce wrong detections and restorations.
|
|
# align and warp each face
|
|
self.face_helper.align_warp_face()
|
|
|
|
# face restoration
|
|
for cropped_face in self.face_helper.cropped_faces:
|
|
# prepare data
|
|
cropped_face_t = img2tensor(
|
|
cropped_face / 255.0, bgr2rgb=True, float32=True
|
|
)
|
|
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
|
|
cropped_face_t = cropped_face_t.unsqueeze(0).to(self.device)
|
|
|
|
try:
|
|
output = self.gfpgan(cropped_face_t, return_rgb=False, weight=weight)[0]
|
|
# convert to image
|
|
restored_face = tensor2img(
|
|
output.squeeze(0), rgb2bgr=True, min_max=(-1, 1)
|
|
)
|
|
except RuntimeError as error:
|
|
print(f"\tFailed inference for GFPGAN: {error}.")
|
|
restored_face = cropped_face
|
|
|
|
restored_face = restored_face.astype("uint8")
|
|
self.face_helper.add_restored_face(restored_face)
|
|
|
|
if not has_aligned and paste_back:
|
|
# upsample the background
|
|
if self.bg_upsampler is not None:
|
|
# Now only support RealESRGAN for upsampling background
|
|
bg_img = self.bg_upsampler.enhance(img, outscale=self.upscale)[0]
|
|
else:
|
|
bg_img = None
|
|
|
|
self.face_helper.get_inverse_affine(None)
|
|
# paste each restored face to the input image
|
|
restored_img = self.face_helper.paste_faces_to_input_image(
|
|
upsample_img=bg_img
|
|
)
|
|
return (
|
|
self.face_helper.cropped_faces,
|
|
self.face_helper.restored_faces,
|
|
restored_img,
|
|
)
|
|
else:
|
|
return self.face_helper.cropped_faces, self.face_helper.restored_faces, None
|