2

图像处理│一张自拍即可实现变老变年轻,带你感受时光流逝之美 - 极客飞兔

 1 year ago
source link: https://www.cnblogs.com/tinygeeker/p/17171441.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

🎈 项目效果#

  • 飞浆是一个由百度推出的深度学习开发平台,为开发者提供了高效、易用、灵活和全面的深度学习开发工具和服务。
  • PaddleGAN是飞浆在图像生成和处理领域的一个代表性项目,通过深度学习的技术和飞浆的支持,PaddleGAN可以实现多种惊人的图像处理效果,例如图像转换、人脸编辑、动态效果生成等等。
  • 其中内置了StyleGAN V2FOM分别实现人脸属性编辑和人脸动画效果。这些技术和应用在很多领域都有广泛的应用,例如娱乐广告电影制作虚拟现实等等。

2894189-20230302125005265-1622940790.gif
官方效果预览

🎈 环境搭建#

  • 这里个人推荐使用 Anaconda 搭建本地环境,因为如果项目太多,后期非常不好管理
  • 另外推荐全局更换 pip 源:pip config set global.index-url https://mirror.baidu.com/pypi/simple
  • 如果未更换源的话,下载会超级慢,甚至下载失败,也可以通过 -i 临时设置源:pip install xxx -i https://mirror.baidu.com/pypi/simple
# 创建环境
conda create --name PaddleGAN python=3.6

#激活环境
activate PaddleGAN

# 安装依赖cmake
pip install cmake -i https://mirror.baidu.com/pypi/simple

# 安装依赖boost 
pip install boost -i https://mirror.baidu.com/pypi/simple

# 安装依赖numpy
pip install numpy -i https://mirror.baidu.com/pypi/simple

# 安装依赖dlib
pip install dlib==19.8.1 -i https://mirror.baidu.com/pypi/simple

🎈 下载PaddleGAN#

  • GAN 是一种深度学习模型,是近年来复杂分布上无监督学习最具前景的方法之一
  • 在安装依赖的时候,因使用的环境是 python3.6,所以需要将 requirements.txt 文件中的 opencv-python 加上一个版本号opencv-python==4.3.0.38,不然默认安装最新版本的,而最新版本的无法被下载成功
# 下载源码
git clone https://gitee.com/PaddlePaddle/PaddleGAN

# 进入项目目录
cd PaddleGAN

# 安装项目依赖,这里一定要设置全局源
pip install -r requirements.txt

# 安装环境
python setup.py develop

🎈 安装飞浆#

  • 安装 CPU 版本,不容易出错,但速度会有点慢,如果有 GPU 尽量使用 GPU 版本
  • 我这里只安装最新的,如果想要安装指定版本,请查看飞浆官方教程
# CPU版本
pip install paddlepaddle

# GPU版本
pip install paddlepaddle-gpu

🎈 生成图片潜码#

  • 需要通过命令生成原图对应的 Latent Code
  • input_image: 输入的图像路径
  • output_path: 生成图片存放的路径
  • weight_paht: 预训练模型路径
  • model_type: PaddleGAN 内置模型类型,若输入 PaddleGAN 已存在的模型类型,weight_paht 将失效,当前可用:ffhq-inversionffhq-toonify
  • seed: 随机数种子
  • size: 模型参数,输出图片的分辨率
  • style_dim: 模型参数,输出图片的分辨率
  • n_mlp: 模型参数,风格z所输入的多层感知层的层数
  • channel_multiplier: 模型参数,通道乘积,影响模型大小和生成图片的质量
  • cpu: 是否使用 cpu 推理,若不使用,请在命令去除
  • 这里从网上找了一张我最喜欢的大甜甜照片来测试

2894189-20230302125036803-1164181911.jpg
# 命令模板
cd applications/
python -u tools/styleganv2.py \
       --input_image <替换为输入的图像路径> \
       --output_path <替换为生成图片存放的文件夹> \
       --weight_path <替换为你的预训练模型路径> \
       --model_type ffhq-inversion \
       --seed 233 \
       --size 1024 \
       --style_dim 512 \
       --n_mlp 8 \
       --channel_multiplier 2 \
       --cpu

# 我使用的命令
python -u applications/tools/pixel2style2pixel.py  --input_image E:/PaddleGAN/results/input/beauty.jpg  --output_path   E:/PaddleGAN/results/output --model_type ffhq-inversion --seed 233  --size 1024  --style_dim 512  --n_mlp 8  --channel_multiplier 2 --cpu

🎈 老龄化处理#

  • latent:要编辑的代表图像的风格向量的路径。可来自 Pixel2Style2Pixel 生成的 dst.npy,也就是上面生成的潜码
  • latent2: 第二个风格向量的路径。来源同第一个风格向量
  • output_path: 生成图片存放的文件夹
  • weight_path: 预训练模型路径
  • model_type: PaddleGAN 内置模型类型,若输入 PaddleGAN 已存在的模型类型,weight_paht 将失效,当前建议使用:ffhq-config-f
  • size: 模型参数,输出图片的分辨率
  • n_mlp: 模型参数,风格z的维度
  • channel_multiplier: 模型参数,通道乘积,影响模型大小和生成图片的质量
  • direction_path: 存放一系列属性名称及对象属性向量的文件路径。默认为空,即使用 ppgan 自带的文件。若不使用,请在命令中去除
  • direction_name: 要编辑的属性名称,对于 ffhq-config-f 有预先准备的这些属性:ageeyes_openeye_distanceeye_eyebrow_distanceeye_ratiogenderlip_ratiomouth_openmouth_rationose_mouth_distancenose_rationose_tippitchrollsmileyaw

2894189-20230302125111813-1444146471.png
老了依旧美爆的大甜甜
# 命令模板
cd applications/
python -u tools/styleganv2editing.py \
       --latent <潜码路径> \
       --output_path <新人脸(年龄变换后)的保存路径> \
       --model_type ffhq-config-f \
       --size 1024 \
       --style_dim 512 \
       --n_mlp 8 \
       --direction_name age \
       --direction_offset 3
       
# 我使用的命令
python -u applications/tools/styleganv2editing.py --latent E:/PaddleGAN/results/output/dst.npy --output_path E:/PaddleGAN/results/old --model_type ffhq-config-f --size 1024 --style_dim 512 --n_mlp 8 --channel_multiplier 2 --direction_name age --direction_offset 3

🎈 年轻化处理#

  • direction_offset: 这个参数的绝对值越大,处理程度越重,绝对值越大显得越年轻,老龄化中则越重

2894189-20230302125214395-1332233469.png
年轻的大甜甜
cd applications/
python -u tools/styleganv2editing.py \
       --latent <潜码路径> \
       --output_path <新人脸(年龄变换后)的保存路径> \
       --model_type ffhq-config-f \
       --size 1024 \
       --style_dim 512 \
       --n_mlp 8 \
       --direction_name age \
       --direction_offset -3

# 我使用的命令
python -u applications/tools/styleganv2editing.py --latent E:/PaddleGAN/results/output/dst.npy --output_path E:/PaddleGAN/results/young --model_type ffhq-config-f --size 1024 --style_dim 512 --n_mlp 8 --channel_multiplier 2 --direction_name age --direction_offset -3

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK