2022-09-07 03:32:28 -06:00
|
|
|
import os
|
2022-09-10 04:53:10 -06:00
|
|
|
|
|
|
|
import cv2
|
2022-09-07 03:32:28 -06:00
|
|
|
import torch
|
|
|
|
|
2022-09-26 09:27:18 -06:00
|
|
|
import modules.face_restoration
|
|
|
|
import modules.shared
|
2023-05-31 10:56:37 -06:00
|
|
|
from modules import shared, devices, modelloader, errors
|
2023-01-25 10:00:09 -07:00
|
|
|
from modules.paths import models_path
|
2022-09-07 03:32:28 -06:00
|
|
|
|
2022-09-26 09:27:18 -06:00
|
|
|
# codeformer people made a choice to include modified basicsr library to their project which makes
|
|
|
|
# it utterly impossible to use it alongside with other libraries that also use basicsr, like GFPGAN.
|
2022-09-07 03:32:28 -06:00
|
|
|
# I am making a choice to include some files from codeformer to work around this issue.
|
2022-09-26 08:29:50 -06:00
|
|
|
model_dir = "Codeformer"
|
|
|
|
model_path = os.path.join(models_path, model_dir)
|
|
|
|
model_url = 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth'
|
2022-09-07 03:32:28 -06:00
|
|
|
|
2022-09-07 04:35:02 -06:00
|
|
|
codeformer = None
|
2022-09-07 03:32:28 -06:00
|
|
|
|
2022-09-26 08:29:50 -06:00
|
|
|
|
|
|
|
def setup_model(dirname):
|
2023-05-29 01:18:15 -06:00
|
|
|
os.makedirs(model_path, exist_ok=True)
|
2022-09-26 08:29:50 -06:00
|
|
|
|
2022-09-07 03:32:28 -06:00
|
|
|
path = modules.paths.paths.get("CodeFormer", None)
|
|
|
|
if path is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
from torchvision.transforms.functional import normalize
|
|
|
|
from modules.codeformer.codeformer_arch import CodeFormer
|
2023-05-09 23:43:42 -06:00
|
|
|
from basicsr.utils import img2tensor, tensor2img
|
2022-09-07 03:32:28 -06:00
|
|
|
from facelib.utils.face_restoration_helper import FaceRestoreHelper
|
2022-11-07 18:12:31 -07:00
|
|
|
from facelib.detection.retinaface import retinaface
|
2022-09-07 03:32:28 -06:00
|
|
|
|
|
|
|
net_class = CodeFormer
|
|
|
|
|
|
|
|
class FaceRestorerCodeFormer(modules.face_restoration.FaceRestoration):
|
|
|
|
def name(self):
|
|
|
|
return "CodeFormer"
|
|
|
|
|
2022-09-26 08:29:50 -06:00
|
|
|
def __init__(self, dirname):
|
2022-09-07 03:32:28 -06:00
|
|
|
self.net = None
|
|
|
|
self.face_helper = None
|
2022-09-26 08:29:50 -06:00
|
|
|
self.cmd_dir = dirname
|
2022-09-07 03:32:28 -06:00
|
|
|
|
|
|
|
def create_models(self):
|
|
|
|
|
|
|
|
if self.net is not None and self.face_helper is not None:
|
2022-09-14 06:06:01 -06:00
|
|
|
self.net.to(devices.device_codeformer)
|
2022-09-07 03:32:28 -06:00
|
|
|
return self.net, self.face_helper
|
2023-02-23 09:42:29 -07:00
|
|
|
model_paths = modelloader.load_models(model_path, model_url, self.cmd_dir, download_name='codeformer-v0.1.0.pth', ext_filter=['.pth'])
|
2022-09-26 08:29:50 -06:00
|
|
|
if len(model_paths) != 0:
|
|
|
|
ckpt_path = model_paths[0]
|
|
|
|
else:
|
|
|
|
print("Unable to load codeformer model.")
|
|
|
|
return None, None
|
2022-09-12 14:24:54 -06:00
|
|
|
net = net_class(dim_embd=512, codebook_size=1024, n_head=8, n_layers=9, connect_list=['32', '64', '128', '256']).to(devices.device_codeformer)
|
2022-09-07 03:32:28 -06:00
|
|
|
checkpoint = torch.load(ckpt_path)['params_ema']
|
|
|
|
net.load_state_dict(checkpoint)
|
|
|
|
net.eval()
|
|
|
|
|
2022-11-07 18:12:31 -07:00
|
|
|
if hasattr(retinaface, 'device'):
|
|
|
|
retinaface.device = devices.device_codeformer
|
2022-09-12 14:24:54 -06:00
|
|
|
face_helper = FaceRestoreHelper(1, face_size=512, crop_ratio=(1, 1), det_model='retinaface_resnet50', save_ext='png', use_parse=True, device=devices.device_codeformer)
|
2022-09-07 03:32:28 -06:00
|
|
|
|
2022-09-12 09:40:06 -06:00
|
|
|
self.net = net
|
|
|
|
self.face_helper = face_helper
|
2022-09-07 03:32:28 -06:00
|
|
|
|
|
|
|
return net, face_helper
|
|
|
|
|
2022-10-04 03:32:22 -06:00
|
|
|
def send_model_to(self, device):
|
|
|
|
self.net.to(device)
|
|
|
|
self.face_helper.face_det.to(device)
|
|
|
|
self.face_helper.face_parse.to(device)
|
|
|
|
|
2022-09-07 04:35:02 -06:00
|
|
|
def restore(self, np_image, w=None):
|
2022-09-07 03:32:28 -06:00
|
|
|
np_image = np_image[:, :, ::-1]
|
|
|
|
|
2022-09-10 04:53:10 -06:00
|
|
|
original_resolution = np_image.shape[0:2]
|
|
|
|
|
2022-09-12 09:40:06 -06:00
|
|
|
self.create_models()
|
2022-09-26 08:29:50 -06:00
|
|
|
if self.net is None or self.face_helper is None:
|
|
|
|
return np_image
|
|
|
|
|
2022-10-04 03:32:22 -06:00
|
|
|
self.send_model_to(devices.device_codeformer)
|
|
|
|
|
2022-09-12 09:40:06 -06:00
|
|
|
self.face_helper.clean_all()
|
|
|
|
self.face_helper.read_image(np_image)
|
|
|
|
self.face_helper.get_face_landmarks_5(only_center_face=False, resize=640, eye_dist_threshold=5)
|
|
|
|
self.face_helper.align_warp_face()
|
2022-09-07 03:32:28 -06:00
|
|
|
|
2023-05-10 02:37:18 -06:00
|
|
|
for cropped_face in self.face_helper.cropped_faces:
|
2022-09-07 03:32:28 -06:00
|
|
|
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
|
|
|
|
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
|
2022-09-12 14:24:54 -06:00
|
|
|
cropped_face_t = cropped_face_t.unsqueeze(0).to(devices.device_codeformer)
|
2022-09-07 03:32:28 -06:00
|
|
|
|
|
|
|
try:
|
|
|
|
with torch.no_grad():
|
2022-09-12 09:40:06 -06:00
|
|
|
output = self.net(cropped_face_t, w=w if w is not None else shared.opts.code_former_weight, adain=True)[0]
|
2022-09-07 03:32:28 -06:00
|
|
|
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
|
|
|
|
del output
|
2023-07-08 08:13:18 -06:00
|
|
|
devices.torch_gc()
|
2023-05-28 23:54:13 -06:00
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report('Failed inference for CodeFormer', exc_info=True)
|
2022-09-07 03:32:28 -06:00
|
|
|
restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))
|
|
|
|
|
|
|
|
restored_face = restored_face.astype('uint8')
|
2022-09-12 09:40:06 -06:00
|
|
|
self.face_helper.add_restored_face(restored_face)
|
2022-09-07 03:32:28 -06:00
|
|
|
|
2022-09-12 09:40:06 -06:00
|
|
|
self.face_helper.get_inverse_affine(None)
|
2022-09-07 03:32:28 -06:00
|
|
|
|
2022-09-12 09:40:06 -06:00
|
|
|
restored_img = self.face_helper.paste_faces_to_input_image()
|
2022-09-07 03:32:28 -06:00
|
|
|
restored_img = restored_img[:, :, ::-1]
|
2022-09-10 04:53:10 -06:00
|
|
|
|
|
|
|
if original_resolution != restored_img.shape[0:2]:
|
|
|
|
restored_img = cv2.resize(restored_img, (0, 0), fx=original_resolution[1]/restored_img.shape[1], fy=original_resolution[0]/restored_img.shape[0], interpolation=cv2.INTER_LINEAR)
|
|
|
|
|
2022-10-04 03:32:22 -06:00
|
|
|
self.face_helper.clean_all()
|
|
|
|
|
2022-09-12 09:40:06 -06:00
|
|
|
if shared.opts.face_restoration_unload:
|
2022-10-04 03:32:22 -06:00
|
|
|
self.send_model_to(devices.cpu)
|
2022-09-12 09:40:06 -06:00
|
|
|
|
2022-09-07 03:32:28 -06:00
|
|
|
return restored_img
|
|
|
|
|
2022-09-07 04:35:02 -06:00
|
|
|
global codeformer
|
2022-09-26 08:29:50 -06:00
|
|
|
codeformer = FaceRestorerCodeFormer(dirname)
|
2022-09-07 04:35:02 -06:00
|
|
|
shared.face_restorers.append(codeformer)
|
2022-09-07 03:32:28 -06:00
|
|
|
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report("Error setting up CodeFormer", exc_info=True)
|
2022-09-07 03:32:28 -06:00
|
|
|
|
|
|
|
# sys.path = stored_sys_path
|