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)
Beispiel #2
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.

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

        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 GPUStats().exclude_all_devices:
            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)

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

        if get_backend() == "amd" and not self._setup_amd(arguments):
            safe_shutdown(got_error=True)