From d1705b08cdd747c1d37c578c8de2fa407cc520cf Mon Sep 17 00:00:00 2001 From: torzdf <36920800+torzdf@users.noreply.github.com> Date: Fri, 16 Nov 2018 17:31:27 +0000 Subject: [PATCH] Improve setup.py for Windows --- INSTALL.md | 80 +++++--- lib/cli.py | 4 +- requirements.txt | 14 +- setup.py | 514 ++++++++++++++++++++++++++++++++--------------- 4 files changed, 412 insertions(+), 200 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 88e041fcd5..b5d65c971b 100755 --- a/INSTALL.md +++ b/INSTALL.md @@ -1,4 +1,5 @@ # Installing Faceswap +- [Installing Faceswap](#installing-faceswap) - [Prerequisites](#prerequisites) - [Hardware Requirements](#hardware-requirements) - [Supported operating systems](#supported-operating-systems) @@ -20,14 +21,17 @@ - [Git](#git) - [Setup](#setup-1) - [Anaconda](#anaconda-1) + - [Set up a virtual environment](#set-up-a-virtual-environment) - [Entering your virtual environment](#entering-your-virtual-environment) - - [Dlib](#dlib) - - [Build Latest Dlib with GPU Support](#build-latest-dlib-with-gpu-support) - - [Easy install of Dlib without GPU Support](#easy-install-of-dlib-without-gpu-support) - [Faceswap](#faceswap) + - [Easy install](#easy-install) + - [Manual install](#manual-install) - [Running Faceswap](#running-faceswap) - [Create a desktop shortcut](#create-a-desktop-shortcut) - [Updating faceswap](#updating-faceswap) + - [Dlib](#dlib) + - [Build Latest Dlib with GPU Support](#build-latest-dlib-with-gpu-support) + - [Easy install of Dlib without GPU Support](#easy-install-of-dlib-without-gpu-support) # Prerequisites Machine learning essentially involves a ton of trial and error. You're letting a program try millions of different settings to land on an algorithm that sort of does what you want it to do. This process is really really slow unless you have the hardware required to speed this up. @@ -199,14 +203,19 @@ At the time of writing Tensorflow (version 1.12) only supports Cuda up to versio Download and install the the correct version of the Cuda Toolkit from: https://developer.nvidia.com/cuda-toolkit-archive +NB: Make a note of the install folder as you'll need to access it in the next step. + ### cuDNN **GPU Only** If you do not have an Nvidia GPU you can skip this step. As with Cuda you will need to install the correct version of cuDNN that the latest Tensorflow supports. At the time of writing this is Tensorflow v1.12 which supports cuDNN version 7.2, but check https://www.tensorflow.org/install/gpu for the latest supported version. -Download and install cuDNN from https://developer.nvidia.com/cudnn. You will need to create an account with Nvidia. +Download cuDNN from https://developer.nvidia.com/cudnn. You will need to create an account with Nvidia. At the bottom of the list of latest cuDNN release will be a link to "Archived cuDNN Releases". Select this and choose the latest version of cuDNN that supports the version of Cuda you installed and is less than or equal to the latest version that Tensorflow supports. (Eg Tensorflow 1.12 supports Cuda 9.0 and cuDNN 7.2. There is not an archived version of cuDNN 7.2 for Cuda 9.0, so select cuDNN version 7.1) +- Open the zip file +- Extract all of the files and folders into your Cuda folder:\ +![cudnn to cudo](https://i.imgur.com/X098w0N.png) ### CMake Install the latest stable release of CMake from https://cmake.org/download/. (Scroll down the page for Latest Releases and select the relevant Binary distribution installer for your OS). @@ -238,7 +247,7 @@ Download and install Git for Windows: https://git-scm.com/download/win. Unless y Reboot your PC, so that everything you have just installed gets registered. ### Anaconda - +#### Set up a virtual environment - Open up Anaconda Navigator - Select "Environments" on the left hand side - Select "Create" at the bottom @@ -255,37 +264,21 @@ To enter the virtual environment: - Hit the ">" arrow next to your faceswap environment and select "Open Terminal" ![Anaconda enter virtual env](https://i.imgur.com/rKSq2Pd.png) -### Dlib -For reasons outside of our control, this is the trickiest part of the process, and most of the prerequisites you installed are to support just Dlib. It is recommended to build Dlib from source for 3 main reasons: -1) To get the latest version -2) Enable GPU Support in Dlib -3) To prevent yourself running into a whole host of issues later in the process. - -If you are not bothered about having GPU support or the latest version, scroll to the end of this section for a simple one-liner to install the CPU version of Dlib. -#### Build Latest Dlib with GPU Support +### Faceswap - If you are not already in your virtual environment follow [these steps](#entering-your-virtual-environment) -- In the terminal type: `git clone https://github.com/davisking/dlib.git` -- Enter the dlib folder: `cd dlib` -- Add Visual Studio to your path by typing: `SET PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin` -- Enter: `python setup.py -G "Visual Studio 14 2015" install --yes USE_AVX_INSTRUCTIONS --yes DLIB_USE_CUDA --clean` +- Get the Faceswap repo by typing: `git clone https://github.com/deepfakes/faceswap.git` +- Enter the faceswap folder: `cd faceswap` -This will build and install dlib for you. It is worth backing up the generated .egg file somewhere so that you can re-install it if you ever need to rather than having to re-compile: -- From within the dlib folder copy the file named `dlib-xx.yy.zz-py3.5-win-amd64.egg` to somewhere safe -- If you ever need to install it again, then from within your virtual environment enter: `python -m easy_install ` +#### Easy install +- Enter `python setup.py` and follow the prompts. -Once Dlib is built, you can remove Visual Studio and CMake from your PC. +If you have issues/errors follow the Manual install steps below. -#### Easy install of Dlib without GPU Support -NB: Don't do this if you have already compiled Dlib with GPU support. -- If you are not already in your virtual environment follow [these steps](#entering-your-virtual-environment) -- In the terminal type: `conda install -c conda-forge dlib` +#### Manual install +If dlib failed to install you can follow the steps to [manually install dlib](#dlib).\ +Once dlib is installed follow these steps: -### Faceswap -- If you are not already in your virtual environment follow [these steps](#entering-your-virtual-environment) -- In the terminal type: `git clone https://github.com/deepfakes/faceswap.git` -- Enter the faceswap folder: `cd faceswap` - Install requirements: `pip install -r requirements.txt` -- Install tkinter (required for the GUI) by typing: `conda install tk` - Install Tensorflow (either GPU or CPU version depending on your setup): - GPU Version: `pip install tensorflow-gpu` - Non GPU Version: `pip install tensorflow` @@ -311,3 +304,30 @@ It's good to keep faceswap up to date as new features are added and bugs are fix - Enter the faceswap folder: `cd faceswap` - Enter the following `git pull --all` - Once the latest version has downloaded, make sure your requirements are up to date: `pip install --upgrade -r requirements.txt` + +## Dlib +You should only need to follow these steps if you want the latest Dlib code or the process was unable to install Dlib for you. + +For reasons outside of our control, this is the trickiest part of the process, and most of the prerequisites you installed are to support just Dlib. It is recommended to build Dlib from source for 3 main reasons: +1) To get the latest version +2) Enable GPU Support in Dlib +3) To prevent yourself running into a whole host of issues later in the process. + +If you are not bothered about having GPU support or the latest version, scroll to the end of this section for a simple one-liner to install the CPU version of Dlib. +### Build Latest Dlib with GPU Support +- If you are not already in your virtual environment follow [these steps](#entering-your-virtual-environment) +- In the terminal type: `git clone https://github.com/davisking/dlib.git` +- Enter the dlib folder: `cd dlib` +- Add Visual Studio to your path by typing: `SET PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin` +- Enter: `python setup.py -G "Visual Studio 14 2015" install --yes USE_AVX_INSTRUCTIONS --yes DLIB_USE_CUDA --clean` + +This will build and install dlib for you. It is worth backing up the generated .egg file somewhere so that you can re-install it if you ever need to rather than having to re-compile: +- From within the dlib folder copy the file named `dlib-xx.yy.zz-py3.5-win-amd64.egg` to somewhere safe +- If you ever need to install it again, then from within your virtual environment enter: `python -m easy_install ` + +Once Dlib is built, you can remove Visual Studio and CMake from your PC. + +### Easy install of Dlib without GPU Support +NB: Don't do this if you have already compiled Dlib with GPU support. +- If you are not already in your virtual environment follow [these steps](#entering-your-virtual-environment) +- In the terminal type: `conda install -c conda-forge dlib` diff --git a/lib/cli.py b/lib/cli.py index 326c4771fd..a877cc12be 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -54,9 +54,9 @@ def test_tkinter(): "the GUI has been disabled. To enable the GUI please " "install the TkInter application.\n\n" "You can try:\n" + " Anaconda: conda install tk\n" " Windows/macOS: Install ActiveTcl Community " - "Edition from " - "www.activestate.com\n" + "Edition from http://www.activestate.com\n" " Ubuntu/Mint/Debian: sudo apt install python3-tk\n" " Arch: sudo pacman -S tk\n" " CentOS/Redhat: sudo yum install tkinter\n" diff --git a/requirements.txt b/requirements.txt index 98e620982e..dd91dc8ae3 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1,18 +1,18 @@ +tqdm +psutil pathlib==1.0.1 scandir==1.7 -h5py==2.8.0 -Keras==2.2.4 -psutil opencv-python scikit-image scikit-learn -face_recognition -cmake -dlib -tqdm matplotlib==2.2.2 ffmpy==0.2.2 nvidia-ml-py3 +h5py==2.8.0 +Keras==2.2.4 +cmake +dlib +face_recognition # tensorflow is included within the docker image. # If you are looking for dependencies for a manual install, diff --git a/setup.py b/setup.py index 4c2de1e3c1..ed51ff0389 100755 --- a/setup.py +++ b/setup.py @@ -2,27 +2,45 @@ """ Install packages for faceswap.py """ # >>> ENV +import ctypes import os +import re import sys import platform + +from subprocess import run, PIPE, Popen + +# Revisions of tensorflow-gpu and cuda/cudnn requirements +TENSORFLOW_REQUIREMENTS = {"1.2": ["8.0", "5.1"], + "1.4": ["8.0", "6.0"], + "1.12": ["9.0", "7.2"]} OS_VERSION = (platform.system(), platform.release()) PY_VERSION = (platform.python_version(), platform.architecture()[0]) -IS_MACOS = (platform.system() == 'Darwin') +IS_MACOS = (platform.system() == "Darwin") +IS_CONDA = ("conda" in sys.version.lower()) LD_LIBRARY_PATH = os.environ.get("LD_LIBRARY_PATH", None) -IS_ADMIN = False -IS_VIRTUALENV = False +try: + IS_ADMIN = os.getuid() == 0 +except AttributeError: + IS_ADMIN = ctypes.windll.shell32.IsUserAnAdmin() != 0 +IS_VIRTUALENV = (hasattr(sys, "real_prefix") + or (hasattr(sys, "base_prefix") + and sys.base_prefix != sys.prefix)) + +CUDA_PATH = "" CUDA_VERSION = "" -ENABLE_DOCKER = True +CUDNN_VERSION = "" +ENABLE_DOCKER = False ENABLE_CUDA = True -COMPILE_DLIB_WITH_AVX_CUDA = True -REQUIRED_PACKAGES = [ - "tensorflow" - ] +COMPILE_DLIB_WITH_AVX = True +REQUIRED_PACKAGES = list() MACOS_REQUIRED_PACKAGES = [ "pynvx==0.0.4" ] -INSTALLED_PACKAGES = {} -MISSING_PACKAGES = [] +INSTALLED_PACKAGES = dict() +MISSING_PACKAGES = list() +FAIL = False + # load requirements list with open("requirements.txt") as req: @@ -54,7 +72,6 @@ def __indent_text_block(text): def term_support_color(): """ Set whether OS Support terminal colour """ - global OS_VERSION return OS_VERSION[0] == "Linux" or OS_VERSION[0] == "Darwin" @@ -76,23 +93,18 @@ def out_warning(text): def out_error(text): """ Format ERROR Text """ + global FAIL trm = "ERROR " if term_support_color(): trm = "{}ERROR {} ".format(COLOR_RED, COLOR_DEFAULT) print(trm + __indent_text_block(text)) - exit(1) + FAIL = True # <<< OUTPUT def check_permission(): """ Check for Admin permissions """ - import ctypes - global IS_ADMIN - try: - IS_ADMIN = os.getuid() == 0 - except AttributeError: - IS_ADMIN = ctypes.windll.shell32.IsUserAnAdmin() != 0 if IS_ADMIN: out_info("Running as Root/Admin") else: @@ -101,12 +113,12 @@ def check_permission(): def check_system(): """ Check the system """ - global OS_VERSION out_info("The tool provides tips for installation\n" "and installs required python packages") out_info("Setup in %s %s" % (OS_VERSION[0], OS_VERSION[1])) if not OS_VERSION[0] in ["Windows", "Linux", "Darwin"]: out_error("Your system %s is not supported!" % OS_VERSION[0]) + exit(1) def ask_enable_cuda(): @@ -124,8 +136,8 @@ def ask_enable_cuda(): def ask_enable_docker(): """ Enable or disable Docker """ global ENABLE_DOCKER - i = input("Enable Docker? [Y/n] ") - if i in ("", "Y", "y"): + i = input("Enable Docker? [y/N] ") + if i in ("Y", "y"): out_info("Docker Enabled") ENABLE_DOCKER = True else: @@ -135,102 +147,236 @@ def ask_enable_docker(): def check_python(): """ Check python and virtual environment status """ - global PY_VERSION, IS_VIRTUALENV - # check if in virtualenv - IS_VIRTUALENV = (hasattr(sys, "real_prefix") - or (hasattr(sys, "base_prefix") and - sys.base_prefix != sys.prefix)) - if PY_VERSION[0].split(".")[0] == "3" and PY_VERSION[1] == "64bit": - out_info("Installed Python: {0} {1}".format(PY_VERSION[0], - PY_VERSION[1])) - return True - - out_error("Please run this script with Python3 64bit and try again.") - return False + out_info("Installed Python: {0} {1}".format(PY_VERSION[0], + PY_VERSION[1])) + if not (PY_VERSION[0].split(".")[0] == "3" + and PY_VERSION[0].split(".")[1] in ("3", "4", "5", "6") + and PY_VERSION[1] == "64bit"): + out_error("Please run this script with Python version 3.3, 3.4, 3.5 or 3.6 " + "64bit and try again.") + exit(1) def check_pip(): """ Check installed pip version """ try: - try: # for pip >= 10 - from pip._internal.utils.misc import (get_installed_distributions, - get_installed_version) - except ImportError: # for pip <= 9.0.3 - from pip.utils import (get_installed_distributions, - get_installed_version) - global INSTALLED_PACKAGES - INSTALLED_PACKAGES = {pkg.project_name: pkg.version - for pkg in get_installed_distributions()} - out_info("Installed PIP: " + get_installed_version("pip")) - return True + import pip except ImportError: out_error("Import pip failed. Please Install python3-pip " "and try again") - return False + exit(1) + pip_version = pip.__version__ + del pip + upgrade_pip() + get_installed_packages() + out_info("Installed pip: {}".format(pip_version)) + + +def upgrade_pip(): + """ Upgrade pip to latest version """ + out_info("Upgrading pip...") + pipexe = [sys.executable, "-m", "pip"] + pipexe.extend(["install", "--no-cache-dir", "-qq", "--upgrade"]) + if not IS_ADMIN and not IS_VIRTUALENV: + pipexe.append("--user") + pipexe.append("pip") + run(pipexe) + + +def get_installed_packages(): + """ Get currently installed packages """ + global INSTALLED_PACKAGES + chk = Popen("{} -m pip freeze".format(sys.executable), + shell=True, stdout=PIPE) + installed = chk.communicate()[0].decode().splitlines() + for pkg in installed: + item = pkg.split("==") + INSTALLED_PACKAGES[item[0]] = item[1] + + +def check_system_dependencies(): + """ Check that system applications are installed """ + out_info("Checking System Dependencies...") + check_cmake() + if OS_VERSION[0] == "Windows": + check_visual_studio() + check_cplus_plus() + + +def check_cmake(): + """ Check CMake is installed for Windows """ + chk = Popen("cmake --version", shell=True, stdout=PIPE, stderr=PIPE) + stdout, stderr = chk.communicate() + stdout = stdout.decode() + if stderr and OS_VERSION[0] == "Windows": + stdout, stderr = check_cmake_windows() + if stderr: + out_error("CMake could not be found. See " + "https://github.com/deepfakes/faceswap/blob/master/INSTALL.md#cmake " + "for instructions") + return + cmake = [re.sub(" +", " ", line.strip()) + for line in stdout.splitlines() + if line.lower().strip().startswith("cmake")][0] + version = cmake[cmake.rfind(" ") + 1:] + out_info("CMake version: {}".format(version)) + + +def check_cmake_windows(): + """ Additional checks for cmake on Windows """ + chk = Popen("wmic product where \"name = 'cmake'\" get installlocation,version", + shell=True, stdout=PIPE, stderr=PIPE) + stdout, stderr = chk.communicate() + if stderr: + return False, stderr + lines = [re.sub(" +", " ", line.strip()) + for line in stdout.decode().splitlines() + if line.strip()] + stdout = lines[1] + location = stdout[:stdout.rfind(" ")] + "bin" + out_info("CMake not found in %PATH%. Temporarily adding: \"{}\"".format(location)) + os.environ["PATH"] += ";{}".format(location) + stdout = "cmake {}".format(stdout) + return stdout, False + + +def check_visual_studio(): + """ Check Visual Studio 2015 is installed for Windows """ + chk = Popen("reg query HKLM\\SOFTWARE\\Microsoft\\VisualStudio\\14.0\\VSPerf", + shell=True, stdout=PIPE, stderr=PIPE) + stdout, stderr = chk.communicate() + if stderr: + out_error("Visual Studio 2015 could not be found. See " + "https://github.com/deepfakes/faceswap/blob/master/" + "INSTALL.md#microsoft-visual-studio-2015 for instructions") + return + vspath = [re.sub(" +", " ", line.strip()) + for line in stdout.decode().splitlines() + if line.lower().strip().startswith("vsperfreportpath")][0] + vspath = vspath[vspath.find("REG_SZ") + 7:] # Hacky! + vspath = vspath.replace("\\Team Tools\\Performance Tools\\VSPerfReport.exe", "") # Hacky! + version = vspath[vspath.rfind(" ") + 1:] + if not version.startswith("14"): + out_error("Visual Studio 2015 could not be found. See " + "https://github.com/deepfakes/faceswap/blob/master/" + "INSTALL.md#microsoft-visual-studio-2015 for instructions") + return + out_info("Visual Studio 2015 version: {}".format(version)) + + +def check_cplus_plus(): + """ Check Visual C++ Redistributable 2015 is instlled for Windows """ + chk = Popen("reg query HKLM\\SOFTWARE\\Classes\\Installer\\Dependencies" + "\\{d992c12e-cab2-426f-bde3-fb8c53950b0d}", + shell=True, stdout=PIPE, stderr=PIPE) + stdout, stderr = chk.communicate() + if stderr: + out_error("Visual C++ 2015 could not be found. Make sure you have selected 'Visual C++' " + "in Visual Studio 2015 Configuration or download the Visual C++ 2015 " + "Redistributable from: " + "https://www.microsoft.com/en-us/download/details.aspx?id=48145") + return + vscpp = [re.sub(" +", " ", line.strip()) + for line in stdout.decode().splitlines() + if line.lower().strip().startswith("displayname")][0] + version = vscpp[vscpp.find("REG_SZ") + 7:] + out_info("Visual Studio C++ version: {}".format(version)) -# only invoked in linux def check_cuda(): - """ Check CUDA Version """ - global CUDA_VERSION - chk = os.popen("ldconfig -p | grep -P -o \"libcudart.so.\d+.\d+\" | " - "head -n 1") - libcudart = chk.read() - if LD_LIBRARY_PATH and not libcudart: + """ Check Cuda for Linux or Windows """ + if OS_VERSION[0] == "Linux": + check_cuda_linux() + elif OS_VERSION[0] == "Windows": + check_cuda_windows() + + +def check_cuda_linux(): + """ Check Linux CUDA Version """ + global CUDA_VERSION, CUDA_PATH + chk = os.popen("ldconfig -p | grep -P \"libcudart.so.\\d+.\\d+\" | head -n 1").read() + if LD_LIBRARY_PATH and not chk: paths = LD_LIBRARY_PATH.split(":") for path in paths: - chk = os.popen("ls {} | grep -P -o \"libcudart.so.\d+.\d+\" | " - "head -n 1".format(path)) - libcudart = chk.read() - if libcudart: + chk = os.popen("ls {} | grep -P -o \"libcudart.so.\\d+.\\d+\" | " + "head -n 1".format(path)).read() + if chk: break - if libcudart: - CUDA_VERSION = libcudart[13:].rstrip() - if CUDA_VERSION: - out_info("CUDA version: " + CUDA_VERSION) - else: - out_error("""CUDA not found. Install and try again. -Recommended version: CUDA 9.0 cuDNN 7.1.3 -CUDA: https://developer.nvidia.com/cuda-downloads -cuDNN: https://developer.nvidia.com/rdp/cudnn-download -""") + + if not chk: + out_error("CUDA not found. Install and try again.\n" + "Recommended version: CUDA 9.0 cuDNN 7.1.3\n" + "CUDA: https://developer.nvidia.com/cuda-downloads\n" + "cuDNN: https://developer.nvidia.com/rdp/cudnn-download") + return + + CUDA_VERSION = chk[13:].rstrip() + if CUDA_VERSION: + out_info("CUDA version: " + CUDA_VERSION) + CUDA_PATH = chk[chk.find("=>") + 3:chk.find("targets") - 1] + + +def check_cuda_windows(): + """ Check Windows CUDA Version """ + global CUDA_VERSION, CUDA_PATH + cuda_keys = [key + for key in os.environ.keys() + if key.lower().startswith("cuda") and key.lower() != "cuda_path"] + if not cuda_keys: + out_error("CUDA not found. See " + "https://github.com/deepfakes/faceswap/blob/master/INSTALL.md#cuda " + "for instructions") + return + + CUDA_VERSION = cuda_keys[0].replace("CUDA_PATH_V", "").replace("_", ".") + CUDA_PATH = os.environ[cuda_keys[0]] + out_info("CUDA version: " + CUDA_VERSION) -# only invoked in linux def check_cudnn(): - """ Check cuDNN Version """ - chk = os.popen("ldconfig -p | grep -P -o \"libcudnn.so.\d\" | head -n 1") - libcudnn = chk.read() - if LD_LIBRARY_PATH and not libcudnn: - paths = LD_LIBRARY_PATH.split(":") - for path in paths: - chk = os.popen("ls {} | grep -P -o \"libcudnn.so.\d\" | " - "head -n 1".format(path)) - libcudnn = chk.read() - if libcudnn: + """ Check Linux or Windows cuDNN Version from cudnn.h """ + global CUDNN_VERSION + cudnn_checkfile = os.path.join(CUDA_PATH, "include", "cudnn.h") + if not os.path.isfile(cudnn_checkfile): + out_error("cuDNN not found. See " + "https://github.com/deepfakes/faceswap/blob/master/INSTALL.md#cudnn " + "for instructions") + return + found = 0 + with open(cudnn_checkfile, "r") as ofile: + for line in ofile: + if line.lower().startswith("#define cudnn_major"): + major = line[line.rfind(" ") + 1:].strip() + found += 1 + elif line.lower().startswith("#define cudnn_minor"): + minor = line[line.rfind(" ") + 1:].strip() + found += 1 + elif line.lower().startswith("#define cudnn_patchlevel"): + patchlevel = line[line.rfind(" ") + 1:].strip() + found += 1 + if found == 3: break - if libcudnn: - cudnn_version = libcudnn[12:].rstrip() - if cudnn_version: - out_info("cuDNN version: " + cudnn_version) - else: - out_error("""cuDNN not found. Install and try again. -Recommended version: CUDA 9.0 cuDNN 7.1.3 -CUDA: https://developer.nvidia.com/cuda-downloads -cuDNN: https://developer.nvidia.com/rdp/cudnn-download -""") + if found != 3: + out_error("cuDNN version could not be determined. See " + "https://github.com/deepfakes/faceswap/blob/master/INSTALL.md#cudnn " + "for instructions") + return + + CUDNN_VERSION = "{}.{}".format(major, minor) + out_info("cuDNN version: {}.{}".format(CUDNN_VERSION, patchlevel)) def ask_continue(): """ Ask Continue with Install """ - i = input("Are System Dependencies met? [y/N] ") + i = input("Please ensure your System Dependencies are met. Continue? [y/N] ") if i in ("", "N", "n"): - out_error('Please install system dependencies to continue') + out_error("Please install system dependencies to continue") + exit(1) def check_missing_dep(): """ Check for missing dependencies """ - global MISSING_PACKAGES, INSTALLED_PACKAGES, ENABLE_CUDA, IS_MACOS + global MISSING_PACKAGES, INSTALLED_PACKAGES, ENABLE_CUDA if ENABLE_CUDA and IS_MACOS: REQUIRED_PACKAGES.extend(MACOS_REQUIRED_PACKAGES) MISSING_PACKAGES = [] @@ -248,66 +394,105 @@ def check_missing_dep(): def check_dlib(): """ Check dlib install requirements """ - global MISSING_PACKAGES, COMPILE_DLIB_WITH_AVX_CUDA + global MISSING_PACKAGES, COMPILE_DLIB_WITH_AVX if "dlib" in MISSING_PACKAGES: - i = input("Compile dlib with AVX (and CUDA if enabled)? [Y/n] ") + i = input("Compile dlib with AVX? [Y/n] ") if i in ("", "Y", "y"): out_info("dlib Configured") - out_warning("Make sure you are using gcc-5/g++-5 " - "and CUDA bin/lib in path") - COMPILE_DLIB_WITH_AVX_CUDA = True + if OS_VERSION[0] == "Linux": + out_warning("Make sure you are using gcc-5/g++-5 " + "and CUDA bin/lib in path") + COMPILE_DLIB_WITH_AVX = True else: - COMPILE_DLIB_WITH_AVX_CUDA = False + COMPILE_DLIB_WITH_AVX = False + + +def update_tf_dep(cpu_only): + """ Update Tensorflow Dependency """ + global CUDA_VERSION, CUDNN_VERSION + + if cpu_only: + REQUIRED_PACKAGES.append("tensorflow") + return + + tf_ver = None + cudnn_inst = CUDNN_VERSION.split(".") + for key, val in TENSORFLOW_REQUIREMENTS.items(): + cuda_req = val[0] + cudnn_req = val[1].split(".") + if cuda_req == CUDA_VERSION and (cudnn_req[0] == cudnn_inst[0] and + cudnn_req[1] >= cudnn_inst[1]): + tf_ver = key + break + if not tf_ver: + out_warning("Tensorflow currently has no official prebuild for your CUDA, cuDNN " + "combination.\nEither install a combination that Tensorflow supports or " + "build and install your own tensorflow-gpu.\r\n" + "CUDA Version: {}\r\n" + "cuDNN Version: {}\r\n" + "Help: " + "Building Tensorflow: https://www.tensorflow.org/install/install_sources\r\n" + "Tensorflow supported versions: " + "https://www.tensorflow.org/install/source#tested_build_configurations".format( + CUDA_VERSION, CUDNN_VERSION)) + + custom_tf = input("Location of custom tensorflow-gpu wheel (leave " + "blank to manually install): ") + if custom_tf and not os.path.isfile(custom_tf): + out_error("{} not found".format(custom_tf)) + return + REQUIRED_PACKAGES.append(custom_tf) + return + + tf_ver = "tensorflow-gpu=={}.0".format(tf_ver) + REQUIRED_PACKAGES.append(tf_ver) def install_missing_dep(): """ Install missing dependencies """ - global MISSING_PACKAGES + global MISSING_PACKAGES, ENABLE_CUDA if MISSING_PACKAGES: out_info("Installing Required Python Packages. " "This may take some time...") - try: - from pip._internal import main as pipmain - except: - from pip import main as pipmain + install_tkinter() for pkg in MISSING_PACKAGES: msg = "Installing {}".format(pkg) out_info(msg) + pipexe = [sys.executable, "-m", "pip"] # hide info/warning and fix cache hang - pipargs = ["install", "-qq", "--no-cache-dir"] + pipexe.extend(["install", "-qq", "--no-cache-dir"]) # install as user to solve perm restriction if not IS_ADMIN and not IS_VIRTUALENV: - pipargs.append("--user") - # compile dlib with AVX ins and CUDA - if pkg.startswith("dlib") and COMPILE_DLIB_WITH_AVX_CUDA: - pipargs.extend(["--install-option=--yes", - "--install-option=USE_AVX_INSTRUCTIONS"]) - pipargs.append(pkg) - # pip install -qq (--user) (--install-options) pkg - pipmain(pipargs) - - -def update_tf_dep(): - """ Update Tensorflow Dependency """ - global CUDA_VERSION - REQUIRED_PACKAGES[0] = "tensorflow-gpu" - if CUDA_VERSION.startswith("8.0"): - REQUIRED_PACKAGES[0] += "==1.4.0" - elif not CUDA_VERSION.startswith("9.0"): - out_warning("Tensorflow has currently no official prebuild for CUDA " - "versions above 9.0.\r\nTo continue, You have to build " - "and install your own tensorflow-gpu.\r\n" - "Help: " - "https://www.tensorflow.org/install/install_sources") - custom_tf = input("Location of custom tensorflow-gpu wheel (leave " - "blank to manually install): ") - if not custom_tf: - del REQUIRED_PACKAGES[0] - return - if os.path.isfile(custom_tf): - REQUIRED_PACKAGES[0] = custom_tf - else: - out_error("{} not found".format(custom_tf)) + pipexe.append("--user") + # compile dlib with AVX and CUDA + if pkg.startswith("dlib"): + if OS_VERSION[0] == "Windows": + pipexe.extend(["--global-option=-G", + "--global-option=Visual Studio 14 2015"]) + opt = "yes" if COMPILE_DLIB_WITH_AVX else "no" + pipexe.extend(["--install-option=--{}".format(opt), + "--install-option=USE_AVX_INSTRUCTIONS"]) + opt = "yes" if ENABLE_CUDA else "no" + pipexe.extend(["--install-option=--{}".format(opt), + "--install-option=DLIB_USE_CUDA"]) + + pipexe.append(pkg) + run(pipexe) + + +def install_tkinter(): + """ Install tkInter on Conda Environments """ + if not IS_CONDA: + return + pkgs = os.popen("conda list").read() + tki = [re.sub(" +", " ", line.strip()) + for line in pkgs.splitlines() + if line.lower().strip().startswith("tk")] + if tki: + return + out_info("Installing tkInter") + with open(os.devnull, "w") as devnull: + run(["conda", "install", "-q", "-y", "tk"], stdout=devnull) def tips_1_1(): @@ -414,22 +599,21 @@ def tips_2_1(): def tips_2_2(): """ Output Tips """ - out_info("""1. Install System Dependencies. -In Windows: -Install CMake x64: https://cmake.org/download/ + out_info("1. Install System Dependencies.\n" + "In Debian/Ubuntu, try:\n" + "apt-get install -y cmake libsm6 libxrender1 libxext-dev python3-tk") -In Debian/Ubuntu, try: -apt-get install -y cmake libsm6 libxrender1 libxext-dev python3-tk -2. Install PIP requirements -You may want to execute `chcp 866` in cmd line -to fix Unicode issues on Windows when installing dependencies -""") +def tips_2_3(): + """ Pip Tips """ + out_info("1. Install PIP requirements\n" + "You may want to execute `chcp 866` in cmd line\n" + "to fix Unicode issues on Windows when installing dependencies") def main(): """" Run Setup """ - global ENABLE_DOCKER, ENABLE_CUDA, CUDA_VERSION, OS_VERSION + global ENABLE_DOCKER, ENABLE_CUDA, CUDA_VERSION check_system() check_python() check_pip() @@ -452,25 +636,33 @@ def main(): tips_1_1() else: tips_1_2() - else: - if ENABLE_CUDA: - # update dep info if cuda enabled - if OS_VERSION[0] == "Linux": - check_cuda() - check_cudnn() - else: - tips_2_1() - out_warning("Cannot find CUDA on non-Linux system") - CUDA_VERSION = input("Manually specify CUDA version: ") - update_tf_dep() + return + + if ENABLE_CUDA: + # update dep info if cuda enabled + if OS_VERSION[0] in ("Linux", "Windows"): + check_cuda() + check_cudnn() else: - tips_2_2() - # finally check dep - ask_continue() - check_missing_dep() - check_dlib() - install_missing_dep() - out_info("All python3 dependencies are met.\r\nYou are good to go.") + tips_2_1() + out_warning("Cannot find CUDA on macOS") + CUDA_VERSION = input("Manually specify CUDA version: ") + update_tf_dep(cpu_only=False) + else: + update_tf_dep(cpu_only=True) + check_system_dependencies() + if FAIL: + exit(1) + if OS_VERSION[0] == "Linux": + tips_2_2() + elif OS_VERSION[0] == "Windows": + tips_2_3() + # finally check dep + ask_continue() + check_missing_dep() + check_dlib() + install_missing_dep() + out_info("All python3 dependencies are met.\r\nYou are good to go.") if __name__ == "__main__":