SoFunction
Updated on 2024-11-19

Python PaddleGAN implementation to adjust the age of people in photos

preamble

I've been trying to research many of the features of the Fly Pulp platform lately, and I've come across a number of interesting features. One of the features that allows you to beautify your photos as well as age them reminded me of a previous Shakeology feature, so it was of particular interest. Spent some time pulling the program down and playing around with it, using some data I found on my own.

PaddleGAN's Github address:github repository

Environment deployment

If you haven't read about it, you may be confused by the README. Instead of reading one tutorial or md in the README, we're going to install the execution environment first. Look mainly at the docs/zh_CN/ file.

Let's first look at what we need to install. The project has provided the paddlepaddle install command, choose CUDA or CPU as per your machine.

There are also some packages that need to be installed additionally.

Note oh, you also need to install dlib, follow my command below and run it.

pip install dlib -i /simple

OK, we have our environment installed, let's start playing with the code.

Project use

Because the core of PaddleGAN's photo editing function is StyleGAN, you can also refer to the StyleGAN project if you are interested. Before editing a picture, we need to extract the Latent Code of the picture first. In short, the image will be pre-processed first, and then the pre-processed data will be used to edit the effect.

preprocessing section

Along these lines, let's first find the preprocessing md description file. Take a look at the following.

Let's look at the parameter descriptions.

You can modify the commands given by the project according to your needs. I use the parameters given by the project, mainly to test the effect. From the parameter description, we can see that the two most important parameters we need to provide are the image path and output directory.

I'll start by releasing the photos I prepared, which you're all very familiar with.

execute a command

python -u applications/tools/ --input_image D:/spyder/PaddleGAN/data_hy/ --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

Execution effect (the model will be downloaded for the first time)

We can see 3 files in the output directory, which is the npy data we need to do the effect later. Let's see what the pre-processed image looks like.

You can see that the details have become clearer, the photo focuses on the head position, the hair has been somewhat processed, and the no-feature has been slightly trimmed.

Photo aging treatment

Let's move on to another md file.

We see another py file that helps us to realize the editing of the image,. Let's take a look at the parameter description.

Note that it's not just the age that can be changed, there are other options as well. 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. so there's actually a lot more effects we can implement.

Here is the aging picture command I prepared

python -u applications/tools/ --latent D:/spyder/PaddleGAN/data_hy/output/ --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

Implementation

(pytorch) D:\spyder\PaddleGAN>python -u applications/tools/ --latent D:/spyder/PaddleGAN/data_hy/output/ --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  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\
[12/29 20:17:09] ppgan INFO: Found C:\Users\huyi\.cache\ppgan\

Look at the effect.

The old state is still noticeable and works well.

Photo rejuvenation

As above, here are my rejuvenation orders:

python -u applications/tools/ --latent D:/spyder/PaddleGAN/data_hy/output/ --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

Comparing the two commands shows that the main difference is the --direction_offset parameter.

Implementation

(pytorch) D:\spyder\PaddleGAN>python -u applications/tools/ --latent D:/spyder/PaddleGAN/data_hy/output/ --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  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\
[12/29 20:20:09] ppgan INFO: Found C:\Users\huyi\.cache\ppgan\

Look at the effect.

It still works well and you can still tell who it is, right?

summarize

I've recently started working on the PaddleGAN program, and I haven't gotten very far into it yet, so if there's something I don't understand correctly, you can leave a comment and correct me. If you like these effects, I can make other effects, such as gender reversal and so on (if you want to see, leave a message to tell me). If you have time, you can also modify the code to generate some interesting effects.

Above is Python PaddleGAN implementation of adjusting the age of photo characters in detail, more information about Python PaddleGAN adjusting the age of photo characters please pay attention to my other related articles!