要先係 Runtime ---> Change Runtime Type 果到設定用 GPU 再 run 以下程式。
References:
# Clone Real-ESRGAN and enter the Real-ESRGAN
!git clone https://github.com/xinntao/Real-ESRGAN.git
%cd Real-ESRGAN
# Set up the environment
!pip install basicsr
!pip install facexlib
!pip install gfpgan
!pip install -r requirements.txt
!python setup.py develop
# Download the pre-trained model
!wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P experiments/pretrained_models
import os
from google.colab import drive
from google.colab import files
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
import time
from PIL import Image
from IPython.display import clear_output
drive.mount('/content/drive', force_remount=False)
path = "/content/drive/MyDrive/photo_upscaling/Input/"
ouput_path = "/content/drive/MyDrive/photo_upscaling/Output/"
photo_input = os.listdir(path)
print(len(photo_input))
i=1
for photo in photo_input:
print("正在處理第%d張相片, 共有%d張相片。"%(i, len(photo_input)))
image_path = os.path.join(path , photo)
!python inference_realesrgan.py -n RealESRGAN_x4plus -i {image_path} -s 4 --half --face_enhance -o {ouput_path}
os.remove(image_path)
clear_output()
i+=1
# if it is out of memory, try to use the `--tile` option
# We upsample the image with the scale factor X3.5
# Arguments
# -n, --model_name: Model names
# -i, --input: input folder or image
# --outscale: Output scale, can be arbitrary scale factore.
# Usage: python inference_realesrgan.py -n RealESRGAN_x4plus -i infile -o outfile [options]...
#A common command: python inference_realesrgan.py -n RealESRGAN_x4plus -i infile --outscale 3.5 --half --face_enhance
# -h show this help
# -i --input Input image or folder. Default: inputs
# -o --output Output folder. Default: results
# -n --model_name Model name. Default: RealESRGAN_x4plus
# -s, --outscale The final upsampling scale of the image. Default: 4
# --suffix Suffix of the restored image. Default: out
# -t, --tile Tile size, 0 for no tile during testing. Default: 0
# --face_enhance Whether to use GFPGAN to enhance face. Default: False
# --half Whether to use half precision during inference. Default: False
# --ext Image extension. Options: auto | jpg | png, auto means using the same extension as inputs. Default: auto