feature: run face enhancement on the GPU

Should run 10x faster
This commit is contained in:
Bryce 2023-01-16 12:29:33 -08:00 committed by Bryce Drennan
parent 03eb7c21b0
commit fad7f17790
3 changed files with 21 additions and 7 deletions

View File

@ -401,10 +401,14 @@ def imagine(
if not safety_score.is_filtered: if not safety_score.is_filtered:
if prompt.fix_faces: if prompt.fix_faces:
logger.info("Fixing 😊 's in 🖼 using CodeFormer...") logger.info("Fixing 😊 's in 🖼 using CodeFormer...")
img = enhance_faces(img, fidelity=prompt.fix_faces_fidelity) with lc.timing("face enhancement"):
img = enhance_faces(
img, fidelity=prompt.fix_faces_fidelity
)
if prompt.upscale: if prompt.upscale:
logger.info("Upscaling 🖼 using real-ESRGAN...") logger.info("Upscaling 🖼 using real-ESRGAN...")
upscaled_img = upscale_image(img) with lc.timing("upscaling"):
upscaled_img = upscale_image(img)
# put the newly generated patch back into the original, full size image # put the newly generated patch back into the original, full size image
if ( if (

View File

@ -13,6 +13,9 @@ from imaginairy.vendored.codeformer.codeformer_arch import CodeFormer
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
face_restore_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
half_mode = face_restore_device == "cuda"
@lru_cache @lru_cache
def codeformer_model(): def codeformer_model():
@ -22,12 +25,14 @@ def codeformer_model():
n_head=8, n_head=8,
n_layers=9, n_layers=9,
connect_list=["32", "64", "128", "256"], connect_list=["32", "64", "128", "256"],
).to("cpu") ).to(face_restore_device)
url = "https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth" url = "https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth"
ckpt_path = get_cached_url_path(url) ckpt_path = get_cached_url_path(url)
checkpoint = torch.load(ckpt_path)["params_ema"] checkpoint = torch.load(ckpt_path)["params_ema"]
model.load_state_dict(checkpoint) model.load_state_dict(checkpoint)
model.eval() model.eval()
if half_mode:
model = model.half()
return model return model
@ -39,7 +44,6 @@ def face_restore_helper():
FaceRestoreHelper loads a model internally so we need to cache it FaceRestoreHelper loads a model internally so we need to cache it
or we end up with a memory leak or we end up with a memory leak
""" """
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
face_helper = FaceRestoreHelper( face_helper = FaceRestoreHelper(
upscale_factor=1, upscale_factor=1,
face_size=512, face_size=512,
@ -47,7 +51,7 @@ def face_restore_helper():
det_model="retinaface_resnet50", det_model="retinaface_resnet50",
save_ext="png", save_ext="png",
use_parse=True, use_parse=True,
device=device, device=face_restore_device,
) )
return face_helper return face_helper
@ -77,16 +81,17 @@ def enhance_faces(img, fidelity=0):
# prepare data # prepare data
cropped_face_t = img2tensor(cropped_face / 255.0, bgr2rgb=True, float32=True) 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) 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("cpu") cropped_face_t = cropped_face_t.unsqueeze(0).to(face_restore_device)
try: try:
with torch.no_grad(): with torch.no_grad():
output = net(cropped_face_t, w=fidelity, adain=True)[0] # noqa output = net(cropped_face_t, w=fidelity, adain=True)[0] # noqa
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1)) restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
del output del output
torch.cuda.empty_cache() torch.cuda.empty_cache()
except Exception as error: # noqa except Exception as error: # noqa
logger.error(f"\tFailed inference for CodeFormer: {error}") logger.exception(f"\tFailed inference for CodeFormer: {error}")
restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1)) restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))
restored_face = restored_face.astype("uint8") restored_face = restored_face.astype("uint8")

View File

@ -382,6 +382,11 @@ def train_diffusion_model(
accumulate_grad_batches=32, accumulate_grad_batches=32,
resume=None, resume=None,
): ):
"""
Train a diffusion model on a single concept.
accumulate_grad_batches used to simulate a bigger batch size - https://arxiv.org/pdf/1711.00489.pdf
"""
batch_size = 1 batch_size = 1
seed = 23 seed = 23
num_workers = 1 num_workers = 1