Exemplo n.º 1
0
    def _configure_backend(self, arguments):
        """ Configure the backend.

        Exclude any GPUs for use by Faceswap when requested.

        Set Faceswap backend to CPU if all GPUs have been deselected.

        Add the Keras import interception code.

        Parameters
        ----------
        arguments: :class:`argparse.Namespace`
            The command line arguments passed to Faceswap.
        """
        if not hasattr(arguments, "exclude_gpus"):
            # Cpu backends will not have this attribute
            logger.debug("Adding missing exclude gpus argument to namespace")
            setattr(arguments, "exclude_gpus", None)

        if arguments.exclude_gpus:
            if not all(idx.isdigit() for idx in arguments.exclude_gpus):
                logger.error(
                    "GPUs passed to the ['-X', '--exclude-gpus'] argument must all be "
                    "integers.")
                sys.exit(1)
            arguments.exclude_gpus = [
                int(idx) for idx in arguments.exclude_gpus
            ]
            set_exclude_devices(arguments.exclude_gpus)

        if ((get_backend() == "cpu" or GPUStats().exclude_all_devices) and
            (self._command == "extract" and arguments.detector == "s3fd")):
            logger.error(
                "Extracting on CPU is not currently for detector: '%s'",
                arguments.detector.upper())
            sys.exit(0)

        if GPUStats().exclude_all_devices and get_backend() != "cpu":
            msg = "Switching backend to CPU"
            if get_backend() == "amd":
                msg += (". Using Tensorflow for CPU operations.")
                os.environ["KERAS_BACKEND"] = "tensorflow"
            set_backend("cpu")
            logger.info(msg)

        # Add Keras finder to the meta_path list as the first item
        sys.meta_path.insert(0, KerasFinder())

        logger.debug("Executing: %s. PID: %s", self._command, os.getpid())

        if get_backend() == "amd":
            plaidml_found = self._setup_amd(arguments)
            if not plaidml_found:
                safe_shutdown(got_error=True)
                sys.exit(1)
Exemplo n.º 2
0
#!/usr/bin/env python3
""" Use custom Importer for importing Keras for tests """
import sys
from lib.utils import KerasFinder

sys.meta_path.insert(0, KerasFinder())