Using Git Repos
Creating predictors that pull code from git repos.
Depending on the complexity of your predictor, you might often find yourself needing to pull code from an existing Git repository. In this guide, we'll run the GFPGAN ↗ face restoration model from Tencent ARC.
Cloning the Repository
We'll largely be following the GPFGAN readme, with minimal additions. First, create a repos.ipynb
notebook and add the following code cell to clone the repo:
# Clone GFPGAN
!git clone https://github.com/TencentARC/GFPGAN.git
# Enter the GFPGAN directory
%cd GFPGAN
git
in predictors, so you don't have to. %cd
magic command instead of the !cd
system command. Cloning Private Repos
To clone a private repository, you will need to specify your Git credentials. First, head over to GitHub to generate a personal access token ↗ (PAT):
Next, open a terminal and run the following command to create a Function secret with the PAT you just created:
# Create a global secret for our GitHub PAT
fxn env create GITHUB_PAT <Your GitHub PAT>
GITHUB_PAT
secret when creating the predictor. With this, you can pull from private repos in your predictor notebook:
# Clone a private GitHub repo
!git clone https://username:$GITHUB_PAT@github.com/TencentARC/GFPGAN.git
username
with your GitHub username. Installing Dependencies
The GPFGAN repository readme lists a set of dependencies that must be installed. Add a code cell and paste the following code:
# Install basicsr - https://github.com/xinntao/BasicSR
%pip install basicsr
# Install facexlib - https://github.com/xinntao/facexlib
%pip install facexlib
# Install OpenCV
%pip install opencv-python-headless
# Install other requirements from the repo
%pip install -r requirements.txt
# Install GPFGAN as a python package
!python setup.py develop
# Install Real-ESRGAN
%pip install realesrgan
Next, let's download the pre-trained model. Paste the following in a new code cell:
# Download the pretrained model
!wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P experiments/pretrained_models
wget
in predictors, so you don't have to. Implementing the Predictor
We will use the inference_gfpgan
↗ script from the repo directly. Add a new code cell and paste the following:
from pathlib import Path
from PIL import Image
import sys
from tempfile import mkstemp, TemporaryDirectory
# This script will be available after cloning the repo
from inference_gfpgan import main
def predict (image: Image.Image):
# Write input image to file
_, image_path = mkstemp(suffix=".jpg")
image.save(image_path)
# Run GPFGAN
with TemporaryDirectory() as result_dir:
# Run the inference script directly
command = f"python inference_gfpgan.py -i {image_path} -o {result_dir} -v 1.3 -s 2"
sys.argv = command.split(" ")[1:]
main()
# Load the result image
image_name = Path(image_path).name
result_path = Path(result_dir) / "restored_imgs" / image_name'
result = Image.open(result_path)
result.load()
# Return the image
return result
Creating the Predictor
Now, let's provision the predictor on Function. We will be running our predictor on an A40
GPU. Open a terminal and run the following command:
# Create the predictor on Function
fxn create @username/repos repos.ipynb --acceleration A40
username
with your Function username. Making a Prediction
Once the predictor is active, test it out on this headshot of 21 Savage:
Open a terminal and run the following command:
# Make a prediction
fxn predict @username/repos --image @savage.jpg
username
with your Function username.