专栏名称: GitHubStore
分享有意思的开源项目
目录
相关文章推荐
艾邦高分子  ·  BOPP电容膜生产企业盘点 ·  6 小时前  
高分子科学前沿  ·  重磅发布:武书连2025中国大学排名! ·  昨天  
艾邦高分子  ·  30+(国内)气凝胶材料生产企业盘点 ·  2 天前  
高分子科学前沿  ·  中国科学院高能物理所陈青ACS ... ·  2 天前  
51好读  ›  专栏  ›  GitHubStore

换脸神器2.0!

GitHubStore  · 公众号  ·  · 2024-07-25 19:46

正文

项目简介

还记得腾讯去年整的那个PhotoMaker吗,当时刷爆了朋友圈。

就是可以把吴彦祖变成苏大强的那个换脸神器。

现在它进化了,叫PhotoMaker V2。

你只需要上传几张自己的照片,然后给它一些提示词,就能生成自己在各种场景、不同风格的照片。

它的工作原理也很简单。

就是把你上传的这些图片进行不断的堆叠,创建成一个统一的ID嵌入,用来保持人物特征的一致性。并以此为基础,再根据你输入的指令去生成图片。

基于这种方法,PhotoMaker可以在几秒钟内完成个性化的人物图像定制,而且无需额外的LoRA训练

这次的PhotoMaker V2在保持原有优势的基础上,在ID保真度和控制能力等方面有了进一步的提升。

简单点说就是生成的人物更像了,而且还能对生成目标内容进行微调,比如说年龄、身份、性别等。

官方还在HF上提供了V2版本的免费试用,感兴趣的朋友们可以去试玩一下!



示例

现实生成




风格化生成

注意:仅更改基本模型并添加 LoRA 模块以获得更好的风格化



依赖关系和安装

  • Python >= 3.8 (推荐使用 Anaconda 或 Miniconda)

  • PyTorch >= 2.0.0

conda create --name photomaker python=3.10conda activate photomakerpip install -U pip
# Install requirementspip install -r requirements.txt
# Install photomakerpip install git+https://github.com/TencentARC/PhotoMaker.git

然后您可以运行以下命令来使用它

from photomaker import PhotoMakerStableDiffusionXLPipeline


下载模型

模型会通过以下两行自动下载:

from huggingface_hub import hf_hub_downloadphotomaker_path = hf_hub_download(repo_id="TencentARC/PhotoMaker", filename="photomaker-v1.bin", repo_type="model")


您也可以选择从此网址手动下载。


💻 如何测试


像扩散器一样使用

  • 依赖性

import torchimport osfrom diffusers.utils import load_imagefrom diffusers import EulerDiscreteSchedulerfrom photomaker import PhotoMakerStableDiffusionXLPipeline
### Load base modelpipe = 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 checkpointpipe.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 imagesinput_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 personalizationprompt = "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')


启动本地gradio演示







请到「今天看啥」查看全文