Python PaddleGAN实现调整照片人物年龄

2022-01-22

目录
  • 前言
  • 环境部署
  • 项目使用
    • 预处理部分
    • 照片老化处理
    • 照片年轻化处理
  • 总结

    前言

    最近在试着研究飞浆平台的许多功能,看到了许多有意思的功能。其中可以将照片美化以及年龄调整这个功能让我想到了之前抖音的一个功能,所以特别感兴趣。花了些时间把项目拉下来玩了玩,用了一些我自己找的数据。

    PaddleGAN的Github地址:github仓库

    环境部署

    如果没有看过相关的文章,可能会被README搞得很迷糊。先不用看README中一个个教程或者md,我们要先安装执行环境。主要看docs/zh_CN/install.md文件。

    先看一下我们需要安装什么。项目已经提供了paddlepaddle的安装命令,按照自己机器的情况选择CUDA或者CPU。

    还有一些包需要补充安装的。

    注意哦,还需要安装dlib,按照我下面的命令执行一下。

    pip install dlib -i https://pypi.douban.com/simple

    OK,我们环境安装好了,我们开始玩玩代码吧。

    项目使用

    因为PaddleGAN实现照片编辑功能核心是StyleGAN,大家有兴趣的话也可以参看一下StyleGAN项目。在编辑图片前,需要先提取图片的Latant Code(潜在因子)。简而言之,会先将图片做预处理,然后使用预处理的数据进行编辑效果。

    预处理部分

    按照这个思路,我们先找到预处理md说明文件。看看下面的内容。

    我们看看pixel2style2pixel.py的参数说明。

    可以按照你的需求修改一下项目给出的命令。我这边使用的参数都是项目给出的,主要测试一下效果。从参数说明我们可以看出我们最重要需要提供的两个参数,分别是图片路径和输出目录。

    先放出我准备的照片,大家都非常熟悉。

    执行命令

    python -u applications/tools/pixel2style2pixel.py --input_image D:/spyder/PaddleGAN/data_hy/337.jpg --output_path  D:/spyder/PaddleGAN/data_hy/output --model_type ffhq-inversion --seed 233 --size 1024 --style_dim 512 --n_mlp 8 --channel_multiplier 2 --cpu

    执行效果(第一次会下载模型)

    我们可以在输出目录看到3个文件,其中dst.npy就是后面我们需要做效果的npy数据。我们先看看预处理后的图片什么样。

    可以看出细节变清晰了,照片集中在头部位置,头发也做了一定的处理,无官稍微修整了一下。

    照片老化处理

    下面我们继续看一下另一个md文件。

    我们看到另一个py文件可以帮助我们实现图片的编辑,styleganv2editing.py。我们先看看参数说明。

    注意哦,不只是可以变换年龄,还有其他的选项。age、eyes_open、eye_distance、eye_eyebrow_distance、eye_ratio、gender、lip_ratio、mouth_open、mouth_ratio、nose_mouth_distance、nose_ratio、nose_tip、pitch、roll、smile、yaw。那么其实还有很多效果我们可以实现。

    下面是我准备的老化图片命令

    python -u applications/tools/styleganv2editing.py --latent D:/spyder/PaddleGAN/data_hy/output/dst.npy --output_path D:/spyder/PaddleGAN/data_hy/old --model_type ffhq-config-f --size 1024 --style_dim 512 --n_mlp 8 --channel_multiplier 2 --direction_name age --direction_offset 3 --cpu

    执行情况

    (pytorch) D:\spyder\PaddleGAN>python -u applications/tools/styleganv2editing.py --latent D:/spyder/PaddleGAN/data_hy/output/dst.npy --output_path D:/spyder/PaddleGAN/data_hy/old --model
    _type ffhq-config-f --size 1024 --style_dim 512 --n_mlp 8 --channel_multiplier 2 --direction_name age --direction_offset 3 --cpu
    C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\skimage\data\__init__.py:107: DeprecationWarning:
        Importing file_hash from pooch.utils is DEPRECATED. Please import from the
        top-level namespace (`from pooch import file_hash`) instead, which is fully
        backwards compatible with pooch >= 0.1.
     
      return file_hash(path) == expected_hash
    [12/29 20:17:06] ppgan INFO: Found C:\Users\huyi\.cache\ppgan\stylegan2-ffhq-config-f.pdparams
    [12/29 20:17:09] ppgan INFO: Found C:\Users\huyi\.cache\ppgan\stylegan2-ffhq-config-f-directions.pdparams

    看看效果

    老态还是很明显的,效果不错。

    照片年轻化处理

    和上面一样,下面是我的年轻化命令:

    python -u applications/tools/styleganv2editing.py --latent D:/spyder/PaddleGAN/data_hy/output/dst.npy --output_path D:/spyder/PaddleGAN/data_hy/young --model_type ffhq-config-f --size 1024 --style_dim 512 --n_mlp 8 --channel_multiplier 2 --direction_name age --direction_offset -3 --cpu

    对比两个命令可以看出主要是--direction_offset参数不同。

    执行情况

    (pytorch) D:\spyder\PaddleGAN>python -u applications/tools/styleganv2editing.py --latent D:/spyder/PaddleGAN/data_hy/output/dst.npy --output_path D:/spyder/PaddleGAN/data_hy/young --mod
    el_type ffhq-config-f --size 1024 --style_dim 512 --n_mlp 8 --channel_multiplier 2 --direction_name age --direction_offset -3 --cpu
    C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\skimage\data\__init__.py:107: DeprecationWarning:
        Importing file_hash from pooch.utils is DEPRECATED. Please import from the
        top-level namespace (`from pooch import file_hash`) instead, which is fully
        backwards compatible with pooch >= 0.1.
     
      return file_hash(path) == expected_hash
    [12/29 20:20:07] ppgan INFO: Found C:\Users\huyi\.cache\ppgan\stylegan2-ffhq-config-f.pdparams
    [12/29 20:20:09] ppgan INFO: Found C:\Users\huyi\.cache\ppgan\stylegan2-ffhq-config-f-directions.pdparams

    看看效果

    效果还是不错的,还能看出是谁,对吗?

    总结

    PaddleGAN项目我最近开始研究,还没有很深入,如果有理解不对的地方可以留言指正。如果大家喜欢这些效果的话,我可以把其他的效果,比如性别反转等等做出来(想看的话,留言告诉我)。有时间的话也可以魔改一下代码生成点有意思的特效。

    以上就是Python PaddleGAN实现调整照片人物年龄的详细内容,更多关于Python PaddleGAN调整照片人物年龄的资料请关注北冥有鱼其它相关文章!

    您可能感兴趣的文章:

    • Python机器学习之实现模糊照片人脸恢复清晰
    • Python实现照片卡通化
    • Python DPED机器学习之实现照片美化
    • Python 人工智能老照片修复算法学习
    • Python实现老照片修复之上色小技巧
    • Python实现对照片中的人脸进行颜值预测

    《Python PaddleGAN实现调整照片人物年龄.doc》

    下载本文的Word格式文档,以方便收藏与打印。