DiLightNet / demo /relighting_gen.py
NCJ's picture
gpu / cpu
c5ac1ed verified
raw
history blame
2.49 kB
import imageio
import numpy as np
import spaces
import torch
from diffusers import UniPCMultistepScheduler, StableDiffusionControlNetPipeline
from diffusers.utils import get_class_from_dynamic_module
from tqdm import tqdm
device = torch.device('cpu')
dtype = torch.float32
if torch.cuda.is_available():
device = torch.device('cuda')
dtype = torch.float16
NeuralTextureControlNetModel = get_class_from_dynamic_module(
"dilightnet/model_helpers",
"neuraltexture_controlnet.py",
"NeuralTextureControlNetModel"
)
controlnet = NeuralTextureControlNetModel.from_pretrained(
"dilightnet/DiLightNet",
torch_dtype=dtype,
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", controlnet=controlnet, torch_dtype=dtype
).to(device)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.set_progress_bar_config(disable=True)
@spaces.GPU
def relighting_gen(masked_ref_img, mask, cond_path, frames, prompt, steps, seed, cfg):
mask = mask[..., :1] / 255.
for i in tqdm(range(frames)):
source_image = masked_ref_img[..., :3] / 255.
cond_diffuse = imageio.v3.imread(f'{cond_path}/hint{i:02d}_diffuse.png') / 255.
if cond_diffuse.shape[-1] == 4:
cond_diffuse = cond_diffuse[..., :3] * cond_diffuse[..., 3:]
cond_ggx034 = imageio.v3.imread(f'{cond_path}/hint{i:02d}_ggx0.34.png') / 255.
if cond_ggx034.shape[-1] == 4:
cond_ggx034 = cond_ggx034[..., :3] * cond_ggx034[..., 3:]
cond_ggx013 = imageio.v3.imread(f'{cond_path}/hint{i:02d}_ggx0.13.png') / 255.
if cond_ggx013.shape[-1] == 4:
cond_ggx013 = cond_ggx013[..., :3] * cond_ggx013[..., 3:]
cond_ggx005 = imageio.v3.imread(f'{cond_path}/hint{i:02d}_ggx0.05.png') / 255.
if cond_ggx005.shape[-1] == 4:
cond_ggx005 = cond_ggx005[..., :3] * cond_ggx005[..., 3:]
hint = np.concatenate([mask, source_image, cond_diffuse, cond_ggx005, cond_ggx013, cond_ggx034], axis=2).astype(np.float32)[None]
hint = torch.from_numpy(hint).to(dtype).permute(0, 3, 1, 2).to(device)
generator = torch.manual_seed(seed)
image = pipe(
prompt, num_inference_steps=steps, generator=generator, image=hint, num_images_per_prompt=1, guidance_scale=cfg, output_type='np',
).images[0] # [H, W, C]
imageio.imwrite(f'{cond_path}/relighting{i:02d}.png', (image * 255).clip(0, 255).astype(np.uint8))