0%

本地安装faceswap与一键使用google的colab搭建faceswap两种方式

0. 摘要

  • 因为本机安装过anaconda,python,tensorflow等环境,所以直接按照版本安装部分依赖即可。

  • 后来发现谷歌的colab有免费的gpu可以白嫖,并且colab内置了tensorflow和图形计算框架,所以省去了搭建环境的烦恼。可以直接使用,也很方便。弊端就是不氪金每天能分配的gpu资源不定并且容易断联,session断开需要重新配置,不过图一乐还是可以的。

1. 本地手动安装

全程安装按照官方指南,见 https://github.com/deepfakes/faceswap/blob/master/INSTALL.md。

2. 问题记录

2.1 问题1

  • 问题:import win32console # pylint: disable=import-errorImportError: DLL load failed while importing win32console: 找不到指定的模块

  • 解决:进入“Anaconda3\Scripts”下找到“pywin32_postinstall.py”文件,并执行“python3 pywin32_postinstall.py -install”

2.2 问题2

  • 问题:在使用pip一键安装requirements_base.txt及requirements_nvidia.txt中的依赖的时候速度慢,并且切换到anaconda的新建虚拟环境中windows下无法执行其中的git命令
  • 解决:速度慢修改anaconda的镜像。git无法访问:手动下载git包并将“requirements_base”中的git相关命令注释掉(git慢的话修改git的代理端口)

2.3 问题3

  • 问题:File “D:\faceswap\lib\gui\menu.py”, line 302, in _get_branchesretcode, stdout.decode(‘utf-8’).strip().replace(“\n”, “ - “)) UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb2 in position 6: invalid start byte
  • 解决: 查看源码发现该句只是写入日志的内容,直接注掉,成功运行

2.4 问题4

  • 问题:一直无法启动gpu进行运算,猜想是缺少插件CUDA toolkit(英伟达计算架构框架)和cudnn(英伟达架构算法)

  • 证明猜想:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #测试tfGPU是否能够使用:
    import tensorflow as tf
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    print('GPU', tf.test.is_gpu_available())
    a = tf.constant(2.0)
    b = tf.constant(4.0)
    print(a + b)

    #结果为如下表示可以使用gpu
    GPU True
    tf.Tensor(6.0, shape=(), dtype=float32)

    #但是报错
    “Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found”等dll文件不存在

    #需要安装CUDA和cudnn
    CUDA:https://developer.nvidia.com/cuda-downloads
    CUDNN: https://developer.nvidia.com/cudnn

2.4.1 安装cuda和cudnn后查看gpu是否启动报错

1
2
3
4
5
问题:Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found

解决方法:Copy <installpath>\cuda\bin\cudnn64_7.dll to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin.
Copy <installpath>\cuda\ include\cudnn.h to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include.
Copy <installpath>\cuda\lib\x64\cudnn.lib to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\lib\x64

3. colab安装

参考:https://github.com/RakaMaru/Faceswap_Google_Colab_Master/blob/master/Faceswap_Google_Colab_Master.ipynb,因为colab的python版本问题又不想修改python版本,进行了部分修改。以下均在colab的笔记本中按顺序执行(前置需要配置一些文件的目录参考上述链接)

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
#@title Use this to check the assigned GPU
!cat /etc/os-release

def install_dependencies():
!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi;
!pip install gputil;
!pip install psutil;
!pip install humanize;%%capture

def printm():
GPUs = GPU.getGPUs()

if len(GPUs) == 0:
print("No GPU available.")
return

gpu = GPUs[0]
process = psutil.Process(os.getpid())
print("Gen RAM Free: " + humanize.naturalsize( psutil.virtual_memory().available ), " | Proc size: " + humanize.naturalsize( process.memory_info().rss))
print("GPU RAM Free: {0:.0f}MB | Used: {1:.0f}MB | Util {2:3.0f}% | Total {3:.0f}MB".format(gpu.memoryFree, gpu.memoryUsed, gpu.memoryUtil*100, gpu.memoryTotal))

from IPython.utils import io
from google.colab import drive
import psutil
import humanize
import os

with io.capture_output() as captured:
install_dependencies()
print("Dependencies installed.")

import GPUtil as GPU
printm()
1
2
3
4
5
6
7
#@title Set Time Zone
!rm /etc/localtime
!ln -s /usr/share/zoneinfo/HST /etc/localtime
!date

#above is for HST, you can find yours in
#/usr/share/zoneinfo
1
! rm -rf ./drive
1
2
3
4
#@title Mount Google Drive

from google.colab import drive
drive.mount('/content/drive', force_remount=True)
1
2
3
4
#@title Cleanup folders if needed

!rm -r face_a
!rm -r face_b
1
2
3
4
5
6
7
8
9
#@title Download training data
!cp "/content/drive/My Drive/colab_files/faceswap/faces/face_a.zip" .
!cp "/content/drive/My Drive/colab_files/faceswap/faces/face_b.zip" .

!unzip face_a.zip -d face_a
!unzip face_b.zip -d face_b

!rm face_a.zip
!rm face_b.zip
1
2
#@title Grab the latest Faceswap
!git clone --single-branch --branch r1.0 https://github.com/deepfakes/faceswap.git
1
2
3
4
#@title Copy configuration files
!cp "/content/drive/My Drive/colab_files/faceswap/config/train.ini" faceswap/config/
!ls -lA faceswap/config/
!cat faceswap/config/train.ini
1
2
3
#@title 删除自带的tensorflow2.3安装1.15.0
! pip install tensorflow==1.15.0
! pip install gast==0.2.2
1
2
3
#@title Install Tensorflow
!pip install -r faceswap/requirements_nvidia.txt
#@ 这里会报错“ERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.” 但没关系
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
#@title 开始训练,参数根据需要修改
num_iterations = "100000"
save_every = "360"
save_model_every = "25000"
batch_num = "8"
num_gpus = "1"

trainer_type = "dlight"

model_dir = "/content/drive/My Drive/colab_files/faceswap/models/model"
alignments_file_a = "face_a/alignments.fsa"
alignments_file_b = "face_b/alignments.fsa"
timelapse_dir = "/content/drive/My Drive/colab_files/faceswap/output/timelapse"
#@title set variables end

!python3 faceswap/faceswap.py train \
-A './face_a/faceA' -ala '{alignments_file_a}' \
-B './face_b/faceB' -alb '{alignments_file_b}' \
-m '{model_dir}' \
-t '{trainer_type}' \
-bs '{batch_num}' \
-it '{num_iterations}' \
-g '{num_gpus}' \
-s '{save_every}' \
-ss '{save_model_every}' \
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
#@title 查看结果:
Setting Faceswap backend to NVIDIA
09/26/2020 16:14:02 INFO Log level set to: INFO
Using TensorFlow backend.
09/26/2020 16:14:04 INFO Model A Directory: /content/face_a/faceA
09/26/2020 16:14:04 INFO Model B Directory: /content/face_b/faceB
09/26/2020 16:14:04 INFO Training data directory: /content/drive/My Drive/colab_files/faceswap/models/model
09/26/2020 16:14:04 INFO ===================================================
09/26/2020 16:14:04 INFO Starting
09/26/2020 16:14:04 INFO Press 'ENTER' to save and quit
09/26/2020 16:14:04 INFO Press 'S' to save model weights immediately
09/26/2020 16:14:04 INFO ===================================================
09/26/2020 16:14:05 INFO Loading data, this may take a while...
09/26/2020 16:14:05 INFO Loading Model from Dlight plugin...
09/26/2020 16:14:05 INFO Using configuration saved in state file
09/26/2020 16:14:10 INFO Loaded model from disk: '/content/drive/My Drive/colab_files/faceswap/models/model'
09/26/2020 16:14:10 INFO Loading Trainer from Original plugin...
09/26/2020 16:14:12 INFO Enabled TensorBoard Logging
[16:14:27] [#04386] Loss A: 0.05719, Loss B: 0.05088
09/26/2020 16:14:32 INFO [Saved models] - Average since last save: face_loss_A: 0.05719, face_loss_B: 0.05088
[16:19:42] [#04746] Loss A: 0.04279, Loss B: 0.03795
09/26/2020 16:19:46 INFO [Saved models] - Average since last save: face_loss_A: 0.04535, face_loss_B: 0.04580
[16:25:01] [#05106] Loss A: 0.04427, Loss B: 0.04492
09/26/2020 16:25:06 INFO [Saved models] - Average since last save: face_loss_A: 0.04406, face_loss_B: 0.04483
[16:25:19] [#05120] Loss A: 0.04438, Loss B: 0.03987

3.1 总结

根据官方指导上面的损失函数lossA和lossB分别训练到0.02和0.01就基本差不多了,最后把训练的模型下载到本地上,然后跑一下就行了。虽然是在云端训练的,但是本地似乎还是需要安装相关的环境,因为模型下载下来了本地使用还是需要环境。

云端的好处就是不用占用本机的内存什么的,还有gpu用方便很多速度也蛮快的。