/** **/

ITCooky Recipes

Lets cooky it yammy things!

How to improve, enlarge faces in a photo with GPEN neural network in Ubuntu!

дата November 25, 2021

I finally found a solution specifically to improve blurry photos while preserving the original face, and it’s amazing!

First of all thanks a lot to Tao Yang, Peiran Ren, Xuansong Xie and Lei Zhang for GAN Prior Embedded Network for Blind Face Restoration in the Wild
Entregan gratis la versión pública del proyecto en github github.com/yangxy/GPEN. GPEN allows you to quadruple faces, it could quadruple many faces on same image. Like all great programmers, they don’t know how to write installation and usage instructions, that’s why I’m here …

In the previous article Инструкция по установке PULSE: Self-Supervised Photo Upsampling на Ubuntu i apparently walked all the rakes with CUDA and Anaconda (and described them in detail), but here it comes out … Anaconda is not needed for sure, obviously only Python is needed and I already have it installed. But CUDA apparently it is needed, it is not in the requirements, and when processing a photo it is not noticed that the video card is working, only with a thousand it starts buzzing like crazy! Therefore, cuda-toolkit must be installed, as I describe in the article that Инструкция по установке PULSE: Self-Supervised Photo Upsampling на Ubuntu!!!

PC Hardware:
OS: Ubuntu 20.04.3 LTS
CPU: AMD® Ryzen 7 3700x 8-core processor × 16
MB: Gigabyte X570 I AORUS PRO WIFI
Mem: 64 GB DDR4 1330 Mhz
GPU: Nvidia GeForce RTX 2070 8Gb
Disk: Samsung SSD 512Gb EVO Plus

Create a folder and clone the project there with git
git clone https://github.com/yangxy/GPEN.git
cd GPEN
cd ./GPEN/weights

here I download the models all links in the githab description(1,6 Gb)
wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/RetinaFace-R50.pth
wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-BFR-512.pth
wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-BFR-512-D.pth
wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-BFR-256.pth
wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-BFR-256-D.pth
wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-Colorization-1024.pth
wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-Inpainting-1024.pth
wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-Seg2face-512.pth
wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/rrdb_realesrnet_psnr.pth

First attempt to remaster images that go with the app
python face_enhancement.py --model GPEN-BFR-512 --size 512 --channel_multiplier 2 --narrow 1 --use_sr --indir examples/imgs --outdir examples/outs-BFR

Error messages appear of which it is easy to understand that it is necessary to install more in Python
pip install opencv-python
pip install torchvision
pip install ninja

a complete list of what you need from Python is like this, apparently the rest I already had it installed
ninja
torch
torchvision
opencv-python
numpy
scikit-image
pillow

And the magic begins, I create in the GPEN folder two more folders pic_in (here I will put images to process) and pic_out (result will appear here)

GPEN quadruples the images, it takes a 512×512 image. The name of the model is GPEN-BFR-512, I don’t know why 512, but there are 256 more, but we took 512. You can see that the worse the original image id, the better the clarity does, because GPEN draws (creates invents) more. Noise needs to be removed from the image, if any – small dots are drawn quite organically like freckles, but waves or streaks are drawn like scars or zombie veins.

Put in pic_in

In GPEN run
python face_enhancement.py --model GPEN-BFR-512 --size 512 --channel_multiplier 2 --narrow 1 --use_sr --indir pic_in --outdir pic_out

And I have the result in pic_out

All aspects of the original face are preserved, the clarity is supernatural, Natalia Oreiro is easily recognizable in childhood and, most importantly, the result is obtained practically instantly, this is fantastic!!!

Now I put that new image in the folder /exampels/grays
I do to color it
python face_colorization.py
and I have the result in /exampels/outs-colorization

Fantastic!!!

For face_colorization.py to make the photo in color alone and not in couple with gray, it is necessary after the line

cv2.imwrite(os.path.join(outdir, '.'.join(filename.split('.')[:-1])+'.jpg'), np.hstack((grayf, colorf)))

add line

cv2.imwrite(os.path.join(outdir, '.'.join(filename.split('.')[:-1])+'_COL.jpg'), colorf)

GPEN also knows how to add missing details to the photo (the nose or smaller details), I’m going to try that too!

The problem with this mode is that here is the full instruction from the authors:

Complete faces:

python face_inpainting.py

And, of course, nothing works. I found a folder in which to put /examples/ffhq-10 I found a folder where /examples/outs-inpainting it appears, but there is no desired effect

How to use the Complete faces function in GPEN
The authors don’t want us to use it, but they don’t forbid it! File face_inpainting.py ir’s a demo, it draws lines on the photo chaotically and then removes them.

We have to remove the part of the code that does this vandalism

In file face_inpainting.py
remove the lines between

28 for _ in range(np.random.randint(1, 4)):

...    

60 return mask

It seems that face_inpainting.py must be given the images of size 1024×1024, so we increase them through Restore face (as described above)

From fot 512×512

To 1024×1024

You can see the curl of the paper that you will have to remove! To do this, you need to prepare an image in a special way in the Gimp! It is necessary to create a layer mask in PNG RGB color and mark there the defects that we want to fix. It is important how yo do it! For the background, thick strokes with a thick brush give a good result, and on faceit it is necessary to mark thinner in some places.

We put the photo in /examples/ffhq-10 we do
python face_inpainting.py
and in /examples/outs-inpainting we have the result

Altogether with the coloring, the result it is magnificent!


4 Responses to “How to improve, enlarge faces in a photo with GPEN neural network in Ubuntu!”

  1. ivax says:

    I’m looking for this but for reference picture of face, have you know any project?

    Thanks

  2. Mawrong says:

    Dear Sir,
    I try to download the pth files,wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-BFR-512.pth.
    But all of the pth filea are gone, how to pet these file? Can not down load form github.com/yangxy. Does the author off these files? Would you please send the link let me download ? thanks.

  3. cedric says:

    Hello does gpen work with ubuntu 22.04? Because I have errors when I run the scripts

    I run python face_inpainting.py

    Traceback (most recent call last):
    File “/home/cedric/LAB/GPEN/face_inpainting.py”, line 5, in
    from face_model.face_gan import FaceGAN
    File “/home/cedric/LAB/GPEN/face_model/face_gan.py”, line 13, in
    from gpen_model import FullGenerator, FullGenerator_SR
    ModuleNotFoundError: No module named ‘gpen_model’

    • Александр says:

      It stops to work for mi on 20.04 – now i am using GFPGAN but it has no color enhance!

Leave a Reply

Your email address will not be published.