File size: 8,897 Bytes
e34aada
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yerfor/Real3DPortrait/blob/main/real3dportrait_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QS04K9oO21AW"
      },
      "source": [
        "Check GPU"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1ESQRDb-yVUG"
      },
      "outputs": [],
      "source": [
        "!nvidia-smi --query-gpu=name,memory.total,memory.free --format=csv,noheader"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "y-ctmIvu3Ei8"
      },
      "source": [
        "Installation"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gXu76wdDgaxo"
      },
      "outputs": [],
      "source": [
        "# install pytorch3d, about 15s\n",
        "import os\n",
        "import sys\n",
        "import torch\n",
        "need_pytorch3d=False\n",
        "try:\n",
        "    import pytorch3d\n",
        "except ModuleNotFoundError:\n",
        "    need_pytorch3d=True\n",
        "if need_pytorch3d:\n",
        "    if torch.__version__.startswith(\"2.1.\") and sys.platform.startswith(\"linux\"):\n",
        "        # We try to install PyTorch3D via a released wheel.\n",
        "        pyt_version_str=torch.__version__.split(\"+\")[0].replace(\".\", \"\")\n",
        "        version_str=\"\".join([\n",
        "            f\"py3{sys.version_info.minor}_cu\",\n",
        "            torch.version.cuda.replace(\".\",\"\"),\n",
        "            f\"_pyt{pyt_version_str}\"\n",
        "        ])\n",
        "        !pip install fvcore iopath\n",
        "        !pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html\n",
        "    else:\n",
        "        # We try to install PyTorch3D from source.\n",
        "        !pip install 'git+https://github.com/facebookresearch/pytorch3d.git@stable'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "DuUynxmotG_-"
      },
      "outputs": [],
      "source": [
        "# install dependencies, about 5~10 min\n",
        "!pip install tensorboard==2.13.0 tensorboardX==2.6.1\n",
        "!pip install pyspy==0.1.1\n",
        "!pip install protobuf==3.20.3\n",
        "!pip install scipy==1.9.1\n",
        "!pip install kornia==0.5.0\n",
        "!pip install trimesh==3.22.0\n",
        "!pip install einops==0.6.1 torchshow==0.5.1\n",
        "!pip install imageio==2.31.1 imageio-ffmpeg==0.4.8\n",
        "!pip install scikit-learn==1.3.0 scikit-image==0.21.0\n",
        "!pip install av==10.0.0 lpips==0.1.4\n",
        "!pip install timm==0.9.2 librosa==0.9.2\n",
        "!pip install openmim==0.3.9\n",
        "!mim install mmcv==2.1.0 # use mim to speed up installation for mmcv\n",
        "!pip install transformers==4.33.2\n",
        "!pip install pretrainedmodels==0.7.4\n",
        "!pip install ninja==1.11.1\n",
        "!pip install faiss-cpu==1.7.4\n",
        "!pip install praat-parselmouth==0.4.3 moviepy==1.0.3\n",
        "!pip install mediapipe==0.10.7\n",
        "!pip install --upgrade attr\n",
        "!pip install beartype==0.16.4 gateloop_transformer==0.4.0\n",
        "!pip install torchode==0.2.0 torchdiffeq==0.2.3\n",
        "!pip install hydra-core==1.3.2 pandas==2.1.3\n",
        "!pip install pytorch_lightning==2.1.2\n",
        "!pip install httpx==0.23.3\n",
        "!pip install gradio==4.16.0\n",
        "!pip install gdown\n",
        "!pip install pyloudnorm webrtcvad pyworld==0.2.1rc0 pypinyin==0.42.0"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "0GLEV0HVu8rj"
      },
      "outputs": [],
      "source": [
        "# RESTART kernel to make sure runtime is correct if you meet runtime errors\n",
        "# import os\n",
        "# os.kill(os.getpid(), 9)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5UfKHKrH6kcq"
      },
      "source": [
        "Clone code and download checkpoints"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-gfRsd9DwIgl"
      },
      "outputs": [],
      "source": [
        "# clone Real3DPortrait repo from github\n",
        "!git clone https://github.com/yerfor/Real3DPortrait\n",
        "%cd Real3DPortrait"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Yju8dQY7x5OS"
      },
      "outputs": [],
      "source": [
        "# download pretrained ckpts & third-party ckpts from google drive, about 1 min\n",
        "!pip install --upgrade --no-cache-dir gdown\n",
        "%cd deep_3drecon/BFM\n",
        "!gdown https://drive.google.com/uc?id=1SPM3IHsyNAaVMwqZZGV6QVaV7I2Hly0v\n",
        "!gdown https://drive.google.com/uc?id=1MSldX9UChKEb3AXLVTPzZQcsbGD4VmGF\n",
        "!gdown https://drive.google.com/uc?id=180ciTvm16peWrcpl4DOekT9eUQ-lJfMU\n",
        "!gdown https://drive.google.com/uc?id=1KX9MyGueFB3M-X0Ss152x_johyTXHTfU\n",
        "!gdown https://drive.google.com/uc?id=19-NyZn_I0_mkF-F5GPyFMwQJ_-WecZIL\n",
        "!gdown https://drive.google.com/uc?id=11ouQ7Wr2I-JKStp2Fd1afedmWeuifhof\n",
        "!gdown https://drive.google.com/uc?id=18ICIvQoKX-7feYWP61RbpppzDuYTptCq\n",
        "!gdown https://drive.google.com/uc?id=1VktuY46m0v_n_d4nvOupauJkK4LF6mHE\n",
        "%cd ../..\n",
        "\n",
        "%cd checkpoints\n",
        "!gdown https://drive.google.com/uc?id=1gz8A6xestHp__GbZT5qozb43YaybRJhZ\n",
        "!gdown https://drive.google.com/uc?id=1gSUIw2AkkKnlLJnNfS2FCqtaVw9tw3QF\n",
        "!unzip 240210_real3dportrait_orig.zip\n",
        "!unzip pretrained_ckpts.zip\n",
        "!ls\n",
        "%cd ..\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LHzLro206pnA"
      },
      "source": [
        "Inference sample"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "!python inference/real3d_infer.py -h"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2aCDwxNivQoS"
      },
      "outputs": [],
      "source": [
        "# sample inference, about 3 min\n",
        "!python inference/real3d_infer.py \\\n",
        "--src_img data/raw/examples/Macron.png \\\n",
        "--drv_aud data/raw/examples/Obama_5s.wav \\\n",
        "--drv_pose data/raw/examples/May_5s.mp4 \\\n",
        "--bg_img data/raw/examples/bg.png \\\n",
        "--out_name output.mp4 \\\n",
        "--out_mode concat_debug \\\n",
        "--low_memory_usage"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "XL0c54l19mBG"
      },
      "source": [
        "Display output video"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "6olmWwZP9Icj"
      },
      "outputs": [],
      "source": [
        "# borrow code from makeittalk\n",
        "from IPython.display import HTML\n",
        "from base64 import b64encode\n",
        "import os, sys\n",
        "import glob\n",
        "\n",
        "mp4_name = './output.mp4'\n",
        "\n",
        "mp4 = open('{}'.format(mp4_name),'rb').read()\n",
        "data_url = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\n",
        "\n",
        "print('Display animation: {}'.format(mp4_name), file=sys.stderr)\n",
        "display(HTML(\"\"\"\n",
        "  <video width=256 controls>\n",
        "        <source src=\"%s\" type=\"video/mp4\">\n",
        "  </video>\n",
        "  \"\"\" % data_url))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "WebUI"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "\n",
        "!python inference/app_real3dportrait.py --share"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "authorship_tag": "ABX9TyPu++zOlOS4yKF4xn4FHGtZ",
      "gpuType": "T4",
      "include_colab_link": true,
      "private_outputs": true,
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}