Thursday | 21 NOV 2024
[ previous ]
[ next ]

Setting up GFPGAN

Title:
Date: 2024-09-26
Tags:  

I recently wanted to sharpen some blurry images and GFPGAN looked promising. These are my set up instructions based on the github and this youtube video.

The first step is to get the repo:

git clone https://github.com/TencentARC/GFPGAN.git
cd GFPGAN

Next we create the conda environment and install python 3.9.

conda activate gfp-test
conda create -n gfp-test python=3.9

Now we can install pytorch, but first we need to get the cuda version if we are using cuda.

nvcc --version

I have cuda 12.6 and so I am going to use 12.4 as there is no pytoch cuda package for 12.6 yet.

The following command is generated on the Pytoch website:

conda install pytorch torchvision torchaudio pytorch-cuda=12.4 -c pytorch -c nvidia

We have some dependencies to install that the repo will use:

pip install opencv-python
pip install scipy
pip install tqdm
pip install yapf
pip install lmdb
pip install pyyaml
pip install basicsr
pip install facexlib
pip install realesrgan

Run the python setup script:

python setup.py develop

Get the model that we will use for the sharpening:

wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P experiments/pretrained_models

At this point the set up is done. We can now use GFPGAN to sharpen an image. Create a directory called media in the repo and place an image that you want to sharpen.

mkdir media
cp ~/Downloads/test.jpg media

Now we can run the routine:

python inference_gfpgan.py -i media -o results -v 1.3 -s 2

If everything went well, there should now be a result directory created, inside which there should be a few different folders. There will be folders for cropped faces, and multiple restorations.

The folder we care about is restored_imgs, this is where the sharpened image will be.

At this point we are done, I did however run into an issue when running the above inference.

I got the following error. The solution was on github and it looks like there is some outdated code in basicsr. Luckily it was a very simple fix.

  File "/home/username/.conda/envs/gfp-test/lib/python3.9/site-packages/basicsr/data/degradations.py", line 8, in <module>
    from torchvision.transforms.functional_tensor import rgb_to_grayscale
ModuleNotFoundError: No module named 'torchvision.transforms.functional_tensor'

The library doesn't have a functional_tensor, it is now called just functional.

I opened the file:

/home/username/.conda/envs/gfp-test/lib/python3.9/site-packages/basicsr/data/degradations.py

Then changed the line:

from torchvision.transforms.functional_tensor import rgb_to_grayscale

to:

from torchvision.transforms.functional import rgb_to_grayscale

Here is a link to the solution from github