Deepfakes with an AMD graphics card tutorial (Updated)

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)

Radeontop (Github link) monitors your GPU usage to make sure your script is making use of it. Go to your home directory or choice of install directory. Grab this package first.

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

Install ComputeCPP

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

Install libssl-dev

sudo apt-get install libssl-dev

Install clinfo

sudo apt-get install clinfo

Run clinfo and verify OpenCL detects your GPU

Install CURL

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

Download Bazel

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.

Locations:
/usr/bin/g++
/usr/bin/gcc
/usr/local/computecpp

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.

cd /tmp/tensorflow_pkg

Install the wheel (replace with the appropriate wheel):

pip3.6 install tensorflow-1.5.0rc0-cp36-cp36m-linux_x86_64.whl

Then:

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.

Extract

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

Train

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.

Convert

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

clinfo
/usr/local/computecpp/bin/computecpp_info

For libcomputecpp.so missing:

export LD_LIBRARY_PATH=/usr/local/computecpp/lib

Restart bashrc

exec bash

For g++ missing

sudo apt-get install g++-4.8

This guide was based on material by /u/Tyrannosaurus1234 and two other anonymous reddit 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.

14 thoughts on “Deepfakes with an AMD graphics card tutorial (Updated)

  1. It appears that the amdgpu drivers don’t work with my graphics card (AMD Radeon (TM) R9 M360). Am I just sol for doing deepfakes?

    • There’s some mention of the amdgpu driver not working for a similar R7 graphics card. See https://bbs.archlinux.org/viewtopic.php?id=221131, for example. It might be that the laptop versions are less supported.

      I’m looking into other ways to get it to work on AMD… quite difficult. Will take at least a month to see if I can get a solution, if it’s even possible.

  2. Configure the faceswap scripts

    pip3.6 install -r requirements.txt
    pip3.6 uninstall tensorflow
    pip3.6 uninstall keras

    in which directory to run ./configure?

    • I don’t think anyone has managed to get it working in Windows yet… am trying a few things but not optimistic.

  3. Having huge problems following this guide, i even tried the precompiled wheel, could you please release a video version of it?

    • I no longer own an AMD card, and yes, it is quite difficult. People generally fight to get it to work, and this guide is based on multiple recollections. No one wants to repeat the process in case they can’t get it to work again…

  4. I’m using RX480. I followed the your guide. but It was failed. The log said “terminate called after throwing an instance of ‘cl::sycl::detail::exception_implementation<(cl::sycl::detail::exception_types)7, cl::sycl::detail::exception_implementation >’ ”
    What is this?

  5. If you aren’t supported by the amd-pro driver, stick with the default Radeon driver and follow these steps:
    1. Do “clinfo”
    2. If the output is 0, Google the clinfo output with “amd” appended.
    3. You’ll find a fix there. Do it and you’ll be set.

Leave a Comment