How to use an AMD graphics card to make deepfakes with Linux
UPDATED. The standard deepfakes code requires CUDA-compatible NVIDIA graphics cards. That means ATI graphics card users were left unable to participate.
Here is a tutorial for running the python version of the deepfakes faceswap using an AMD card in a Linux environment on a fresh install of Ubuntu 16.04 LTS. Note that this is experimental, and some users report success while many others report difficulties. Please post in the forum thread with questions and comments.
Installing basic components
Operating system requirements
The following tutorial has been confirmed to work under Ubuntu 16.04, which is probably one of the most common Linux flavors and quite user friendly. If you need to install it, go here.
The tutorial has not been validated for Cygwin users who are running Linux directly on top of a Windows installation. You can learn how to dual boot your computer here.
Install OpenCL headers
sudo apt-get install ocl-icd-opencl-dev opencl-headers
Install AMDGPU-Pro drivers
Visit support.amd.com and install the the most recent driver for your graphics card. Extract the file and type the following in that directory:
./amdgpu-pro-install -y sudo reboot
(or reboot manually)
If you are using a pre-Vega GPU only, you need to use the command:
./amdgpu-pro-install -y –opencl=legacy
Return to the install directory and type:
./amdgpu-pro-install -compute -y
Install other dependencies
sudo apt-get install zlib1g-dev sudo apt-get install cmake sudo apt-get install git
Install Radeontop (Optional)
cd ~/ sudo apt-get install libncurses5-dev libncursesw5-dev git clone https://github.com/clbr/radeontop sudo make sudo make install
Install Python 3.6.4
Download the Python 3.6.4 source code here.
Extract the files. Uncomment line 209 to 212 in ./Modules/setup after ./configure (see here for why). Go to the install directory.
./configure sudo make sudo make install
Install pip 3.6 (a package manager)
sudo apt-get install python3-pip pip install --upgrade pip
Clone the faceswap script and edit requirements
Go back to your main install directory.
cd ~/ git clone https://github.com/deepfakes/faceswap
Find or create the requirements.txt file within the faceswap directory. Paste into faceswap/requirements.txt:
absl-py bleach click cycler decorator dlib enum34 face-recognition face-recognition-models futures h5py html5lib imageio Keras Markdown matplotlib moviepy networkx numpy opencv-contrib-python opencv-python pathlib Pillow pip protobuf pyparsing python-dateutil pytz PyWavelets scandir scikit-image scipy setuptools six Theano tqdm Werkzeug tensorflow tensorflow-tensorboard wheel
Configure the faceswap scripts
pip3.6 install -r requirements-gpu.txt pip3.6 uninstall tensorflow pip3.6 uninstall keras
Build Bazel from source
Download computecpp from here.
Extract and copy ComputeCPP to /usr/local/ (Ctrl+L to find.)
sudo mkdir /usr/local/computecpp sudo cp -R /computecpp/lib /usr/local/computecpp sudo cp -R /computecpp/doc /usr/local/computecpp sudo cp -R /computecpp/include /usr/local/computecpp sudo cp -R /computecpp/bin /usr/local/computecpp
Add the following to the end of your ~/.bashrc file:
export TF_NEED_OPENCL=1 export HOST_CXX_COMPILER=/usr/bin/g++-4.8 export HOST_C_COMPILER=/usr/bin/gcc-4.8 export COMPUTECPP_TOOLKIT_PATH=/usr/local/computecpp export COMPUTE=:0
Extract the archive you downloaded to the directory /usr/local/computecpp
sudo apt-get install libssl-dev
sudo apt-get install clinfo
Run clinfo and verify OpenCL detects your GPU
sudo apt-get install curl linux-generic
Build Tensorflow with OpenCL support
Download and install Tensorflow.
cd ~/ git clone https://github.com/lukeiwanski/tensorflow.git cd tensorflow git checkout dev/amd_gpu
sudo apt-get install openjdk-8-jdk echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add - sudo apt-get update && sudo apt-get install bazel sudo apt-get upgrade bazel
Install and activate Env
sudo pip3.6 install virtualenv virtualenv faceswap_env/ cd ~/faceswap/faceswap_env/bin source activate
NOTE: commands in RED are entered from within Env
Configure and build
pip3.6 install -r requirements.txt pip3.6 uninstall tensorflow pip3.6 uninstall keras
cd ~/tensorflow ./configure
Confirm your Python install location, Python Library path, and various options, /faceswap/faceswap_env/bin/python3, site-packages, etc.
Select the following options.
NO to jemalloc support
NO to Google Cloud support
NO to Hadoop support
NO to Amazon S3 support
NO to XLA JIT support
NO to GDR support
NO to VERBS support
YES to OpenCL SYCL support
YES to ComputeCPP support
NO to CUDA support
NO to MPI support
Next, build the pip wheel (slow step):
bazel build -c opt --config=sycl //tensorflow/tools/pip_package:build_pip_package mkdir /tmp/tensorflow_pkg bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
If you have a modern system, you can try replacing the first line with the line below (see here for more information):
bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.1 –copt=-msse4.2 --config=sycl -k //tensorflow/tools/pip_package:build_pip_package
Proceed if there are no errors.
Install the wheel (replace with the appropriate wheel):
pip3.6 install tensorflow-1.5.0rc0-cp36-cp36m-linux_x86_64.whl
pip3.6 install keras pip3.6 install face_recognition
This should cover the major installation steps.
Follow the instructions ~/faceswap/USAGE.md and test the faceswap script.
You probably want to run
sudo radeontop sudo watch sensors
to check that your GPU is being used and to monitor your GPU temperature.
Brief faceswap primer
Make sure your Env is activated.
Say you have an example data set of Trump and Cage in /photo/trump and /photo/cage, respectively.
python faceswap.py extract -i ~/faceswap/photo/trump -o ~/faceswap/data/trump python faceswap.py extract -i ~/faceswap/photo/cage -o ~/faceswap/data/cage
python faceswap.py train -A ~/faceswap/data/trump -B ~/faceswap/data/cage -m ~/faceswap/models/
To show a preview:
python faceswap.py train -A ~/faceswap/data/trump -B ~/faceswap/data/cage -m ~/faceswap/models/ -p
Use the -h switch for the help option.
python faceswap.py convert -i ~/faceswap/photo/trump/ -o ~/faceswap/output/ -m ~/faceswap/models/
Create a video
See the tutorial.
Other useful commands
Clear Bazel cache-bug
bazel clean //clear bazel cache-bug
Clear Bazel cache
bazel clean --expunge
Update your package list
sudo apt-get update
Find your GPU information
For libcomputecpp.so missing:
For g++ missing
sudo apt-get install g++-4.8
Thanks to the anonymous users who contributed to refining this tutorial.
Please post in the forum thread if you have issues, as it is easier to collect everything there.