Learn more about AI Face Restoration with GFPGAN on Vultr Cloud. Take a minute and discover what our Vultr Support Team has to say.
AI Face Restoration with GFPGAN on Vultr Cloud: A Complete Setup Guide
Looking for a way to restore old, damaged, or low-resolution photos, especially human faces?
GFPGAN is a breakthrough AI model that can help you do just that. Developed by researchers Xintao Wang, Yu Li, Honglun Zhang, and Ying Shan, GFPGAN offers a powerful way to upscale and restore facial details in photos, producing realistic, high-quality results even from challenging images.
Today, we will explore what GFPGAN is, how it works, and guide us through the steps to deploy and use it on a cloud GPU server.
An Overview:
What Is GFPGAN?
GFPGAN is a specialized algorithm based on Generative Adversarial Networks designed specifically for blind face restoration. It is a process that enhances and reconstructs faces in photos without needing prior knowledge of the damage or degradation type.
In short, GFPGAN can take a blurry, old, or damaged face photo and make it look fresh, clear, and realistic again.
The magic behind GFPGAN involves techniques similar to those used in StyleGAN2 but tuned specifically for restoration tasks, making it extremely accurate and efficient.
Getting Started: Prerequisites
Before diving into GFPGAN, make sure you have:
- Access to an Ubuntu cloud GPU server with at least 10GB GPU VRAM (such as an Nvidia A100).
- SSH access to the server.
- A non-root user with sudo privileges for safer operations.
Setting Up the Server Environment
- Create a directory to save generated images:
mkdir -p /home/user/output_images
- Then, install PyTorch with CUDA 11.8 support:
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu118
- Next, install Jupyter Notebook to run and manage the code interactively:
pip3 install notebook
- Allow the Jupyter Notebook port (8888) through the firewall:
sudo ufw allow 8888
- Then, launch Jupyter Notebook:
jupyter notebook –ip=0.0.0.0
- Next, access Jupyter via your browser at:
http://YOUR_SERVER_IP:8888/tree?token=YOUR_TOKEN
Install GFPGAN Model Dependencies
Inside a new Jupyter Notebook, install the required libraries:
!pip install realesrgan gfpgan basicsr
Then import the essential modules:
from gfpgan import GFPGANer
from basicsr.archs.rrdbnet_arch import RRDBNet
from realesrgan import RealESRGANer
from IPython.display import display, Image
import requests
import numpy as np
import cv2
import os
Initialize the GFPGAN Model
Set up the model checkpoints and background enhancement tools:
arch = 'clean'
model_name = 'GFPGANv1.4'
gfpgan_checkpoint = 'https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth'
realersgan_checkpoint = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth'
rrdbnet = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
bg_upsampler = RealESRGANer(
scale=2,
model_path=realersgan_checkpoint,
model=rrdbnet,
tile=400,
tile_pad=10,
pre_pad=0,
half=True
)
restorer = GFPGANer(
model_path=gfpgan_checkpoint,
upscale=2,
arch=arch,
channel_multiplier=2,
bg_upsampler=bg_upsampler
)
Load and Restore an Image
Define a helper function to download images from URLs:
def download_image(url):
response = requests.get(url)
image_array = np.frombuffer(response.content, dtype=np.uint8)
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
return image
To load an image from a URL:
image = download_image('https://example.com/image.jpg')
To run the GFPGAN restoration:
cropped_faces, restored_faces, restored_img = restorer.enhance(image)
Here’s what the outputs mean:
- cropped_faces: Faces cropped from the original image.
- restored_faces: Restored faces.
- restored_img: The full image with restored faces.
View the Results
Define a function to display images within Jupyter:
def cv2_imshow(image):
_, encoded_image = cv2.imencode('.jpg', image)
display(Image(data=encoded_image.tobytes()))
To view faces or the final image, use the appropriate index `i` (0 for first face, 1 for second, etc.):
cv2_imshow(cropped_faces[i]) # Original cropped face
cv2_imshow(restored_faces[i]) # Restored face
cv2_imshow(restored_img) # Full restored image
Save the Restored Images
To save the output image:
output_directory = "/home/user/output_images/"
output_filename = "restored_image.jpg"
output_filepath = os.path.join(output_directory, output_filename)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
cv2.imwrite(output_filepath, restored_img)
We can download our saved images using SFTP:
sftp user@YOUR_SERVER_IP
sftp> cd output_images
sftp> get restored_image.jpg
sftp> exit
Monitor GPU Memory Usage
Check the GPU usage inside Jupyter to ensure resources are properly allocated:
!nvidia-smi
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
GFPGAN is a cutting-edge tool that brings remarkable improvements to face restoration tasks, whether you’re dealing with old photos, low-resolution images, or other imperfections. By deploying it on a cloud GPU server and running it through Jupyter Notebook, we can harness its full potential with ease.
In brief, our Support Experts took us through AI Face Restoration with GFPGAN on Vultr Cloud.
0 Comments