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)
Esempio n. 2
0
    def execute_script(self, arguments):
        """ Performs final set up and launches the requested :attr:`_command` with the given
        command line arguments.

        Monitors for errors and attempts to shut down the process cleanly on exit.

        Parameters
        ----------
        arguments: :class:`argparse.Namespace`
            The command line arguments to be passed to the executing script.
        """
        set_system_verbosity(arguments.loglevel)
        is_gui = hasattr(arguments, "redirect_gui") and arguments.redirect_gui
        log_setup(arguments.loglevel, arguments.logfile, self._command, is_gui)
        success = False

        if self._command != "gui":
            self._configure_backend(arguments)
        try:
            script = self._import_script()
            process = script(arguments)
            process.process()
            success = True
        except FaceswapError as err:
            for line in str(err).splitlines():
                logger.error(line)
        except KeyboardInterrupt:  # pylint: disable=try-except-raise
            raise
        except SystemExit:
            pass
        except Exception:  # pylint: disable=broad-except
            crash_file = crash_log()
            logger.exception("Got Exception on main handler:")
            logger.critical(
                "An unexpected crash has occurred. Crash report written to '%s'. "
                "You MUST provide this file if seeking assistance. Please verify you "
                "are running the latest version of faceswap before reporting",
                crash_file)

        finally:
            safe_shutdown(got_error=not success)
Esempio n. 3
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)
Esempio n. 4
0
    def execute_script(self, arguments):
        """ Run the script for called command """
        log_setup(arguments.loglevel, arguments.logfile, self.command)
        logger.debug("Executing: %s. PID: %s", self.command, os.getpid())
        try:
            script = self.import_script()
            process = script(arguments)
            process.process()
        except KeyboardInterrupt:  # pylint: disable=try-except-raise
            raise
        except SystemExit:
            pass
        except Exception:  # pylint: disable=broad-except
            crash_file = crash_log()
            logger.exception("Got Exception on main handler:")
            logger.critical("An unexpected crash has occurred. Crash report written to %s. "
                            "Please verify you are running the latest version of faceswap "
                            "before reporting", crash_file)

        finally:
            safe_shutdown()