最近对AI换脸比较感兴趣,就看了一下faceswap的使用。
1.下载代码:
https://github.com/deepfakes/faceswap
2.安装python 3.xx版本,之前是2.x不能正常工作
3.运行setup.py
wudi:faceswap xiepengchong$ python setup.py
WARNING Running without root/admin privileges
INFO The tool provides tips for installation
and installs required python packages
INFO Setup in Darwin 17.7.0
INFO Installed Python: 3.5.1 64bit
INFO Encoding: UTF-8
INFO Upgrading pip...
INFO Installed pip: 7.1.2
Enable Docker? [y/N] y
INFO Docker Enabled
Enable CUDA? [Y/n] Y
INFO CUDA Enabled
WARNING Nvidia-Docker is only supported on Linux.
Only CPU is supported in Docker for your system
Enable Docker? [y/N] y
INFO Docker Enabled
WARNING CUDA Disabled
INFO 1. Install Docker
https://www.docker.com/community-edition
2. Build Docker Image For Faceswap
docker build -t deepfakes-cpu -f Dockerfile.cpu .
3. Mount faceswap volume and Run it
# without GUI
docker run -tid -p 8888:8888 \
--hostname deepfakes-cpu --name deepfakes-cpu \
-v /Users/xiepengchong/Code/opensource/faceswap:/srv \
deepfakes-cpu
# with gui. tools.py gui working.
## enable local access to X11 server
xhost +local:
## create container
nvidia-docker run -tid -p 8888:8888 \
--hostname deepfakes-cpu --name deepfakes-cpu \
-v /Users/xiepengchong/Code/opensource/faceswap:/srv \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e DISPLAY=unix$DISPLAY \
-e AUDIO_GID=`getent group audio | cut -d: -f3` \
-e VIDEO_GID=`getent group video | cut -d: -f3` \
-e GID=`id -g` \
-e UID=`id -u` \
deepfakes-cpu
4. Open a new terminal to run faceswap.py in /srv
docker exec -it deepfakes-cpu bash
INFO That's all you need to do with a docker. Have fun.
4.安装成功之后,
wudi:faceswap xxx$ docker build -t deepfakes-cpu -f Dockerfile.cpu .
Sending build context to Docker daemon 4.608kB
Step 1/9 : FROM tensorflow/tensorflow:1.12.0-py3
---> 39bcb324db83
Step 2/9 : RUN apt-get update -qq -y && apt-get install -y libsm6 libxrender1 libxext-dev python3-tk && apt-get clean && rm -rf /var/lib/apt/lists/*
---> Using cache
---> 17f5296331cc
Step 3/9 : COPY requirements.txt /opt/
---> Using cache
---> 116f0d42098f
Step 4/9 : RUN pip3 install --upgrade pip
---> Using cache
---> cce9e0b59cb3
Step 5/9 : RUN pip3 install cmake
---> Using cache
---> 22a904d8a8e6
Step 6/9 : RUN pip3 install dlib
---> Using cache
---> 72988b187cc1
Step 7/9 : RUN pip3 --no-cache-dir install -r /opt/requirements.txt && rm /opt/requirements.txt
---> Using cache
---> 7dd54dcf4d74
Step 8/9 : WORKDIR "/srv"
---> Using cache
---> e3710a07742a
Step 9/9 : CMD ["/bin/bash"]
---> Using cache
---> 0e9ef903e02d
Successfully built 0e9ef903e02d
Successfully tagged deepfakes-cpu:latest
运行
docker run -tid -p 8888:8888 --hostname deepfakes-cpu --name deepfakes-cpu -v /Users/xiepengchong/Code/opensource/faceswap:/srv deepfakes-cpu
执行
docker exec -it deepfakes-cpu bash
root@deepfakes-cpu:/srv# ls
Dockerfile.cpu INSTALL.md README.md before chenglong_dest dest faceswap.log input liudehua models origin2 plugins scripts setup.py tools.py
Dockerfile.gpu LICENSE USAGE.md chenglong config dest2 faceswap.py lib liudehua_dest origin output requirements.txt setup.cfg tools
5.下载图片模型(使用google-images-download)
安装工具
git clone https://github.com/hardikvasa/google-images-download.git
cd google-images-download && sudo python setup.py install
下载图片:
wudi:google-images-download xxx$ googleimagesdownload --keywords "张三" --limit 20
Item no.: 1 --> Item name = 张三
Evaluating...
Starting Download...
Completed Image ====> 1. 1-1q130204600202.png
Completed Image ====> 2. a2f204b0a13c48d3bfc57b5268fcf81e.png
Completed Image ====> 3. bdxprajefqa.jpg
Completed Image ====> 4. wmwgh-rg_400x400.jpeg
Completed Image ====> 5. 1_120523010400_1.gif
Completed Image ====> 6. 1-1f326102k0.png
Completed Image ====> 7. 631287a928a4467e9352a947de59c2a2.png
Completed Image ====> 8. 2.png
Completed Image ====> 9. 1_130530100040_1.gif
Completed Image ====> 10. 24292655aca76ad759a.png
Completed Image ====> 11. 1.jpeg
Completed Image ====> 12. 5-160z6161s0.jpg
Completed Image ====> 13. 57c8f7cd255bd.jpg
Completed Image ====> 14. 5fxu38soc4.jpg
Completed Image ====> 15. 1-15052p31058.jpg
Completed Image ====> 16. 32450758d38363612f1.jpg
Completed Image ====> 17. 362361_1.jpg
Completed Image ====> 18. l3refwww_iphonedict22tjwww_normal_7_0_10cltjcloud_titlehostwww.17jit.png
Completed Image ====> 19. f12c69bf28b7f17c9311b71b2e516a4c.png
Completed Image ====> 20. 1425635c5y430-52156.jpg
Errors: 0
Everything downloaded!
Total time taken: 41.5787889957 Seconds
下面开始进行AI模型训练:
1.将我们找到的图片都裁剪为大头照:
python faceswap.py extract -i ./chenglong/ -o ./chenglong_dest
python faceswap.py extract -i ./liudehua/ -o ./liudehua_dest
2.训练模型(A-輸入目標人物 B-輸出的效果人物 m-生成的模型路徑,经过及其波折的过程训练成功了,查看错误问题3)
python faceswap.py train -A ./dest -B ./dest2 -m ./models/
3.转换图片(由于一直训练模型比较费劲,所以先下载的网上的模型进行尝试)
感谢:https://anonfile.com/p7w3m0d5be/face-swap.zip
python faceswap.py convert -i ./origin/ -o ./output/ -m ./models1/
root@deepfakes-cpu:/srv# python faceswap.py convert -i ./origin/ -o ./output1/ -m ./models1/
04/06/2019 04:23:04 INFO Log level set to: INFO
04/06/2019 04:23:08 INFO Input Directory: /srv/origin
04/06/2019 04:23:08 WARNING No Alignments file found. Extracting on the fly.
04/06/2019 04:23:08 WARNING NB: This will use the inferior dlib-hog for extraction and dlib pose predictor for landmarks. It is recommended to perfom Extract first for superior results
04/06/2019 04:23:08 INFO Loading Detect from Dlib_Hog plugin...
04/06/2019 04:23:08 INFO Loading config: '/srv/config/extract.ini'
04/06/2019 04:23:08 INFO Loading Align from Dlib plugin...
04/06/2019 04:23:08 WARNING No GPU detected. Switching to CPU mode
04/06/2019 04:23:09 INFO Initializing Dlib-HOG Detector...
04/06/2019 04:23:09 INFO Initialized Dlib-HOG Detector...
04/06/2019 04:23:09 INFO Initializing Dlib Pose Predictor...
04/06/2019 04:23:10 INFO Initialized Dlib Pose Predictor.
04/06/2019 04:23:10 INFO Loading Model from Original plugin...
Using TensorFlow backend.
04/06/2019 04:23:17 INFO Loading config: '/srv/config/train.ini'
04/06/2019 04:23:17 WARNING No existing state file found. Generating.
04/06/2019 04:23:17 INFO Updating legacy model name from: 'encoder.h5' to 'original_encoder.h5'
04/06/2019 04:23:17 INFO Updating legacy model name from: 'decoder_A.h5' to 'original_decoder_A.h5'
04/06/2019 04:23:17 INFO Updating legacy model name from: 'decoder_B.h5' to 'original_decoder_B.h5'
04/06/2019 04:23:17 INFO Adding new config item to state file: 'dssim_loss': 'False'
04/06/2019 04:23:17 INFO Adding new config item to state file: 'penalized_mask_loss': 'True'
04/06/2019 04:23:17 INFO Adding new config item to state file: 'icnr_init': 'False'
04/06/2019 04:23:17 INFO Using configuration saved in state file
04/06/2019 04:23:18 INFO Adding model topology to legacy weights file: '/srv/models1/original_decoder_A.h5'
04/06/2019 04:23:18 INFO Adding model topology to legacy weights file: '/srv/models1/original_encoder.h5'
04/06/2019 04:23:22 INFO Adding model topology to legacy weights file: '/srv/models1/original_decoder_B.h5'
04/06/2019 04:23:22 INFO Loaded model from disk: '/srv/models1'
04/06/2019 04:23:22 INFO Loading Convert from Masked plugin...
100%|#########################################################################################################################################################################| 1/1 [00:00<00:00, 1.12it/s]
04/06/2019 04:23:24 INFO -------------------------
04/06/2019 04:23:24 INFO Images found: 1
04/06/2019 04:23:24 INFO Faces detected: 1
04/06/2019 04:23:24 INFO -------------------------
04/06/2019 04:23:24 INFO Process Succesfully Completed. Shutting Down...
root@deepfakes-cpu:/srv# python faceswap.py convert -i ./origin/ -o ./output/ -m ./models1/
04/06/2019 04:27:24 INFO Log level set to: INFO
04/06/2019 04:27:27 INFO Input Directory: /srv/origin
04/06/2019 04:27:27 WARNING No Alignments file found. Extracting on the fly.
04/06/2019 04:27:27 WARNING NB: This will use the inferior dlib-hog for extraction and dlib pose predictor for landmarks. It is recommended to perfom Extract first for superior results
04/06/2019 04:27:27 INFO Loading Detect from Dlib_Hog plugin...
04/06/2019 04:27:27 INFO Loading config: '/srv/config/extract.ini'
04/06/2019 04:27:28 INFO Loading Align from Dlib plugin...
04/06/2019 04:27:28 WARNING No GPU detected. Switching to CPU mode
04/06/2019 04:27:28 INFO Initializing Dlib-HOG Detector...
04/06/2019 04:27:28 INFO Initialized Dlib-HOG Detector...
04/06/2019 04:27:28 INFO Initializing Dlib Pose Predictor...
04/06/2019 04:27:29 INFO Initialized Dlib Pose Predictor.
04/06/2019 04:27:29 INFO Loading Model from Original plugin...
Using TensorFlow backend.
04/06/2019 04:27:35 INFO Loading config: '/srv/config/train.ini'
04/06/2019 04:27:35 INFO Using configuration saved in state file
04/06/2019 04:27:38 INFO Loaded model from disk: '/srv/models1'
04/06/2019 04:27:38 INFO Loading Convert from Masked plugin...
0%| | 0/3 [00:00<?, ?it/s]libpng warning: iCCP: known incorrect sRGB profile
100%|#########################################################################################################################################################################| 3/3 [00:01<00:00, 1.62it/s]
04/06/2019 04:27:40 INFO -------------------------
04/06/2019 04:27:40 INFO Images found: 3
04/06/2019 04:27:40 INFO Faces detected: 3
04/06/2019 04:27:40 INFO -------------------------
04/06/2019 04:27:40 INFO Process Succesfully Completed. Shutting Down...
最终对比:(从学习的角度还是比较成功的,但是从商用的角度,还是有待提高的,后续再研究一下训练模型的方法)
错误列表:
错误问题1:
wudi:faceswap xxx$ docker run -tid -p 8888:8888 --rm --hostname deepfakes-cpu --name deepfakes-cpu -v /Users/xiepengchong/Code/opensource/faceswap:/srv deepfakes-cpu
docker: Error response from daemon: Conflict. The container name "/deepfakes-cpu" is already in use by container "5c0c9e4ab311c67cb31c5b9a70c16e9acdd3585cc23f931171b76732c33ab983". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
解决办法:我们通过
docker ps -a 查看谁在占用
wudi:faceswap xxx$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c0c9e4ab311 deepfakes-cpu "/bin/bash" 2 days ago Exited (255) 20 minutes ago 6006/tcp, 0.0.0.0:8888->8888/tcp deepfakes-cpu
621b4f1cb0fa 72988b187cc1 "/bin/sh -c 'pip3 --…" 2 days ago Exited (2) 2 days ago practical_turing
786acf1d6888 7551dd8d3f16 "/bin/sh -c 'pip3 in…" 4 days ago Exited (1) 4 days ago adoring_wing
b63e66495e6d 94421f3df34e "/bin/sh -c 'pip3 in…" 4 days ago Exited (1) 4 days ago hardcore_leakey
3a430bf52575 022db20631a8 "/bin/sh -c 'pip3 in…" 4 days ago Exited (1) 4 days ago gallant_spence
d3c747ad6b1d 022db20631a8 "/bin/sh -c 'pip3 in…" 4 days ago Exited (1) 4 days ago friendly_lichterman
d4637db65da3 022db20631a8 "/bin/sh -c 'pip3 in…" 6 days ago Exited (1) 6 days ago inspiring_chandrasekhar
cf055365a4bf 022db20631a8 "/bin/sh -c 'pip3 in…" 6 days ago Exited (1) 6 days ago vigorous_hofstadter
ea59052c6e01 022db20631a8 "/bin/sh -c 'pip3 in…" 6 days ago Exited (1) 6 days ago optimistic_mcclintock
删除占用的列表
wudi:faceswap xxx$ docker stop deepfakes-cpu
deepfakes-cpu
wudi:faceswap xxx$ docker rm deepfakes-cpu
deepfakes-cpu
错误问题2:
wudi:faceswap xiepengchong$ docker run -tid -p 8888:8888 --hostname deepfakes-cpu --rm --name deepfakes-cpu -v /Users/xiepengchong/Code/opensource/faceswap:/srv deepfakes-cpu
docker: invalid reference format: repository name must be lowercase.
See 'docker run --help'.
解决办法:
docker run -tid -p 8888:8888 --hostname deepfakes-cpu --rm --name deepfakes-cpu -v /Users/xiepengchong/Code/opensource/faceswap:/srv deepfakes-cpu
错误问题3:
root@deepfakes-cpu:/srv# python faceswap.py train -A liudehua_dest -B chenglong_dest -m ./models/
04/01/2019 13:05:12 INFO Log level set to: INFO
Using TensorFlow backend.
04/01/2019 13:05:15 INFO Model A Directory: /srv/liudehua_dest
04/01/2019 13:05:15 INFO Model B Directory: /srv/chenglong_dest
04/01/2019 13:05:15 INFO Training data directory: /srv/models
04/01/2019 13:05:15 INFO ===============================================
04/01/2019 13:05:15 INFO - Starting -
04/01/2019 13:05:15 INFO - Press 'ENTER' to save and quit -
04/01/2019 13:05:15 INFO - Press 'S' to save model weights immediately -
04/01/2019 13:05:15 INFO ===============================================
04/01/2019 13:05:16 INFO Loading data, this may take a while...
04/01/2019 13:05:16 INFO Loading Model from Original plugin...
04/01/2019 13:05:23 INFO Loading config: '/srv/config/train.ini'
04/01/2019 13:05:23 WARNING No existing state file found. Generating.
04/01/2019 13:05:24 INFO Creating new 'original' model in folder: '/srv/models'
04/01/2019 13:05:25 INFO Loading Trainer from Original plugin...
04/01/2019 13:05:26 INFO Enabled TensorBoard Logging
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python3.5/logging/handlers.py", line 1431, in _monitor
record = self.dequeue(True)
File "/usr/lib/python3.5/logging/handlers.py", line 1380, in dequeue
return self.queue.get(block)
File "<string>", line 2, in get
File "/usr/lib/python3.5/multiprocessing/managers.py", line 717, in _callmethod
kind, result = conn.recv()
File "/usr/lib/python3.5/multiprocessing/connection.py", line 250, in recv
buf = self._recv_bytes()
File "/usr/lib/python3.5/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self._recv(4)
File "/usr/lib/python3.5/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
Killed
没有任何错误信息,也没有生成crash文件,所以尝试增加修改训练模式进行测试
python faceswap.py train -A liudehua_dest -B chenglong_dest -m ./models/ -L TRACE --trainer villain --batch-size 2 --save-interval 20
增加了参数(错误变成如下):
04/03/2019 04:30:52 VERBOSE Total params: 21,543,555
04/03/2019 04:30:52 VERBOSE Trainable params: 21,543,555
04/03/2019 04:30:52 VERBOSE Non-trainable params: 0
04/03/2019 04:30:52 VERBOSE __________________________________________________________________________________________________
04/03/2019 04:30:52 VERBOSE Using Mean Absolute Error Loss
04/03/2019 04:30:52 INFO Loading Trainer from Original plugin...
04/03/2019 04:30:54 INFO Enabled TensorBoard Logging
2019-04-03 04:31:05.529692: W tensorflow/core/framework/allocator.cc:122] Allocation of 268435456 exceeds 10% of system memory.
2019-04-03 04:31:05.953608: W tensorflow/core/framework/allocator.cc:122] Allocation of 268435456 exceeds 10% of system memory.
2019-04-03 04:31:06.248059: W tensorflow/core/framework/allocator.cc:122] Allocation of 268435456 exceeds 10% of system memory.
2019-04-03 04:31:06.493635: W tensorflow/core/framework/allocator.cc:122] Allocation of 268435456 exceeds 10% of system memory.
Killed
发现是内存不足,所以修改训练模型为lightweight(中间尝试过更轻量级的模型,但是也失败了)
python faceswap.py train -A ./liudehua_dest/ -B ./chenglong_dest/ -m ./models/ --trainer lightweight --batch-size 2
开始训练,但是时间及其长。
在此过程中有许多小问题,
- 生成的alighments.json是在liudehua目录的,应该拷贝到dest目录下,因为训练的时候,需要找这个文件。
- memory-saving-gradients 只试用GPU编译优化,所以mac没用
- 发生过ValueError: Cannot create group in read only mode,将model里面的内容都删除了,后来就不复现了
错误问题4:
Installed /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/google_images_download-2.5.0-py3.5.egg
Processing dependencies for google-images-download==2.5.0
Searching for selenium
Reading https://pypi.python.org/simple/selenium/
Download error on https://pypi.python.org/simple/selenium/: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:719) -- Some packages may not be found!
Couldn't find index page for 'selenium' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on https://pypi.python.org/simple/: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:719) -- Some packages may not be found!
No local packages or working download links found for selenium
error: Could not find suitable distribution for Requirement.parse('selenium')
解决办法:直接手动安装selenium
到官网看一下https://pypi.org/project/selenium/2.44.0/
pip install selenium==2.44.0
pip3 install selenium==2.44.0
可能提示没有权限,使用sudo运行即可。