Skip to content

Instantly share code, notes, and snippets.

@rebroad
Last active March 6, 2023 22:52
Show Gist options
  • Save rebroad/145e6942550961612a4292d17839ac13 to your computer and use it in GitHub Desktop.
Save rebroad/145e6942550961612a4292d17839ac13 to your computer and use it in GitHub Desktop.
codeformer_inference.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/rebroad/145e6942550961612a4292d17839ac13/codeformer_inference.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZjdQE0kKcqjA"
},
"source": [
"<p align=\"center\">\n",
" <img src=\"https://user-images.githubusercontent.com/14334509/179359809-bd45566a-486d-418f-83fa-67bbbba8c45c.png\" height=120>\n",
"</p>\n",
"\n",
"# CodeFormer Inference Demo \n",
"## Towards Robust Blind Face Restoration with Codebook Lookup Transformer (NeurIPS 2022)\n",
"Shangchen Zhou, Kelvin C.K. Chan, Chongyi Li, Chen Change Loy\n",
"\n",
"[![GitHub Stars](https://img.shields.io/github/stars/sczhou/CodeFormer?style=social)](https://github.com/sczhou/CodeFormer) [![arXiv](https://img.shields.io/badge/arXiv-Paper-<COLOR>.svg)](https://arxiv.org/abs/2206.11253) [![Hugging Face](https://img.shields.io/badge/Demo-%F0%9F%A4%97%20Hugging%20Face-blue)](https://huggingface.co/spaces/sczhou/CodeFormer) ![visitors](https://visitor-badge.glitch.me/badge?page_id=sczhou/CodeFormer)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_U5Bu-qie_aH"
},
"source": [
"# 1. Preparations\n",
"Before start, make sure that you choose\n",
"* Hardware Accelerator = GPU (in the Runtime menu -> Change runtime type)\n",
"\n",
"Then, we clone the repository, set up the envrironment, and download the pre-trained model."
]
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OOLUtheyjj-T",
"outputId": "3b94b7e0-63be-4c68-89c0-97ebfedb0e62"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/drive\n"
]
}
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "8SG9AcLQO_FQ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f4c5a6d8-9ff5-4739-e44f-7697d7c8b89a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"/content\n",
"Cloning into 'CodeFormer'...\n",
"remote: Enumerating objects: 478, done.\u001b[K\n",
"remote: Counting objects: 100% (83/83), done.\u001b[K\n",
"remote: Compressing objects: 100% (49/49), done.\u001b[K\n",
"remote: Total 478 (delta 42), reused 62 (delta 32), pack-reused 395\u001b[K\n",
"Receiving objects: 100% (478/478), 12.63 MiB | 24.17 MiB/s, done.\n",
"Resolving deltas: 100% (202/202), done.\n",
"/content/CodeFormer\n",
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Collecting addict\n",
" Downloading addict-2.4.0-py3-none-any.whl (3.8 kB)\n",
"Requirement already satisfied: future in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 2)) (0.16.0)\n",
"Requirement already satisfied: lmdb in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 3)) (0.99)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 4)) (1.22.4)\n",
"Requirement already satisfied: opencv-python in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 5)) (4.6.0.66)\n",
"Requirement already satisfied: Pillow in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 6)) (8.4.0)\n",
"Requirement already satisfied: pyyaml in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 7)) (6.0)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 8)) (2.25.1)\n",
"Requirement already satisfied: scikit-image in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 9)) (0.19.3)\n",
"Requirement already satisfied: scipy in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 10)) (1.10.1)\n",
"Collecting tb-nightly\n",
" Downloading tb_nightly-2.13.0a20230306-py3-none-any.whl (5.6 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.6/5.6 MB\u001b[0m \u001b[31m29.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: torch>=1.7.1 in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 12)) (1.13.1+cu116)\n",
"Requirement already satisfied: torchvision in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 13)) (0.14.1+cu116)\n",
"Requirement already satisfied: tqdm in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 14)) (4.64.1)\n",
"Collecting yapf\n",
" Downloading yapf-0.32.0-py2.py3-none-any.whl (190 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m190.2/190.2 KB\u001b[0m \u001b[31m14.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting lpips\n",
" Downloading lpips-0.1.4-py3-none-any.whl (53 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m53.8/53.8 KB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: gdown in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 17)) (4.4.0)\n",
"Requirement already satisfied: chardet<5,>=3.0.2 in /usr/local/lib/python3.8/dist-packages (from requests->-r requirements.txt (line 8)) (4.0.0)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.8/dist-packages (from requests->-r requirements.txt (line 8)) (1.26.14)\n",
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.8/dist-packages (from requests->-r requirements.txt (line 8)) (2.10)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.8/dist-packages (from requests->-r requirements.txt (line 8)) (2022.12.7)\n",
"Requirement already satisfied: networkx>=2.2 in /usr/local/lib/python3.8/dist-packages (from scikit-image->-r requirements.txt (line 9)) (3.0)\n",
"Requirement already satisfied: imageio>=2.4.1 in /usr/local/lib/python3.8/dist-packages (from scikit-image->-r requirements.txt (line 9)) (2.9.0)\n",
"Requirement already satisfied: PyWavelets>=1.1.1 in /usr/local/lib/python3.8/dist-packages (from scikit-image->-r requirements.txt (line 9)) (1.4.1)\n",
"Requirement already satisfied: tifffile>=2019.7.26 in /usr/local/lib/python3.8/dist-packages (from scikit-image->-r requirements.txt (line 9)) (2023.2.27)\n",
"Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.8/dist-packages (from scikit-image->-r requirements.txt (line 9)) (23.0)\n",
"Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (1.8.1)\n",
"Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (2.2.3)\n",
"Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (0.4.6)\n",
"Requirement already satisfied: grpcio>=1.48.2 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (1.51.3)\n",
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (3.4.1)\n",
"Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (2.16.1)\n",
"Requirement already satisfied: protobuf>=3.19.6 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (3.19.6)\n",
"Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (57.4.0)\n",
"Collecting tensorboard-data-server<0.8.0,>=0.7.0\n",
" Downloading tensorboard_data_server-0.7.0-py3-none-manylinux2014_x86_64.whl (6.6 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.6/6.6 MB\u001b[0m \u001b[31m26.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (0.38.4)\n",
"Requirement already satisfied: absl-py>=0.4 in /usr/local/lib/python3.8/dist-packages (from tb-nightly->-r requirements.txt (line 11)) (1.4.0)\n",
"Requirement already satisfied: typing-extensions in /usr/local/lib/python3.8/dist-packages (from torch>=1.7.1->-r requirements.txt (line 12)) (4.5.0)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.8/dist-packages (from gdown->-r requirements.txt (line 17)) (3.9.0)\n",
"Requirement already satisfied: six in /usr/local/lib/python3.8/dist-packages (from gdown->-r requirements.txt (line 17)) (1.15.0)\n",
"Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.8/dist-packages (from gdown->-r requirements.txt (line 17)) (4.6.3)\n",
"Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.8/dist-packages (from google-auth<3,>=1.6.3->tb-nightly->-r requirements.txt (line 11)) (4.9)\n",
"Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.8/dist-packages (from google-auth<3,>=1.6.3->tb-nightly->-r requirements.txt (line 11)) (5.3.0)\n",
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.8/dist-packages (from google-auth<3,>=1.6.3->tb-nightly->-r requirements.txt (line 11)) (0.2.8)\n",
"Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.8/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tb-nightly->-r requirements.txt (line 11)) (1.3.1)\n",
"Requirement already satisfied: importlib-metadata>=4.4 in /usr/local/lib/python3.8/dist-packages (from markdown>=2.6.8->tb-nightly->-r requirements.txt (line 11)) (6.0.0)\n",
"Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.8/dist-packages (from werkzeug>=1.0.1->tb-nightly->-r requirements.txt (line 11)) (2.1.2)\n",
"Requirement already satisfied: PySocks!=1.5.7,>=1.5.6 in /usr/local/lib/python3.8/dist-packages (from requests->-r requirements.txt (line 8)) (1.7.1)\n",
"Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.8/dist-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tb-nightly->-r requirements.txt (line 11)) (3.15.0)\n",
"Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.8/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tb-nightly->-r requirements.txt (line 11)) (0.4.8)\n",
"Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.8/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tb-nightly->-r requirements.txt (line 11)) (3.2.2)\n",
"Installing collected packages: yapf, addict, tensorboard-data-server, lpips, tb-nightly\n",
" Attempting uninstall: tensorboard-data-server\n",
" Found existing installation: tensorboard-data-server 0.6.1\n",
" Uninstalling tensorboard-data-server-0.6.1:\n",
" Successfully uninstalled tensorboard-data-server-0.6.1\n",
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
"tensorboard 2.11.2 requires tensorboard-data-server<0.7.0,>=0.6.0, but you have tensorboard-data-server 0.7.0 which is incompatible.\u001b[0m\u001b[31m\n",
"\u001b[0mSuccessfully installed addict-2.4.0 lpips-0.1.4 tb-nightly-2.13.0a20230306 tensorboard-data-server-0.7.0 yapf-0.32.0\n",
"running develop\n",
"running egg_info\n",
"creating basicsr.egg-info\n",
"writing basicsr.egg-info/PKG-INFO\n",
"writing dependency_links to basicsr.egg-info/dependency_links.txt\n",
"writing requirements to basicsr.egg-info/requires.txt\n",
"writing top-level names to basicsr.egg-info/top_level.txt\n",
"writing manifest file 'basicsr.egg-info/SOURCES.txt'\n",
"reading manifest file 'basicsr.egg-info/SOURCES.txt'\n",
"adding license file 'LICENSE'\n",
"writing manifest file 'basicsr.egg-info/SOURCES.txt'\n",
"/usr/local/lib/python3.8/dist-packages/torch/utils/cpp_extension.py:476: UserWarning: Attempted to use ninja as the BuildExtension backend but we could not find ninja.. Falling back to using the slow distutils backend.\n",
" warnings.warn(msg.format('we could not find ninja.'))\n",
"running build_ext\n",
"Creating /usr/local/lib/python3.8/dist-packages/basicsr.egg-link (link to .)\n",
"Adding basicsr 1.3.2 to easy-install.pth file\n",
"\n",
"Installed /content/CodeFormer\n",
"Processing dependencies for basicsr==1.3.2\n",
"Searching for gdown==4.4.0\n",
"Best match: gdown 4.4.0\n",
"Adding gdown 4.4.0 to easy-install.pth file\n",
"Installing gdown script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for lpips==0.1.4\n",
"Best match: lpips 0.1.4\n",
"Adding lpips 0.1.4 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for yapf==0.32.0\n",
"Best match: yapf 0.32.0\n",
"Adding yapf 0.32.0 to easy-install.pth file\n",
"Installing yapf script to /usr/local/bin\n",
"Installing yapf-diff script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for tqdm==4.64.1\n",
"Best match: tqdm 4.64.1\n",
"Adding tqdm 4.64.1 to easy-install.pth file\n",
"Installing tqdm script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for torchvision==0.14.1+cu116\n",
"Best match: torchvision 0.14.1+cu116\n",
"Adding torchvision 0.14.1+cu116 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for torch==1.13.1+cu116\n",
"Best match: torch 1.13.1+cu116\n",
"Adding torch 1.13.1+cu116 to easy-install.pth file\n",
"Installing convert-caffe2-to-onnx script to /usr/local/bin\n",
"Installing convert-onnx-to-caffe2 script to /usr/local/bin\n",
"Installing torchrun script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for tb-nightly==2.13.0a20230306\n",
"Best match: tb-nightly 2.13.0a20230306\n",
"Adding tb-nightly 2.13.0a20230306 to easy-install.pth file\n",
"Installing tensorboard script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for scipy==1.10.1\n",
"Best match: scipy 1.10.1\n",
"Adding scipy 1.10.1 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for scikit-image==0.19.3\n",
"Best match: scikit-image 0.19.3\n",
"Adding scikit-image 0.19.3 to easy-install.pth file\n",
"Installing skivi script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for requests==2.25.1\n",
"Best match: requests 2.25.1\n",
"Adding requests 2.25.1 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for PyYAML==6.0\n",
"Best match: PyYAML 6.0\n",
"Adding PyYAML 6.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for Pillow==8.4.0\n",
"Best match: Pillow 8.4.0\n",
"Adding Pillow 8.4.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for opencv-python==4.6.0.66\n",
"Best match: opencv-python 4.6.0.66\n",
"Adding opencv-python 4.6.0.66 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for numpy==1.22.4\n",
"Best match: numpy 1.22.4\n",
"Adding numpy 1.22.4 to easy-install.pth file\n",
"Installing f2py script to /usr/local/bin\n",
"Installing f2py3 script to /usr/local/bin\n",
"Installing f2py3.8 script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for lmdb==0.99\n",
"Best match: lmdb 0.99\n",
"Adding lmdb 0.99 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for future==0.16.0\n",
"Best match: future 0.16.0\n",
"Adding future 0.16.0 to easy-install.pth file\n",
"Installing futurize script to /usr/local/bin\n",
"Installing pasteurize script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for addict==2.4.0\n",
"Best match: addict 2.4.0\n",
"Adding addict 2.4.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for beautifulsoup4==4.6.3\n",
"Best match: beautifulsoup4 4.6.3\n",
"Adding beautifulsoup4 4.6.3 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for six==1.15.0\n",
"Best match: six 1.15.0\n",
"Adding six 1.15.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for filelock==3.9.0\n",
"Best match: filelock 3.9.0\n",
"Adding filelock 3.9.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for typing-extensions==4.5.0\n",
"Best match: typing-extensions 4.5.0\n",
"Adding typing-extensions 4.5.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for Markdown==3.4.1\n",
"Best match: Markdown 3.4.1\n",
"Adding Markdown 3.4.1 to easy-install.pth file\n",
"Installing markdown_py script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for google-auth-oauthlib==0.4.6\n",
"Best match: google-auth-oauthlib 0.4.6\n",
"Adding google-auth-oauthlib 0.4.6 to easy-install.pth file\n",
"Installing google-oauthlib-tool script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for google-auth==2.16.1\n",
"Best match: google-auth 2.16.1\n",
"Adding google-auth 2.16.1 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for tensorboard-plugin-wit==1.8.1\n",
"Best match: tensorboard-plugin-wit 1.8.1\n",
"Adding tensorboard-plugin-wit 1.8.1 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for protobuf==3.19.6\n",
"Best match: protobuf 3.19.6\n",
"Adding protobuf 3.19.6 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for wheel==0.38.4\n",
"Best match: wheel 0.38.4\n",
"Adding wheel 0.38.4 to easy-install.pth file\n",
"Installing wheel script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for setuptools==57.4.0\n",
"Best match: setuptools 57.4.0\n",
"Adding setuptools 57.4.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for tensorboard-data-server==0.7.0\n",
"Best match: tensorboard-data-server 0.7.0\n",
"Adding tensorboard-data-server 0.7.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for absl-py==1.4.0\n",
"Best match: absl-py 1.4.0\n",
"Adding absl-py 1.4.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for grpcio==1.51.3\n",
"Best match: grpcio 1.51.3\n",
"Adding grpcio 1.51.3 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for Werkzeug==2.2.3\n",
"Best match: Werkzeug 2.2.3\n",
"Adding Werkzeug 2.2.3 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for networkx==3.0\n",
"Best match: networkx 3.0\n",
"Adding networkx 3.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for imageio==2.9.0\n",
"Best match: imageio 2.9.0\n",
"Adding imageio 2.9.0 to easy-install.pth file\n",
"Installing imageio_download_bin script to /usr/local/bin\n",
"Installing imageio_remove_bin script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for PyWavelets==1.4.1\n",
"Best match: PyWavelets 1.4.1\n",
"Adding PyWavelets 1.4.1 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for tifffile==2023.2.27\n",
"Best match: tifffile 2023.2.27\n",
"Adding tifffile 2023.2.27 to easy-install.pth file\n",
"Installing lsm2bin script to /usr/local/bin\n",
"Installing tiff2fsspec script to /usr/local/bin\n",
"Installing tiffcomment script to /usr/local/bin\n",
"Installing tifffile script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for packaging==23.0\n",
"Best match: packaging 23.0\n",
"Adding packaging 23.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for urllib3==1.26.14\n",
"Best match: urllib3 1.26.14\n",
"Adding urllib3 1.26.14 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for certifi==2022.12.7\n",
"Best match: certifi 2022.12.7\n",
"Adding certifi 2022.12.7 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for chardet==4.0.0\n",
"Best match: chardet 4.0.0\n",
"Adding chardet 4.0.0 to easy-install.pth file\n",
"Installing chardetect script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for idna==2.10\n",
"Best match: idna 2.10\n",
"Adding idna 2.10 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for PySocks==1.7.1\n",
"Best match: PySocks 1.7.1\n",
"Adding PySocks 1.7.1 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for importlib-metadata==6.0.0\n",
"Best match: importlib-metadata 6.0.0\n",
"Adding importlib-metadata 6.0.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for requests-oauthlib==1.3.1\n",
"Best match: requests-oauthlib 1.3.1\n",
"Adding requests-oauthlib 1.3.1 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for pyasn1-modules==0.2.8\n",
"Best match: pyasn1-modules 0.2.8\n",
"Adding pyasn1-modules 0.2.8 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for rsa==4.9\n",
"Best match: rsa 4.9\n",
"Adding rsa 4.9 to easy-install.pth file\n",
"Installing pyrsa-decrypt script to /usr/local/bin\n",
"Installing pyrsa-encrypt script to /usr/local/bin\n",
"Installing pyrsa-keygen script to /usr/local/bin\n",
"Installing pyrsa-priv2pub script to /usr/local/bin\n",
"Installing pyrsa-sign script to /usr/local/bin\n",
"Installing pyrsa-verify script to /usr/local/bin\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for cachetools==5.3.0\n",
"Best match: cachetools 5.3.0\n",
"Adding cachetools 5.3.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for MarkupSafe==2.1.2\n",
"Best match: MarkupSafe 2.1.2\n",
"Adding MarkupSafe 2.1.2 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for zipp==3.15.0\n",
"Best match: zipp 3.15.0\n",
"Adding zipp 3.15.0 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for oauthlib==3.2.2\n",
"Best match: oauthlib 3.2.2\n",
"Adding oauthlib 3.2.2 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Searching for pyasn1==0.4.8\n",
"Best match: pyasn1 0.4.8\n",
"Adding pyasn1 0.4.8 to easy-install.pth file\n",
"\n",
"Using /usr/local/lib/python3.8/dist-packages\n",
"Finished processing dependencies for basicsr==1.3.2\n",
"Downloading: \"https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/detection_Resnet50_Final.pth\" to /content/CodeFormer/weights/facelib/detection_Resnet50_Final.pth\n",
"\n",
"100% 104M/104M [00:02<00:00, 38.2MB/s]\n",
"Downloading: \"https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/parsing_parsenet.pth\" to /content/CodeFormer/weights/facelib/parsing_parsenet.pth\n",
"\n",
"100% 81.4M/81.4M [00:03<00:00, 26.8MB/s]\n",
"Downloading: \"https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth\" to /content/CodeFormer/weights/CodeFormer/codeformer.pth\n",
"\n",
"100% 359M/359M [00:01<00:00, 283MB/s]\n"
]
}
],
"source": [
"# Clone CodeFormer and enter the CodeFormer folder\n",
"%cd /content\n",
"!rm -rf CodeFormer\n",
"!git clone https://github.com/sczhou/CodeFormer.git\n",
"%cd CodeFormer\n",
"!mkdir -p inputs/user_upload\n",
"\n",
"# Set up the environment\n",
"# Install python dependencies\n",
"!pip install -r requirements.txt\n",
"# Install basicsr\n",
"!python basicsr/setup.py develop\n",
"\n",
"# Download the pre-trained model\n",
"!python scripts/download_pretrained_models.py facelib\n",
"!python scripts/download_pretrained_models.py CodeFormer\n",
"\n",
"# Visualization function\n",
"import cv2\n",
"import matplotlib.pyplot as plt\n",
"def display(img1, img2):\n",
" fig = plt.figure(figsize=(25, 10))\n",
" ax1 = fig.add_subplot(1, 2, 1) \n",
" plt.title('Input', fontsize=16)\n",
" ax1.axis('off')\n",
" ax2 = fig.add_subplot(1, 2, 2)\n",
" plt.title('CodeFormer', fontsize=16)\n",
" ax2.axis('off')\n",
" ax1.imshow(img1)\n",
" ax2.imshow(img2)\n",
"def imread(img_path):\n",
" img = cv2.imread(img_path)\n",
" img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
" return img"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GzLAguVdix6_"
},
"source": [
"# 2. Test on your images 😀\n",
"\n",
"Upload images (or video) to the inputs/user_upload folder.\n",
"\n",
"* Old photos\n",
"* AI-created face images by DALLE2/Midjourney/Stable Diffusion\n",
"\n",
"If CodeFormer is helpful to your photos, please help star our [repo](https://github.com/sczhou/CodeFormer). Thanks! 🤗 \n",
"\n",
"[![GitHub Stars](https://img.shields.io/github/stars/sczhou/CodeFormer?style=social)](https://github.com/sczhou/CodeFormer)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Cj2YQGg3J0TQ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "64512b2f-4c83-4551-ab5d-42154fde0f45"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Face detection model: retinaface_resnet50\n",
"Background upsampling: True, Face upsampling: True\n",
"[1/1] Processing: 333586288_870463320712318_8725578998261518105_n.jpg\n"
]
}
],
"source": [
"# Inference the uploaded images\n",
"#@markdown `CODEFORMER_FIDELITY`: Balance the quality (lower number) and fidelity (higher number)<br>\n",
"# you can add '--bg_upsampler realesrgan' to enhance the background\n",
"CODEFORMER_FIDELITY = 0.7 #@param {type:\"slider\", min:0, max:1, step:0.01}\n",
"#@markdown `BACKGROUND_ENHANCE`: Enhance background image with Real-ESRGAN<br>\n",
"BACKGROUND_ENHANCE = True #@param {type:\"boolean\"}\n",
"#@markdown `FACE_UPSAMPLE`: Upsample restored faces for high-resolution AI-created images<br>\n",
"FACE_UPSAMPLE = True #@param {type:\"boolean\"}\n",
"if BACKGROUND_ENHANCE:\n",
" if FACE_UPSAMPLE:\n",
" !python inference_codeformer.py -s 4 -w $CODEFORMER_FIDELITY --input_path inputs/user_upload/ --bg_upsampler realesrgan --face_upsample\n",
" else:\n",
" !python inference_codeformer.py -w $CODEFORMER_FIDELITY --input_path inputs/user_upload/ --bg_upsampler realesrgan\n",
"else:\n",
" !python inference_codeformer.py -w $CODEFORMER_FIDELITY --input_path inputs/user_upload/\n"
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "yD7CyILJFekq"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "xW11ACpFFfJt"
},
"outputs": [],
"source": [
"# Inference the uploaded images\n",
"#@markdown `CODEFORMER_FIDELITY`: Balance the quality (lower number) and fidelity (higher number)<br>\n",
"# you can add '--bg_upsampler realesrgan' to enhance the background\n",
"CODEFORMER_FIDELITY = 0.7 #@param {type:\"slider\", min:0, max:1, step:0.01}\n",
"#@markdown `BACKGROUND_ENHANCE`: Enhance background image with Real-ESRGAN<br>\n",
"BACKGROUND_ENHANCE = True #@param {type:\"boolean\"}\n",
"#@markdown `FACE_UPSAMPLE`: Upsample restored faces for high-resolution AI-created images<br>\n",
"FACE_UPSAMPLE = True #@param {type:\"boolean\"}\n",
"if BACKGROUND_ENHANCE:\n",
" if FACE_UPSAMPLE:\n",
" !python inference_codeformer.py -w $CODEFORMER_FIDELITY --input_path inputs/user_upload/6thform.mp4 --bg_upsampler realesrgan --face_upsample\n",
" else:\n",
" !python inference_codeformer.py -w $CODEFORMER_FIDELITY --input_path inputs/user_upload/6thform.mp4 --bg_upsampler realesrgan\n",
"else:\n",
" !python inference_codeformer.py -w $CODEFORMER_FIDELITY --input_path inputs/user_upload/6thform.mp4\n"
]
},
{
"cell_type": "code",
"source": [
"import os\n",
"\n",
"%cd /content/drive/MyDrive/6thform_0.7\n",
"\n",
"# Run the FFmpeg command to create the output.mp4 file\n",
"!ffmpeg -i final_results/%06d.png -i /content/CodeFormer/inputs/user_upload/6thform.mp4 -map 0:v:0 -map 1:a:0 -c:v libx264 -crf 23 -preset veryslow -c:a copy -r $(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate /content/CodeFormer/inputs/user_upload/6thform.mp4 | awk -F/ '{print $1}') -y output.mp4 -vn\n",
"\n",
"#from google.colab import files\n",
"#files.download('output.mp4')"
],
"metadata": {
"id": "-vskKWcSkPAp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!pip install colab-xterm\n",
"%load_ext colabxterm\n",
"\n",
"%xterm"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "whE8HLgaCoBd",
"outputId": "76d35cbe-41db-4eb8-8eb1-37ff1cfbf0b9"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"UsageError: Line magic function `%xterm` not found.\n"
]
}
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"provenance": [],
"include_colab_link": true
},
"gpuClass": "standard",
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment