본문 바로가기

파이썬

GFPGAN 사용기

https://github.com/TencentARC/GFPGAN

 

GitHub - TencentARC/GFPGAN: GFPGAN aims at developing Practical Algorithms for Real-world Face Restoration.

GFPGAN aims at developing Practical Algorithms for Real-world Face Restoration. - TencentARC/GFPGAN

github.com

 

os : window11

python: 3.11.9

gpu: 4080 super

 

1. GFPGAN github 홈페이지 참고하여 환경 구축

Clone repo

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

Install dependent packages

# Install basicsr - https://github.com/xinntao/BasicSR
# We use BasicSR for both training and inference
pip install basicsr

# Install facexlib - https://github.com/xinntao/facexlib
# We use face detection and face restoration helper in the facexlib package
pip install facexlib

pip install -r requirements.txt
python setup.py develop

# If you want to enhance the background (non-face) regions with Real-ESRGAN,
# you also need to install the realesrgan package
pip install realesrgan

Download pre-trained models: GFPGANv1.3.pth

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

 

자신의 컴퓨터에 맞는 pytorch 설치

https://pytorch.org/get-started/locally/

 

Get Started

Set up PyTorch easily with local installation or supported cloud platforms.

pytorch.org

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128

 

2. test

Inference!

python inference_gfpgan.py -i inputs/whole_imgs -o results -v 1.3 -s 2

 

ModuleNotFoundError: No module named 'torchvision.transforms.functional_tensor'

에러발생시

.venv\Lib\site-packages\basicsr\data\degradations.py 에 import 부분
from torchvision.transforms.functional_tensor import rgb_to_grayscale

>>> 수정

from torchvision.transforms.functional import rgb_to_grayscale

 

\GFPGAN\results 에 결과 확인

 

3. 학습

ffhq 다운로드

\GFPGAN\options\train_gfpgan_v1.yml // 옵션들 수정

datasets:
  train:
    name: train_GFPGANv1_512 # 학습 결과 폴더 이름
    dataroot_gt: datasets/ffhq/ffhq_512 # ffhq 에서 다운 받은 이미지
    out_size: 512 # 512로 지정하면 512x512 이미지로 학습하고, 복원 결과도 512x512

    crop_components: true #보통 high-resolution 학습, 또는 부위별 손상 복원 강화에 사용
    component_path: experiments/pretrained_models/FFHQ_eye_mouth_landmarks_512.pth # 512 해상도의 FFHQ용 컴포넌트 위치 landmark를 담고 있는 pre-trained PyTorch 모델 파일

    dataroot_lq: datasets/faces/validation/input # 저화질 이미지
    dataroot_gt: datasets/faces/validation/reference # 원본 이미지
    
network_g: 
  out_size: 512 # Generator의 입출력 크기.
  num_style_feat: 512 # StyleGAN 기반 네트워크에서 스타일 벡터(feature) 차원
  decoder_load_path: experiments/pretrained_models/StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth # Decoder에 사용할 사전 학습(pre-trained) StyleGAN2 모델 경로
  
network_d:
  out_size: 512 # Discriminator(판별기) 네트워크 입출력 이미지 크기

# 눈/입 등 주요 얼굴 부위만 별도 판단하는 서브-판별기(Discriminator)
network_d_left_eye:
  type: FacialComponentDiscriminator

network_d_right_eye:
  type: FacialComponentDiscriminator

network_d_mouth:
  type: FacialComponentDiscriminator

>>> ffhq 이미지를 32로 바꿨으며 수정을 이렇게함

datasets:
  train:
    name: train_GFPGANv1_32 # 학습 결과 폴더 이름
    dataroot_gt: C:\Temp\ffhq\gt_32 # ffhq 에서 다운 받은 이미지
    out_size: 32 

    crop_components: false
    component_path: 

    dataroot_lq: C:\Temp\ffhq\lq_32 # 저화질 이미지
    dataroot_gt: C:\Temp\ffhq\gt_32 # 원본 이미지
    
network_g: 
  out_size: 32 # Generator의 입출력 크기.
  num_style_feat: 32 # StyleGAN 기반 네트워크에서 스타일 벡터(feature) 차원
  decoder_load_path: 
  
network_d:
  out_size: 32 # Discriminator(판별기) 네트워크 입출력 이미지 크기

# 눈/입 등 주요 얼굴 부위만 별도 판단하는 서브-판별기(Discriminator)
#network_d_left_eye:
#  type: FacialComponentDiscriminator
#
#network_d_right_eye:
#  type: FacialComponentDiscriminator
#
#network_d_mouth:
#  type: FacialComponentDiscriminator

 

Training

python -m torch.distributed.launch --nproc_per_node=4 --master_port=22021 gfpgan/train.py -opt options/train_gfpgan_v1.yml --launcher pytorch

>>>>

python gfpgan/train.py -opt options/train_gfpgan_v1.yml

 

에러발생

ValueError: betas must be either both floats or both Tensors 

\gfpgan\gfpgan\models\gfpgan_model.py

betas = (0**net_g_reg_ratio, 0.99**net_g_reg_ratio)

>>>

betas = (0.9**net_g_reg_ratio, 0.99**net_g_reg_ratio)

 

에러발생

cv2.error: OpenCV(4.11.0) :-1: error: (-5:Bad argument) in function 'imencode'
> Overload resolution failed:
>  - Can't parse 'params'. Sequence item with index 1 has a wrong type
>  - Can't parse 'params'. Sequence item with index 1 has a wrong type

\GFPGAN\.venv\Lib\site-packages\basicsr\data\degradations.py

encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), quality]

>>>>

encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), int(quality)]

 

에러발생

NameError: name 'fused_act_ext' is not defined

BASICSR_JIT=True 환경변수 등록