SimSwap复现指引及代码分析【2023有更新】

news/2024/10/22 15:12:35/

SimSwap复现指引及代码分析【2023有更新】

  • 0、前言
  • 1、复现指引
    • 环境配置指引
    • Inference for image or video face swapping
    • Inference参数解析
    • Inference用法示例
      • 图像
      • 视频
  • 2、结果分析
  • 3、代码分析

0、前言

论文讲解在:https://blog.csdn.net/qq_45934285/article/details/127701689

1、复现指引

环境配置指引

  1. 首先一步一步执行我的环境:
    官方的安装教程不要用!!!过时了,看下面我的环境!!!
'''官方教程已过时!!!踩坑了'''
conda create -n simswap python=3.6 -y
conda activate simswap
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=10.2 -c pytorch -y
pip install --ignore-installed imageio
pip install insightface==0.2.1 onnxruntime moviepy # 比较慢而且会报出一些红色的ERROR,暂且跳过不用管。不管不行会报错
pip install onnxruntime-gpu
pip install opencv-python

我的环境!!!😚😚😚

'''我的安装教程'''
conda create -n simswap python=3.8 -y
conda activate simswap
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia -y
pip install --ignore-installed imageio
pip install insightface==0.2.1 onnxruntime moviepy	#成功
pip install onnxruntime-gpu # 成功

之后会遇到:

  • ValueError: This ORT build has [‘TensorrtExecutionProvider’, ‘CUDAExecutionProvider’, ‘CPUExecutionProvider’] enabled. Since ORT 1.9, you are required to explicitly set the providers parameter when instantiating InferenceSession. For example, onnxruntime.InferenceSession(…, providers=[‘TensorrtExecutionProvider’, ‘CUDAExecutionProvider’, ‘CPUExecutionProvider’], …)
  • 解决办法:将anaconda3/envs/simswap/lib/python3.8/site-packages/insightface/model_zoo/model_zoo.py的第23行的session = onnxruntime.InferenceSession(self.onnx_file, None)改为session = onnxruntime.InferenceSession(self.onnx_file, None, providers=['TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider'])

还会遇到

  • AttributeError: module ‘numpy’ has no attribute ‘float’.
    np.float was a deprecated alias for the builtin float. To avoid this error in existing code, use float by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.float64 here.
    The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  • 解决方案成功解决AttributeError: module ‘numpy‘ has no attribute ‘float‘.

至此环境配置完毕!下面是所需要的checkpoint。

  1. 然后下载antelope.zip,解压到./insightface_func/models(需要新建文件夹models)
    这是使用Insightface的面部检测和对齐方法进行图像预处理。
  2. 下载79999_iter.pth然后放到./parsing_model/checkpoint(需要新建文件夹checkpoint)
    这是使用来自 face-parsing.PyTorch 的人脸解析进行图像后处理。
  3. 下载arcface_checkpoint.tar然后新建一个arcface_model文件夹,直接将arcface_checkpoint.tar放入。
    下载checkpoints.zip或者512.zip(建议直接下一个512体验高清就行了) 然后新建一个checkpoints文件夹,直接将512.zip解压放入,里面是一个550000_net_G.pth文件
    注意2023-04-25:修复了"AttributeError: ‘SGD’ object has no attribute ‘defaults’ now" 错误。如果您已经下载了arcface_checkpoint.tar,请重新下载。此外,您还需要更新 ./models/ 中的脚本。

至此环境准备结束。

Inference for image or video face swapping

先试试环境是否有问题,图片是代码包中给的。我用的是512的checkpoint,无水印,a是源脸,b是目标脸。输出到./output/result_whole_swapsingle.jpg

python test_wholeimage_swapmulti.py --no_simswaplogo  --crop_size 512 --use_mask  --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --pic_b_path ./demo_file/multi_people.jpg --output_path ./output/

如图是可以成功换脸的:(为了方便查看将图像裁剪到了128x128)
请添加图片描述请添加图片描述
请添加图片描述

Inference参数解析

参数函数
–nameSimSwap 训练日志名称
–pic_a_path要交换的源面图像路径
–pic_b_path目标人脸图像路径
–pic_specific_path待交换特定面孔的图像路径
–multisepcific_dir用于多特定面交换的图像文件夹路径
–video_path与源面互换的视频路径
–temp_path存放中间文件的路径
–output_path换脸结果存放目录路径
–no_simswaplogo控制是否去水印的超参数
–use_mask控制是否使用人脸解析的超参数,视觉效果更好(推荐使用)

Inference用法示例

--crop_size 224表示图像预处理,相应的改为--crop_size 512也是一样。不加--crop_size就是对已经面部对齐的图像进行简单的面部交换。
加上--no_simswaplogo就是无水印。

图像

  • 仅交换一张图像中的一张脸(人脸检测置信度最高的一张)。结果将保存到 ./output/result_whole_swapsingle.jpg
python test_wholeimage_swapsingle.py --crop_size 224 --use_mask  --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --pic_b_path ./demo_file/multi_people.jpg --output_path ./output/ 
  • 交换一张图片中的所有面孔。结果将保存到 ./output/result_whole_swapmulti.jpg
python test_wholeimage_swapmulti.py --crop_size 224 --use_mask  --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --pic_b_path ./demo_file/multi_people.jpg --output_path ./output/ 
  • 交换一张图片中的特定面孔。结果将保存到 ./output/result_whole_swapspecific.jpg
python test_wholeimage_swapspecific.py --crop_size 224 --use_mask  --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --pic_b_path ./demo_file/multi_people.jpg --output_path ./output/ --pic_specific_path ./demo_file/specific2.png 
  • 在一个图像中交换具有多个特定 ID 的多个特定面部。结果将保存到 ./output/result_whole_swap_multispecific.jpg
python test_wholeimage_swap_multispecific.py --crop_size 224 --use_mask  --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_b_path ./demo_file/multi_people.jpg --output_path ./output/ --multisepcific_dir ./demo_file/multispecific 

视频

  • 只交换视频中的一张脸(人脸检测置信度最高的那一张)。
python test_video_swapsingle.py --crop_size 224 --use_mask --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --video_path ./demo_file/multi_people_1080p.mp4 --output_path ./output/multi_test_swapsingle.mp4 --temp_path ./temp_results 
  • 交换视频中的所有面孔。
python test_video_swapmulti.py --crop_size 224 --use_mask --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --video_path ./demo_file/multi_people_1080p.mp4 --output_path ./output/multi_test_swapmulti.mp4 --temp_path ./temp_results 
  • 交换视频中的特定面孔。
python test_video_swapspecific.py --crop_size 224 --use_mask --pic_specific_path ./demo_file/specific1.png --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path ./demo_file/Iron_man.jpg --video_path ./demo_file/multi_people_1080p.mp4 --output_path ./output/multi_test_specific.mp4 --temp_path ./temp_results 

换脸时,需要给出换脸人的照片。然后将图片路径分配给参数“–pic_specific_path”。这张图片应该是正面,并且展示了整个头部和颈部,可以帮助准确换脸(如果你还不知道如何选择图片,可以参考./demo_file/specific*.png).如果这张图是从要改的视频里截的就更好了。

  • 在视频中用多个特定 ID 交换多个特定面孔。
python test_video_swap_multispecific.py --crop_size 224 --use_mask  --name people --Arc_path arcface_model/arcface_checkpoint.tar --video_path ./demo_file/multi_people_1080p.mp4 --output_path ./output/multi_test_multispecific.mp4 --temp_path ./temp_results --multisepcific_dir ./demo_file/multispecific 

您分配给“–multisepcific_dir”的文件夹应该如下所示:

$Your folder name$├── DST_01.jpg(png)
└── DST_02.jpg(png)
└──...
└── SRC_01.jpg(png)
└── SRC_02.jpg(png)
└──...

结果就是视频中SRC_01.jpg(png)对应的人脸会被DST_01.jpg(png)对应的人脸替换掉。然后将SRC_02.jpg(png)对应的字符替换为DST_02.jpg(png)的人脸,以此类推。注意使用自己的数据命名时,不要去掉SRC_(DST_)01.jpg(png)中的0等。

总体可以记忆为:一换一,一换具体一,一换具体多,具体多换具体多。四种

2、结果分析

  • 源脸的脸型几乎没有传递到目标脸,也就是说直接重用目标脸的脸型,如果两张图片脸型相差过大效果就不好了。
  • 清晰度不高,肤色也没变,光照保存的也一般,还有很多问题。
  • 不过很多方法是可以借鉴的。

3、代码分析

打算下一篇博客写按照论文的pipeline梳理一遍代码,感兴趣的可以follow一下。🥰🥰🥰


http://www.ppmy.cn/news/295010.html

相关文章

深度学习技巧应用19-可视化的模型训练功能TensorBoard的使用,也能在pytorch框架下实现

大家好,我是微学AI,今天给大家介绍一下深度学习技巧应用19-可视化的模型训练功能TensorBoard的使用,也能在pytorch框架下实现,当今深度学习技术的飞速发展使得模型训练变得更加复杂,工程师需要具备更高的技术能力去把控模型, TensorBoard便是一款优秀的可视化工具,它可以…

Lecture 15 Probabilistic Context-Free Grammar

目录 Ambiguity in Parsing Basics of PCFGsBasics of PCFGsStochastic Generation with PCFGs PCFG ParsingCYK for PCFGs Limitations of CFGPoor Independence AssumptionsLack of Lexical Conditioning Ambiguity in Parsing Context-Free grammars assign hierarchical st…

奥黛丽.赫本的美丽之本

For attractive lips, speak words of kindness. 若要优美的嘴唇,要讲亲切的话; For lovely eyes, seek out the good in people. 若要可爱的眼睛,要看到别人的好处; For a slim figure, share your foodwith the hungry. 若…

【奥黛丽赫本】女神一样的优雅

缺失的父爱 我们可以选择自己的事业和前途,但却无法选择自己的父母和家庭,无论他们是好是坏,是善良还是邪恶,早在前世注定,割不断的亲情会牵系一生 赫本的父亲是一名银行家,他与赫本母亲的婚姻并没有持续…

推荐电影 奥黛丽赫本的十大经典电影 1953-1989

奥黛丽赫本的十部经典电影 1.《罗马假日》 (Roman Holiday,1953) 导演:威廉惠勒 风情指数★★★★★ 看点:奥黛丽赫本和格里高利派克的完美组合 如果没有奥黛丽赫本,或许《罗马假日》只会作为一部平庸…

关于奥黛丽赫本一生的详细介绍

发信人: allisonz (我要好好想想接下来怎么走), 信区: Movie标 题: 关于奥黛丽赫本一生的详细介绍发信站: 日月光华 (2004年08月12日22:00:31 星期四) 奥黛丽.赫本(Audrey Hepburn)于1929年5月4日出生于比利时布鲁塞尔,逝于1993年1月20日。她…

《奥黛丽·赫本》一辈子活在优雅里

《奥黛丽赫本》一辈子活在优雅里 奥黛丽赫本:1929/05/04-1993/01/20,英国女演员,凭电影《罗马假日》获奥斯卡最佳女演员奖。投身公益事业,一生荣誉无数。 美国四大艺术奖项——电影奥斯卡奖、戏剧托尼奖、音乐格莱美奖和电视艾美奖…

想必大家都为测试用例头疼过,那么如何才能设计出一个“好的”测试用例

目录 前言: “好的”测试用例具备的特征 1.等价类划分方法 2.边界值分析方法 “好的”测试用例的设计方法 测试用例设计的其他经验 作为测试人员,需要注意以下几点: 前言: 设计一个“好的”测试用例&…