在这里介绍一个AI制作艺术分身的开源库。
https://github.com/TencentARC/PhotoMaker/tree/main
PhotoMaker是通过Stacked ID Embedding定制逼真的人体照片。
这里有个部署好的demo网站,我们先来试看下效果
https://huggingface.co/spaces/TencentARC/PhotoMaker
当然这里可以用自己的靓照。各位看官自便哈。
这里期望是“photo of a man img”,同时手持苹果“take a apple”,坐在车里"sit inside car",穿着钢铁侠衣服"wear iron man suit"。
提交Submit,得到生成的图片。
这里人脸都被遮住了,我们删除提示词"wear iron man suit”。于是就有了如下图片
然后我们把提示词“photo of a man img”,改成女生像"photo of a woman img"
我们再用一些描述美女的词来生成图像“instagram photo, portrait photo of a woman img, colorful, perfect face, natural skin, hard shadows, film grain”
注意这里生成配置有个Advanced Option,可以进一步定制生成结果。
我们再来进行另一组图片生成一个以周星星为原型的厨娘,她穿着围裙在杀鱼。“protrait photo of a woman img, cooking, wear an apron, kill fish”。
官方提供了更丰富的demo,我们也可以拿来demo。
conda create --name photomaker python=3.10
conda activate photomaker
pip install -U pip
# Install requirements
pip install -r requirements.txt
# Install photomaker
pip install git+https://github.com/TencentARC/PhotoMaker.git
然后可以运行以下命令来使用它
from photomaker import PhotoMakerStableDiffusionXLPipeline
模型会通过以下两行自动下载:
from huggingface_hub import hf_hub_download
photomaker_path = hf_hub_download(repo_id="TencentARC/PhotoMaker", filename="photomaker-v1.bin", repo_type="model")
您也可以选择从此网址手动下载。
import torch
import os
from diffusers.utils import load_image
from diffusers import EulerDiscreteScheduler
from photomaker import PhotoMakerStableDiffusionXLPipeline
### Load base model
pipe = PhotoMakerStableDiffusionXLPipeline.from_pretrained(
base_model_path, # can change to any base model based on SDXL
torch_dtype=torch.bfloat16,
use_safetensors=True,
variant="fp16"
).to(device)
### Load PhotoMaker checkpoint
pipe.load_photomaker_adapter(
os.path.dirname(photomaker_path),
subfolder="",
weight_name=os.path.basename(photomaker_path),
trigger_word="img" # define the trigger word
)
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
### Also can cooperate with other LoRA modules
# pipe.load_lora_weights(os.path.dirname(lora_path), weight_name=lora_model_name, adapter_name="xl_more_art-full")
# pipe.set_adapters(["photomaker", "xl_more_art-full"], adapter_weights=[1.0, 0.5])
pipe.fuse_lora()
### define the input ID images
input_folder_name = './examples/newton_man'
image_basename_list = os.listdir(input_folder_name)
image_path_list = sorted([os.path.join(input_folder_name, basename) for basename in image_basename_list])
input_id_images = []
for image_path in image_path_list:
input_id_images.append(load_image(image_path))
# Note that the trigger word `img` must follow the class word for personalization
prompt = "a half-body portrait of a man img wearing the sunglasses in Iron man suit, best quality"
negative_prompt = "(asymmetry, worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth, grayscale"
generator = torch.Generator(device=device).manual_seed(42)
images = pipe(
prompt=prompt,
input_id_images=input_id_images,
negative_prompt=negative_prompt,
num_images_per_prompt=1,
num_inference_steps=num_steps,
start_merge_step=10,
generator=generator,
).images[0]
gen_images.save('out_photomaker.png')
运行以下命令:
python gradio_demo/app.py
Asian woman img
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。